/*---------------------------------------------------------*
 *    We.Blog - Blog engine -
 * 
 *
 *    Aim : Javascript functions
 *    For : We.Blog
 *---------------------------------------------------------*/

// Adding text in Textarea
function addText(instext, textarea_name)
{
	if (textarea_name == null){
		var textarea_name = 'we_article';
	}
	var mess = document.getElementById(textarea_name);

	// IE support
	if (document.selection){
		mess.focus();
		sel = document.selection.createRange();
		sel.text = instext;
		document.form.focus();
	}else if (mess.selectionStart || mess.selectionStart == '0'){ // MOZILLA/NETSCAPE support
		var startPos = mess.selectionStart;
		var endPos = mess.selectionEnd;
		var chaine = mess.value;

		mess.value = chaine.substring(0, startPos) + instext + chaine.substring(endPos, chaine.length);

		mess.selectionStart = startPos + instext.length;
		mess.selectionEnd = endPos + instext.length;
		mess.focus();
	}else{
		mess.value += instext;
		mess.focus();
	}
}

// Tools' function (bold, italic, etc...)
function addTools(tags, fieldname)
{
	val = tags.split('][');
	
	if (window.getSelection){ // recent Mozilla versions
		var selectedString = window.getSelection();
	}else if (document.all){ // MSIE 4+
		var selectedString = document.selection.createRange().text;
	}else if (document.getSelection){ // older Mozilla versions
		var selectedString = document.getSelection();
	}
	
	if (selectedString){
		if (navigator.appName != 'Netscape'){
			text = val[0]+']'+selectedString+'['+val[1];
			addText(text, fieldname);
		}else{
			mozWrap(document.getElementById(fieldname), val[0] +']', '[' + val[1]);
		}
	}
	
	return false;
}

// From http://www.massless.org/mozedit/
function mozWrap(txtarea, open, close)
{
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd == 1 || selEnd == 2) selEnd = selLength;
	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + open + s2 + close + s3;
	return;
}

// Show/Close layout
function Show(id, method)
{
	var d = document.getElementById(id);
	
	if (method == 'display'){
		var hid = d.style.display;
		if (d){
			if (hid == 'block' || hid == 'inline' || hid == null){
				d.style.display = 'none';
			}else{
				d.style.display = 'block';
			}
		}
		return d.style.display;
	}else if (method == 'visibility'){
		var hid = d.style.visibility;
		if (d){
			if (hid == 'visible' || hid == null){
				d.style.visibility = 'hidden';
			}else{
				d.style.visibility = 'visible';
			}
		}
		return d.style.visibility;
	}
}

// Copy text to clipboard (Img include)
function CopytoF(text, mode)
{
	var modes = new Array();
		modes['sound'] = 'mp3|wav|wma';
		modes['img'] = 'jpg|png|gif|bmp';
		modes['video'] = 'avi|mpg|mpe|mpa|mpeg|asf|wmv';
		modes['flv'] = 'flv';
		modes['flash'] = 'swf';
		
	for (keyvar in modes)
	{
		var all_ext = new String(modes[keyvar]).split('|');
		for (ext in all_ext)
		{
			if (all_ext[ext] == mode.toLowerCase()){
				var mode = keyvar;
			}
		}
	}
	
	if (mode == 'img'){
		text = '[img]'+text+'[/img]';
	}else if (mode == "sound"){
		text = "[sound]"+text+"[/sound]";
	}else if (mode == "flv"){
		text = "[video=flv]"+text+"[/video]";
	}else if (mode == "flash"){
		text = "[flash]"+text+"[/flash]";
	}else if (mode == "video"){
		text = "[video]"+text+"[/video]";
	}else{
		text = "[link]"+text+"[/link]";
	}
	addText(text, null);
}

// Dynamic Time + Multi
// Based on http://editeurjavascript.com/scripts/scripts_temps_2_24.php
function ShowTimeDyn(source, lang)
{
	var d = document.getElementById(source);
	krucial = new Date;
	heure = krucial.getHours();
	min = krucial.getMinutes();
	sec = krucial.getSeconds();
	jour = krucial.getDate();
	mois = krucial.getMonth() + 1;
	annee = krucial.getFullYear();
	
	if (sec < 10){ sec0 = '0'; }else{ sec0 = ''; }
	if (min < 10){ min0 = '0'; }else{ min0 = ''; }
	if (heure < 10){ heure0 = '0'; }else{ heure0 = ''; }

	if (lang != 'fr'){
		if (heure > 12 || heure == 0){
			heure = Math.abs(heure - 12);
			sec += ' pm';
		}else{
			sec += ' am';
		}
	}
	DinaHeure = heure0 + heure + ':' + min0 + min + ':' + sec0 + sec;
	which = DinaHeure;
	if (d){ d.innerHTML = which; }
	
	setTimeout("ShowTimeDyn('"+source+"', '"+lang+"')", 1000);
}

// Ajax Link (Need XHRConnection included in the design core)
function Link(IDL, IDcount, newnumber)
{
	document.getElementById(IDcount).innerHTML = '['+newnumber+']';
	
	var XHR = new XHRConnection();
	XHR.appendData('ID', IDL);
	
	XHR.sendAndLoad('./wb_ajax/link.php', 'POST', Go_To);

	return false;
}
// Goes with Link()
function Go_To(obj)
{
	if (obj.responseText){
		document.location = obj.responseText;
	}
}

function Refresh_antispam(ID)
{
	var D = new Date();
	document.getElementById(ID).setAttribute('src', './wb_ajax/antispam.php');
	document.getElementById(ID).setAttribute('src', document.getElementById(ID).getAttribute('src')+'?reload&'+D.getTime());
}

function Where_To_Log(ID, other)
{
	if (Show(ID, 'display') == 'block'){
		Show(other, 'display');
	}else{
		Show(ID, 'display');
	}
}

function Flv_video()
{
	var s = new Array('', 'lite', 'medium', 'large');
	
	var q = prompt('Which size for the video do you want ?\n 1 = lite\n 2 = medium (default)\n 3 = large');
	if (!q || isNaN(q) || (q > 4 && q < 1)){ q = 2; }

	var url = prompt('Type the url of your media\n i.e: http://www.youtube.com/v/****-*****\n http://www.dailymotion.com/swf/*****');
	
	addText('[flv='+s[q]+']'+url+'[/flv]', null);
}

function Lightbox_me(img, title, group, img_code)
{
	if (img == null){
		var img = prompt('Please type the url to your original image:');
	}
	
	if (title){
		var title_q = prompt('For the moment, you use "'+title+'" as Lightbox title\n Do you want to keep it ? (If not, please fill in else do not and continue)');
		if (title_q){ title = title_q; }
	}else{
		var title = prompt('What title do you want to use with that image ?');
	}
	
	if (group == null){
		var group = prompt('Please type the group your image is refered to');
	}
	
	if (img_code == null){
		var img_code = prompt('And last but not least, please type your thumbnail\'s url');
	}
	
	addText('[lightbox='+img+'|'+title+'|'+group+']'+img_code+'[/lightbox]');
}

function Make_list(setdef)
{
	var type = prompt('Please choose your type list :\n(a|A) : Alphabetical\n1 : Alphanumerical\n(i|I) : Roman numbers\n(d|rectangle|circle|disc): Decimal..\n(none)');
	if (type != ""){
		type = '='+type;
	}
	
	addText('[list'+type+']tes[/list]');
}

// Preview coms Function
function Preview_com()
{
	var text = document.getElementById('we_com');
	if (text.value != ''){
		document.getElementById('preview_com').style.display = 'block';
	
		var XHR = new XHRConnection();
		XHR.appendData('text', text.value);
		XHR.appendData('name', document.getElementById('we_name').value);
		XHR.appendData('mail', document.getElementById('we_mail').value);
	
		run = function() { XHR.sendAndLoad('./wb_ajax/preview_com.php', 'POST', 
			function (obj)
			{
				/* FX */
				Change_opacity('preview_com', 0);
				document.getElementById('preview_com').innerHTML = obj.responseText;
				
			    var speed = Math.round(1000 / 100);
			    var timer = 0;
				for (x = 0; x <= 100; x++)
				{
					setTimeout("Change_opacity('preview_com', "+x+")", (timer * speed)); 
					timer++;
				}}
			);
		}
		setTimeout("run()", 2000);
	}
}

function Change_opacity(id, opa)
{
	var obj = document.getElementById(id).style;
	obj.opacity = (opa / 100);
    obj.filter = "alpha(opacity="+opa+")";
    obj.MozOpacity = (opa / 100);
    obj.KhtmlOpacity = (opa / 100);
}

function Message(id, bFade, bBlock)
{
	if (id != 'we_message'){
		if (bBlock == true){
			if (Show(id, 'display') != 'block'){
				Show(id, 'display');
			}
		}
	}
	Show(id, 'visibility');
	Change_opacity(id, 0);
	
	var speed = Math.round(1000 / 100);
	var timer = 0;
	for (x = 0; x <= 100; x++)
	{
		setTimeout("Change_opacity('"+id+"', "+x+")", (timer * speed)); 
		timer++;
	}
	
	if (bFade == true){
		rewind = function () 
		{
			var speed = Math.round(1000 / 100);
		    var timer = 0;
			for (x = 0; x <= 100; x++)
			{
				setTimeout("Change_opacity('"+id+"', "+(100 - x)+")", (timer * speed)); 
				timer++;
			}
		}
		setTimeout("rewind()", 8000);
		setTimeout("Show('"+id+"', 'visibility')", 10000);
	}
}
