// WWW.OYOBOO.COM - oyoboo.js
// Javascripts used by the site



// INITIALISATIONS
var ajaxSupported=check_AJAX();
var Notes=new Array();
var Appreciations=new Array('', 'average', 'good', 'best');

window.onload=music_store_init;



// INITIALISATIONS : loading functions
function music_store_init() {
	
	// MP3 dewplayer control
	play_music();
	
	// POLL behaviour
	poll_init();
	
	// PLAY / BASKET images behaviour
	roll_over();
	
	
	// LOGO behaviour
	if (document.getElementById("oyologo")) {
		document.getElementById("oyologo").onmouseover=function() {
			document.images[0].src="illustration-sonore/oyoboo-musique-libre-droits.gif";
		}
		document.getElementById("oyologo").onmouseout=function() {
			document.images[0].src="illustration-sonore/oyoboo-musique-libre-droit.gif";
		}
	}
	
	// BOOKMARK
	if (document.getElementById("bookmark")) {
		document.getElementById("bookmark").onclick=function() {
			
			// FireFox
			if (navigator.appName!='Microsoft Internet Explorer') {
				window.sidebar.addPanel("Oyoboo - Catalogue de musique libre de droit","http://www.oyoboo.com/","");
			}
			
			// Internet Explorer
			else {
				window.external.AddFavorite("http://www.oyoboo.com/","Oyoboo - Catalogue de musique libre de droit");
			}
			
			return false;
			
		}
	}
	
	// FORM : LOGIN
	if (document.getElementById("my_login")) {
		if (document.getElementById("email")) {
			document.getElementById("email").onfocus=document.getElementById("email").onclick=function() {
				if (document.getElementById("email").value=="E-mail...") {
					document.getElementById("email").value='';
				}
			}
		}
		if (document.getElementById("password")) {
			document.getElementById("password").onfocus=document.getElementById("password").onclick=function() {
				if (document.getElementById("password").value=="******") {
					document.getElementById("password").value='';
				}
			}
		}
	}
			
	
	// FORM : ACCOUNT CREATION
	if (document.getElementById("account")) {
		// firstname
		if (document.getElementById("firstname")) {
			document.getElementById("firstname").onfocus=document.getElementById("firstname").onclick=function() {
				if (document.getElementById("firstname").value=="Votre prénom...") {
					document.getElementById("firstname").value='';
				}
			}
		}
		// lastname
		if (document.getElementById("lastname")) {
			document.getElementById("lastname").onfocus=document.getElementById("lastname").onclick=function() {
				if (document.getElementById("lastname").value=="Votre nom...") {
					document.getElementById("lastname").value='';
				}
			}
		}
		// address
		if (document.getElementById("address")) {
			document.getElementById("address").onfocus=document.getElementById("address").onclick=function() {
				if (document.getElementById("address").innerHTML=="Votre adresse...") {
					document.getElementById("address").innerHTML='';
				}
			}
		}
		// zip
		if (document.getElementById("zip")) {
			document.getElementById("zip").onfocus=document.getElementById("zip").onclick=function() {
				if (document.getElementById("zip").value=="Votre code postal...") {
					document.getElementById("zip").value='';
				}
			}
		}
		// city
		if (document.getElementById("city")) {
			document.getElementById("city").onfocus=document.getElementById("city").onclick=function() {
				if (document.getElementById("city").value=="Votre ville...") {
					document.getElementById("city").value='';
				}
			}
		}
		// email3
		if (document.getElementById("email3")) {
			document.getElementById("email3").onfocus=document.getElementById("email3").onclick=function() {
				if (document.getElementById("email3").value=="Votre e-mail...") {
					document.getElementById("email3").value='';
				}
			}
		}
		// password3
		if (document.getElementById("password3")) {
			document.getElementById("password3").onfocus=document.getElementById("password3").onclick=function() {
				if (document.getElementById("password3").value=="******") {
					document.getElementById("password3").value='';
				}
			}
		}
		// password3_confirm
		if (document.getElementById("password3_confirm")) {
			document.getElementById("password3_confirm").onfocus=document.getElementById("password3_confirm").onclick=function() {
				if (document.getElementById("password3_confirm").value=="******") {
					document.getElementById("password3_confirm").value='';
				}
			}
		}
	}
	
	
	// FORM : ACCOUNT MODIFICATION
	
	
	// FORM : ACCOUNT BASKET LOGIN
	if (document.getElementById("account-login")) {
		// email2
		if (document.getElementById("email2")) {
			document.getElementById("email2").onfocus=document.getElementById("email2").onclick=function() {
				if (document.getElementById("email2").value=="Login") {
					document.getElementById("email2").value='';
				}
			}
		}
		// password2
		if (document.getElementById("password2")) {
			document.getElementById("password2").onfocus=document.getElementById("password2").onclick=function() {
				if (document.getElementById("password2").value=="********") {
					document.getElementById("password2").value='';
				}
			}
		}
	}
	
	// FORM : ACCOUNT BASKET CREATION
	if (document.getElementById("account-create")) {
		// firstname
		if (document.getElementById("firstname")) {
			document.getElementById("firstname").onfocus=document.getElementById("firstname").onclick=function() {
				if (document.getElementById("firstname").value=="Votre prénom...") {
					document.getElementById("firstname").value='';
				}
			}
		}
		// lastname
		if (document.getElementById("lastname")) {
			document.getElementById("lastname").onfocus=document.getElementById("lastname").onclick=function() {
				if (document.getElementById("lastname").value=="Votre nom...") {
					document.getElementById("lastname").value='';
				}
			}
		}
		// address
		if (document.getElementById("address")) {
			document.getElementById("address").onfocus=document.getElementById("address").onclick=function() {
				if (document.getElementById("address").innerHTML=="Votre adresse...") {
					document.getElementById("address").innerHTML='';
				}
			}
		}
		// zip
		if (document.getElementById("zip")) {
			document.getElementById("zip").onfocus=document.getElementById("zip").onclick=function() {
				if (document.getElementById("zip").value=="Votre code postal...") {
					document.getElementById("zip").value='';
				}
			}
		}
		// city
		if (document.getElementById("city")) {
			document.getElementById("city").onfocus=document.getElementById("city").onclick=function() {
				if (document.getElementById("city").value=="Votre ville...") {
					document.getElementById("city").value='';
				}
			}
		}
		// email3
		if (document.getElementById("email3")) {
			document.getElementById("email3").onfocus=document.getElementById("email3").onclick=function() {
				if (document.getElementById("email3").value=="Votre e-mail...") {
					document.getElementById("email3").value='';
				}
			}
		}
		// password3
		if (document.getElementById("password3")) {
			document.getElementById("password3").onfocus=document.getElementById("password3").onclick=function() {
				if (document.getElementById("password3").value=="******") {
					document.getElementById("password3").value='';
				}
			}
		}
		// password3_confirm
		if (document.getElementById("password3_confirm")) {
			document.getElementById("password3_confirm").onfocus=document.getElementById("password3_confirm").onclick=function() {
				if (document.getElementById("password3_confirm").value=="******") {
					document.getElementById("password3_confirm").value='';
				}
			}
		}
	}
	
	// FORM : PROMO-CODE GLOBAL
	if (document.getElementById("promo-code")) {
		if (document.getElementById("promo")) {
			document.getElementById("promo").onfocus=document.getElementById("promo").onclick=function() {
				if (document.getElementById("promo").value=="Votre code...") {
					document.getElementById("promo").value='';
				}
			}
		}
	}
	
	// FORM : PROMO-CODE BASKET
	if (document.getElementById("promo-code-basket")) {
		if (document.getElementById("promo-basket")) {
			document.getElementById("promo-basket").onfocus=document.getElementById("promo-basket").onclick=function() {
				if (document.getElementById("promo-basket").value=="Votre code...") {
					document.getElementById("promo-basket").value='';
				}
			}
		}
	}

	
	// FORM : SEARCH
	if (document.getElementById("search")) {
		if (document.getElementById("keyword")) {
			document.getElementById("keyword").onfocus=document.getElementById("keyword").onclick=function() {
				if (document.getElementById("keyword").value=="Rechercher par mots clefs...") {
					document.getElementById("keyword").value='';
				}
			}	
		}
	}
	
}



// AJAX support check
function check_AJAX() {
	
	// XMLHttpRequest : standard use
	if (window.XMLHttpRequest) {
	  
		return true;
	 
	} 
	
	// XMLHttpRequest : IE use
	else {
		
		// IE use : ActiveXObject
		if (window.ActiveXObject) {
		   
			try {
		   
				// IE use : standar use
				xhr = new ActiveXObject("Msxml2.XMLHTTP");
				
			} 
			
			catch (e) {
				
				try { 
				
					// IE use : older versions
	        		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	        		
	      		} 
	      		
	      		catch (e) {
		      		
		      		// IE use : unsupported version
	        		return false;
	      		}
	      		
	    	}
	    	
	    }
	 
	}

	return true;
		
}


// PLAY / BASKET images roll-over behaviour : initialisation
function roll_over() {
	
	if (!document.getElementById || !document.getElementsByTagName) {
			return true;
	}
	
	var available_images=document.getElementsByTagName('img');
	
	for (i=0; i<available_images.length; ++i) {
		
		if (available_images[i].getAttribute("src")) {
			
			var image_src=available_images[i].getAttribute("src");
			
			// Play button
			if (image_src.substr(image_src.length-20, 20) == "listen-music-mp3.png") {
			
				available_images[i].onmouseover=function() {
					this.src='/illustration-sonore/play-music-mp3.gif';
				}
				
				available_images[i].onmouseout=function() {
					this.src='/illustration-sonore/listen-music-mp3.png';
				}
				
			}
			
		
			// Basket button
			else if (image_src.substr(image_src.length-19, 19) == "acheter-musique.png") {
				
					available_images[i].onmouseover=function() {
						this.src='/illustration-sonore/telecharger-mp3.gif';
					}
					
					available_images[i].onmouseout=function() {
						this.src='/illustration-sonore/acheter-musique.png';
					}
					
				}
				
			}
	}
	
	return true;
	
}


// MP3 DEWPLAYER CONTROL : initialisation
function play_music() {
	
	if (!document.getElementById || !document.getElementsByTagName) {
			return true;
	}
	
	var referent=document.getElementById('marketing');
	
	if (referent) {
		
		var liens_son=referent.getElementsByTagName('a');
		
		for (i=0; i<liens_son.length; ++i) {
			
			if (liens_son[i].getAttribute("href") && (liens_son[i].getAttribute("rel") == "mp3")) {
				
				
				liens_son[i].onclick=function() { 
					
					var adresse_son=this.href;
					var titre_son=this.title;
					
					// URL
					var pre_son=adresse_son.substring(adresse_son.lastIndexOf("/"));

					var son=pre_son.substring(1);

					var pre_categorie=adresse_son.substring(0,adresse_son.length-pre_son.length);
					pre_categorie=pre_categorie.substring(pre_categorie.lastIndexOf("/"));
					
					var categorie=pre_categorie.substring(1);
					
					// TITLE META DATA
					var titre=titre_son.substring(18,titre_son.indexOf("(")-2);
					var titre_court=(titre.length>22?titre.substring(0,21)+"...":titre);
					
					var meta=titre_son.split(' | ');

					var appreciation=parseInt(meta[3].substr(7,1));
					var bpm=meta[2].substring(0,meta[2].length-3);
					var duree=meta[1];
					var genre=meta[0];
					var auteur=meta[4].substring(9,meta[4].length-1);
					
					genre=genre.substring(genre.indexOf("(")+1);
					
					Notes[son]=appreciation;
					
					
					if (document.getElementById('ecouter-musique')) {
						
						document.getElementById('listen-music').innerHTML='<object id="dewplayer-oyoboo" width="180" height="20" data="/dewplayer.swf?son=/listen/'+son+'&amp;autostart=1&amp;autoreplay=1" type="application/x-shockwave-flash"><param name="movie" value="dewplayer.swf?son=/listen/'+son+'&amp;autostart=1&amp;autoreplay=1" /><param name="quality" value="high" /><param name="bgcolor" value="277621" /><param name="allowScriptAccess" value="sameDomain" /><param name="swremote"  value="&amp;FlashVarsValue=BackToTheHtml%20example" /><param name="flashvars" value="&amp;FlashVarsValue=BackToTheHtml%20example" /></object>';
						/*var dp = document.getElementById("dewplayer-oyoboo");
						dp.rewind();
						dp.setVariable("son", '/listen/'+son);
						dp.play();*/
						
						document.getElementById('music-title').innerHTML='<a href="/basket/add/'+son+'" title="Ajouter le titre \''+titre+'\' à mon panier">'+titre_court+'</a>';
						
						if (bpm=='0') {
							if (document.getElementById('music-bpm')) {
								document.getElementById('music-bpm').innerHTML='';
							}
						}
						else {
							if (document.getElementById('music-bpm')) {
								document.getElementById('music-bpm').innerHTML=bpm;
							}
						}
						
						document.getElementById('music-author').innerHTML=auteur;
						
						document.getElementById('music-length').innerHTML=duree;
						
						document.getElementById('music-style').innerHTML='<a href="catalog/'+categorie+'" title="Ecouter tous les titres de la catégorie \''+genre+'\'">'+genre+'</a>';

						var myPoll='';

						// Votes : 0 to appreciation
						for (var i=1; i<=appreciation; i++) {
							myPoll+='<a href="/vote/'+Appreciations[i]+'/'+categorie+'/'+son+'" title="Voter pour le titre \''+titre+'\' : '+i+'/3" rel="poll"><img name="poll-'+son+'-'+i+'" src="illustration-sonore/voter-musique.png" alt="Voter pour le titre \''+titre+'\' : '+i+'/3" title="Voter pour le titre \''+titre+'\' : '+i+'/3" /></a>';
						}
						
						// Votes : appreciation+1 to 3
						for (var j=appreciation+1; j<=3; j++) {
							myPoll+='<a href="/vote/'+Appreciations[j]+'/'+categorie+'/'+son+'" title="Voter pour le titre \''+titre+'\' : '+j+'/3" rel="poll"><img name="poll-'+son+'-'+j+'" src="illustration-sonore/musique-voter.png" alt="Voter pour le titre \''+titre+'\' : '+j+'/3" title="Voter pour le titre \''+titre+'\' : '+j+'/3" /></a>';
						}
						
						// Link to basket
						myPoll+='<a id="acheter-musique" href="basket/add/'+son+'" title="Ajouter le titre \''+titre+'\' à mon panier"><img id="musique-acheter" src="illustration-sonore/acheter-musique.png" alt="Ajouter le titre \''+titre+'\' à mon panier" title="Ajouter le titre \''+titre+'\' à mon panier" /></a>'
						
						document.getElementById('poll').innerHTML=myPoll;
						
						
						// Behaviour re-initialisation	
						music_store_init();
						
						
						return false;
						
					}
					
					else {
														
						return true;
						
					}
					
				}
				
			}
			
		}
		
	}
	
	return true;
	
}



// POLL : initialisation
function poll_init() {
	
	if (!document.getElementById || !document.getElementsByTagName) {
			return true;
	}
	
	var myPoll=document.getElementById("poll");

	if (myPoll) {
				
		// Inactivate poll links			
		var liens_poll=myPoll.getElementsByTagName('a');
		
		for (i=0; i<liens_poll.length; ++i) {
			
			if (liens_poll[i].getAttribute("href") && (liens_poll[i].getAttribute("rel") == "poll")) {
				
				liens_poll[i].onclick=function() { 
					//alert("click");
					// Do not follow link (if AJAX)
					return !ajaxSupported;
					
				}
				
			}
			
		}
		
		//if (ajaxSupported) {
			
			// Activate poll behaviour
			for (var j=1; j<=3; j++) {
				
				var images_poll=myPoll.getElementsByTagName('img');
				
				//alert("size="+images_poll[0].name);
				var proposition=images_poll[0].name.substr(5,images_poll[j].name.length-7);
				//alert("prop="+proposition);
				Notes[proposition]=0;
				
				for (j=0; j<3; ++j) {
					
					images_poll[j].onmouseover=poll_rollover;
					images_poll[j].onmouseout=poll_rollout;
					images_poll[j].onclick=poll_select;
					
					//var proposition=images_poll[j].name.substr(5,images_poll[j].name.length-7);
					//alert("proposition="+proposition);
					
					//alert("img = "+images_poll[j].src);
					//if (images_poll[j].src=="http://www.oyoboo.dev/illustration-sonore/voter-musique.png") {
					if (images_poll[j].src.indexOf("voter-musique.png")!=-1) {
						Notes[proposition]=j+1;
					}
					//alert("Notes="+Notes[proposition]);
				}
	
			}
				
		}
		
	//}
	
	return true;
	
}



// POLL : roll-over
function poll_rollover(ref) {
	
	var test=this.name

	if (test!='') {
		ref=test;
	}
	
	//alert("over: ref="+ref);
	var maProposition=ref.substr(5,ref.length-7);
	var maNote=parseInt(ref.substr(ref.length-1, 1));

	// Update status : 1 to selected note
	for (var i=1; i<=maNote; i++) {
		document.images["poll-"+maProposition+"-"+i].src="/illustration-sonore/voter-musique.png";
	}
	
	// Update status : selected note to 3
	for (var j=maNote+1; j<=3; j++) {
		document.images["poll-"+maProposition+"-"+j].src="/illustration-sonore/musique-voter.png";
	}

	return true;
	
}




// POLL : roll-out
function poll_rollout(ref) {
	
	ref=this.name;

	//alert("out : ref="+ref);
	var maProposition=ref.substr(5,ref.length-7);
	var maNote=parseInt(ref.substr(ref.length-1, 1));
	//alert("out : ref="+ref+" / maProp="+maProposition+" / maNote="+maNote);
	poll_rollover("poll-"+maProposition+"-"+Notes[maProposition]);
	
	return true;
	
}



// POLL : wait animation
function poll_wait(proposition,status) {
	
	if (status==3) {
		status=1;
	}
	else {
		status++;
	}
	
	poll_rollover("poll-"+proposition+"-"+status);
	
	var maChaine="poll_wait(\""+proposition+"\","+status+")";
	
	setTimeout(maChaine, 1000);
	
}



// POLL : click
function poll_select(ref) {
	
	ref=this.name;

	var maProposition=ref.substr(5,ref.length-7);
	var maNote=ref.substr(ref.length-1, 1);

	//alert("maProp="+maProposition+" - maNote="+maNote);
	
	var xhr=new XMLHttpRequest();
	
	xhr.onreadystatechange=function() {
		
		if (xhr.readyState==1) {
			
			// Please wait...
			//alert("GO!");
			poll_wait(maProposition,0);
			//alert("Vote go...");
			
		}
		
		if (xhr.readyState==4) {
			
			if (xhr.status==200) {
								
				// Data feedback treatment			
				var maReponse=xhr.responseXML;
				
				var reponseVote=maReponse.getElementsByTagName("music");

				if (reponseVote[0].getAttribute("note")) {
					
					Notes[maProposition]=reponseVote[0].getAttribute("note");
					
					var reponseStatus=reponseVote[0].getAttribute("status");
					
					//alert("Status="+reponseStatus);
					
					// ... A COMPLETER ...
					
					poll_rollover("poll-"+maProposition+"-"+Notes[maProposition]);
					
					// ... PB GESTION STATUS + SURCHARGE MEMOIRE ...
					
				}
				
			}
			
		}
		
	}
	
	xhr.open("GET", "/votes.php?proposition="+maProposition+"&vote="+maNote, true);
	
	//alert("good!");
	xhr.send(null);
	
	return true;
	
}

