/*
 * Copyright 2004-2005 Uwyn bvba/sprl <info[remove] at uwyn dot com>
 * http://www.uwyn.com
 * Distributed under the terms of the GNU General Public License, v2 or later
 * $Id: blog_common.js 2275 2005-08-11 18:31:15Z gbevin $
 */
function toggleBlogNav()
{
	if (blognav_shown)
	{
		blognav_shown = 0;
		createCookie("blognav", "0");
		Element.hide('blognav_close');
		Element.show('blognav_open');
		Effect.BlindUp('blognav_effect');
	}
	else
	{
		blognav_shown = 1;
		createCookie("blognav", "1");
		Element.hide('blognav_open');
		Element.show('blognav_close');
		Effect.BlindDown('blognav_effect');
	}
	
	return false;
}
		
function createCookie(name, value, days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

function tag(el, theTagStart, theTagEnd)
{
	if (!insertTagAtCursor(el, theTagStart, theTagEnd))
	{
		el.value += theTagStart.replace("\$", "")+theTagEnd.replace("\$", "");
		el.focus();
	}

	return false;
}

function emoticon(el, emoticon)
{
	if (!insertAtCursor(el, emoticon, 0))
	{
		el.value += emoticon;
		el.focus();
	}

	return false;
}

function insertTagAtCursor(el, tagStart, tagEnd)
{
	var inStart = false;
	var finalCursorPos = -1;
	if (tagEnd.indexOf("$") != -1)
	{
		finalCursorPos = tagEnd.indexOf("$");
	}
	else if (tagStart.indexOf("$") != -1)
	{
		finalCursorPos = tagStart.indexOf("$");
		inStart = true;
	}
	
	// ie support
	if (document.selection)
	{
		el.focus();
		sel = document.selection.createRange();
		
		var length = sel.text.length;
		
		sel.text = tagStart.replace("\$","") + sel.text + tagEnd.replace("\$","");

		if (inStart)
		{
			sel.move('character', (-1*(tagStart.length + length + tagEnd.length - 1) + finalCursorPos));
		}
		else
		{
			sel.move('character', (-1*(tagEnd.length - 1) + finalCursorPos));
		}

		sel.select();
		
		el.focus();

		return true;
	}
	// gecko support
	else if (el.selectionStart || el.selectionStart == '0')
	{
		var start = el.selectionStart;
		var end = el.selectionEnd;

		el.value = el.value.substring(0, start) +
			tagStart.replace("\$","") + el.value.substring(start,end) +
			tagEnd.replace("\$", "") + el.value.substring(end, el.value.length);
			
		if (el.setSelectionRange)
		{
			el.focus();

			var pos = 0;
			if (inStart)
			{
				pos = start + finalCursorPos;
			}
			else
			{
				pos = end + tagStart.length + finalCursorPos;
			}

			el.setSelectionRange(pos, pos);
		}

		return true;
	}

	return false;
}

function insertAtCursor(el, value, caretOffset)
{
	// ie support
	if (document.selection)
	{
		el.focus();
		sel = document.selection.createRange();
		sel.text = value;
		sel.collapse(true);
		sel.moveStart('character', caretOffset);
		sel.moveEnd('character', caretOffset);
		sel.select();

		return true;
	}
	// gecko support
	else if (el.selectionStart || el.selectionStart == '0')
	{
		var start = el.selectionStart;
		var end = el.selectionEnd;

		el.value = el.value.substring(0, start) + value + el.value.substring(end, el.value.length);

		if (el.setSelectionRange)
		{
			el.focus();

			var pos = end+value.length+caretOffset;
			el.setSelectionRange(pos, pos);
		}

		return true;
	}

	return false;
}

function getObj(name)
{
	if (document.getElementById)
	{
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	}
	else if (document.all)
	{
		this.obj = document.all[name];
		this.style = document.all[name].style;
	}
	else if (document.layers)
	{
		this.obj = document.layers[name];
		this.style = document.layers[name];
	}
}

function deleteEntry(message, delete_url)
{
	var selection = confirm(message);
	if (selection)
	{
		window.location = encodeUrl(delete_url);
	}
}

function encodeUrl(url)
{
	var params = url.substring(url.indexOf("?"));
	
	var encodedParams = "";
	while (params.indexOf("=") != -1)
	{
		var eqindex = params.indexOf("=") + 1;
		var ampindex = params.indexOf("&");
		
		if (ampindex == 0)
		{
			encodedParams += "&";
			params = params.substring(1);
			
			continue;
		}
		
		// no amps left... goto end of string
		if (ampindex == -1) { ampindex = params.length; }
		if (encodedParams.length > 1)
		{
			encodedParams += params.substring(0, eqindex) + escape(params.substring(eqindex, ampindex));
		}
		else
		{
			encodedParams = params.substring(0, eqindex) + escape(params.substring(eqindex, ampindex));
		}
		
		if (params.indexOf("&") == -1)
		{
			params = "";
		}
		else
		{
			params = params.substring(params.indexOf("&"));
		}
	}
	
	return url.substring(0, url.indexOf("?")) + encodedParams;
}

function getEmoticonObj()
{
	var markupLanguage = new getObj("markupLanguage");
	if (markupLanguage.obj.value == "BBCode" || markupLanguage.obj.value == "Radeox")
	{
		this.normal = ":)";
		this.wink = ";)";
		this.bigeye = "=)";
		this.nose = ":-)";
		this.cry = ":'(";
		this.sad = ":(";
		this.sceptical = ":/";
		this.bigsmile = ":D";
		this.indifferent = ":|";
		this.tongue = ":p";
		this.shocked = ":o";
		this.cool = "8)";
	}
	else if (markupLanguage.obj.value == "XHTML")
	{
		var tag = "<img src=\"images/blog/$1\" width=\"16\" height=\"16\" alt=\"$2\" />";

		this.normal = tag.replace("$1", "emoticon-normal\.gif").replace("$2", "\:\)");
		this.wink = tag.replace("$1", "emoticon-wink\.gif").replace("$2", "\;\)");
		this.bigeye = tag.replace("$1", "emoticon-bigeye\.gif").replace("$2", "\=\)");
		this.nose = tag.replace("$1", "emoticon-nose\.gif").replace("$2", "\:\-\)");
		this.cry = tag.replace("$1", "emoticon-cry\.gif").replace("$2", "\:\'\(");
		this.sad = tag.replace("$1", "emoticon-sad\.gif").replace("$2", "\:\(");
		this.sceptical = tag.replace("$1", "emoticon-sceptical\.gif").replace("$2", "\:\/");
		this.bigsmile = tag.replace("$1", "emoticon-bigsmile\.gif").replace("$2", "\:\D");
		this.indifferent = tag.replace("$1", "emoticon-indifferent\.gif").replace("$2", "\:\|");
		this.tongue = tag.replace("$1", "emoticon-tongue\.gif").replace("$2", "\:\p");
		this.shocked = tag.replace("$1", "emoticon-shocked\.gif").replace("$2", "\:\o");
		this.cool = tag.replace("$1", "emoticon-cool\.gif").replace("$2", "\8\)");
	}
}

function getMarkupLanguageObj()
{
	// read default and set up the passed in object
	var markupLanguage = new getObj("markupLanguage");
	
	if (markupLanguage.obj.value == "BBCode")
	{
		this.b_start = "[b]";
		this.b_end = "$[/b]";
		
		this.i_start = "[i]";
		this.i_end = "$[/i]";
		
		this.u_start = "[u]";
		this.u_end = "$[/u]";
		
		this.code_start = "[code]";
		this.code_end = "$[/code]";
		
		this.url_start = "[url=$]";
		this.url_end = "[/url]";
	}
	else if (markupLanguage.obj.value == "Radeox")
	{
		this.b_start = "__";
		this.b_end = "$__";
		
		this.i_start = "~~";
		this.i_end = "$~~";
		
		this.u_start = "++";
		this.u_end = "$++";
		
		this.code_start = "{code}";
		this.code_end = "${code}";
		
		this.url_start = "{link:";
		this.url_end = "|$}";
	}
	else if (markupLanguage.obj.value == "XHTML")
	{
		this.b_start = "<b>";
		this.b_end = "$</b>";
		
		this.i_start = "<i>";
		this.i_end = "$</i>";
		
		this.u_start = "<u>";
		this.u_end = "$</u>";
		
		this.code_start = "<div id=\"codebody\"><pre>";
		this.code_end = "$</pre></div>";
		
		this.url_start = "<a href=\"$\">"
		this.url_end = "</a>"
	}
}
