/*

COPYRIGHT 2008
--------------------------
Toute reproduction, distribution totale ou partielle de son contenu sans autorisation est illicite expose le contrevenant à des poursuites judiciaires. 

*/


function url(u)
{
	return formatUrl.replace("LINKY", u)
}

function getObj(id)
{
	if (document.all) { return document.all[id]; } 
	else {	return document.getElementById(id);	}
}

function validateJSON(jsonText)
{
	return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(jsonText.replace(/"(\\.|[^"\\])*"/g, '')));
}

function trim(myString) 
{ 
	return myString.replace(/^\s+/g,'').replace(/\s+$/g,'') 
} 

function getOffsetPosition(oObj, inTYPE)
{
	var iVal = 0;
	var sType = 'oObj.offset' + inTYPE;
	while (oObj && oObj.tagName != 'BODY')
	{
		iVal += eval(sType);
		oObj = oObj.offsetParent;
	}
	return iVal;
}

function is_numeric(num)
{
	var exp = new RegExp('^[0-9-.]+$','g');
	return exp.test(num);
}

function displayBox(divName, iconName) 
{
	var box = getObj(divName);  
	if (iconName) var icon = getObj(iconName);
	
	if (box.style.display == 'block')
	{
		box.style.display = 'none';
		showSelects();
		return;
	}
	
	box.style.display = 'block';
	
	if (iconName)
	{
		box.style.top = (getOffsetPosition(icon, 'Top')+icon.height+1)+'px';
		box.style.left = getOffsetPosition(icon, 'Left') + 'px';
	}
	
	hideSelects();
}

function CDownloadUrl(method, url, func) {
	var httpObj;
	var browser = navigator.appName;
	if (browser.indexOf("Microsoft") > -1){
		httpObj = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		httpObj = new XMLHttpRequest();
	}
	httpObj.open(method, url, true);
	if (func !== null) {
		httpObj.onreadystatechange = function() {
			if(httpObj.readyState == 4){
				if (httpObj.status == 200) {
					var contenttype = httpObj.getResponseHeader('Content-Type');
					if (contenttype.indexOf('xml')>-1) {
						func(httpObj.responseXML);
					} else {
						func(httpObj.responseText);
					}
				} else {
					func('Error: '+httpObj.status);
				}
			}
		};
	}
	httpObj.send(null);   
}

function submitForm(form)
{
	var buttons = form.getElementsByTagName("button"); 
	var lenght = buttons.length;
	
	for (var i = 0; i < lenght; i++)
	{
    	buttons[i].disabled = 'disabled';
	}
}

function hideSelects()
{	
	var sel = document.getElementsByTagName('select');
	
	var l = sel.length;
	
	for (var i = 0; i < l; i++)
	{
		sel[i].style.visibility = 'hidden';
	}
	
	var parent = window.parent.document;
	
	if (parent)
	{
		var sel = parent.getElementsByTagName('select');
		
		var l = sel.length;
		
		for (var i = 0; i < l; i++)
		{
			sel[i].style.visibility = 'hidden';
		}
	}
}

function showSelects()
{
	var sel = document.getElementsByTagName('select');
	
	var l = sel.length;
	
	for (var i = 0; i < l; i++)
	{
		sel[i].style.visibility = 'visible';
	}
	
	var parent = window.parent.document;
	
	if (parent)
	{
		var sel = parent.getElementsByTagName('select');
		
		var l = sel.length;
		
		for (var i = 0; i < l; i++)
		{
			sel[i].style.visibility = 'visible';
		}
	}
}

function favDelGame(gameId)
{
	if (confirm(textDelFavGame))
	{
		CDownloadUrl('get', url('fav?gameId='+gameId), null);
		
		getObj('gameBlockId'+gameId).style.display = 'none';
	}
}


function gameAddFav(gameId)
{
	var callu = url('fav?gameId='+gameId);
	
	CDownloadUrl('get', callu, function (result)
	{
		alert((result == 'add'?textSuccessAddFav:textSuccessDelFav));
		
		thisGameInFav = thisGameInFav?0:1;
		
		getObj('textAddFav').innerHTML = thisGameInFav?textDelOfFav:textAddOfFav;
	});
}

function gameZoom(pourcent)
{
	var gameContenerDiv = getObj('pGameContener');
	var gameDiv = getObj('pGame');
		
	var newWidth = (gameCurrentWidth*pourcent);
	var newHeight = Math.round(((gameCurrentHeight*newWidth)/gameCurrentWidth));
	
	if (newWidth > gameMaxWidth)
	{
		bw = newWidth;
		newWidth = gameMaxWidth;
		newHeight = Math.round((newHeight * newWidth) / bw);
	}
	
	if (newWidth > gameMaxWidth || newWidth < gameMinWidth) return false;

	gameDiv.style.height = newHeight+'px';
	gameContenerDiv.style.width = newWidth+'px';
	gameContenerDiv.style.height = newHeight+'px';
	
	gameCurrentWidth = newWidth;
	gameCurrentHeight = newHeight;
	
	gameIsResize = true;
}
	
function displayGameFullScreen()
{
	if (gameFullScreen) return false;
	
	if (gameIsResize && gameCurrentWidth > gameNormalWidth)
	{
		width = gameCurrentWidth;
		height = gameCurrentHeight;
	}
	else
	{
		width = gameNormalWidth;
		height = gameNormalHeight;
	}

	var gameDiv = getObj('pGame');
	var gameContenerDiv = getObj('pGameContener');
	var shaderDiv = getObj('fullScreen');
	var closeFullScreenLink = getObj('fullScreenClose');
	var clientWidth = window.innerWidth?window.innerWidth:document.documentElement.clientWidth;
	var clientHeight = window.innerHeight?window.innerHeight:document.documentElement.clientHeight;
	var screenHeight = screen.height;
	
	closeFullScreenLink.style.display = 'block';
	//closeFullScreenLink.style.left = Math.round((clientWidth/2+width/2)-78)+'px';
	//closeFullScreenLink.style.top = Math.round((clientHeight/2-height/2)-28)+'px';
	closeFullScreenLink.style.right = '0px';
	closeFullScreenLink.style.top = '0px';
	closeFullScreenLink.style.zIndex = 15;
	
	shaderDiv.style.width = clientWidth+'px';
	shaderDiv.style.height = screenHeight+'px';
	shaderDiv.style.display = 'block';
	shaderDiv.style.zIndex = 5;
	
	gameContenerDiv.style.width = width+'px';
	gameContenerDiv.style.height = height+'px';
	gameContenerDiv.style.zIndex = 10;
	
	gameContenerDiv.style.marginTop = Math.round((clientHeight-height)/2)+'px';
	shaderDiv.appendChild(gameContenerDiv);
	
	gameFullScreen = true;
	
	scroll(0,0);
}

function removeGameFullScreen()
{
	if (!gameFullScreen) return false;
	
	var gameDiv = getObj('pGame');
	var gameContenerDiv = getObj('pGameContener');	
	var shaderDiv = getObj('fullScreen');
	var closeFullScreenLink = getObj('fullScreenClose');

	shaderDiv.style.display = 'none';
	
	gameContenerDiv.style.width = gameCurrentWidth+'px';
	gameContenerDiv.style.height = gameCurrentHeight+'px';	
	gameContenerDiv.style.marginTop = '0px';
	gameDiv.appendChild(gameContenerDiv);
	
	closeFullScreenLink.style.display = 'none';
	
	gameFullScreen = false;
		
	scroll(0,0);
}

function rateOver(num)
{
	if (gameIsRate) return false;
	
	getObj('rateGame').style.backgroundPosition = '0px -'+(180-((Math.round(num)-1)*20))+'px';
}

function rateMove(num)
{
	if (num == '0') num = -5;
	
	getObj('rateGame').style.backgroundPosition = '0px -'+(80-((Math.round(num)-1)*20))+'px';
}

function rateOut(num)
{
	if (gameIsRate) return false;
	
	rateMove(num);
}

function rateGame(gameId, note, noteDef)
{
	if (gameIsRate) return false;

	var callu = url('rate?gameId='+gameId+'&note='+note);
	
	CDownloadUrl('get', callu, function (result)
	{
		rateMove(note);
		
		gameIsRate = true;
		
		alert(textVoteOk);
		
		//getObj('rateOk').style.display = 'block';
	});
}
