﻿var textLoading = lg_loading + '<br/><img src="img/progressbar.gif" alt="'+lg_loading+'"/>';

function affPopupDownload(cle,titre){
	var url = "download_choice.php?cle="+cle;
	var win = new Window({className: "alphacube", title: titre, draggable: false, width:360, height:150, zIndex: 100, url: url, showEffectOptions: {duration:0.5}})
	win.showCenter(); 
}
function affTabContact( ) {
	closeAlldiv();
	affPopup( 'divContact' );
}
function popupCgu(){
	$('chk_cgu').checked = true;
	var top=(screen.height-500)/2;
  var left=(screen.width-930)/2;
  window.open("cgu.php","","top="+top+",left="+left+",width=930,height=500,menubar=yes,scrollbars=yes,statusbar=yes");
}
function affPopupCGV(){
	$('chk_cgv').checked = true;
	var top=(screen.height-500)/2;
  var left=(screen.width-930)/2;
  window.open("cgv.php","","top="+top+",left="+left+",width=930,height=500,menubar=yes,scrollbars=yes,statusbar=yes");
}
function closeAlldiv( ) {
	$$("div.popup").each( function( el ){
		el.hide();
	});
	$$("div.popup2").each( function( el ){
		el.hide();
	});
}
/** Initialisation de l'application */
function initApplication( ) {

	$('ref').hide();
	closeAlldiv();
	$('grise').show();

	var opt = {
    method: 'post',
    postBody: "action=initApplication",
    onSuccess: function(t) {

    	var rep = eval("(" + t.responseText + ")");
    	 $('logging').innerHTML = rep.htmlLogging;
    	 $('content').innerHTML = rep.htmlCatalogue;
    	 $('panierNb').innerHTML = rep.htmlPanier;
    	 $('historiqueContent').innerHTML = rep.htmlHisto;
    	 $('divFiltre').innerHTML = rep.htmlFiltre;
    	 $('lecteurContent').innerHTML = rep.htmlBienvenue;
    	 $('divRecherche').innerHTML = rep.htmlRecherche;
    	 $('initApplication').hide();
    	 $('grise').hide();
    	 $('texteCritereRecherche').hide();
    	 tooltips();
    	 ecouteLinkWin();
    }
  }
  new Ajax.Request('dispatcher.php', opt);

	//referencement();
}
function changeLangue( lang ){
	var opt = {
    method: 'post',
    postBody: "action=language&lang="+lang,
    onSuccess: function(t) {
    	 window.location = '.';
    }
  }
  new Ajax.Request('dispatcher.php', opt);
}
function referencement() {
	var URL = window.location.href;
	//Affiche un popup
	if( eregi( URL, 'act' ) )
	{
		if( eregi( URL, 'tarif' ) )
			affPopup("divTarif");
		else if( eregi( URL, 'societe' ) )
			affPopup("divMusicjag");
		else if( eregi( URL, 'contact' ) )
			affPopup("divContact");
	}
	else if( eregi( URL, 'ecoute' ) )
	{
		var id = getParam('ecoute');
		ecoute( id );
	}
}
function getParam(name){
	var start=location.search.indexOf("?"+name+"=" );
	if (start<0) start=location.search.indexOf("&"+name+"=" );
	if (start<0) return '';
	start += name.length+2;
	var end=location.search.indexOf("&",start)-1;
	if (end<0) end=location.search.length;
	var result='';
	for(var i=start;i<=end;i++) {
		var c=location.search.charAt(i);
		result=result+(c=='+'?' ':c);
	}
	return unescape(result);
}
function ecouteLinkWin( ) {
	var win = '';
	var liens = $$('body a');
	liens.each( function( ele ) {
		if( eregi(ele.className,"win")  )
		{
			$(ele).observe('click', function(e) {
				Event.stop(e);
				affPopup( this.getAttribute('attr') );
			} );
		}
	});
}
var overlay = '';
function affPopup( nomPopup ){
	var opt = {
    method: 'post',
    postBody: "action=popup&titre="+escape(nomPopup),
    onSuccess: function(t) {
    	 var rep = eval("(" + t.responseText + ")");
    	 if( $(nomPopup) )
    	 {
    	 	new Effect.ScrollTo('all',{duration:0.5} );
			 	$(nomPopup).innerHTML = rep.htmlRendu;
			 	var h = getScrollHeight() + document.viewport.getHeight();
			 	$('grise').style.height = h+"px";
    	 	$('grise').show();
    	 	$(nomPopup).show();

    	 	if( "divCompte" == nomPopup )
    	 	{
    	 		initPopup("divCompte");
    	 		if( $('tableMorTriable') ) new TableOrderer('tableMorTriable',{data : rep.dataMor});
    	 		if( $('tableCmdtriable') ) new TableOrderer('tableCmdtriable',{data : rep.dataCmd});
    	 		if( $('tableDwnTriable') ) new TableOrderer('tableDwnTriable',{data : rep.dataDwn, unsortedColumn : ['action']});
    	 	}
    	 	if( "divPanier" == nomPopup )
    	 		if( $('tablePanTriable') ) new TableOrderer('tablePanTriable',{data : rep.dataPan });

    	 	if( "divContact" == nomPopup )
    	 		initPopup("divContact");
    	 	if( "divCompo" == nomPopup )
    	 		initPopup("divCompo");
    	 	if( "divMusicjag" == nomPopup )
    	 		initPopup("divMusicjag");
    	 	if( "divTarif" == nomPopup )
    	 		initPopup("divTarif");
    	 	tooltips();
    	}
    }
  }
  new Ajax.Request('dispatcher.php', opt);
}
function closeWin( div ){
	$('grise').hide();
	$(div).hide();
}
function triResultat( typeTri ) {
		//définition de l'order
		var order = '';
		( $(typeTri).getAttribute('attr') == '' || $(typeTri).getAttribute('attr') == 'asc'  ) ? order = 'desc' : order = 'asc';
		$('content').innerHTML = '<p style="text-align:center;">'+textLoading+'</p>';
		var opt = {
	    method: 'post',
	    postBody: "action=tri&type="+ escape( typeTri )+"&order="+escape( order ),
	    onSuccess: function(t) {
	    	var rep = eval("(" + t.responseText + ")");
	     	$('content').innerHTML = rep.htmlCatalogue;
				tooltips();
	    }
	  }
	  new Ajax.Request('dispatcher.php', opt);

}
function commander( numCommande ) {
	var opt = {
    method: 'post',
    postBody: "action=commander&numCommande=" + escape( numCommande ),
    onSuccess: function(t) {
    	 var rep = eval("(" + t.responseText + ")");
    	 if( rep.loggued == 0 )
    	 {
    	 	alert( rep.texte );
    	 	if(rep.htmlLogging)
    	 	{
	    	 	$('logging').innerHTML = rep.htmlLogging;
	    	 	$$('#logging a').each(function(ele){
					ele.observe('click', function(e) {affPopup( this.getAttribute('attr') )});
				} );
	    	 }
	    }
		 else if( rep.loggued == 1 )
    	 {
    	 		closeAlldiv();
    	 		if( $('catalogue')) $('catalogue').remove();
    	 		if( $('lat_droite')) $('lat_droite').remove();
    	 		if( $('footer')) $('footer').innerHTML = '<br/><br/><br/><i>'+lg_call+' 33 (0)2 97 53 06 39</i><br/>';
    	 		if( rep.etape == 1 )
    	 		{
    	 			$('commande').innerHTML = rep.texte;
    	 			$('divFormProducteur').innerHTML = rep.producteur;
     	 		}
    	 		$('conteneur').show();
    	 		$('grise').hide();
    	 		$('divPanier').hide();

    	 		if( "" != numCommande )
    	 		{
    	 			$('cmdEtape2').show();
    	 			if( $('listCommande2').innerHTML == "" )
    	 				$('trDestination').hide();
    	 			if( $('listCommande3').innerHTML == "" )
							$('trDetail').hide();
    	 			$('trType').show();
				  	if( rep.intituleType == 0 )
				  	 	$$('.isaffaiche').each( function(ele){ ele.hide(); } );
				  	else
				  	 	$$('.isaffaiche').each( function(ele ){ ele.show(); } );
    	 		}
    	 		else
    	 		{
    	 			$('cmdEtape2').hide();
    	 			$('trDetail').hide();
    	 			$('trDestination').hide();
    	 			$('trType').hide();
    	 		}



    	 		//Form producteur
    	 		//$('pTva').hide();
			 		//$('prod_tva').value = '';
    	 }
    }
  }
  new Ajax.Request('dispatcher.php', opt);
}
/** Incruste les tooltips **/
function tooltips( ) {
	/*
	$$(".action").each( function(link) {
		new Tooltip(link, {backgroundColor: "#FC9", borderColor: "#C96", textColor: "#000", textShadowColor: "#FFF"});
	});
	*/
}
/** Met à 0 les indes des select */
function resetFiltre( ) {
	$$('select.selFilter').each( function( ele ){
		ele.selectedIndex = 0;
	});
}
/** Met à vide le champ recherche */
function resetRecherche( ) {
	$('search').value = '';
}
/** Navigation result */
function page( limite ) {
	closeAlldiv();
	new Effect.Appear('content', {duration:0.2, fps:25, from:1.0, to:0.2});
	if( $('filtre') ) $('filtre').hide();
	if( $('recherche') ) $('recherche').hide();
	if( $('filtreO') ) $('filtreO').show();
	if( $('rechercheO') ) $('rechercheO').show();
	var opt = {
    method: 'post',
    postBody: "action=catalogue&limit="+ escape( limite ),
    onSuccess: function(t) {
     	$('content').innerHTML = t.responseText;
    	new Effect.ScrollTo('catalogue',{duration:0.5} );
			new Effect.Appear('content', {duration:0.5, fps:25, from:0.2, to:1.0});
			new Effect.Pulsate('lienSelected');
			tooltips();
    }
  }
  new Ajax.Request('dispatcher.php', opt);

}
/** Affichage des div filtre / recherche */
function affDiv( idShow, idHide ){
	closeAlldiv();
	$(idHide).hide();
	$(idShow).show();
}
/** Liens pied de page */
function listenAncre( ) {
	closeAlldiv();
	if( $('contentModal') )
	{
		var liensAncre = $A( $$('a.ancre') );
		liensAncre.each( function( ele ) {
			$(ele).observe('click', function(a) {
				Event.stop(a);
				new Effect.ScrollTo(this.getAttribute('attr'),{duration:0.5} );
			} );
		});
	}
}
/** Ecoute d'un morceau */
function ecoute( id ) {

	$$('a.ecoute').each( function(ele){
		if( ele.getAttribute('rel') )
		{
			var tr = ele.getAttribute('rel');
			if( null != tr)
				$(tr).removeClassName('enlecture');
		}
	});

	$('lecteurContent').innerHTML = "<div class='textLoadingPlayer'>"+textLoading+"</div>";
	var opt = {
    method: 'post',
    postBody: "action=lectureHistorique&idMorc="+ escape( id ),
    onSuccess: function(t) {
    	 var rep = eval("(" + t.responseText + ")");
    	 $('lecteurContent').innerHTML = rep.htmlLecteur;
    	 if( rep.shake )
    	 {
    	 	$('historiqueContent').innerHTML += rep.htmlHisto;
    	 	tooltips();
    	 }
    	 $$('a.ecoute').each( function(ele){
    	 		if( ele.getAttribute('attr') == id )
    			{
    				Effect.Pulsate( ele, { pulses: 5, duration: 1.5 });
    				var tr = ele.getAttribute('rel');
    	 			$(tr).addClassName('enlecture');
    	 		}
    	 });
    	 if(vip==1){
			playYvette();
		}
    }
  }
  new Ajax.Request('dispatcher.php', opt);
}
/** Ajout d'un morceau dans le panier */
function ajout( id ) {
	$('panierContent').innerHTML = '';
	$('panierContent').show();
	var opt = {
    method: 'post',
    postBody: "action=panierAjout&id="+ escape( id ),
    onSuccess: function(t) {
    	  var rep = eval("(" + t.responseText + ")");
    	  $('panierContent').innerHTML = rep.titre;
				$('panierNb').innerHTML = rep.nb;
				new Effect.Fade('panierContent',{delay:0.5});
		}
  }
  new Ajax.Request('dispatcher.php', opt);
}
/** Suppression d'un article du panier */
function supprime( id ) {
	$('trPanier'+id).remove();
	new Ajax.Updater( 'panierNb','dispatcher.php',{ method:'post', parameters:{action:'panierSup',id:id},
		onSuccess:function(){new Effect.Pulsate('panier'); }
	} );
}
/** Suppression du panier */
function videPanier( ) {
	var opt = {
    method: 'post',
    postBody: "action=panierVide",
    onSuccess: function(t) {
    	 var rep = eval("(" + t.responseText + ")");
    	 $('panierNb').innerHTML  = rep.htmlpanier;
    	 $('divPanier').innerHTML = rep.htmlpanierVisu;
    	 tooltips();
    }
  }
  new Ajax.Request('dispatcher.php', opt);
}
function reprendrePanier( intVal ) {
	var opt = {
    method: 'post',
    postBody: "action=reprendrePanier&val="+escape( intVal ),
    onSuccess: function(t) {
    	 var rep = eval("(" + t.responseText + ")");
    	 $('panierNb').innerHTML = rep.htmlPanier;
    	 tooltips();
    	 $('grise').hide();
    	 $('divContenuCommande').hide();
    }
  }
  new Ajax.Request('dispatcher.php', opt);
}
/** Information d'un morceau */
var boolInsertion = true;
var oldIdBtInfo 	= -1; //lors d'un 2eme clique on supprime la versionTr
function info( id, idLigne ) {

	if( oldIdBtInfo != id )
	{
		oldIdBtInfo = id;
		if( boolInsertion )
		{
			boolInsertion = false;
			var txt = templateVersion();
			new Insertion.After( 'tr'+idLigne, txt );

			if( $('versionTr') )
				$('versionTr').remove();
			var opt = {
		    method: 'post',
		    postBody: "action=version&id="+ escape( id ),
		    onSuccess: function(t) {
		   		$('versionTrAtt').remove();
		   		new Insertion.After( 'tr'+idLigne, t.responseText );
		    	boolInsertion = true;
		    }
		  }
		  new Ajax.Request('dispatcher.php', opt);
		}
		tooltips();
	}
	else if( $('versionTr') )
	{
		oldIdBtInfo = -1;
		$('versionTr').remove();
	}
}
function fermerInfo( ) {
	$('versionTr').remove();
	boolInsertion = true;
	oldIdBtInfo 	= -1;
}
/** Ajoute un morceau à l'historique */
function historique( id ) {
	var opt = {
    method: 'post',
    postBody: "action=ajoutHisto&id="+ escape( id ),
    onSuccess: function(t) {
   		$('historiqueContent').innerHTML += t.responseText ;
   		new Effect.Pulsate('historique');
   		tooltips();
    }
  }
	new Ajax.Request('dispatcher.php', opt);
}
/** Lance le player */
function lecture( id ) {
	ecoute( id );
}
/** Ajoute un filtre */
function addFilter( typeFiltre, val ) {
	//met a 1 la visibilié
	 $$('#critereContent a').each( function( ele ) {
		 ele.style.opacity = 1;
	 });
	$('content').innerHTML = '<p style="text-align:center;margin-top:50px;">'+textLoading+'</p>';
	var opt = {
    method: 'post',
    postBody: "action=actionfilter&op=add&type="+ escape( typeFiltre )+"&val="+ escape( val ),
    onSuccess: function(t) {
    	var rep = eval("(" + t.responseText + ")");
    	$('content').innerHTML = rep.htmlCatalogue;

    	if('bibliotheque' == typeFiltre || 'selection' == typeFiltre)
    	{
    	  $$('#critereContent a').each( function(ele){
    		 if(eregi(ele.id,'bibliotheque') || eregi(ele.id,'selection') )
    			 $( ele.id ).remove();
    	  });
    	}
    	$('critereContent').innerHTML += rep.htmlFiltre;

    	var idFiltre 	= typeFiltre.toString();
    	var nomDiv 	= 'sel' + idFiltre.substr(0, 1).toUpperCase() + idFiltre.substr(1);
    	if( $(nomDiv) )
    		$(nomDiv).selectedIndex = 0;

    	tooltips();

    	if( rep.htmlClignote != '' )
    		new Effect.Pulsate( rep.htmlClignote );
    }
  }
  new Ajax.Request('dispatcher.php', opt);
}
/** Supprime un filtre */
function supFiltre( type, val ) {
	$('content').innerHTML = '<p style="text-align:center;">'+textLoading+'</p>';
	var opt = {
    method: 'post',
    postBody: "action=actionfilter&op=sup&type="+ escape( type )+"&val="+ escape( val ),
    onSuccess: function(t) {
    	 var rep = eval("(" + t.responseText + ")");
    	 $('content').innerHTML = rep.htmlCatalogue;
    	 $('critereContent').innerHTML = rep.htmlFiltre;
    	 tooltips();
    }
  }
  new Ajax.Request('dispatcher.php', opt);
}
/** Recherche */
function recherche( ) {
	var mot = $('search').value;
	if( "" != mot && mot.length > 2 )
	{
		$('content').innerHTML = '<p style="text-align:center;">'+textLoading+'</p>';
		$('texteCritereRecherche').show();
		if( mot.length < 30 )
			$('critereRecherche').innerHTML = mot;
		var opt = {
	    method: 'post',
	    postBody: "action=recherche&mot="+ escape( mot ),
	    onSuccess: function(t) {
	    	 var rep = eval("(" + t.responseText + ")");
	    	 $('content').innerHTML = rep.htmlCatalogue;
	    	 tooltips();
	    }
	  }
  	new Ajax.Request('dispatcher.php', opt);
  }
}
function supRecherche( ) {
	$('content').innerHTML = '<p style="text-align:center;">'+textLoading+'</p>';
	$('search').value = '';
	var opt = {
	    method: 'post',
	    postBody: "action=recherche&mot=",
	    onSuccess: function(t) {
	    	 var rep = eval("(" + t.responseText + ")");
	    	 $('content').innerHTML = rep.htmlCatalogue;
	    	 $('critereRecherche').innerHTML = '';
	    	 $('texteCritereRecherche').hide();
	    	 tooltips();
	    }
	  }
  	new Ajax.Request('dispatcher.php', opt);
}
function templateVersion() {
	return '<tr id="versionTrAtt"><td colspan="4"><table id="version" style="width:95%;"><tr><td colspan="4"><img src="img/progressbar.gif" alt=""/> '+lg_loading+'...</td></tr></table></td></tr>';
}
function modifCompteUser(  ) {
	var opt = {
	    method: 'post',
	    postBody: "action=modifCompteUser&" + $('modifCompteForm').serialize(),
	    onSuccess: function(t) {
	    	var rep = eval("(" + t.responseText + ")");
	    	if( rep.error == 1 )
	    		alert( rep.msg );
	    	else
	    	{
	    	 	$('logging').innerHTML = rep.logging;
	    	 	$('liendivCompte').observe('click', function(e) {
							Event.stop(e);
							affPopup( this.getAttribute('attr') );
						} );
	    	 	alert( rep.alert );
	    	}
	    }
	   }
  new Ajax.Request('dispatcher.php', opt);
}
function afficheDetailMorceau( idMorceau ) {
	var opt = {
	    method: 'post',
	    postBody: "action=affDetailMorceau&cleMorceau=" + escape( idMorceau ),
	    onSuccess: function(t) {
	    	var rep = eval("(" + t.responseText + ")");
	    		var h = getScrollHeight() + document.viewport.getHeight();
			 		$('grise').style.height = h+"px";
	    	 $('grise').show();
	    	 $('divInfoMorceau').innerHTML = rep.html;
	    	 $('divInfoMorceau').show();
	    }
	}
  new Ajax.Request('dispatcher.php', opt);
}
/* initialisation des popup avec tabs */
function initPopup( nomPopup ) {

	var nameDiv1 = "#"+nomPopup+" div.affichable";
	var nameDiv2 = "#"+nomPopup+" li a";

	//cache tous les divs sauf le 1er
	var first = 1;
	if( $(nomPopup) )
	{
		$$(nameDiv1).each( function( el ) {
			if( first == 1 ) first = 2;
			else el.hide();
		});
	}
	//ecouteur sur les liens du menu
	if( $(nomPopup) )
	{
		$$(nameDiv2).each( function( el ) {
			Event.observe( el ,'click', function( e ){
				Event.stop( e );
				//cache tous les div
				$$(nameDiv1).each( function( ele ) {ele.hide();});
				//récupère le div à afficher
				var attr = el.getAttribute('attr');
				//attr est vide si click sur le lien "fermer fenetre"
				if( "" != attr ) $(attr).show();
				//supprime tous les backgroundColor
				$$(nameDiv2).each( function( eleA ) {eleA.className = ''});
				//backgroundColor affecté pour le div affiché
				el.className = 'activer';
			});
		});
	}
}

/** gestion des formulaire de contact */
function formContactSubmit( origine ) {
	var opt = {
	    method: 'post',
	    postBody: "action=formContact&origine="+escape( origine )+ "&" + $('formContact').serialize(),
	    onSuccess: function(t) {
	    	var rep = eval("(" + t.responseText + ")");
	    	alert( rep.msg );
	    	if( rep.error == 0 )
	    		$('msg').value = '';
	    }
	   }
  new Ajax.Request('dispatcher.php', opt);
}
/** tarif */
function devisExpress(intitule){
	var opt = {
	    method: 'post',
	    postBody: "action=affLicencePrix&idcat="+escape(intitule)+"&nbSec="+$('seconde').value,
	    onSuccess: function(t) {
	    	var rep = eval("(" + t.responseText + ")");
	    	$('devisExpress_result').innerHTML = rep.html;
	    }
	}
  new Ajax.Request('dispatcher.php', opt);
}
function configureMorceau( cle ) {
	var hauteur = 550;
	var largeur = 850;
	var options = null
	var top =(screen.height-hauteur)/2;
	var left =(screen.width-largeur)/2;
	window.open("modifMorceau.php?cle="+escape(cle),"","top="+top+",left="+left+",width="+largeur+",height="+hauteur+","+options);
}
function eregi( chaine, find ) {
  var norm = new RegExp( find );
  return norm.exec( chaine )!=null;
}
function getScrollHeight(){
   var h = window.scrollMaxY ||
   				 window.screen.height ||
           document.body.scrollTop ||
           document.documentElement.scrollTop;

   return h ? h : 0;
}
function initEvent( e ) {
	Event.stop(e);
	/** Initialisation de l'application */
	initApplication();
}
Event.observe(window ,'load', initEvent);

