var g_objImages = new Array();
var g_strPageName = "index";
var g_strPlayerAction = "stop";
var g_strLastRollDateId = "none";
var g_strLastSelectedDateId = "none";
var g_strLastRollSongId = "none";
var g_strLastSelectedSongId = "none";
var g_intLastArticleId = "-1";

function pip ( p_intId ) {
	window.location.href = "boutiqueSession.php?ACTION=pip&ID="+p_intId+"&Q="+document.getElementById("pipCount"+p_intId).value;
}
var g_intTotalCommande = 0;
function commandePort( p_boolPort ) {
	if (g_intTotalCommande == 0) { 
		g_intTotalCommande = eval(document.getElementById("totalCommande").innerHTML);
	}
	document.getElementById("totalCommande").innerHTML = ( p_boolPort ? g_intTotalCommande+",00 € + port ("+document.getElementById("port").innerHTML+",00 €) = " + eval(g_intTotalCommande + eval(document.getElementById("port").innerHTML)) : g_intTotalCommande )
}
function showArticle( p_objId ) {
	var l_intArticleId = replace(p_objId.id,'article','');
	document.getElementById("description"+l_intArticleId).style.display = "block";
	document.getElementById("article"+l_intArticleId).style.background = "#c7c29a";
	
	if (g_intLastArticleId!="-1" && g_intLastArticleId!=l_intArticleId) {
		document.getElementById("description"+g_intLastArticleId).style.display = "none";
		document.getElementById("article"+g_intLastArticleId).style.background = "#adac73";
		document.getElementById("imgTheLoupe"+g_intLastArticleId).style.display = "none";
	}
	g_intLastArticleId = l_intArticleId;

}
function showZoom( p_objId ) {
	var l_intArticleId = replace(p_objId.id,'imgTheLoupe','');
	var l_strImgSrc = document.getElementById("imgTheMini"+l_intArticleId).src;
	document.getElementById("imgTheZoom").src = replace(l_strImgSrc,'mini','');
	document.getElementById("imgZoom").style.display = "block";
	// menu autres images à zoomer
	var l_intNbreImage = document.getElementById("imgnbre"+l_intArticleId).innerHTML;
	if ( l_intNbreImage > 1 ) {
		var l_strMenuHTML = '';
		for ( var l_intCount = 1; l_intCount <= l_intNbreImage; ++l_intCount ) {
			l_strMenuHTML += 
				'<img src="'+replace(l_strImgSrc,'1',''+l_intCount)+'" alt="imgZoomMini'+l_intCount+'" id="imgZoomMini'+l_intCount+'" onClick="changeZoom(this);" onMouseover="showZoomLoupe(this);">'+
				'<div style="position:absolute;"><img src="img/loupe.png" alt="imgZoomLoupe'+l_intCount+'" id="imgZoomLoupe'+l_intCount+'" onClick="changeZoomLoupe(this);" onMouseout="hideZoomLoupe(this);" class="imgZoomLoupe"></div>';
		}
		document.getElementById("menuZoom").innerHTML = l_strMenuHTML;
	}
}
function changeZoom( p_objId ) {
	document.getElementById("imgTheZoom").src = 'img/waiting.png';
	//alert(p_objId.src);
	document.getElementById("imgTheZoom").src = replace(p_objId.src,'mini','');

}
function changeZoomLoupe( p_objId ) {
	//document.getElementById("imgTheZoom").src = 'img/waiting.png';
	var l_intArticleId = replace(p_objId.id,'imgZoomLoupe','');
	//alert(p_objId.id);
	//document.getElementById("menuSelect").innerHTML = p_objId.id + ' img src = ' + replace(document.getElementById("imgZoomMini"+l_intArticleId).src,'mini','');
	document.getElementById("imgTheDivZoom").innerHTML = '<img src="' + replace(document.getElementById("imgZoomMini"+l_intArticleId).src,'mini','') + '" class="imgTheZoom" id="imgTheZoom"  onClick="hideZoom();">';

}
function hideZoom() {
	document.getElementById("imgZoom").style.display = "none";
	document.getElementById("imgTheZoom").src = 'img/waiting.png';
	document.getElementById("menuZoom").innerHTML = '';
}
function showZoomLoupe( p_objId ) {
	var l_intArticleId = replace(p_objId.id,'imgZoomMini','');
	document.getElementById("imgZoomLoupe"+l_intArticleId).style.display = "block";
}
function showLoupe( p_objId ) {
	var l_intArticleId = replace(p_objId.id,'img','');
	document.getElementById("imgTheLoupe"+l_intArticleId).style.display = "block";
}
function hideZoomLoupe( p_objId ) {
	p_objId.style.display = "none";
}
function hideLoupe( p_objId ) {
	p_objId.style.display = "none";
}
function hideTheLoupe( p_objId ) {
	var l_intArticleId = replace(p_objId.id,'imgSide','');
	if ( document.getElementById("imgTheLoupe"+l_intArticleId) != null ) document.getElementById("imgTheLoupe"+l_intArticleId).style.display = "none";
}

function comment( p_objId ) {
	var l_objId = replace(p_objId.id,'commentLink','');
	document.getElementById('comment'+l_objId).innerHTML = 
		'<form method="post" action="saveComment.php" target="saveFrame" id="commentForm">'+
			'<p>Entrez votre message et votre adresse électronique puis cliquez sur "Enregistrer" :'+
			'<textarea name="commentText" id="commentText"></textarea>'+
			'<input name="commentMail" value="adresse électronique" onClick="this.value=\'\';">'+
			'<input type="hidden" name="spectacleId" value="'+l_objId+'">'+
			'<input type="submit" value="Enregistrer"></p>'+
		'</form>'+
		'';
	document.getElementById('commentText').focus();
	document.getElementById('frame'+l_objId).innerHTML = 
		'<iframe style="display:none;" id="saveFrame" name="saveFrame"></iframe>';
}
function getComments(p_intId) {
	if (g_strLastSelectedDateId!=p_intId) {
	document.getElementById('frame'+p_intId).innerHTML = 
		'<iframe style="display:none;" id="getFrame" name="getFrame" src="saveComment.php?spectacleId='+p_intId+'"></iframe>';
	}

}
var l_intSalaire = 125;

function checkTimeShow( p_objTimeShow ) {
	p_objTimeShow.value = replace(p_objTimeShow.value,' ','');
	var l_intPartNumberShow = ( p_objTimeShow.value.indexOf('x') > -1 ? parseInt(p_objTimeShow.value.substring(0,p_objTimeShow.value.indexOf('x'))) : 1 );
	var l_intPartTimeShow = parseInt(p_objTimeShow.value.substring(p_objTimeShow.value.indexOf('x')+1,p_objTimeShow.value.length));
	p_objTimeShow.value = l_intPartNumberShow+' x '+l_intPartTimeShow+' min';
	var l_intInitTime = 90;
	var l_intTarifBase = 2;
	var l_intTarifIncrement = 0.982;
	
	if ( l_intPartNumberShow*l_intPartTimeShow < l_intInitTime ) l_intPartNumberShow = l_intPartNumberShow * ( 1 + ((l_intPartNumberShow*l_intPartTimeShow/l_intInitTime*5)+l_intInitTime-(l_intPartNumberShow*l_intPartTimeShow))/(l_intInitTime+(l_intPartNumberShow*l_intPartTimeShow)) );
	
	document.getElementById('soloSelect').innerHTML = 'SOLO : guitare et chant ('+Math.round(Math.round(Math.round(l_intTarifBase*l_intPartNumberShow*l_intPartTimeShow*l_intSalaire/l_intInitTime)/10))*10+' €)';
	document.getElementById('duoSelect').innerHTML = 'DUO : solo + flûte traversière ('+Math.round(Math.round(Math.round((l_intTarifBase+l_intTarifIncrement)*l_intPartNumberShow*l_intPartTimeShow*l_intSalaire/l_intInitTime)/10))*10+' €)';
	document.getElementById('trioSelect').innerHTML = 'TRIO : duo + violoncelle ('+Math.round(Math.round(Math.round((l_intTarifBase+l_intTarifIncrement*2)*l_intPartNumberShow*l_intPartTimeShow*l_intSalaire/l_intInitTime)/10))*10+' €)';
	document.getElementById('quartetSelect').innerHTML = 'QUARTET : trio + batterie ('+Math.round(Math.round(Math.round((l_intTarifBase+l_intTarifIncrement*3)*l_intPartNumberShow*l_intPartTimeShow*l_intSalaire/l_intInitTime)/10))*10+' €)';
	changePrice( document.getElementById('formuleShow') );
}
function changePrice( p_objElementSelect ) {
	var l_arrayOptions = p_objElementSelect.getElementsByTagName('option');
	for ( var l_intCount = 0; l_intCount < l_arrayOptions.length; ++l_intCount ) {
		if ( l_arrayOptions[l_intCount].selected ) {
			
			var l_arrayPriceSelect1 = l_arrayOptions[l_intCount].innerHTML.split("(");
			var l_arrayPriceSelect2 = l_arrayPriceSelect1[1].split(")");
			//alert( parseInt(l_arrayPriceSelect2[0]) );
			document.getElementById('price').value = parseInt(l_arrayPriceSelect2[0]);
			
		}
	}
}
function rollSong( p_objElementSong ) {
	var p_intId = p_objElementSong.id.substring(4);
	if ( p_intId != g_strLastSelectedSongId ) {
		document.getElementById('SongTitle'+p_intId).style.color = "#393939";
		document.getElementById('SongAuteur'+p_intId).style.color = "#6a141e";
		document.getElementById('SongImgLeft'+p_intId).src = "img/listImage.gif";
		document.getElementById('SongImgRight'+p_intId).src = "img/listImageRight.gif";
		document.getElementById('Song'+p_intId).style.background = "url('img/lineRoll.png') center repeat";
		if (g_strLastRollSongId != "none" && g_strLastRollSongId!=g_strLastSelectedSongId) {
			document.getElementById('SongTitle'+g_strLastRollSongId).style.color = "white";
			document.getElementById('SongAuteur'+g_strLastRollSongId).style.color = "#283b29";
			document.getElementById('SongImgLeft'+g_strLastRollSongId).src = "img/tr.gif";
			document.getElementById('SongImgRight'+g_strLastRollSongId).src = "img/tr.gif";
			document.getElementById('Song'+g_strLastRollSongId).style.background = "transparent";
		}
		g_strLastRollSongId = p_intId;
	}
}
function selectSong( p_objElementSong ) {
	var p_intId = p_objElementSong.id.substring(4);
	if (g_strLastSelectedSongId != "none") {
		document.getElementById('SongText'+g_strLastSelectedSongId).style.display = "none";
		document.getElementById('Song'+g_strLastSelectedSongId).style.background = "transparent";
		document.getElementById('Song'+g_strLastSelectedSongId).style.height = "20px";
	}
	rollSong( p_objElementSong );
	g_strLastSelectedSongId = p_intId;
	document.getElementById('SongText'+p_intId).style.display = "block";
	document.getElementById('Song'+p_intId).style.background = "url('img/lineSelect.png') center repeat";
	document.getElementById('Song'+p_intId).style.height = "450px";
	document.getElementById('infoPencarte').style.display = "none";
}
function closeSong( p_objElementSong ) {
	var p_intId = p_objElementSong.id.substring(4);
	if ( g_strLastSelectedSongId == p_intId ) {
		document.getElementById('SongText'+g_strLastSelectedSongId).style.display = "none";
		document.getElementById('Song'+g_strLastSelectedSongId).style.background = "transparent";
		rollSong( p_objElementSong );
		g_strLastSelectedSongId = "none";
	}
}
function rollDate( p_objElementDate ) {
	var p_intId = p_objElementDate.id.substring(4);
	if ( p_intId != g_strLastSelectedDateId ) {
		document.getElementById('dateEtLieu'+p_intId).style.color = "#393939";
		document.getElementById('dateFormule'+p_intId).style.color = "#6a141e";
		if ( document.getElementById('thisArrow') == null ) {
			document.getElementById('dateImgLeft'+p_intId).src = "img/listImage.gif";
			document.getElementById('dateImgRight'+p_intId).src = "img/listImageRight.gif";
		}
		document.getElementById('date'+p_intId).style.background = "url('img/lineRoll.png') center repeat";
		if (g_strLastRollDateId != "none" && g_strLastRollDateId!=g_strLastSelectedDateId) {
			document.getElementById('dateEtLieu'+g_strLastRollDateId).style.color = (document.getElementById('dateEtLieu'+g_strLastRollDateId).title=="old"?"#6d6d49":(document.getElementById('dateEtLieu'+g_strLastRollDateId).title=="now"?"#6c0426":"white"));
			document.getElementById('dateFormule'+g_strLastRollDateId).style.color = "#283b29";
			document.getElementById('dateImgLeft'+g_strLastRollDateId).src = "img/tr.gif";
			document.getElementById('dateImgRight'+g_strLastRollDateId).src = "img/tr.gif";
			document.getElementById('date'+g_strLastRollDateId).style.background = "transparent";
		}
		g_strLastRollDateId = p_intId;
	}
}
function selectDate( p_objElementDate ) {
	var p_intId = p_objElementDate.id.substring(4);
	if (g_strLastSelectedDateId != "none") {
		document.getElementById('dateInfos'+g_strLastSelectedDateId).style.display = "none";
		document.getElementById('date'+g_strLastSelectedDateId).style.background = "transparent";
	}
	rollDate( p_objElementDate );
	document.getElementById('dateInfos'+p_intId).style.display = "block";
	document.getElementById('date'+p_intId).style.background = "url('img/lineSelect.png') center repeat";
	getComments(p_intId);
	g_strLastSelectedDateId = p_intId;
	document.getElementById('infoPencarte').innerHTML = "<div>&nbsp;<p>Laissez vos impressions après avoir assisté à un concert du groupe en cliquant sur le bouton 'Ajouter un commentaire'.</p></div>";

}
function checkFrames() {
	var l_boolIsPlayerHere = ( typeof (parent.frames['PLAYERframe']) != "undefined" && typeof (parent.frames['PLAYERframe'].document.embeds['playerMP3']) != "undefined" );
	if (!l_boolIsPlayerHere) window.location.href="player.php?PAGE_NAME="+g_strPageName;
	return l_boolIsPlayerHere;
}
function initPlayer() {
	setTimeout("showMessage()",1500);
	g_strPlayerAction = "play";
}
function showMessage() {
	alert(parent.frames['PLAYERframe'].document.getElementById('playerMP3').hasAttributes());
}
function playEmbed() {
	if ( checkFrames() ) {
		parent.frames['PLAYERframe'].document.getElementById('playerMP3').stop();
		parent.frames['PLAYERframe'].document.getElementById('playerMP3').play();
	}
}
function stopEmbed() {
	if ( checkFrames() ) parent.frames['PLAYERframe'].document.document.getElementById('playerMP3').stop();
}
function initIMG() {

	var l_objBody = document.getElementsByTagName('body');
	g_strPageName = l_objBody[0].id;
	var l_strHtmlInTag = '';
		
	var l_objMenuLinks = document.getElementById("mainMenu").getElementsByTagName('a');
	// construction HTML du menu GÉNÉRALE de navigation
	for ( var l_intCount = 0; l_intCount < l_objMenuLinks.length; ++l_intCount ) {
		if (l_objMenuLinks[l_intCount].title!='') {
			l_strHtmlInTag = l_objMenuLinks[l_intCount].innerHTML;
			l_objMenuLinks[l_intCount].innerHTML = '<img id="img'+l_objMenuLinks[l_intCount].title+'" src="'+loadImageRoll(l_objMenuLinks[l_intCount].title,('photo1,photo2,photo3,photo4'.indexOf(l_objMenuLinks[l_intCount].title)>-1?'gif':'png'))+'" onMouseover="imageRoll(this);" onMouseout="imageRoll(this);" onMousedown="imageClick(this);" onClick="imageClick(this);" alt="'+l_strHtmlInTag+'">';
			//l_objMenuLinks[l_intCount].title = l_strHtmlInTag;

		}
	}
	if (document.getElementById("photosMembres")!=null) {
		
		var l_strHtmlInTag = '';
		var l_objMenuLinks = document.getElementById("photosMembres").getElementsByTagName('a');
		// construction HTML du menu GÉNÉRALE de navigation
		for ( var l_intCount = 0; l_intCount < l_objMenuLinks.length; ++l_intCount ) {
			if (l_objMenuLinks[l_intCount].title!='') {
				l_strHtmlInTag = l_objMenuLinks[l_intCount].innerHTML;
				l_objMenuLinks[l_intCount].innerHTML = '<img id="img'+l_objMenuLinks[l_intCount].title+'" src="'+loadImageRoll(l_objMenuLinks[l_intCount].title,('photo1,photo2,photo3,photo4'.indexOf(l_objMenuLinks[l_intCount].title)>-1?'gif':'png'))+'" onMouseover="imageRoll(this);" onMouseout="imageRoll(this);" onMousedown="imageClick(this);" onClick="imageClick(this);" alt="'+l_strHtmlInTag+'">';
				//l_objMenuLinks[l_intCount].title = l_strHtmlInTag;
	
			}
		}
	}
	
	
	if ( g_strPageName == "devis" && document.getElementById('timeShow') != null ) {
		checkTimeShow( document.getElementById('timeShow') );
	}
	if ( document.getElementById('inscriptionDisque') != null ) {
		document.getElementById('inscriptionDisque').action = 'saveEmail.php';
	}
	if ( g_strPageName == "contact" ) {
		document.getElementById('contactForm').action = 'saveEmail.php';
		document.getElementById('contactText').focus();
	}
	if ( g_strPageName == "boutique" ) {
		if (document.getElementById('livraisonForm') != null) {
			document.getElementById('livraisonForm').action = 'saveEmail.php';
		}
	}
}
function checkDate( p_objDate ) {
	if (p_objDate.value=='17/10/2009'||p_objDate.value=='18/10/2009') {
		l_intSalaire = 60;
		checkTimeShow( document.getElementById('timeShow') );
	}
}

g_objImageBulle = new Image();
g_objImageBulle.src = 'img/bulle.png';
function bulle( p_objId ) {
	
	/*var l_strTitle = "";
	if ( document.getElementById('bulle').innerHTML != '' ) {
		document.getElementById('bulle').innerHTML = '';
		document.getElementById('bulle').style.display = "none";
	} else {
		if ( p_objId.title != null && p_objId.title != '' ) {
			l_strTitle = p_objId.title;
			document.getElementById('bulle').innerHTML = "<span><img src='"+g_objImageBulle.src+"'></span><div>"+document.getElementById(l_strTitle).innerHTML+"</div>";
			document.getElementById('bulle').style.display = "block";
		}
	} */
}

/////////////////////////////////////////////////////////
//// CHARGEMENT IMAGES ROLL /////////////////////////////
/////////////////////////////////////////////////////////
function loadImageRoll ( p_strImageName, p_strImageExtension ) {
	var l_intImageIndex = g_objImages.length;

	g_objImages[l_intImageIndex] = new Array();
	for ( var l_intCount = 0; l_intCount < 3; ++l_intCount ) {
		g_objImages[l_intImageIndex][l_intCount] = new Image();
		g_objImages[l_intImageIndex][l_intCount].src = "img/"+p_strImageName+( l_intCount == 0 ? (p_strImageName.indexOf(g_strPageName)>-1||p_strImageName.indexOf(g_strPlayerAction)>-1?'active':'') : ( l_intCount == 1 ? 'roll' : 'click' ) )+"."+p_strImageExtension;
	}
	return g_objImages[l_intImageIndex][0].src;
}
/////////////////////////////////////////////////////////
//// IMAGE ROLLING //////////////////////////////////////
/////////////////////////////////////////////////////////
function imageRoll ( p_objImage ) {
	g_boolAutoRoll = true;
	for ( var l_intCount = 0; l_intCount < g_objImages.length; ++l_intCount ) {
		if ( typeof( g_objImages[l_intCount] ) != 'undefined' ) {
			for ( var l_intCountImage = 0; l_intCountImage <= 2; ++l_intCountImage ) {
				if ( typeof( g_objImages[l_intCount][l_intCountImage] ) != 'undefined' ) {
					if ( g_objImages[l_intCount][l_intCountImage].src == p_objImage.src ) {
						if ( l_intCountImage == 0 ) { 
							p_objImage.src = g_objImages[l_intCount][1].src;
							return;
						} else {
							p_objImage.src = g_objImages[l_intCount][0].src;
							return;
						}
					}
				}
			}			
		}
	}
}
/////////////////////////////////////////////////////////
//// IMAGE CLICKING //////////////////////////////////////
/////////////////////////////////////////////////////////
function imageClick ( p_objImage ) {
	
	for ( var l_intCount = 0; l_intCount < g_objImages.length; ++l_intCount ) {
		if ( typeof( g_objImages[l_intCount] ) != 'undefined' ) {
			for ( var l_intCountImage = 0; l_intCountImage < 2; ++l_intCountImage ) {
				if ( typeof( g_objImages[l_intCount][l_intCountImage] ) != 'undefined' ) {
					if ( g_objImages[l_intCount][l_intCountImage].src == p_objImage.src ) {
						p_objImage.src = g_objImages[l_intCount][2].src;
					}
				}
			}			
		}
	}
}



/////////////////////////////////////////////////////////
//// REPLACE STRING /////////////////////////////////////
/////////////////////////////////////////////////////////
function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}
