var g_intMaxLeft;
var g_intFlyerLeft;
var g_objImageFlyer2 = new Image();

function init( p_strPageName ) {
	g_strPageName = p_strPageName;
	// construction du menu
	if ( document.getElementById('topMain') != null ) {
		var l_objLinks = document.getElementById('topMain').getElementsByTagName('a');
		var l_strHTMLmenu = '';
		for( var l_intCount = 0; l_intCount < l_objLinks.length; ++l_intCount ) {
			l_strHTMLmenu += 
				'<td '+
					'class="link" '+
					'onClick="window.location.href=\''+l_objLinks[l_intCount].href+'\';" '+ 
					( l_objLinks[l_intCount].href == document.location.href ? 
						'id="thisPage"' : 
						'onMouseover="this.style.background=\'url(img/menuBackLink.gif) repeat-x center\';this.style.color=\'#fdf342\';" '+ 
						'onMouseout="this.style.background=\'none\';this.style.color=\'white\';" '
					)+'>'+
					l_objLinks[l_intCount].innerHTML+
				'</td>'+
				( l_intCount < l_objLinks.length-1 ? '<td><img src="img/cellBorder.gif"></td>' : '' );
		}
		document.getElementById('displayMenu').innerHTML = 
			'<table width="96%" border="0"><tr><td><a href="index.php" title="Retour à la page d\'accueil du site" id="indexLink">&nbsp;</a></td>'+
			l_strHTMLmenu+'</tr></table>';
	} else if (p_strPageName == 'pubSalon') {
		document.write('<img src="'+document.getElementById('invit').src+'">');
		window.print();
		return;
	}	
	
	// Apparence des titre
	var l_objTitle = document.getElementById('middleMain').getElementsByTagName('h1');
	for( var l_intCount = 0; l_intCount < l_objTitle.length; ++l_intCount ) {
		l_objTitle[l_intCount].innerHTML = "<div>"+l_objTitle[l_intCount].innerHTML+"<span>"+l_objTitle[l_intCount].innerHTML+"</span></div>";
	}	

	
	if ( p_strPageName == 'index' ) {
		//document.getElementById('flyerFrame').style.display = 'block';
		initCollection(1, false, true);
		g_intMaxLeft = (screen.availWidth)/2;
		/*g_intFlyerLeft = -200;
		if ( (version >= 5.5) && (version < 7) ) {
			g_intMaxLeft = (screen.availWidth)/2+150;
			g_intFlyerLeft = 0;
		}
		g_objImageFlyer2.src = 'img/flyer1.jpg';*/

		//showFlyer();
		animImgOn();
		
	} else if ( p_strPageName == 'contact' ) {	
	} else if ( p_strPageName == 'exposant' ) {	
	} else if ( p_strPageName == 'pubSalon' ) {	
		
	} else if ( p_strPageName == 'flash_info' ) {	
	} else if ( p_strPageName == 'livredor' ) {	
		document.getElementById('livreFORM').action = 'saveLivreDor.php';
	} else if ( p_strPageName == 'showImg' ) {
		document.getElementById('showImg').innerHTML = 
			document.getElementById(document.getElementById('imgRef').innerHTML).innerHTML +
			"<img src='photos/"+document.getElementById('pageName').innerHTML+(document.getElementById('imgRub')==null?"":"/"+document.getElementById('imgRub').innerHTML)+"/"+document.getElementById('imgId').innerHTML+".jpg'>";
		window.focus();
	} else {
		initCollection(1, true, false);
	}
}
function animImgOn() {
	document.getElementById('50pourcent').style.display = 'block';
	setTimeout( "animImgOff();",2500 );
}
function animImgOff() {
	document.getElementById('50pourcent').style.display = 'none';
	setTimeout( "animImgOn();",200 );
}
function showFlyer() {
	if ( g_intFlyerLeft <= g_intMaxLeft - 450 ) {
		g_intFlyerLeft = g_intFlyerLeft + 20;
		document.getElementById('flyerFrame').style.left = g_intFlyerLeft+'px';
		setTimeout( "showFlyer();",100 );
	} else {
		if ( (version >= 5.5) && (version < 7) ) document.getElementById('flyerFrame').innerHTML = '<img src="img/flyer1.jpg" id="flyer" onClick="window.open(\'pubSalon.php?FROM=site\',\'pub\',\'directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,width=800,height=630\');">';
		document.getElementById('flyer').src = g_objImageFlyer2.src;
		setTimeout( "document.getElementById('flyer').src = 'img/flyer1.jpg';",10 );
		setTimeout( "document.getElementById('flyer').style.display='none';",2500 );
	}	
}

/////////////////////////////////////////////////////////
//// IMAGES PNG /////////////////////////////////////////
/////////////////////////////////////////////////////////
var arVersion = navigator.appVersion.split("MSIE");
var version = parseFloat(arVersion[1]);
function fixPNG(myImage) {
	if ((version >= 5.5) && (version < 7) && (document.body.filters)) { 
		var imgID = (myImage.id) ? "id='" + myImage.id + "' " : "";
		var imgClass = (myImage.className) ? "class='" + myImage.className + "' " : "";
		var imgTitle = (myImage.title) ? 
		           "title='" + myImage.title  + "' " : "title='" + myImage.alt + "' ";
	
		//if (imgOnMouseOver!='') alert(imgOnMouseOver);
		var imgStyle = "display:inline-block;" + myImage.style.cssText;
		var strNewHTML = "<span " + imgID + imgClass + imgTitle 
		              + " style=\"" + "width:" + myImage.width 
		              + "px; height:" + myImage.height 
		              + "px;" + imgStyle + ";"
		              + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		              + "(src=\'" + myImage.src + "\', sizingMethod='scale');\" "
		              + "></span>";
		myImage.outerHTML = strNewHTML; 
	}
}

///////////////////////
// Script COLLECTION //
///////////////////////

var g_objAllImgCollection = new Array();
var g_objAllInfoProducts = new Array();
var g_objImagesCollection = new Object();
var g_intNumberImgCollection = 0;
var g_intNumberImgToShow = 4;
var g_intMoveAmp = 3;
var g_intImgWidth = 83;
var g_intImgHeight = 75;
var g_intPosition = 0;
var g_intPositionMini = 0;
var g_boolRightDirection;
var g_boolStopMoving;
var g_boolLeftRight;
function initCollection( p_intRubriqueId, p_boolLeftRight, p_boolMoveImage ) {
	var l_strMenuHTML = '';
	var l_objAllRubrique = document.getElementById('collectionInfo').getElementsByTagName('h2');
	for ( var l_intCount = 0; l_intCount < l_objAllRubrique.length; ++l_intCount ) {
		l_strMenuHTML += '<p><a href="#" class="h2" style="display:block;" onClick="initCollection('+(l_intCount+1)+','+p_boolLeftRight+','+p_boolMoveImage+');"'+( (l_intCount+1)==p_intRubriqueId ? ' id="this"' : '' )+'>'+l_objAllRubrique[l_intCount].innerHTML+'</a></p>';
	}
	if (p_boolLeftRight) document.getElementById('collectionMenu').innerHTML = l_strMenuHTML;
	// dans la rubrique sélectionnée on regarde tous les articles
	g_objAllImgCollection = new Array();
	g_objAllInfoProducts = document.getElementById('rub'+p_intRubriqueId).getElementsByTagName('div'); 
	g_intNumberImgCollection = g_objAllInfoProducts.length;
	
	// si on doit faire bouger les images
	if ( p_boolMoveImage ) {
		// on gère le nombre minimum d'article à faire tourner dans la boucle
		if ( g_intNumberImgCollection <= g_intNumberImgToShow ) g_intNumberImgToShow = g_intNumberImgCollection - 1;
		if ( g_intNumberImgToShow <= 2 ) g_intNumberImgToShow = 3;
		// on commence par cacher la cellule principale du slide, on l'affichera quand tout sera chargé et positionné
		document.getElementById('slideCell').style.display='none';
		if (p_boolLeftRight) {
			document.getElementById('slideFrame').innerHTML = '';
			document.getElementById('slideCell').style.width = ( (g_intNumberImgToShow-1)*g_intImgWidth )+'px';
			document.getElementById('toShowCell').innerHTML = '';
			document.getElementById('confirmMessage').innerHTML = '';
		} else {
			document.getElementById('slideCell').style.height = ( (g_intNumberImgToShow-1)*g_intImgHeight )+'px';
		}
	}	
	
	// affichage des images
	var l_strHTML = '';
	if ( !p_boolMoveImage ) l_strHTML = '<table border="0"><tr><td Valign="top" align="center">'+"\n";
	var l_intCellCount = -1;
	var l_boolFirstCell = true;
//	for ( var l_intCount = 1; l_intCount < g_intNumberImgCollection+1; ++l_intCount ) {
	for ( var l_intCount = g_intNumberImgCollection; l_intCount > 0; --l_intCount ) {
		
		var l_intImgRub = p_intRubriqueId;
		if (l_objAllRubrique.length<=1) l_intImgRub = 0;
		
		// chargement des images de la collection
		g_objAllImgCollection[l_intCount] = new Image();
		g_objAllImgCollection[l_intCount].src = 'photos/'+g_strPageName+'/'+(l_objAllRubrique.length>1?p_intRubriqueId+'/':'')+l_intCount+'mini.jpg';
		
		// si l'article n'a pas été supprimé
		if ( document.getElementById('productName'+p_intRubriqueId+'-'+l_intCount).innerHTML != '-' ) {
		
			// si on n'est pas en mouvement on gère le nombre de colonne pour passer à la ligne
			if ( !p_boolMoveImage ) {
				if (!l_boolFirstCell) l_strHTML += '</td>'+"\n";
				if (++l_intCellCount==g_intNumberImgToShow) {
					l_strHTML += '</tr><tr><td Valign="top" align="center">'+"\n";
					l_intCellCount = 0;
					l_boolFirstCell = true;
				}
				if (!l_boolFirstCell) l_strHTML += '<td Valign="top" align="center">'+"\n";
				if ( l_boolFirstCell ) l_boolFirstCell = false;
			}
		
			// affichage des images miniatures de la rubrique en cours dans le slide de la collection
			l_strHTML += 
			'<img ' +
				'onClick="showImageCollection('+l_intCount+','+l_intImgRub+');" ' +
				'onMouseover="g_boolStopMoving=true;" ' +
				//'onMouseover="this.style.filter=\'alpha(opacity=80)\';g_boolStopMoving=true;" ' +
				//'onMouseout="this.style.filter=\'none\';" ' +
				'alt="Agrandir cette photo" ' +
				'id="slideImg'+l_intCount+'" ' +
				'src="'+g_objAllImgCollection[l_intCount].src+'" ' +
				'style="'+(p_boolLeftRight?'left':'top')+':'+l_intCount*(g_boolLeftRight?g_intImgWidth:g_intImgHeight)+'px;">'+"\n"+
			( p_boolMoveImage?'':'<div class="commentProduct">'+document.getElementById('productName'+p_intRubriqueId+'-'+l_intCount).innerHTML+'</div>'+"\n" );
		
		}
	}
	if ( !p_boolMoveImage ) l_strHTML += 
		'</td></tr></table>'+"\n" +
		"<h6>Les robes de mariées ci-dessus ne représentent qu'un échantillon de notre magasin.<br>"+
		"Pour plus de renseignements ou pour accéder à notre gamme complète, n'hésitez pas à <a href='contact.php'>nous contacter</a>.</h6>";
	document.getElementById('slideFrame').innerHTML = l_strHTML;
	g_boolRightDirection = true;
	g_boolStopMoving = false;
	g_boolLeftRight = p_boolLeftRight;
	if ( p_boolMoveImage ) setTimeout( "document.getElementById('slideCell').style.display='block';moveImageCollection();",500 );
}
function moveImageCollection() {
	
	if ( !g_boolStopMoving ) {
		// Pour toutes les images
		for ( var l_intCount = 1; l_intCount < g_intNumberImgCollection+1; ++l_intCount ) {
			g_intPosition = parseInt( replace((g_boolLeftRight?document.getElementById('slideImg'+l_intCount).style.left:document.getElementById('slideImg'+l_intCount).style.top),'px','') );
			// chargement des images de la collection
			if ( !g_boolRightDirection ) {
				g_intPosition = (g_intPosition <= g_intMoveAmp ? (g_intNumberImgCollection*(g_boolLeftRight?g_intImgWidth:g_intImgHeight)) : ( g_intPosition - g_intMoveAmp ));
			} else {
				g_intPosition = (g_intPosition >= (g_intNumberImgCollection*(g_boolLeftRight?g_intImgWidth:g_intImgHeight)) ? g_intMoveAmp : ( g_intPosition + g_intMoveAmp ));
			}
			if ( g_boolLeftRight ) document.getElementById('slideImg'+l_intCount).style.left =  g_intPosition + "px";
			if ( !g_boolLeftRight ) document.getElementById('slideImg'+l_intCount).style.top =  g_intPosition + "px";
			if ( g_intNumberImgCollection > g_intNumberImgToShow && g_intPosition > ((g_intNumberImgToShow+(g_boolLeftRight?1:0))*(g_boolLeftRight?g_intImgWidth:g_intImgHeight)) ) {
				document.getElementById('slideImg'+l_intCount).style.display = 'none';
			} else {
				document.getElementById('slideImg'+l_intCount).style.display = 'block';
			}
		}
		setTimeout( "moveImageCollection();",100 );
	}
	
}
function showImageCollection( p_intId, p_intRub ) {
	if ( g_strPageName == 'index' ) {
		window.location.href='robes_de_mariees.php';
	} else {
		window.open('showImg.php?FILE='+g_strPageName+( p_intRub == 0 ? '' : '&RUB='+p_intRub )+'&ID='+p_intId,'photo','directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,width=460,height=495');
	}
}

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