

// DOM operation
function next_element(n)
{
	do n = n.nextSibling;
	while (n && n.nodeType != 1);
	return n;
}

function prev_element(n) 
{
	do n = n.previousSibling;
	while (n && n.nodeType != 1);
	return n;
}

function firstChild_element(n) 
{
	n = n.firstChild;
	if (n && n.nodeType != 1) n = next_element(n);
	return n;
}

// ajax
function ajax_new_request()
{
	var ajaxRequest;
	try {
		ajaxRequest = new XMLHttpRequest();
	} catch (e) {
		try {
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				// browser does not support
				alert("Browser does not support Shoutbox requests.");
				return false;
			}
		}
	}
	return ajaxRequest;
}

// navigation

function switch_section_visibility(section_id) 
{	
	if(document.getElementById("close_"+section_id)!=null)
	{
		if(document.getElementById("close_"+section_id).style.display=='none')
		{
			//alert("montrer");
			document.getElementById("open_"+section_id).style.display='none';
			document.getElementById("close_"+section_id).style.display='inline';
			document.getElementById("content_"+section_id).style.display="block";
		} else {
				if(document.getElementById("open_"+section_id)!=null)
				{
				//alert("cacher");
				document.getElementById("open_"+section_id).style.display='inline';
				document.getElementById("close_"+section_id).style.display='none';
				document.getElementById("content_"+section_id).style.display="none";
				}
			}
	}		
}	

function scroll_to_id(target_id)
{
	var window_client_visible_height=document.documentElement.clientHeight;
	var main_frame_top=document.getElementById("main_frame").offsetTop;
	var main_frame_height=document.getElementById("main_frame").offsetHeight;
	if(document.getElementById(target_id))
	{
		var target_y_pos = document.getElementById(target_id).offsetTop ;
		//alert(window_client_visible_height+ '#' + main_frame_height +'#' + main_frame_top + '#' + target_y_pos )
		 //window.scrollTop(0, 400);
		document.getElementById("main_frame").scrollTop=target_y_pos -(main_frame_top+10) ;
	}	
}

function show_loading()
{
	if(!document.getElementById("pullup_loading"))
	{
		var loader_div=document.createElement("div");
		loader_div.setAttribute("id","pullup_loading");
		var loader_image=document.createElement("img");
		loader_image.setAttribute("src","img/pul-lup_loader.gif");
		loader_image.setAttribute("class","loading");
		loader_div.appendChild(loader_image);
		document.getElementById(MAIN_FRAME_ID).insertBefore(loader_div,document.getElementById(MAIN_FRAME_ID).firstChild);
	}
}

function remove_loading()
{
	if(document.getElementById("pullup_loading"))
	{
		document.getElementById(MAIN_FRAME_ID).removeChild(document.getElementById("pullup_loading"));
	}
}





function load_href(chapter_name,section_ids,mode, target_div)
{	
	show_loading();
	
	switch (mode)
	{
		case 1 :
			document.getElementById(GRADIENT_ID).className = chapter_name;
			document.getElementById(MAIN_FRAME_ID).className = chapter_name;
			break;
			
	}	
	
	htmlRequest = ajax_new_request();
	if (htmlRequest==null) 
	{
		alert ("Browser does not support HTTP requests.");
		return;
	}
	switch (mode)
	{
		case 1 :
			htmlRequest.open("GET", INCLUDE_FOLDER+chapter_name+".html", true);
			break;
		case 2:
			/*alert (section_ids);*/
			if(section_ids)
			{
				htmlRequest.open("GET", INCLUDE_FOLDER+chapter_name+"/"+section_ids+".html", true); 
			}
			else  {
					htmlRequest.open("GET", INCLUDE_FOLDER+"get_random.php?chapter=../"+INCLUDE_FOLDER+chapter_name, true);
					
			}
			break;		
	}

	htmlRequest.onreadystatechange=function() 
		{
			
			if (htmlRequest.readyState==4 && htmlRequest.status==200) 
			{
				var target_div_id=MAIN_FRAME_ID;
				switch (mode)
				{
					case 1 :
						target_div_id=MAIN_FRAME_ID;
						break;
					case 2 :
						target_div_id=MAIN_SUBFRAME_ID;
						break;
					default :
						target_div_id=target_div;
					
				}	
				
				if(document.getElementById(target_div_id))
				{
					document.getElementById(target_div_id).innerHTML = htmlRequest.responseText;
					main_focus(target_div_id);
					remove_loading();
					
				}
				else{
					alert(target_div_id);	
					remove_loading();
				}
				
				switch (mode)
				{
					case 1:
						switch(chapter_name)
						{
							case 'artists': 
							case 'mixes':
							case 'styles':
							//case 'listen':
								load_href(chapter_name,section_ids,2);
						}		
				}
				
				if (section_ids)
				{
					for(i=0;i<section_ids.length;i++)
					{
						switch_section_visibility(section_ids[i]);
					}	
					if(section_ids.length)
					{
						scroll_to_id(section_ids[0]);
					}
				}
				
				alernate_sub('mix_all_list',"tr",1,'tr_odd','');
				alernate_sub('mix_list',"tr",0,'','tr_odd');
				alernate_sub('style_mix_list',"tr",1,'','tr_odd');
				
				//alernate_sub('sub_main_frame',"tr",1,'tr_odd','');
				alernate_sub('artists_table',"tr",1,'tr_odd','');
			}
		}
	htmlRequest.send(null);
}	


function load_external(ext_href,chapter_name,section_ids,div_target)
{
	if(chapter_name)
	{
		document.getElementById(GRADIENT_ID).className = chapter_name;
		document.getElementById(MAIN_FRAME_ID).className = chapter_name;
	}	
	show_loading();
	htmlRequest = ajax_new_request();
	if (htmlRequest==null) 
	{ 
		alert ("Browser does not support HTTP requests.");
		return;
	}
	htmlRequest.open("GET", ext_href, true);
	htmlRequest.onreadystatechange=function() 
		{
			if (htmlRequest.readyState==4 ) 
			{
				//alert(htmlRequest.status);
				document.getElementById(div_target).innerHTML = htmlRequest.responseText;
				main_focus(div_target);
				remove_loading();
				
			}
			remove_loading();		
		}
	htmlRequest.send(null);
}

function show_engine()
{
	htmlRequest = ajax_new_request();
	if (htmlRequest==null) 
	{
		//alert ("Browser does not support HTTP requests.");
		return;
	}
	else{
		show_loading();
		//debug("0");
		htmlRequest.open("GET", INCLUDE_FOLDER+"engine.php", true);
		htmlRequest.onreadystatechange=function() 
		{
			if (htmlRequest.readyState==4) 
			{
						document.getElementById(MAIN_FRAME_ID).innerHTML = htmlRequest.responseText;					
			}	
			remove_loading();				
			
		}
		htmlRequest.send(null);
	}
	
}

function get_selected(id)
{
	selected = new Array();
	var ob=document.getElementById(id);
	for (var i = 0; i < ob.options.length; i++) 
		if (ob.options[ i ].selected) selected.push(ob.options[ i ].value);
	return selected;	
}	

function array_to_string(array_input,select_name)
{
	var res='';
	for(key in array_input)
	{
	 res += '&'+select_name+'['+key+']='+array_input[key];
	}
	return res;
}

function show_engine_results(label, tags, artists)
{
	//debug(label);
	htmlRequest = ajax_new_request();
	if (htmlRequest==null) 
	{
		//alert ("Browser does not support HTTP requests.");
		return;
	}
	else{
		show_loading();
		htmlRequest.open("POST", INCLUDE_FOLDER+"engine.php", true);
		htmlRequest.onreadystatechange=function() 
		{
			if (htmlRequest.readyState==4) 
			{
				document.getElementById("mix_answers").innerHTML = htmlRequest.responseText;	
				main_focus("mix_answers")
			}	
			remove_loading();				
			
		}
		// var label_selected= document.getElementById("label").getElementsByTagName("option").selected;
		// debug(label);		//debug(tags);		debug(artists);
		var tags_pos='';
		if(tags.length>0)
		{
			tags_post= array_to_string(tags,'tags');
		}
		var artists_post='';
		if(artists.length>0)
		{
			artists_post= array_to_string(artists,'artists_selected');
		}
		var label_post='';
		if(label.length>0)
		{
			label_post=array_to_string(label,'label');
		}
		var mix_sort_type=get_selected("mix_sort");
		//debug("sort : "+mix_sort_type+"#");
		var mix_sort_post="&mix_sort="+mix_sort_type[0];
		//debug(tags_post+artists_post+label_post);
		htmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		htmlRequest.send(tags_post+artists_post+label_post+mix_sort_post);
	}
	
}


function sub_load(chapter_name,href,id_target,title,show_title_mode)
{ 
		document.getElementById(GRADIENT_ID).className = chapter_name;
		document.getElementById(MAIN_FRAME_ID).className = chapter_name;
		htmlRequest = ajax_new_request();
		if (htmlRequest==null) 
		{
			//alert ("Browser does not support HTTP requests.");
			return;
		}
		else{
			//alert ("load");
			htmlRequest.open("GET", INCLUDE_FOLDER+href, true);
			htmlRequest.onreadystatechange=function() 
				{
					if (htmlRequest.readyState==4 && htmlRequest.status==200) 
					{
						switch(show_title_mode)
						{
							case 0: 
								document.getElementById(id_target).innerHTML = htmlRequest.responseText;
								break;
							default: 
								res_text="<h"+show_title_mode+" class="+chapter_name+">"+title+"</h"+show_title_mode+">";
								document.getElementById(id_target).innerHTML = htmlRequest.responseText;
						}		
					}
				}
			htmlRequest.send(null);
		}	
}

function isMouseLeave(htmlElement, e) {
	if (!e) var e = window.event;
	var fromElement = (window.event) ? e.srcElement : e.target;
	var toElement = (e.relatedTarget) ? e.relatedTarget : e.toElement;
	while (toElement != htmlElement && toElement != document.body)
		toElement= toElement.parentNode;
	if (toElement == htmlElement) return false;
	else return true;
}

function hideContextMenu($contextMenu, e) {
	if (isMouseLeave($contextMenu, e)) {
		$contextMenu.style.visibility="Hidden";
	}
}

function writeCookie(nom, valeur) {
	var argv=writeCookie.arguments;
	var argc=writeCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : null;
	var path=(argc > 3) ? argv[3] : null;
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	document.cookie=nom+"="+escape(valeur)+
		((expires==null) ? "" : ("; expires="+expires))+
		((path==null) ? "" : ("; path="+path))+
		((domain==null) ? "" : ("; domain="+domain))+
		((secure==true) ? "; secure" : "");
}

function getCookieVal(offset) {
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1) endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function getCookie(nom) {
	var arg=nom+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen) {
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0) break;
	}
	return null;
}

function debug(txt, id) {
	document.getElementById('debug').style.visibility = "visible";
	if (document.getElementById(id)) {
		document.getElementById(id).innerHTML = txt;
	} else {
		var div = document.createElement('div');
		if (id) div.id = id;
		var text = document.createTextNode(txt);
		div.appendChild(text);
		document.getElementById('debugContent').appendChild(div);
	}
}

function hasClass(ele,cls) 
{
	if(ele.className)
	{
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
	}
	else return false;
}
function addClass(ele,cls) 
{
	if(ele.className)
	{
		if (!this.hasClass(ele,cls)) ele.className += " "+cls;
	}
	else ele.className=cls;
}
function removeClass(ele,cls)
{
	if (hasClass(ele,cls))
	{
		var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,' ');
	}
}


function alernate_sub(id_array,type,from,odd_class,even_class)
{
	if(document.getElementById(id_array))
	{
		var i=0;
		if (document.getElementById(id_array).getElementsByTagName(type).length>0)
		{	
			var tr_array=document.getElementById(id_array).getElementsByTagName(type);
			for(j in tr_array)
			{
				if(j>=from)
				{
					switch((i-from)%2)
					{
						case 0:
							old_class=even_class;
							new_class=odd_class;
							break;
						case 1 : 
							old_class=odd_class;
							new_class=even_class;
							break;
					}
					if(tr_array[j].className)
					{	
						if(hasClass(tr_array[j],old_class))
							removeClass(tr_array[j],old_class);
					}
					if(new_class!="")
					addClass(tr_array[j],new_class);
				}	
				i++;
				
			}	
		}
	}
}

