var req;
function synch(url,parametre) 
{
	req = false;
    if(window.XMLHttpRequest && !(window.ActiveXObject)) 
	{
    	try 
		{
			req = new XMLHttpRequest();
		}
		catch(e) 
		{
			req = false;
		}
    } 
	else if(window.ActiveXObject) 
	{
       	try 
		{
        	req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e) 
		{
        	try 
			{
          		req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e) 
			{
          		req = false;
			}
		}
    }
	if(req) 
	{
		req.open("POST", url, false);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.send(parametre);
		if(req.readyState == 4) 
			return(req.responseText);
		else 
			return(false);
	}
}


var type = 0;
function flou(id)
{
	showAndHide(id);
	document.getElementById('flouter').style.display = "inline";
	document.getElementById('flouter').addEventListener("click", function(){ showAndHide(id);document.getElementById('flouter').style.display="none" }, false);
}
function showAndHide(id)
{
	if(!$("#"+id).is(":visible"))
		$("#"+id).animate({ height: 'show', opacity: 'show' }, 'slow');
	else
		$("#"+id).animate({ height: 'hide', opacity: 'hide' }, 'slow');	
}
function menu(_type)
{
	if(_type != type)
	{
		if(_type == 0)
			document.getElementById("sous_menu").innerHTML = '';
		else if(_type==1)
			document.getElementById("sous_menu").innerHTML = '<a href="radio?l='+_langue+'" onclick="document.location.hash=\'radio/l/'+_langue+'\';return false;"><div>'+_radio_langue+'</div></a><a href="radio"  onclick="document.location.hash=\'radio\';return false;"><div>'+_radio_world+'</div></a><a href="radio.php?w=1" onclick="document.location.hash=\'radio/w/1\';return false;"><div>'+_radio_web+'</div></a>';
		else if(_type==3)
			document.getElementById("sous_menu").innerHTML = '<div>'+_reveil+'</div><div>'+_app+'</div>';
		type = _type;
	}
}

var radio_clef = "";
var radio_id = 0;
function changeRadio(clef,id)
{
	if(radio_clef != clef)
	{	
		flux = synch("script/php/player.php","act=flux&radio="+clef);
		document.getElementById("for_ie").innerHTML = 
		'<object style="height: 32px; width: 220px;" codebase="http://www.microsoft.com/windows/mediaplayer" classid="clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95" id="player-radio-object_player"><param value="'+flux+'" name="filename"/><param value="true" name="autostart"/><param value="true" name="showcontrols"/><param value="5" name="Volume"/><embed id="embed_player" height="32" width="220" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#version=6,0,0,0" pluginspage="http://www.microsoft.com/windows/mediaplayer/" showcontrols="1" showdisplay="1" loop="1" showaudiocontrols="1" volume="16" showstatusbar="1" enabled="1" playcount="1" autosize="0" autostart="1" type="application/x-mplayer2" src="'+flux+'"/></object>';
		document.getElementById("player-radio-logo").src="http://www.titreradio.com/style/images/logo/"+clef+"_logo.png";
		radio_clef = clef;
		radio_id = id;
		changeContent();
	}
}
var plus_de_radio = false;
function selectSearchRadio(id)
{
	document.getElementById('player_radio').value = id;
	$('#player_radio').css('backgroundColor','#72C2B3');
	$('#player_radio').css('border','1px solid #0A69A1');
	window.scrollTo(0,0);
}
function plusDeRadio()
{
	if(!plus_de_radio)
	{
		document.getElementById('player_radio').value = synch("script/php/player.php","act=plus_de_radio");
		plus_de_radio = true;
	}
}
var id_s = 0;
function changeContent()
{
	song = eval('(' + synch("script/php/player.php","act=last&radio="+radio_id)+')');
	if(song["valid"] == "true")
	{
		if(song["id_s"] != id_s)
		{
			$("#player-radio-info").html('<em style="font-size:11px;">'+song["songs"]["date_time"][0]+"</em><br/>"+song["songs"]["nom"][0]+"<br/>"+song["songs"]["titre"][0]);
			$("#lien-ico-am").attr("href","http://www.amazon.fr/gp/search?ie=UTF8&tag=titreracom-21&keywords="+escape(song["songs"]["nom"][0])+"&index=music");
			$("#lien-ico-tw").attr("href","http://twitter.com/home?status=%23titreradio+j+aime+"+escape(song["songs"]["nom"][0]));
		}
		id_s = song["id_s"];
	}
	/*else
		document.getElementById("wrap").style.display = "none";*/
}

window.setInterval(function ()
{
	if(radio_id > 0)
		changeContent();
},15000);



var hash_en_cours = "";
window.setInterval(function () 
{
	hash_url = document.location.hash.substring(1);
	if(hash_url == "")
	{
		hash_en_cours = hash_url;
	}
	else if (hash_en_cours != hash_url)
	{
		$("#content").html("<center><img src='style/images/style/loading.gif'/></center>");
		if(hash_url == "index")
		{
			redirect("index","",hash_url);
		}
		else if (hash_url == "reveilmatin")
		{
			redirect("reveilmatin","",hash_url);
		}
		else if (hash_url == "appfacebook")
		{
			//redirect("index","",hash_url);
		}
		else if (hash_url == "radio")
		{
			redirect("radio","",hash_url);
		}
		else
		
		{
			tab_hash = hash_url.split('/');
			if(tab_hash[0] == "radio" && tab_hash[1] == "l" && tab_hash.length == 3)
			{
				redirect("radio","&l="+tab_hash[2],hash_url);
			}
			else if(tab_hash[0] == "radio" && tab_hash[1] == "l" && tab_hash[3] == "p" && tab_hash.length == 5)
			{
				redirect("radio","&l="+tab_hash[2]+"&p="+tab_hash[4],hash_url);
			}
			else if(tab_hash[0] == "radio" && tab_hash[1] == "p" && tab_hash.length == 3)
			{
				redirect("radio","&p="+tab_hash[2],hash_url);
			}
			else if(tab_hash[0] == "radio" && tab_hash[1] == "w" && tab_hash[2] == "1" && tab_hash.length == 3)
			{
				redirect("radio","&w=1",hash_url);
			}
			else if(tab_hash[0] == "radio" && tab_hash[1] == "w" && tab_hash[2] == "0" && tab_hash.length == 3)
			{
				redirect("radio","&w=0",hash_url);
			}
			else if(tab_hash[0] == "radio" && tab_hash[1] == "t" && tab_hash.length == 3)
			{
				redirect("radio","&t="+tab_hash[2],hash_url);
			}
			else if(tab_hash[0] == "radio" && tab_hash.length == 2)
			{
				redirect("radio","&r="+tab_hash[1],hash_url);
			}
			else if(tab_hash[0] == "artiste" && tab_hash.length == 2)
			{
				redirect("artiste","&a="+tab_hash[1],hash_url);
			}
			else if(tab_hash[0] == "artiste" && tab_hash.length == 3)
			{
				redirect("artiste","&a="+tab_hash[1]+"&s="+tab_hash[2],hash_url);
			}
			else if (tab_hash[0] == "search" && tab_hash.length == 2)
			{
				redirect("recherche","&k="+tab_hash[1],hash_url);
			}
			//"find/"+radio+"/"+jour+"/"+heure+"/"+minute;
			else if (tab_hash[0] = "find" && tab_hash.length == 7)
			{
				redirect("trouve","&r="+tab_hash[1]+"&j="+tab_hash[2]+"/"+tab_hash[3]+"/"+tab_hash[4]+"&h="+tab_hash[5]+"&m="+tab_hash[6],hash_url);
			}
			else
			{
				redirect("index","","index");
			}
		}
	}
},100);

function all_search()
{
	document.location.hash='search/'+document.getElementById("search_text").value;
}

function redirect(page,parametre,hash)
{
	$('#content').html(synch("http://www.titreradio.com/script/php/ajax.php",'_page='+page+parametre+'&'+'act=redirect&javascript=1'));
	document.location.hash = hash;
	hash_en_cours = hash;
	window.scrollTo(0,0);
	//pageTracker._trackPageview(page+"?"+parametre+"["+hash+"]");
}


var slideMenu=function(){
	var sp,st,t,m,sa,l,w,sw,ot;
	return{
		build:function(sm,sw,mt,s,sl,h){
			sp=s; st=sw; t=mt;
			m=document.getElementById(sm);
			sa=m.getElementsByTagName('li');
			l=sa.length; w=m.offsetWidth; sw=w/l;
			ot=Math.floor((w-st)/(l-1)); var i=0;
			for(i;i<l;i++){s=sa[i]; s.style.width=sw+'px'; this.timer(s)}
			if(sl!=null){m.timer=setInterval(function(){slideMenu.slide(sa[sl-1])},t)}
		},
		timer:function(s){s.onmouseover=function(){clearInterval(m.timer);m.timer=setInterval(function(){slideMenu.slide(s)},t)}},
		slide:function(s){
			var cw=parseInt(s.style.width,'10');
			if(cw<st){
				var owt=0; var i=0;
				for(i;i<l;i++){
					if(sa[i]!=s){
						var o,ow; var oi=0; o=sa[i]; ow=parseInt(o.style.width,'10');
						if(ow>ot){oi=Math.floor((ow-ot)/sp); oi=(oi>0)?oi:1; o.style.width=(ow-oi)+'px'}
						owt=owt+(ow-oi)}}
				s.style.width=(w-owt)+'px';
			}else{clearInterval(m.timer)}
		}
	};
}();


function form_search()
{
	radio = document.getElementById("player_radio").value;
	jour = document.getElementById("player_datepicker").value;
	heure = document.getElementById("select_heure").value;
	minute = document.getElementById("select_minute").value;
	document.location.hash = "find/"+radio+"/"+jour+"/"+heure+"/"+minute;
}
