//FICHIER JAVASCRIPT PRINCIPAL DU SITE

function formLoginOpen(){
	$('#hdr_actions').css({display:"none"});
	
	$('#hdr_connexionDialog_overlay').css({display:"block"});
	$('#hdr_connexionDialog').css({display:"block"});
}

function formLoginClose(){
	$('#hdr_connexionDialog').css({display:"none"});
	$('#hdr_connexionDialog_overlay').css({display:"none"});
	$('#hdr_actions').css({display:"block"});
}

//Ouvrir une popup vers le Player
function popupPlayer(groupe_id, fiche_id, chanson_id, album_id){
	window.open('/player-'+groupe_id+'-'+fiche_id+'-'+chanson_id+'-'+album_id+'.html','player','height=383,width=770,status=no,toolbar=no,menubar=no,location=no,top=150,left=150');
}


//Ouvre une popup avec configurée avec les paramètres donnés
function popupBug(strURL,strType,strHeight,strWidth){
	var strOpt='';
	var w=null;
	
	if(strType == 'elastic')
		strOptions='scrollbars, top=200, left=430, height='+strHeight+',width='+strWidth;
	if(strType == 'fixed')
		strOptions='top=200, left=500, height='+strHeight+',width='+strWidth;
	
	w=window.open(strURL, 'w', strOptions);
}


function popupContact(strURL,strType,strHeight,strWidth){
	var strOpt='';
	var w=null;
	
	if(strType == 'elastic')
		strOptions='scrollbars, top=200, left=430, height='+strHeight+',width='+strWidth;
	if(strType == 'fixed')
		strOptions='top=200, left=500, height='+strHeight+',width='+strWidth;
	
	w=window.open(strURL+'/'+document.location.href, 'w', strOptions);
}


/**
 *  Ouvre une popup à la taille de la page Aide sur le BBCode
 */
function popupAideBBCode(lien){
	window.open(lien,'AideBBCode','height=600,width=600,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,directories=no,status=no');
	return false;
}

//Ouvre une pop-up vers la page d'achat de parts (upmyprod)
/*
function popupAchatParts(lien){
	window.open(lien,'AchatParts','height=840,width=800,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,directories=no,status=no');
	return false;
}
*/


//Ouvre une pop-up vers la page d'achat de parts (upmyprod)
function popupAchatParts(lien,production){
	window.open(lien+production+'-'+document.getElementById('nombrePart-'+production).value,'AchatParts','height=840,width=800,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,directories=no,status=no');
	return false;
}

function popupAchatPartsDetail(lien,production){
	window.open(lien+production+'-'+document.getElementById('nombrePart').value,'AchatParts','height=840,width=800,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,directories=no,status=no');
	return false;
}



//Récupére un élément de la page à partir de son id (alias de document.getElementById)
function get(id){
	return document.getElementById(id);
}


//Equivalent JavaScript de la fonction htmlspecialchars() de PHP
function htmlspecialchars(string){
	string=string.replace(/&/g,'&amp;');
	string=string.replace(/"/g,'&quot;');
	string=string.replace(/\'/g,'&#39;');
	string=string.replace(/</g,'&lt;');
	string=string.replace(/>/g,'&gt;');
	return string;
}


//Equivalent JavaScript de la fonction htmlspecialchars_decode() de PHP
function htmlspecialchars_decode(string){
	string=string.replace(/&amp;/g,'&');
	string=string.replace(/&lt;/g,'<');
	string=string.replace(/&gt;/g,'>');
	string=string.replace(/&quot;/g,'"');
	string=string.replace(/&#39;/g,"'");
	return string;
}


//Permet de vérifier la validité d'une date au format jj-mm-aaaa (Source : http://www.javascriptfr.com/code.aspx?ID=15737)
function isDate(d){
	if(d == "") return false;
	
	e=new RegExp("^[0-9]{1,2}\/[0-9]{1,2}\/([0-9]{2}|[0-9]{4})$");
	
	if(!e.test(d)) return false;// On teste l'expression régulière pour valider la forme de la date
	
	// On sépare la date en 3 variables pour vérification, parseInt() converti du texte en entier
	j=parseInt(d.split("/")[0], 10); // jour
	m=parseInt(d.split("/")[1], 10); // mois
	a=parseInt(d.split("/")[2], 10); // année
	
	// Si l'année n'est composée que de 2 chiffres on complète automatiquement
	if(a < 1000){
		if(a < 89) a+=2000; // Si a < 89 alors on ajoute 2000 sinon on ajoute 1900
		else a+=1900;
	}
	
	// Définition du dernier jour de février
	// Année bissextile si annnée divisible par 4 et que ce n'est pas un siècle, ou bien si divisible par 400
	if(a%4 == 0 && a%100 !=0 || a%400 == 0) fev=29;
	else fev=28;
	
	// Nombre de jours pour chaque mois
	nbJours=new Array(31,fev,31,30,31,30,31,31,30,31,30,31);
	
	// Enfin, retourne vrai si le jour est bien entre 1 et le bon nombre de jours, idem pour les mois, sinon retourn faux
	return ( m >= 1 && m <=12 && j >= 1 && j <= nbJours[m-1] );
}



/**
 * Construit un datepicker (=calendrier JS) avec jQuery
 * Nécessite : jquery.js, ui.core.js, ui.datepicker.js, css/jquery/datepicker.css
 * Documentation : http://jqueryui.com/demos/datepicker/
 * @param    selector    jquery_selector    Sélecteur de type jQuery comme #id, .class, input, etc.
 */
function makeDatepicker(jquery_selector){
	$(jquery_selector).datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'dd-mm-yy',
		firstDay: 1,
		dayNamesMin: ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa'],
		dayNames: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'],
		monthNames: ['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre']
	});
}

/**
 * Construit un datepicker (=calendrier JS) avec jQuery
 * Nécessite : jquery.js, ui.core.js, ui.datepicker.js, css/jquery/datepicker.css
 * Documentation : http://jqueryui.com/demos/datepicker/
 * @param    selector    jquery_selector    Sélecteur de type jQuery comme #id, .class, input, etc.
 * @param    string    year_range    la fourchette d'année à afficher (ex:1950:2001)
 * @param    selector    default_date    la date selectionnée par defaut par rapport à ce jour (ex : '-20y' ou '+3m' ou '-7d')
 */
function makeDatepicker2(jquery_selector,year_range,default_date){
	$(jquery_selector).datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'dd-mm-yy',
		firstDay: 1,
		dayNamesMin: ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa'],
		dayNames: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'],
		monthNames: ['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre'],
		yearRange: year_range,
		defaultDate: default_date
	});
}

/**
 *  Publicité
 */
function pub(_page,_format){
	sas_pageid='5259/'+_page;// Page
	sas_formatid=_format;// Format
	sas_target='';// Targeting
	SmartAdServer(sas_pageid,sas_formatid,sas_target);
}

function skyscraper(){
	document.write('<object type="application/x-shockwave-flash"');
	document.write('data="http://www.zicmeup.com/flash/pub/Autopromo_Sky.swf"');
	document.write('width="160" height="600">');
	document.write('<param name="movie" value="http://www.zicmeup.com/flash/pub/Autopromo_Sky.swf" />');
	document.write('</object>');
}

function banniere(){
	document.write('<object type="application/x-shockwave-flash"');
	document.write('data="http://www.zicmeup.com/flash/pub/Autopromo_Bandeauhaut.swf"');
	document.write('width="728" height="90">');
	document.write('<param name="movie" value="http://www.zicmeup.com/flash/pub/Autopromo_Bandeauhaut.swf" />');
	document.write('</object>');
}

function pave(){
	document.write('<object type="application/x-shockwave-flash"');
	document.write('data="http://www.zicmeup.com/flash/pub/Autopromo_Carre.swf"');
	document.write('width="300" height="250">');
	document.write('<param name="movie" value="http://www.zicmeup.com/flash/pub/Autopromo_Carre.swf" />');
	document.write('</object>');
}

//redirection à partir d'une value de select box
function redirSelect(valeur){
	var adresse= valeur;
	if(adresse != '') self.location=adresse;
}

function confirmRedir(url,msg){
	if(confirm(msg)) document.location.replace(url);
}

function montreBloc(identifiant){
	var x=document.getElementById(identifiant);
	var typeNoeud=x.nodeName;
	if(typeNoeud=='TR' || typeNoeud=='tr'){
		if(navigator.appName=='Netscape') x.style.display='table-row';
		else x.style.display='inline';
	}
	else{
		x.style.display='block';
	}
	x.style.visibility='visible';
}

function cacheBloc(identifiant){
	var x=document.getElementById(identifiant);
	x.style.display='none';
	x.style.visibility='hidden';
}

function montreCache(identifiant){
	if(document.getElementById(identifiant).style.visibility=='visible') cacheBloc(identifiant);
	else montreBloc(identifiant);
}

//permet de récupérer les paramètre de l'URL cf PHP
function ParamUrl(){
	var param=new Array();
	var tmp=unescape(window.location.search).substr(1).split("&");
	var inter;
	for(i=0; i<tmp.length; i++){
		inter=tmp[i].indexOf("=");
		if(inter>=0) param[tmp[i].substr(0,inter)]=tmp[i].substr(inter+1)
		else param[tmp[i]]="";
	}
	return param;
}

//Utilise jQuery pour créer des tooltips sur les éléments dont le sélecteur jQuery est donné
function initializeTooltip(selector){
	$(document).ready(function(){
		$(function(){
			$(selector).tooltip({
				track:true,
				delay:0,
				showBody:' - ',
				showURL:false
			});
		});
	});
}

//remplace la fonction alert de javascript
//requis, jQuery dialog
function dAlert(titre,texte){
	$('#alerteDialog').remove();
	jQuery("<div>",{
		id:"alerteDialog"
	}).appendTo("body");
	$('#alerteDialog').html(texte);
	$('#alerteDialog').attr("title",titre);
	$('#alerteDialog').dialog({
		autoOpen:true,
		width:350,
		height:'auto',
		modal:true,
		resizable:false,
		position:'center',
		overlay:{opacity:0.5,background:'#000'},
		buttons:{'OK':function(){$(this).dialog('close');}}
	});
}
