var g_strLastLinkElementHTML = null;
var g_objLastElementId = null;
var g_objLoupeImg = new Image();
g_objLoupeImg.src = 'img/loupe.png';
function showLoupe( p_objElement ) {
	if (p_objElement.id!=g_objLastElementId) {
		if (g_objLastElementId!=null) { 
			document.getElementById(g_objLastElementId).innerHTML = g_strLastLinkElementHTML;
		}
		g_strLastLinkElementHTML = p_objElement.innerHTML;
		document.getElementById(p_objElement.id).innerHTML = '<span id="loupePosition" onClick="window.location.href=\''+p_objElement.href+'\';"><img src="'+g_objLoupeImg.src+'"></span>'+g_strLastLinkElementHTML;
	}
	g_objLastElementId = p_objElement.id;
}

function showOtherPhoto() {
		document.getElementById("otherPhoto").style.display = "block";
		setTimeout("showOtherPhotoOn();",g_intWaitingTime);
		setTimeout("showOtherPhotoOff();",g_intWaitingTime*3);
		setTimeout("hideOtherPhoto();",g_intWaitingTime*4);
}
function showOtherPhotoOn() {
		var l_objPhoto = document.getElementById("otherPhoto").getElementsByTagName('img');
		rollImg(l_objPhoto[0]);
}
function showOtherPhotoOff() {
		var l_objPhoto = document.getElementById("otherPhoto").getElementsByTagName('img');
		rollImgOut(l_objPhoto[0]);
		g_intWaitingTime = 150;
}
function hideOtherPhoto() {
		document.getElementById("otherPhoto").style.display = "none";
}

var g_objLastElementRow = null;
function rowShow( p_objElement ) {
	if (g_objLastElementRow != null ) {
		g_objLastElementRow.style.background = "transparent";
		g_objLastElementRow.style.color = "black";
	}
	p_objElement.style.background = "url('img/calqueGray.png')";
	p_objElement.style.color = "white";
	g_objLastElementRow = p_objElement;
}
var l_intLastMenuId = 0;
function openMenu( p_objElement ) {
	closeMenu();
	var l_intId = replace(p_objElement.id,'menu','');
	document.getElementById('underMenu'+l_intId).style.display = 'block';
	l_intLastMenuId = l_intId;
}
function closeMenu() {
	if (l_intLastMenuId != 0) document.getElementById('underMenu'+l_intLastMenuId).style.display = 'none';
}
var g_strLastImgRoll = '';
var g_objLastImgElement = null;
var g_intImgId = 0;
var g_boolRoll = false;
function rollImg( p_objElement ) {
	if (g_intImgId == 0) {
		g_strLastImgRoll = p_objElement.src;
		g_intImgId = replace(p_objElement.id,'img','');
		g_objLastImgElement = p_objElement;
		g_objLastImgElement.src = g_objImages[g_intImgId][2].src;
		spendTime();
	}
}
function rollImgTrans() {
	if (g_objImages[g_intImgId]!=null) g_objLastImgElement.src = g_objImages[g_intImgId][1].src;
}
function rollImgOut() {
	if (g_strLastImgRoll!='') g_objLastImgElement.src = g_strLastImgRoll;
	g_strLastImgRoll = '';
	g_intImgId = 0;
}
var g_intWaitingTime = 150;
function spendTime() {
	setTimeout("rollImgTrans()",g_intWaitingTime);
}
var g_objImages = new Array();
function initIMG() {
	var l_objBody = document.getElementsByTagName('body');
	g_strPageName = l_objBody[0].id;
		
	var l_objMenuLinks = document.getElementById("menuPage").getElementsByTagName('img');
	for ( var l_intCount = 0; l_intCount < l_objMenuLinks.length; ++l_intCount ) {
		loadImageRoll(replace(l_objMenuLinks[l_intCount].id,'img',''),l_objMenuLinks[l_intCount].src);
	}
	
	if (g_strPageName == "index" || g_strPageName == "onglerie" || g_strPageName == "institut" || g_strPageName == "contact" ) {
		if ( document.getElementById("photo") != null ) {
			var l_objMenuLinks = document.getElementById("photo").getElementsByTagName('img');
			for ( var l_intCount = 0; l_intCount < l_objMenuLinks.length; ++l_intCount ) {
				loadImageRoll(replace(l_objMenuLinks[l_intCount].id,'img',''),l_objMenuLinks[l_intCount].src);
			}
		}
		if ( g_strPageName == "institut" || g_strPageName == "index" ) {
			if ( document.getElementById("otherPhoto") != null ) {
				var l_objMenuLinks = document.getElementById("otherPhoto").getElementsByTagName('img');
				for ( var l_intCount = 0; l_intCount < l_objMenuLinks.length; ++l_intCount ) {
					loadImageRoll(replace(l_objMenuLinks[l_intCount].id,'img',''),l_objMenuLinks[l_intCount].src);
				}
			}
		}
	}
	if ( document.getElementById("formComment") != null ) {
		document.getElementById("formComment").action = 'saveComment.php';
	}
}
function loadImageRoll ( p_strImageId, p_strImageName ) {
	g_objImages[p_strImageId] = new Array();
	for ( var l_intCount = 0; l_intCount < 3; ++l_intCount ) {
		g_objImages[p_strImageId][l_intCount] = new Image();
		g_objImages[p_strImageId][l_intCount].src = replace(p_strImageName,'.png',( l_intCount==1? 'ON': (l_intCount==2?'OFF':'') )+'.png');
	}
}

/////////////////////////////////////////////////////////
//// 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;
}
