var detect = navigator.userAgent.toLowerCase();
var newsIndex = 0;

/*
	Test if a string contains a substring
*/
function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

/*
	Open a file and add it to id
*/
function submitForm(id,link)
{ 
    var xhr; 
    try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
    catch (e) 
    {
        try {xhr = new ActiveXObject('Microsoft.XMLHTTP');}
        catch (e2) 
        {
          try {  xhr = new XMLHttpRequest();     }
          catch (e3) {  xhr = false;   }
        }
     }
 
    xhr.onreadystatechange  = function()
    { 
         if(xhr.readyState  == 4)
         {
              if(xhr.status  == 200) 
                document.getElementById(id).innerHTML = xhr.responseText; 
              else 
                document.getElementById(id).innerHTML = "Error code " + xhr.status;
         }
    }; 

   xhr.open( "GET", link,  true); 
   xhr.send(null); 
}

/*
	Reload the screenshots page
*/
function reloadScreenshots(id,start,lang)
{
	submitForm(id,"../screenshotsExplorer.php?screenshotsStart=" + start + "&lang=" + lang);
}

function reloadNews(order,numberOfLines, newsMax)
{
	var start;
	
	document.getElementById("upArrowNews").style.display = 'block';
	document.getElementById("downArrowNews").style.display = 'block';
	
	document.getElementById("upArrowNewsHidden").style.display = 'none';
	document.getElementById("downArrowNewsHidden").style.display = 'none';
	
	if(order > 0)
	{
		start = newsIndex - numberOfLines;
		if(start <= 0)
		{
			start = 0;

			document.getElementById("upArrowNews").style.display = 'none';
			document.getElementById("downArrowNews").style.display = 'block';
			
			document.getElementById("upArrowNewsHidden").style.display = 'block';
			document.getElementById("downArrowNewsHidden").style.display = 'none';
		}
	}
	else
	{
		start = newsIndex + numberOfLines;
		if(start + numberOfLines >= newsMax)
		{
			if(start + numberOfLines > newsMax)
				start = newsMax - 1;
			document.getElementById("upArrowNews").style.display = 'block';
			document.getElementById("downArrowNews").style.display = 'none';
			
			document.getElementById("upArrowNewsHidden").style.display = 'none';
			document.getElementById("downArrowNewsHidden").style.display = 'block';			
		}
	}
		
	for(var i = 0 ; i < newsMax ; i++)
	{
		document.getElementById("newsTrLine" + i).style.display = 'none';
		if(i >= start && i < (start + numberOfLines))
			document.getElementById("newsTrLine" + i).style.display = 'table-row';
	}
	
	newsIndex = start;
}

/*
	Open an image
*/
function imageExplorerOpen(url,width,height)
{
	var border = 5;
	var redimBorder = 40;
	var box = document.getElementById("imageExplorer"); 
	var content = document.getElementById("imageExplorerContent");
	
	var finalRatio = 100;
	
	//IE Version
	if(checkIt('msie'))
	{
		if(document.documentElement.clientWidth < width)
			finalRatio = ((document.documentElement.clientWidth - redimBorder) * 100) / width;
			
		if(finalRatio > 100)
			finalRatio = 100;

		if((((document.documentElement.clientHeight - redimBorder) * 100) / height) < finalRatio)
			finalRatio = ((document.documentElement.clientHeight - redimBorder) * 100) / height;
	}
	else
	{
		//Other Web browsers
		if(window.innerWidth < width)
			finalRatio = ((window.innerWidth - redimBorder) * 100) / width;
			
		if(finalRatio > 100)
			finalRatio = 100;
			
		if((((window.innerHeight - redimBorder) * 100) / height) < finalRatio)
			finalRatio = ((window.innerHeight - redimBorder) * 100) / height;
	}
	
	content.style.padding = "0";
	content.innerHTML = "<img src=" + url + " width="+((width * finalRatio) / 100)+" height="+((height * finalRatio) / 100)+" alt=\"\" />";
	box.style.display = "block";	
	box.style.position = "absolute";
	box.style.width = (width * finalRatio) / 100 + "px";
	box.style.height = (height * finalRatio) / 100 + "px";
	box.style.marginLeft = "-" + (((width * finalRatio) / 100 + border) / 2) + "px";
	
	//IE Version
	if(checkIt('msie'))
	{
		if(finalRatio < 100)
			box.style.top = (document.documentElement.scrollTop + ((document.documentElement.clientHeight - ((height * finalRatio) / 100)) / 2)) - 7 + "px";
		else
			box.style.top = (document.documentElement.scrollTop + (document.documentElement.clientHeight - height) / 2) - 7 + "px";
	}
	else
	{
		if(checkIt('mozilla'))
		{
			if(finalRatio < 100)
				box.style.top = (window.pageYOffset + ((document.documentElement.clientHeight - ((height * finalRatio) / 100)) / 2)) - 7 + "px";
			else
				box.style.top = (window.pageYOffset + (document.documentElement.clientHeight - height) / 2) - 7 + "px";
		}
		else
		{
			//Other Web browsers
			if(finalRatio < 100)
				box.style.top = (box.parentNode.scrollTop + ((window.innerHeight - ((height * finalRatio) / 100)) / 2)) - 7 + "px";
			else
				box.style.top = (box.parentNode.scrollTop + (window.innerHeight - height) / 2) - 7 + "px";
		}
	}
		
	box.style.padding = border + "px";
	box.style.left = "50%";
	box.style.backgroundImage = "url(../imgs/iEB.png)";
}

/*
	Close an image
*/
function imageExplorerClose()
{
	document.getElementById('imageExplorer').style.display = "none";
}

/*
	Preload some images
*/
function preloadImages()
{
	img1 = new Image(); 
	img1.src = "../imgs/screenshots_next_hover.png";
	img2 = new Image(); 
	img2.src = "../imgs/screenshots_previous_hover.png";
	img3 = new Image(); 
	img3.src = "../imgs/iEB.png";
	img4 = new Image(); 
	img4.src = "../imgs/home_fr_hover.png";
	img5 = new Image(); 
	img5.src = "../imgs/dwn_fr_hover.png";
	img6 = new Image(); 
	img6.src = "../imgs/home_en_hover.png";
	img7 = new Image(); 
	img7.src = "../imgs/dwn_en_hover.png";
	img8 = new Image(); 
	img8.src = "../imgs/forums_fr_hover.png";
	img9 = new Image(); 
	img9.src = "../imgs/forums_en_hover.png";
	img10 = new Image();
	img10.src = "../imgs/news_previous_hover.png";
	img11 = new Image(); 
	img11.src = "../imgs/news_next_hover.png";
}
