/**
 * function show_hide()
 *
 * show or hide defined element, if sh_link exists change it's text
 *
 * @param string sh_element "target element to show/hide"
 * @param_string sh_link "link whoose text should ba changed"
 * @param string link_show "link text when element visible"
 * @param string link_hide "link text when element hidden"
 */
function show_hide(sh_element,sh_link,link_show,link_hide){
	var status = document.getElementById(sh_element).style.display;

	switch(status){
		case "block":
			document.getElementById(sh_element).style.display = "none";
			if(document.getElementById(sh_link)){
				document.getElementById(sh_link).innerHTML = link_hide;
				document.getElementById(sh_link).blur();
			}
			break;
		case "none":
			document.getElementById(sh_element).style.display = "block";
			if(document.getElementById(sh_link)){
				document.getElementById(sh_link).innerHTML = link_show;
				document.getElementById(sh_link).blur();
			}
			break;
	}
}


/**
 * function smile()
 *
 * inser smile into guestbook message text
 *
 * @param int num "smile number"
 */
function smile(num){
	document.getElementById("text").value += "["+num+"]";
	document.getElementById("text").focus();
}




/**
 * function createRequest()
 *
 * create new XMLHttpRequest
 *
 * @param int id "indicator Id"
 * @param string msg_error "error message to display if loading fails"
 * @param string url "requested file url"
 */
function createRequest(id,msg_error,url){
	if(window.XMLHttpRequest){
		httpRequest = new XMLHttpRequest();
	}
	if(window.ActiveXObject){
		httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}

	httpRequest.open("GET",url,true);
	httpRequest.onreadystatechange = function () {processRequest(id,msg_error);};
	httpRequest.send(null);
}


/**
 * function processRequest()
 *
 * process XMLHttpRequest, write text response into element
 *
 * @param int id "indicator Id"
 * @param string msg_error "error message to display if loading fails"
 */
function processRequest(id,msg_error){
	if(httpRequest.readyState==4){
		if(httpRequest.status==200){
			var data_string = httpRequest.responseText;
			document.getElementById("palette_"+id).innerHTML = data_string;
		}else{
			document.getElementById("palette_"+id).className = "msg_error";
			document.getElementById("palette_"+id).innerHTML = "<big>"+msg_error+"</big>";
		}
	}else{
		document.getElementById("palette_"+id).innerHTML = "<img src=\"images/loading.gif\" alt=\"loading...\" />";
	}
}


/**
 * function palette()
 *
 * show or hide color palette
 *
 * @param string lng "current language"
 * @param int td "indicator Id"
 * @param string msg_error "error message to display if loading fails"
 * @param string link_show "link text and title when palette hidden"
 * @param string link_hide "link text and title when palette displayed"
 */
function palette(lng,id,msg_error,link_show,link_hide){

	/**
	 * if palette element is empty, call for color palette and display it
	 */
	if(document.getElementById("palette_"+id).innerHTML==""){
		createRequest(id,msg_error,"color_palette.php?lng="+lng+"&id="+id);
		document.getElementById("link_"+id).innerHTML = link_hide;
		document.getElementById("link_"+id).title = link_hide;

	/**
	 * if palette element is filled with something, empty it and set indicator's div background to default
	 */
	}else{
		document.getElementById("palette_"+id).innerHTML = "";
		document.getElementById("indikator_"+id).style.background = "url(images/layout/bg_content_box.png) repeat-y";
		document.getElementById("link_"+id).innerHTML = link_show;
		document.getElementById("link_"+id).title = link_show;
	}
}


/**
 * function change_bgcolor()
 *
 * change element's background to given color
 *
 * @param int id "indicator Id"
 * @param string color "color's HEX representation (without leading #)"
 */
function change_bgcolor(id,color){
	document.getElementById("indikator_"+id).style.background = "#"+color;
}


/**
 * function change_hexcolor()
 *
 * change element's background to color in input field
 *
 * @param int id "indicator Id"
 */
function change_hexcolor(id){
	document.getElementById("indikator_"+id).style.background = "#"+document.getElementById("hex_"+id).value;
}


/**
 * function change_hex()
 *
 * change input value
 *
 * @param int id "indicator Id"
 * @param string value "input field value"
 */
function change_hex(id,value){
	document.getElementById("hex_"+id).value = value;
}


/**
 * function generator_form()
 *
 * enable or disable form element depending on selected type of generated code
 *
 * no params needed ;-)
 */
function generator_form(){
	switch(document.getElementById("vygenerovat").value){
		case "html":
			document.getElementById("standard").disabled = false;
			document.getElementById("standard").className = "";
			break;
		case "bbcode":
			document.getElementById("standard").disabled = true;
			document.getElementById("standard").className = "disabled";
			break;
		case "url":
			document.getElementById("standard").disabled = true;
			document.getElementById("standard").className = "disabled";
			break;
	}
}


/**
 * function menu()
 *
 * move left menu when scrolling page
 *
 * no params needed :-)
 */
function menu(){

	/**
	 * function load_menu()
	 *
	 * catch menu element, set some properties and define function to position it. then return element object
	 */
	function load_menu(){
		var element = document.getElementById("menu");
		element.position = function(y){ if(document.styleSheets[0].disabled==false) this.style.marginTop = y+"px"; }
		element.y = 0;
		return element;
	}

	/**
	 * window.function move_menu()
	 *
	 * get current window scroll position, change menu object Y property and set top margin. then
	 * call itself again
	 */
	window.move_menu = function(){
	  if(document.documentElement.scrollTop){
			posy = document.documentElement.scrollTop;
	  }else{
			posy = document.body.scrollTop;
	  }

		menu_object.y += (posy-menu_object.y)/7;
		menu_object.position(menu_object.y);
		setTimeout("move_menu()",10);
	}

	/**
	 * this cool stuff will be disabled for fucking Internet Explorer, because it's object drawing
	 * is too slow and causes menu to shrug when movind. fuck you MSIE users.
	 */
	if(navigator.appName!="Microsoft Internet Explorer"){
		menu_object = load_menu();
		move_menu();
	}
}