// Tous droits réservés © 2005 Hexatek
// All rights reserved © 2005 Hexatek

var menu, currentLanguage;

// Lit le cookie et retourne la langue
function redirectLanguagePage()
{
	// The following line is for testing
	// alert( "The cookie contains:\n" + document.cookie );
	var re = new RegExp(/(^|;\s)lang=([^|;]+)(;|$)/);
	var bLang = re.test( document.cookie );

	var langue = "";

	if( bLang )
		langue = re.exec( document.cookie )[2];

	switch( langue )
	{
		case "fr":
		  location.replace( "francais/special-semaine-hexatek.html" );
			break;
		
		case "en":
			location.replace( "english/hexatek-weekly-special.html" );		
			break;
	}		
}

function getProfileName()
{
	var re = new RegExp(/(^|;\s)contact=([^|;]+)(;|$)/);
	var bLang = re.test( document.cookie );

	var profileName = "";
	
	if( bLang )
	{
		profileName = re.exec( document.cookie )[2];
		profileName = profileName.substring( profileName.indexOf( "=" ) +1, profileName.indexOf( "&" ) )
		profileName = unescape( profileName.replace( /\+/g, " " ) );
	}
	
	return profileName;
}

function getProfile()
{
	var greeting = "";
	var profileName = getProfileName();
	
	if( currentLanguage == "fr" )
	{
		greeting = "Mon profil";
		if( profileName != "" )
			greeting = "Bonjour " + profileName;
		
		document.write( "<a href=\"showprofile.jsp?lang=FR\">"+ greeting +"</a>" );
	}
	else
	{
		greeting = "My profile";
		if( profileName != "" )
			greeting = "Hello " + profileName;

		document.write( "<a href=\"showprofile.jsp?lang=EN\">"+ greeting +"</a>" );
	}
}


function writeCookie( langue ) {
	// écrit un cookie pour garder la langue de l'utilisateur ou null pour l'effacer
	var sSiteRelRoot = "/";
	document.cookie = "lang="+ langue + ";expires="+(new Date((new Date().valueOf())+365*24*60*60*1000)).toGMTString()+";path="+sSiteRelRoot;
}

function getImageExtension( image ) {
	var dotPos;
	
	dotPos = image.src.lastIndexOf( "." );
	return image.src.substr( dotPos, image.src.length - dotPos );
}


function getImagePath( image ) {
	var slashPos = image.src.lastIndexOf( "/" );
	
	return image.src.substr( 0, slashPos + 1 );
}

/**
 * Retourne le URI après le nom de domaine, incluant les paramètres
 * Ex: http://www.hexatek.com/francais/mapage.html
 * Retourne: /francais/mapage.html
 */
function getURI( url )
{
	var pos = url.indexOf( "/", url.indexOf( "//" ) +2 );
	url = url.slice( pos, url.length );
	url = escape( url );
	
	return url;
}

function getPageName( url )
{
	// enlève les anchors
	var pos = url.lastIndexOf( "#" );
	if( pos > 0 )
		url = url.slice( 0, pos );
		
	// enlève les paramètres jsp
	pos = url.indexOf( "?" );
	if( pos > 0 )
		url = url.slice( 0, pos );

	// va chercher le nom de la page
	pos = url.lastIndexOf( "/" );
	return url.slice( pos +1, url.length );
}

function getPageParameter( url ) {
	var parameter = "";

	var pos = url.lastIndexOf( "?" );
	if( pos > 0 )
		parameter = url.slice( pos, url.length );
		
	return parameter;
}

function getPageAnchor( url ) {
	var anchor = "";

	var pos = url.lastIndexOf( "#" );
	if( pos > 0 )
		anchor = url.slice( pos, url.length );
		
	return anchor;
}


function toEnglish( links ) {
	var currentPage = getPageName( document.location.href );
	var parameter = getPageParameter( document.location.href );
	var anchor = getPageAnchor( document.location.href );
	
	writeCookie( "en" );
	
	// pour le moment, retourne à la page de recherche
	if( currentPage == "resultat-produit.jsp" )
		links.href = "../english/product-search.jsp"
	else
	{
		for( var i = 0; i < menu.length; i++ ) {
			if( currentPage == getPageName( menu[i][4] ) )
			{
				pos = parameter.indexOf( "lang" );
				
				if( pos > 0 )
					parameter = parameter.replace( "FR", "EN" );
				
				links.href = "../english/" + getPageName( menu[i][6] ) + parameter + anchor;
				break;
			}
		}
	}
}


function toFrench( links ) {
	var currentPage = getPageName( document.location.href );
	var parameter = getPageParameter( document.location.href );
	var anchor = getPageAnchor( document.location.href );
	
	writeCookie( "fr" );
	
	// pour le moment, retourne à la page de recherche
	if( currentPage == "product-result.jsp" )
		links.href = "../francais/recherche-produit.jsp"
	else
	{
		for( var i = 0; i < menu.length; i++ ) {
			if( currentPage == getPageName( menu[i][6] ) )
			{
				pos = parameter.indexOf( "lang" );
				if( pos > 0 )
					parameter = parameter.replace( "EN", "FR" );
				
				links.href = "../francais/" + getPageName( menu[i][4] ) + parameter + anchor;
				break;
			}
		}
	}
}


function zoneHexatek( links ) {
	// redirecttion de la zone Hexatek
	links.href = "http://www.hexatek.com/ZoneHexatek"
}



function openPopupWindow( image, winWidth, winHeight, title )
{
	var hWin, topPos, leftPos, htmlContent, settings;

	// set paramètres de haiteur et largeur de la fenêtre
	winWidth +=30;
	winHeight +=40;

	NewWinWidth = (winWidth) ? (winWidth) : 0;
	NewWinHeight = (winHeight) ? (winHeight): 0;
	NewWinWidth = (NewWinWidth > screen.availWidth - 60) ? screen.availWidth - 60: NewWinWidth;
	NewWinHeight = (NewWinHeight > screen.availHeight - 60) ? screen.availHeight - 60: NewWinHeight;

	topPos = ( screen.availHeight - NewWinHeight ) / 2 -15
	leftPos = ( screen.availWidth - NewWinWidth ) / 2 - 15
	
	// set le contenu html
	settings = 'menubar=no,,height=' + NewWinHeight + ',width=' + NewWinWidth + ',top=' + topPos + ',left='+ leftPos;
	htmlContent = '<html><head><title>' + title + '</title><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"></head><body bgcolor=#7A9BB2 leftmargin=0 topmargin=0 marginwidth=0 marginheight=0><table cellpadding="3" cellspacing="0" align="center"><tr><td><div><strong>' + title + '</strong></div></td></tr></table><table cellpadding="0" cellspacing="0" align="center"><tr><td><img src="' + image + '"></td></tr></table></body></html>'
	
	// créer la fenêtre popup
	hWin = window.open( "", "", settings, false );
	hWin.document.open();
	hWin.document.write( htmlContent );
	hWin.document.close(); 	
	
	hWin.focus();
	if( hWin.opener == null ) hWin.opener = self;
}


function openPopupProduct( url, winWidth, winHeight )
{
	var hWin, topPos, leftPos, htmlContent, settings;

	// set paramètres de haiteur et largeur de la fenêtre
//	winWidth +=30;
//	winHeight +=40;

	NewWinWidth = (winWidth) ? (winWidth) : 0;
	NewWinHeight = (winHeight) ? (winHeight): 0;
	NewWinWidth = (NewWinWidth > screen.availWidth - 60) ? screen.availWidth - 60: NewWinWidth;
	NewWinHeight = (NewWinHeight > screen.availHeight - 60) ? screen.availHeight - 60: NewWinHeight;

	topPos = ( screen.availHeight - NewWinHeight ) / 2 -15
	leftPos = ( screen.availWidth - NewWinWidth ) / 2 - 15
	
	// set le contenu html
	settings = 'menubar=no,,height=' + NewWinHeight + ',width=' + NewWinWidth + ',top=' + topPos + ',left='+ leftPos;
//	settings = 'menubar=no,,height=' + winHeight + ',width=' + winWidth;
	
	// créer la fenêtre popup
	hWin = window.open( url, "", settings, false );
	hWin.focus();
	if( hWin.opener == null ) hWin.opener = self;
}










function getDocHeight() {
	var docHeight;
	
   if (typeof document.height != 'undefined') {
     docHeight = document.height;
   }
   else if (document.compatMode && document.compatMode != 'BackCompat') {
     docHeight = document.documentElement.scrollHeight;
   }
   else if (document.body && typeof document.body.scrollHeight != 'undefined') {
     docHeight = document.body.scrollHeight;
   } 
	
	return docHeight;
}


function populateMenu() {
	var menuItems = new Array();
//	var counter = 0;

	if( currentLanguage == "fr" ) {
		id = 3;
	}
	else {
		id = 5;
	}

	// popule le menu
	for( var i = 0; i < menu.length; i++ ) {
		if( menu[i][0] == 1 ) {
			indice = menuItems.length;
			
			menuItems[indice] = new Array()
			menuItems[indice][0] = menu[i][id]
			menuItems[indice][1] = menu[i][id +1]
			
			if( menuItems[indice][0].slice( 0, 1 ) == "|" ) {
			}
			else {
				menuItems[indice][6] = "0";
//				counter++
			}
			
			if( menuItems[indice][0].slice( 0, 2 ) == "|-" ) {
				menuItems[indice][6] = "1";
			}
			
			if( i == 0 ) {
				menuItems[indice][7] = "0" //counter; //"0";
			}
		}
	}

	return menuItems;
}

function createPath() {
	var path, parentId, itemId, pathArray = new Array();
	var curPage;

	if( currentLanguage == "fr" ) {
		id = 4;
	}
	else {
		id = 6;
	}	
	
	path = '';

	curPage = getPageName( document.location.href );
		
//	alert(curPage);
		
	for( var i = menu.length -1; i >= 0; i-- ) {
		pos = menu[i][id].lastIndexOf( "/" );
		page = menu[i][id].slice( pos +1, menu[i][id].length );
		
		if( curPage == getPageName( menu[i][id] )	) {
			break;
		}
	}
	
	parentId = menu[i][2];
	itemId = menu[i][1]
	pathArray[pathArray.length] = itemId
	
	while( parentId != 0 ) {
		for( var j = 0; j < i; j++ ) {
			if( menu[j][1] == parentId ) {
				parentId = menu[j][2];
				itemId = menu[j][1];
				pathArray[pathArray.length] = itemId;
				break;
			}
		}
	}
		
	for( i = pathArray.length -1; i >= 0; i-- ) {
		for( j = 0; j < menu.length; ++j ) {
			if( pathArray[i] == menu[j][1] ) {
				
				itemDescription = menu[j][id -1];
				pos = itemDescription.lastIndexOf( "|" );
				itemDescription = itemDescription.slice( pos +1, itemDescription.length );
				itemDescription = itemDescription.replace( "<br>", " " );
				
				itemUrl = menu[j][id];
				
				if( i > 0 ) {
					path += '<a href="' + itemUrl + '">' + itemDescription + '</a>' + " > ";
				}
				else {
					path += itemDescription;
				}
				
				break;
			}
		}
	}
	
	document.write( path );
}

function hwel( links ) {
	page = location.protocol + "//" + location.host + "/WebEdit/WebEdit.jsp?page=" + links.href;
	hWin = window.open( page, "", "", false );
}


function findPosX( eElement )
{
	var nLeftPos = eElement.offsetLeft;
	var eParElement = eElement.offsetParent;

	while( eParElement != null )
	{
		nLeftPos += eParElement.offsetLeft;
		eParElement = eParElement.offsetParent;
	}
	
	return nLeftPos;
}

function findPosY( eElement )
{
	var nTopPos = eElement.offsetTop;
	var eParElement = eElement.offsetParent;
	while( eParElement != null )
	{
			nTopPos += eParElement.offsetTop;
			eParElement = eParElement.offsetParent;
	}
	
	return nTopPos;
}

function setValue( aValue )
{
	document.product_search.query.value = aValue;
}

function setPosition( aElement )
{
	var lElement = document.product_search.query;
	
	aElement.T_STICKY = true;
	aElement.T_FIX = [findPosX( lElement ), findPosY( lElement ) + lElement.offsetHeight];
	aElement.T_PADDING = 0;
	return TooltipText();
}
