/**
 * function getElement()
 *
 * identify document element by Id
 *
 * @param string e "element Id"
 */  
function getElement(e){
	if(document.all) return document.all[e];
	else return document.getElementById(e);
}


/**
 * function xicht_switch()
 *
 * switch xicht on lead-off page
 *
 * no params needed, but initialize status variable first... ;-)
 */
var xicht_status = "grey";

function xicht_switch(){
	switch(xicht_status){
		case "grey":
			getElement("xicht").src = "images/xicht_color.jpg";
			xicht_status = "color";
			change_opacity("xicht",0,0.1);
			break;
		case "color":
			getElement("xicht").src = "images/xicht_grey.jpg";
			xicht_status = "grey";
			change_opacity("xicht",0,0.1);
			break;
	}
}


/**
 * function change_opacity()
 *
 * change image opacity until full (1) or none (0)
 *
 * @param string e "target element"
 * @param int opacity "opacity to start with"
 * @param int step "opacity step in each loop (positive for increasing, negative for decreasing)
 */
function change_opacity(e,opacity,step){
	if((opacity<1 && step>0) || (opacity>0 && step <0)){
		getElement(e).style.opacity = opacity; // for Gecko and Opera (normal browsers)
		getElement(e).style.filter = "alpha(opacity="+opacity*100+");"; // for motherfucking MSIE
		opacity += step;
		setTimeout("change_opacity('"+e+"',"+opacity+","+step+");", 50);
	}
}


/**
 * function sn()
 *
 * show or hide scatter's network div. also change link text and blur the link
 *
 * no params needed, just switching where variable ;-)
 */
var where = "in";
function sn(){
	switch(where){
		case "in":
			resize_y('sn',30,103,5);
			getElement("sn_image").src = "images/sn/hn_normal.png";
			getElement("sn_href").blur();
			where = "out";
			break;
		case "out":
			resize_y('sn',103,30,-5);
			getElement("sn_image").src = "images/sn/sn_normal.png";
			getElement("sn_href").blur();
			where = "in";
			break;
	}
}


/**
 * function resize_y()
 *
 * change height of element
 *
 * @param string e "element to resize"
 * @param int y_current "current height"
 * @param int y_to "target height"
 * @param int y_step "step in one loop, can also be negative (to decrease size)"   
 */
function resize_y(e,y_current,y_to,y_step){
	/**
	 * set element's new position after this step
	 */
	y_current += y_step;

	/**
	 * this variable will tell us later, if element is already on target position
	 */
	var y_on = true;

	/**
	 * depending on step number (+ or -), current and target position, determine whether to move element or not
	 * if element was moved, we must set on_position variable to false, otherwise no other loop will be passed
	 */
	if((y_step>0 && y_current<=y_to)||(y_step<0 && y_current>=y_to)){
		getElement(e).style.height = y_current+"px";
		y_on = false;
	}

	/**
	 * if on_position is false, it means that element hasn't reached it's target position so this
	 * function must call itself again and go on...
	 */
	if(y_on==false){
		setTimeout("resize_y('"+e+"',"+y_current+","+y_to+","+y_step+");", 20);

	/**
	 * if function will not be called again, we must force move element to it's target position
	 * this is because there can be some pixels missing to reach target position. this skip is
	 * never bigger than defined step, so dont worry about how it will look :-)	 	 
	 */
	}else{
		getElement(e).style.height = y_to+"px";
	}
}


/**
 * function sn_link()
 *
 * change image of show/hide scatter's network accrding to current network display status
 *
 * @param string action "hover action"
 *
 * this function also requires "where" variable declared previously
 */
function sn_link(action){
	switch(action){
		case "over":
			if(where=="in") getElement("sn_image").src = "images/sn/sn_hover.png";
			if(where=="out") getElement("sn_image").src = "images/sn/hn_hover.png";
			break;
		case "out":
			if(where=="in") getElement("sn_image").src = "images/sn/sn_normal.png";
			if(where=="out") getElement("sn_image").src = "images/sn/hn_normal.png";
			break;
	}
}


/**
 * function lfm()
 *
 * get statistics from Last.fm and display them as unordered list
 *
 * no params needed ;-)
 */
function lfm() {
	var container = document.getElementById("lfm");

	/**
	 * display loader and loader text
	 */
	container.style.textAlign = "center";
	var loader_img = document.createElement("img");
	loader_img.src = "images/loading.gif";
	loader_img.style.margin = "10px 0 0 0";
	container.appendChild(loader_img);

	var loader_text = document.createElement("p");
	loader_text.innerHTML = "Načítám data z Last.fm...";
	loader_text.style.textAlign = "center";
	loader_text.style.fontSize = "9px";
	container.appendChild(loader_text);

	/**
	 * load XML source and parse it, start creating list
	 */
	var xml = new JKL.ParseXML("inc/lfm.php");
	var root = xml.parse();
	var list = document.createElement("ol");

	/**
	 * go through items and create list
	 */
	for (var i in root.rss.channel.item) {
		var item = root.rss.channel.item[i];
		var li = document.createElement("li");
		var li_a = document.createElement("a");
		li_a.href = item.link;
		li_a.title = item.title;
		li_a.innerHTML = item.title;
		li.appendChild(li_a);
		list.appendChild(li);
	}

	/**
	 * append list, remove loader and loader text and set text align of
	 * container bac to left
	 */
	container.appendChild(list);
	container.removeChild(loader_img);
	container.removeChild(loader_text);
	container.style.textAlign = "left";
}