var isOpera = false;
var isIE = false;
var isFF = false;
var isSafari = false;
var recentHash = '';
var currentPage = '';
var pollingBlocked = false;
var docTitle = '';

//some functions called already in the header
detectBrowser();
initialiseStateFromURL();


function initialiseStateFromURL()
{
	if(window.location.hash.length < 2)
   		window.location.hash = '#1000.php';   	
	recentHash = window.location.hash.substring(1).replace(/\$/, '?');
	currentPage = recentHash;
}

function checkConsistency()
{
	urlHash = window.location.hash.substring(1).replace(/\$/, '?');
	frameHref = contents.location.href;	
	pos = frameHref.indexOf('contents');
	fileName = frameHref.substring(pos + 9);
		
	if ((recentHash != urlHash) ||
		(recentHash != fileName) ||
		(fileName != urlHash))
		alert('recentHash: ' + recentHash +
			  '\nurlHash: ' + urlHash +
			  '\nfileName: ' + fileName);

}


function initialize() {
	contents.location.replace('contents/'+recentHash); 
	pollingBlocked = true;
	if(isIE) fixTitle();	
	setTimeout("pollHash()", 500);  
	updateMenu(recentHash);
}

//do not include hash in the title
function fixTitle()
{
	docTitle = document.title;
	pos = docTitle.indexOf('#');
	docTitle = docTitle.substring(0,pos);
	document.title = docTitle;
}

function pollHash() {
  var urlHash = window.location.hash.substring(1).replace(/\$/,'?');
  var frameHref = contents.location.href;	
  var pos = frameHref.indexOf('contents');
  var fileName = frameHref.substring(pos + 9);	
  var busy = false;
    
  if (pollingBlocked)
  {
		busy = true;		
		if (urlHash == recentHash && urlHash == fileName)
		{
			pollingBlocked = false;
		}
		else if (isIE && recentHash == fileName)
		{
			window.location.hash = fileName.replace(/\?/,'$');
			
			pollingBlocked = false;
		}
		
		else
		{
		}

	}

  if (urlHash!=recentHash && !busy) {
      
//    checkConsistency();  
    pollingBlocked = true;
    busy = true;
    recentHash = urlHash;

  	// URL has changed, update the UI accordingly.
  	loadContents(recentHash);
    	
    // do the animation!
		updateMenu(recentHash);
    }
    
    // checking consistency for the address    
    
  if (urlHash != fileName && !busy) //user clicked the back button
	{
   		pollingBlocked = true;
   		busy = true;
  		if (isIE)
    	{
    		window.location.hash = fileName;
    		recentHash = fileName;
    	}		
		else if (isFF)
    	{
    		contents.location.replace('contents/'+urlHash);
    		recentHash = urlHash;
    	}		
		else
    	{
    		window.location.hash = fileName.replace(/\?/,'$');
    		recentHash = fileName;
    	}	
    	// do the animation!
		updateMenu(recentHash);    	
	}
	setTimeout("pollHash()", 500);  
}


function openPage(pageName, queryString)
{
    
        // si on a clique sur le menu 3D, on redirige vers NVP3D
        // ... ben non, j'ai pas de logiciel pour editer le menu Flash :S
        if (pageName == '7000.php') {
            window.location.href = 'http://www.nvp3d.com';
            return;
        }
	pollingBlocked = true;
  updateHash(pageName); //	top.location.hash = pageName;
	recentHash = pageName;

	loadContents(pageName);
	
	//do the animation!
	updateMenu(recentHash);
	contents.focus();
}

function openPage2(pageName)
{
	pollingBlocked = true;
  updateHash(pageName); //	top.location.hash = pageName;
	recentHash = pageName;

	loadContents('../'+pageName);
	
	//do the animation!
	updateMenu(recentHash);
	contents.focus();
}

function updateHash(newHash)
{
  newHash = newHash.replace(/\?/, '$');
  if (!isIE)
  {
    top.location.hash = newHash;
  }
  else // fix the title bar in IE
    document.title = docTitle;
}

function loadContents(pageName)
{
	if (isIE)
	{
		contents.location = 'contents/' + pageName;
	}

	if (isFF)
	{
		contents.location.replace('contents/' + pageName);
	}
	else
	{
		contents.location = 'contents/' + pageName;
	}
	
	if (isSafari)
	 history.go(-1);
}



function detectBrowser()
{
	var agt=navigator.userAgent.toLowerCase();
	var appVer = navigator.appVersion.toLowerCase();

	var is_opera = (agt.indexOf("opera") != -1);

	var is_mac = (agt.indexOf("mac")!=-1);
	var iePos  = appVer.indexOf('msie');
	if (iePos !=-1) {
		if(is_mac) {
			var iePos = agt.indexOf('msie');
		}
	}

	var is_konq = (agt.indexOf('konqueror') !=-1);

	var is_safari = ((agt.indexOf('safari')!=-1)&&(agt.indexOf('mac')!=-1))?true:false;
	var is_khtml  = (is_safari || is_konq);

	var is_gecko = ((!is_khtml)&&(navigator.product)&&(navigator.product.toLowerCase()=="gecko"))?true:false;


	var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
					&& (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
					&& (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1)
					&& (!is_khtml) && (!(is_moz)) && (!is_fb) && (!is_fx));


	var is_ie   = ((iePos!=-1) && (!is_opera) && (!is_khtml));

	var is_aol   = (agt.indexOf("aol") != -1);

	var is_fb = ((agt.indexOf('mozilla/5')!=-1) && (agt.indexOf('spoofer')==-1) &&
		 (agt.indexOf('compatible')==-1) && (agt.indexOf('opera')==-1)  &&
		 (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1)     &&
		 (is_gecko) && (navigator.vendor=="Firebird"));
	var is_fx = ((agt.indexOf('mozilla/5')!=-1) && (agt.indexOf('spoofer')==-1) &&
		 (agt.indexOf('compatible')==-1) && (agt.indexOf('opera')==-1)  &&
		 (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1)     &&
		 (is_gecko) && ((navigator.vendor=="Firefox")||(agt.indexOf('firefox')!=-1)));
	var is_moz   = ((agt.indexOf('mozilla/5')!=-1) && (agt.indexOf('spoofer')==-1) &&
			(agt.indexOf('compatible')==-1) && (agt.indexOf('opera')==-1)  &&
			(agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1)     &&
			(is_gecko) && (!is_fb) && (!is_fx) &&
			((navigator.vendor=="")||(navigator.vendor=="Mozilla")||(navigator.vendor=="Debian")));

	var is_win   = ( (agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1) );
	var is_os2   = ((agt.indexOf("os/2")!=-1) ||
		(navigator.appVersion.indexOf("OS/2")!=-1) ||
		(agt.indexOf("ibm-webexplorer")!=-1));

	var is_mac    = (agt.indexOf("mac")!=-1);
	if (is_mac) { is_win = !is_mac; } 

	var is_sun   = (agt.indexOf("sunos")!=-1);
	var is_linux = (agt.indexOf("inux")!=-1);

	isIE = is_ie;
	//isOpera = is_opera;
	//isFF = is_fx;
	isFF = (is_gecko || is_opera);
	isSafari = isSafari;
}


function launchSearch()
{
	searchbox=document.getElementById('searchbox');
	var query=searchbox.value;
	query = encodeURI(query);
	query = query.replace(/\x2B/g, '%2B');	
	query = query.replace(/\%20/g, '+');	
  
	openPage('9000.php?q='+query);

}

function changeLanguage(oldLangStr, newLangStr)
{
  var location = window.location.href;
  window.location = location.replace(oldLangStr, newLangStr);
  return false;
}
