//Utilities functions to manage gui

var currentZIndex = 1;
var openVelocity = 1;
var openedDivs = new Object();
var open_timeout = null;

var currentZone = "";
var main_table = null;
var cache_haut = null;
var cache_bas = null;
var cache_gauche = null;
var cache_droite = null;

var game_choose_ui_loaded = false;

var last_news_date = 1272745388765;

function InitUi()
{
    document.body.oncontextmenu = function(){return false;}
    
	var bodyWidth = document.body.clientWidth;
	var bodyHeight = document.body.clientHeight;
	
	//Target width/height : 1420 - 722

	var availableWidth = (bodyWidth > 1420) ? 1420 : bodyWidth;
	var availableHeight = (720 * availableWidth) / 1420; 
	
	
	var initialLeft = bodyWidth - ((bodyWidth - availableWidth) * 1.5)
	var initialTop = (availableHeight < bodyHeight) ?  bodyHeight - ((bodyHeight - availableHeight) * 1.5) : bodyHeight;
	
	
	var imgFond = document.getElementById("IntroDiv_fond");
	var introText = document.getElementById("IntroDiv_text");
    
	var isKnownUser = (getCookie("ethnoludie_" + culture) == "1");
	if (!isKnownUser) {
		var startLeft = bodyWidth - 100;

    
   		var img = getAlphaImg("img/fond_intro.png", "1421px", "722px");
    	img.style.position = "absolute";
    
    	img.setAttribute("oWidth", "1421");
    	img.setAttribute("oHeight", "722");
    	img.setAttribute("oTop", "5");
    	img.setAttribute("oLeft", startLeft);
    	imgFond.appendChild(img);

	    
    	introText.setAttribute("oTop", "70");
    	introText.setAttribute("oLeft", startLeft + 90);
	}
	else {
		imgFond.parentNode.removeChild(imgFond);
		introText.parentNode.removeChild(introText);
	}
    
    var acceuil = document.getElementById("MAIN_ACCEUIL_DIV");
    
    
    //Set img news
    var imgNews = getAlphaImg("img/fond_news.png", "917px", "213px");
    imgNews.id = "News_div_img";
    imgNews.style.position = "absolute";
    
    imgNews.setAttribute("oWidth", "917");
    imgNews.setAttribute("oHeight", "213");
    imgNews.setAttribute("oTop", "5");
    imgNews.setAttribute("oLeft", "500");
    acceuil.appendChild(imgNews);
    
    //Set close img
    var imgCloseNews = getAlphaImg("img/close_news.png", "11px", "11px");
    imgCloseNews.id = "News_close_img";
    imgCloseNews.style.position = "absolute";
    imgCloseNews.style.cursor = "pointer";
    imgCloseNews.onclick = HiddeTotalDivInfo;
    
    imgCloseNews.setAttribute("oWidth", "11");
    imgCloseNews.setAttribute("oHeight", "11");
    imgCloseNews.setAttribute("oTop", "40");
    imgCloseNews.setAttribute("oLeft", "1390");
    acceuil.appendChild(imgCloseNews);
    
    
    //hidde all infos divs
    HiddeTotalDivInfo();
    
    var newsDivText = document.getElementById("News_div_Text_" + culture);
    newsDivText.parentNode.style.display = "none";
    
    if (isKnownUser) {
        var lastNewsDateCookie = parseInt(getCookie("last_news_" + culture), 10);
        if (isNaN(lastNewsDateCookie)) {
            lastNewsDateCookie = 0;
        }
        
        if (lastNewsDateCookie < last_news_date) {
            imgNews.style.display = "";
            imgCloseNews.style.display = "";
            newsDivText.parentNode.style.display = "";
            newsDivText.style.display = "";
        }
    }
    
    setCookie("last_news_" + culture, new Date().getTime());
	
    
    main_table = document.getElementById("MAIN_TABLE");
	main_table.style.top = -initialTop; main_table.style.left = -initialLeft;

	SetDivSize(document.getElementById("MAIN_TOURNAMENT_DIV"), bodyWidth, bodyHeight);
	SetDivSize(document.getElementById("MAIN_INSCRIPTION_DIV"), bodyWidth, bodyHeight);
	SetDivSize(acceuil, bodyWidth, bodyHeight);
	SetDivSize(document.getElementById("MAIN_STATS_DIV"), bodyWidth, bodyHeight);
	SetDivSize(document.getElementById("MAIN_PLAY_DIV"), bodyWidth, bodyHeight);

	
	if (beta)
	{
		var str = "<table style=\"position:absolute;TOP:0;width:98%\"><tr><td align=\"center\">";
		str += "<table bgcolor=\"#CCCCCC\" style=\"cursor:pointer;width:300px\"><tr>";
		str += "<td onClick=\"GoToDiv('MAIN_TOURNAMENT_DIV', OnTournamentClick);\">Tournois</td>";
		str += "<td onClick=\"GoToDiv('MAIN_STATS_DIV', OpenStatisticsPopup);\">Statistiques</td><td onClick=\"OpenUserAccountPopup(event);\">Mon compte</td>";
    	str += "</tr></table>";
		str += "</td></tr></table>";
		
		
		acceuil.innerHTML += str;
	}
	
	cache_haut = document.getElementById("cache_haut");
	cache_haut.style.width = bodyWidth;

	cache_bas = document.getElementById("cache_bas");
	cache_bas.style.width = bodyWidth;
	
	
	cache_gauche = document.getElementById("cache_gauche");
	cache_gauche.style.height = bodyHeight;
	
	
	cache_droite = document.getElementById("cache_droite");
	cache_droite.style.height = bodyHeight;
	
	currentZone = "MAIN_ACCEUIL_DIV";
	
    
    //Copyright
    var divCopyright = document.createElement("div");
    divCopyright.style.position = "absolute";
    divCopyright.style.left = "0px";
    divCopyright.style.bottom = "0px";
    
    if (document.location.href.indexOf("ethnoludie.com") != -1) {
        
        divCopyright.innerHTML = "<img src=\"img/logsc_en.gif\" />";
    } else {
        
        divCopyright.innerHTML = "<img src=\"img/logsc_fr.gif\"  />";
    }

    acceuil.appendChild(divCopyright);
    
	OnBodyResize();
    
    if (!isKnownUser) {
		setCookie("ethnoludie_" + culture, "1", 15); 
		
    	var factor = acceuil.clientWidth / 1420;
    	startLeft *= factor;
    
    	var fctHandler = function() { StartDisplayIntro(imgFond.firstChild, introText, 0)};
        setTimeout(fctHandler, 1000);
        fctHandler = null;
	}
}

function StartDisplayIntro(img, text, step) {
    
    if (step == 0) {
        img.style.left = -5;
        img.setAttribute("oLeft", "-5");
        
        text.style.left = 85;
        text.setAttribute("oLeft", "85");
        
        var fctHandler = function() { StartDisplayIntro(img, text, 1)};
        setTimeout(fctHandler, 100);
        fctHandler = null;
    }
    
    else if (step == 1) {
        img.style.left = 10;
        img.setAttribute("oLeft", "10");
        
        text.style.left = 100;
        text.setAttribute("oLeft", "100");
        
        var fctHandler = function() { StartDisplayIntro(img, text, 2)};
        setTimeout(fctHandler, 100);
        fctHandler = null;
    }
    
    else if (step == 2) {
        img.style.left = 5;
        img.setAttribute("oLeft", "5");
        
        text.style.left = 90;
        text.setAttribute("oLeft", "90");
    }
}

function LoadGameChooseUi()
{
	var div = document.getElementById("MAIN_PLAY_DIV");
	
	var srcGame = "<img src=\"img/Jeux_1440.jpg\" class=\"NoSelectable\" style=\"width:100%;height:100%\" />";
	
	//Title
	srcGame += "<div class=\"Game_Title_Img\" oTop=\"15\" oLeft=\"650\" oWidth=\"410\" oHeight=\"150\">";
    srcGame += getAlphaImgText(images_localise["Game_Title_img"].trads[culture], "100%", "100%");
    srcGame += "</div>";
	
	//home
	srcGame += "<div class=\"Game_Accueil\" oTop=\"510\" oLeft=\"1290\"><table cellpadding=0 cellspacing=0><tr><td>";
    srcGame += getAlphaImgText(images_localise["Game_Img_Home"].trads[culture], "104px", "42px", "Game_Img_Home", 104, 42, "Game_Accueil_Img", "GoToDiv('MAIN_ACCEUIL_DIV', OnLoadAcceuil);");
    srcGame += "</td></tr><tr><td>";
    srcGame += getAlphaImgText("img/game_home_bas_1440.png", "104px", "85px", null, 104, 42, "Game_Accueil_Img");
    srcGame += "</td></tr></table></div>";
	
	//Awale
	srcGame += "<div class=\"Game_Awale\" oTop=\"600\" oLeft=\"560\"><table cellpadding=0 cellspacing=0><tr><td>";
    srcGame += getAlphaImgText("img/awale.png", "143px", "58px", null, 143, 58, "NoSelect_Img", "OnGameChoose(event, 0);");
    srcGame += "</td></tr><tr><td>";
    srcGame += getAlphaImgText("img/awale_bas.png", "143px", "89px", null, 143, 89, "NoSelect_Img", null);
    srcGame += "</td></tr></table></div>";
	
	//Jeu royal d'ur
	srcGame += "<div class=\"Game_Ur\" oTop=\"588\" oLeft=\"1125\"><table cellpadding=0 cellspacing=0><tr><td>";
    srcGame += getAlphaImgText(images_localise["Game_Img_Ur"].trads[culture], "143px", "58px", "Game_Img_Ur", 191, 90, "NoSelect_Img", "OnGameChoose(event, 1);");
    srcGame += "</td></tr><tr><td>";
    srcGame += getAlphaImgText("img/ur_bas.png", "143px", "58px", null, 191, 68, "NoSelect_Img");
    srcGame += "</td></tr></table></div>";
	
	//kukuli
	srcGame += "<div class=\"Game_Kukuli\" oTop=\"550\" oLeft=\"850\"><table cellpadding=0 cellspacing=0><tr><td>";
    srcGame += getAlphaImgText("img/kukuli.png", "141px", "58px", null, 141, 58, "NoSelect_Img", "OnGameChoose(event, 2);");
    srcGame += "</td></tr><tr><td>";
    srcGame += getAlphaImgText("img/kukuli_bas.png", "141px", "50px", null, 141, 50, "NoSelect_Img");
    srcGame += "</td></tr></table></div>";
	
	//backgammon
	srcGame += "<div class=\"Game_Backgammon\" oTop=\"540\" oLeft=\"250\"><table cellpadding=0 cellspacing=0><tr><td>";
    srcGame += getAlphaImgText("img/backgammon.png", "170px", "57px", null, 170, 57, "NoSelect_Img", "OnGameChoose(event, 3);");
    srcGame += "</td></tr><tr><td>";
    srcGame += getAlphaImgText("img/backgammon_bas.png", "170px", "71px", null, 170, 71, "NoSelect_Img");
    srcGame += "</td></tr></table></div>";
	
	//Jeu de table libre
	srcGame += "<div class=\"Game_Tables\" oTop=\"500\" oLeft=\"620\"><table cellpadding=0 cellspacing=0><tr><td>";
    srcGame += getAlphaImgText(images_localise["Game_Img_Tables"].trads[culture], "170px", "56px", "Game_Img_Tables", 170, 56, "NoSelect_Img", "OnGameChoose(event, 4);");
    srcGame += "</td></tr><tr><td>";
    srcGame += getAlphaImgText("img/tables_bas.png", "170px", "73px", null, 170, 73, "NoSelect_Img");
    srcGame += "</td></tr></table></div>";
	
	//plakoto
	srcGame += "<div class=\"Game_Plakoto\" oTop=\"610\" oLeft=\"110\"><table cellpadding=0 cellspacing=0><tr><td>";
    srcGame += getAlphaImgText("img/plakoto.png", "172px", "75px", null, 172, 75, "NoSelect_Img", "OnGameChoose(event, 5);");
    srcGame += "</td></tr><tr><td>";
    srcGame += getAlphaImgText("img/plakoto_bas.png", "172px", "32px", null, 172, 32, "NoSelect_Img");
    srcGame += "</td></tr></table></div>";
	
    
    //Dou shou qi
    if (beta) {
            srcGame += "<div style=\"position:absolute;top:0px;left:0px;background:black;color:#ffFFFF;cursor:pointer\" onclick=\"OnGameChoose(event, 6);\">Dou Shou Qi</div>";
    }

	
	div.innerHTML = srcGame;
	OnBodyResize();
}

function OnBodyResize()
{
	var bodyWidth = document.body.clientWidth;
	var bodyHeight = document.body.clientHeight;
	
	var acceuil = document.getElementById("MAIN_ACCEUIL_DIV");	
	SetDivSize(acceuil, bodyWidth, bodyHeight);
	
	var tournois = document.getElementById("MAIN_TOURNAMENT_DIV");
	SetDivSize(tournois, bodyWidth, bodyHeight);
	
	var inscription = document.getElementById("MAIN_INSCRIPTION_DIV");
	SetDivSize(inscription, bodyWidth, bodyHeight);
	
	var stats = document.getElementById("MAIN_STATS_DIV");
	SetDivSize(stats, bodyWidth, bodyHeight);
	
	var play = document.getElementById("MAIN_PLAY_DIV");
	SetDivSize(play, bodyWidth, bodyHeight);

	
	var availableWidth = (bodyWidth > 1420) ? 1420 : bodyWidth;
	var availableHeight = (720 * availableWidth) / 1420;
	var cache_height = (availableHeight < bodyHeight) ? parseInt((bodyHeight / 2) - (availableHeight / 2) + 1, 10) : 0;
	
	var imgWidth = acceuil.clientWidth;
	
	var cache_width = (imgWidth < bodyWidth) ? parseInt((bodyWidth / 2) - (imgWidth / 2) + 1, 10) : 0;
	
	cache_haut.style.height = (cache_height > 0) ? cache_height : "1px";
	cache_haut.style.display = (cache_height > 0) ? "" : "none";
	cache_haut.style.width = bodyWidth;
	
	cache_bas.style.height = (cache_height > 0) ? cache_height : "1px";
	cache_bas.style.display = (cache_height > 0) ? "" : "none";
	cache_bas.style.width = bodyWidth;
	
	cache_gauche.style.height = bodyHeight;
	cache_gauche.style.width = (cache_width > 0) ? cache_width : "1px";
	cache_gauche.style.display = (cache_width > 0) ? "" : "none";
	
	
	cache_droite.style.height = bodyHeight;
	cache_droite.style.width = (cache_width > 0) ? cache_width : "1px";
	cache_droite.style.display = (cache_width > 0) ? "" : "none";
	
	if (currentZone == "MAIN_ACCEUIL_DIV")
	{
		var initialLeft = bodyWidth - ((bodyWidth - acceuil.clientWidth) * 1.5);
		var initialTop = (availableHeight < bodyHeight) ?  bodyHeight - ((bodyHeight - availableHeight) * 1.5) : bodyHeight;
	
		main_table.style.top = -initialTop; main_table.style.left = -initialLeft; 
	}
	else if (currentZone == "MAIN_STATS_DIV")
	{
		var initialTop = (reso_h < bodyHeight) ?  bodyHeight - ((bodyHeight - reso_h) * 1.5) : bodyHeight;
		var endX = (reso_w < bodyWidth) ?  (2 * bodyWidth) - ((bodyWidth - reso_w) * 2.5) : bodyWidth * 2;
		
		main_table.style.top = -initialTop; main_table.style.left = -endX;
	}
	else if (currentZone == "MAIN_TOURNAMENT_DIV")
	{
		var initialTop =  (reso_h < bodyHeight) ?  ((bodyHeight / 2) - (reso_h / 2)) : 0;
		var initialLeft = (reso_w < bodyWidth) ?  bodyWidth - ((bodyWidth - reso_w) * 1.5) : bodyWidth;
		main_table.style.left = -initialLeft;
		main_table.style.top = initialTop;
	}
	else if (currentZone == "MAIN_INSCRIPTION_DIV")
	{
		var initialTop = (reso_h < bodyHeight) ?  bodyHeight - ((bodyHeight - reso_h) * 1.5) : bodyHeight;
		main_table.style.left = 0;
		main_table.style.top = -initialTop;
	}
	else if (currentZone == "MAIN_PLAY_DIV")
	{
		var initialLeft = bodyWidth - ((bodyWidth - acceuil.clientWidth) * 1.5);
		var initialTop = (availableHeight < bodyHeight) ?  bodyHeight - ((bodyHeight - availableHeight) * 1.5) : bodyHeight;
		initialTop = -2 * initialTop;
		
		main_table.style.top = initialTop - cache_height; main_table.style.left = -initialLeft; 
	}
	
	SetTxtSizes();
}

function EnterClick()
{
    var background = document.getElementById("IntroDiv_fond");
    if (background) {
        background.parentNode.removeChild(background);
    }
    
    var textIntro = document.getElementById("IntroDiv_text");
    if (textIntro) {
        textIntro.parentNode.removeChild(textIntro);
    }
}

function SetDivSize(div, bodyWidth, bodyHeight)
{
	//Target width/height : 1420 - 72
	
	if (bodyWidth > 1420) bodyWidth = 1420;
	div.style.width = bodyWidth;
	
	var neededHeight = (720 * bodyWidth) / 1420;
	
	if (neededHeight > bodyHeight) {
		div.style.width = (1420 * bodyHeight) / 720;
	}
	
	div.style.height = (bodyHeight > neededHeight) ? neededHeight : bodyHeight;
	div.style.overflowY = "hidden";
	div.style.overflowX = "hidden";
}


function SetTxtSizes() {

	//Calcul factor
	var acceuil = document.getElementById("MAIN_ACCEUIL_DIV");	
	var factor = acceuil.clientWidth / 1420;

	var divs = document.getElementsByTagName("div");
	for (var i = 0; i < divs.length; i++) {
		
		var child = divs[i];
		
		var oTop = child.getAttribute("oTop");
		if ((oTop != null) && (oTop != ""))
		{
			child.style.top = parseInt(oTop, 10) * factor;
		}
		
		var oLeft = child.getAttribute("oLeft");
		
		if ((oTop != null) && (oTop != ""))
		{
			child.style.left = parseInt(oLeft, 10) * factor;
		}
		
		var oFont = child.getAttribute("oFont");
		if ((oFont != null) && (oFont != ""))
		{
            var size = TestZero(parseInt(oFont, 10) * factor); 
			child.style.fontSize = size + "pt";
		}
		
		var oWidth = child.getAttribute("oWidth");
		if ((oWidth != null) && (oWidth != ""))
		{
			child.style.width = TestZero(parseInt(oWidth, 10) * factor) + "px";
		}
		
		var oHeight = child.getAttribute("oHeight");
		if ((oHeight != null) && (oHeight != ""))
		{
			child.style.height = TestZero(parseInt(oHeight, 10) * factor) + "px";
		}
	}
	
	var divs = document.getElementsByTagName("img");
	for (var i = 0; i < divs.length; i++) {
		
		var child = divs[i];
		
		var oTop = child.getAttribute("oTop");
		if ((oTop != null) && (oTop != ""))
		{
			child.style.top = parseInt(oTop, 10) * factor;
		}
		
		var oLeft = child.getAttribute("oLeft");
		
		if ((oLeft != null) && (oLeft != ""))
		{
			child.style.left = parseInt(oLeft, 10) * factor;
		}
		
		var oWidth = child.getAttribute("oWidth");
		if ((oWidth != null) && (oWidth != ""))
		{
			child.style.width = TestZero(parseInt(oWidth, 10) * factor) + "px";
		}
		
		var oHeight = child.getAttribute("oHeight");
		if ((oHeight != null) && (oHeight != ""))
		{
			child.style.height = TestZero(parseInt(oHeight, 10) * factor) + "px";
		}
	}
    
    var refs = document.getElementsByTagName("a");
	for (var i = 0; i < refs.length; i++) {
        var child = refs[i];
        var oFont = child.getAttribute("oFont");
		if ((oFont != null) && (oFont != ""))
		{
            var size = TestZero(parseInt(oFont, 10) * factor); 
			child.style.fontSize = size + "pt";
		}
    }
}

function TestZero(num) {
	return (num <= 0) ? 1 : num;	
}

function OnLoadAcceuil()
{
}

function HiddeTotalDivInfo() {
    
    //remove all    
    document.getElementById("News_div_img").style.display = "none";
    document.getElementById("News_close_img").style.display = "none";

    
    //text
    document.getElementById("News_div_Text_fr").style.display = "none";
    document.getElementById("News_div_Text_en").style.display = "none";
    document.getElementById("Links_div_Text_fr").style.display = "none";
    document.getElementById("Links_div_Text_en").style.display = "none";
    document.getElementById("LinksInternal_div_Text_en").style.display = "none";
    document.getElementById("LinksInternal_div_Text_fr").style.display = "none";
    
    document.getElementById("Links_div_Title_fr").style.display = "none";
    document.getElementById("Links_div_Title_en").style.display = "none";
}

function DisplayNews() {
    document.getElementById("News_div_img").style.display = "";
    document.getElementById("News_close_img").style.display = "";
    
    //Hidde others
    document.getElementById("Links_div_Text_" + culture).style.display = "none";
    document.getElementById("Links_div_Title_" + culture).style.display = "none";
    document.getElementById("LinksInternal_div_Text_" + culture).style.display = "none";
    
    //display news
    var newsDiv = document.getElementById("News_div_Text_" + culture);
    newsDiv.style.display = "";
    newsDiv.parentNode.style.display = "";
    
}

function DisplayLinks() {
    document.getElementById("News_div_img").style.display = "";
    document.getElementById("News_close_img").style.display = "";
    
    //Hidde others
    document.getElementById("News_div_Text_" + culture).style.display = "none";
    document.getElementById("LinksInternal_div_Text_" + culture).style.display = "none";
    
    //display links
    var linksDiv = document.getElementById("Links_div_Text_" + culture);
    linksDiv.style.display = "";
    linksDiv.parentNode.style.display = "";
    
    //Set span class
    var titleSpan = document.getElementById("Links_div_Title_" + culture);
    titleSpan.style.display = "";
    titleSpan.firstChild.className = "SpanLinksTitleSelected";
    titleSpan.lastChild.className = "SpanLinksTitle";
}

function DisplayInternalLinks() {
    //Hidde others
    document.getElementById("Links_div_Text_" + culture).style.display = "none";
    
    //Display internal links
    document.getElementById("LinksInternal_div_Text_" + culture).style.display = "";
    
    //Set span class
    var titleSpan = document.getElementById("Links_div_Title_" + culture);
    titleSpan.lastChild.className = "SpanLinksTitleSelected";
    titleSpan.firstChild.className = "SpanLinksTitle";
}

function GoToDiv(divName, onEnd, bodyWidth, bodyHeight)
{
	var endX = 0;
	var endY = 0;
	currentZone = "";
	
	bodyWidth = (bodyWidth) ? bodyWidth : document.body.clientWidth;
	bodyHeight = (bodyHeight) ? bodyHeight : document.body.clientHeight;
	var availableWidth = (bodyWidth > 1420) ? 1420 : bodyWidth;
	var availableHeight = (720 * availableWidth) / 1420;
	
	var acceuil = document.getElementById("MAIN_ACCEUIL_DIV");	
	
	if (divName == "MAIN_STATS_DIV")
	{
		endX = (reso_w < bodyWidth) ?  (2 * bodyWidth) - ((bodyWidth - reso_w) * 2.5) : bodyWidth * 2;
		endY = (reso_h < bodyHeight) ?  bodyHeight - ((bodyHeight - reso_h) * 1.5) : bodyHeight;
		
		endY = -endY;
		endX = -endX;
	}
	else if (divName == "MAIN_ACCEUIL_DIV")
	{
		endX = bodyWidth - ((bodyWidth - acceuil.clientWidth) * 1.5);
		endX = -endX;
		
		endY = (availableHeight < bodyHeight) ?  bodyHeight - ((bodyHeight - availableHeight) * 1.5) : bodyHeight;
		endY = -endY;
	}
	else if (divName == "MAIN_TOURNAMENT_DIV")
	{
		endX = (reso_w < bodyWidth) ?  bodyWidth - ((bodyWidth - reso_w) * 1.5) : bodyWidth;
		endY =  (reso_h < bodyHeight) ?  ((bodyHeight / 2) - (reso_h / 2)) : 0;
		
		endX = -endX;
		endY = endY;
	}
	else if (divName == "MAIN_INSCRIPTION_DIV")
	{
		endX = (reso_w < bodyWidth) ?  ((bodyWidth / 2) - (reso_w / 2)) : 0;
		endY = (reso_h < bodyHeight) ?  bodyHeight - ((bodyHeight - reso_h) * 1.5) : bodyHeight;
		endY = -endY;
	}
	else if (divName == "MAIN_PLAY_DIV")
	{
		var cache_height = (availableHeight < bodyHeight) ? parseInt((bodyHeight / 2) - (availableHeight / 2) + 1, 10) : 0;
		
		if (game_choose_ui_loaded == false)
		{
			game_choose_ui_loaded = true;
			LoadGameChooseUi();
		}
		
		endY = (availableHeight < bodyHeight) ?  bodyHeight - ((bodyHeight - availableHeight) * 1.5) : bodyHeight;
		endY = -2 * endY - cache_height;
		
		endX = bodyWidth - ((bodyWidth - acceuil.clientWidth) * 1.5);
		endX = -endX;
	}
	
	var redo = false;
	var diff = parseInt(main_table.style.left, 10) - endX;
	
	if (diff > 20)
	{
		main_table.style.left = parseInt(main_table.style.left, 10) - 20;
		redo = true;
	}
	else if (diff < -20)
	{
		main_table.style.left = parseInt(main_table.style.left, 10) + 20;
		redo = true;
	}
	else
	{
		main_table.style.left = endX;
	}
	
	var diff = parseInt(main_table.style.top, 10) - endY;
	if (diff > 100)
	{
		main_table.style.top = parseInt(main_table.style.top, 10) - 100;
		redo = true;
	}
	else if (diff < -100)
	{
		main_table.style.top = parseInt(main_table.style.top, 10) + 100;
		redo = true;
	}
	else
	{
		main_table.style.top = endY;
	}
	
	if (redo)
	{
		setTimeout(function() {GoToDiv(divName, onEnd, bodyWidth, bodyHeight)}, 100);
	}
	else
	{
		currentZone = divName;
		if (onEnd != null) onEnd();
	}
	
}

function CreateAndOpenDiv(divId, e, endTop, endLeft, width, height, fctHandler, parent)
{
	var div = document.getElementById(divId);
	
	//Display identification panel
	if (!div)
	{
		div = document.createElement("div");
		div.id = divId;
		div.style.zIndex = ++currentZIndex;
		
		parent = (parent) ? parent : document.body;
		
		parent.appendChild(div);
	}
	else
	{
		div.style.background = "";
		div.innerHTML = "";
	}
	
	div.style.position = "absolute";
	div.style.width = "1px";
	div.style.height = "1px";
	div.style.border = "1px solid black";
	
	var widthFactor = 1;
	var heightFactor = 1;
	var sensx = 1;
	var sensy = 1;
	OpenDiv(div, endTop, endLeft, height, width, sensx, sensy, widthFactor, heightFactor, fctHandler, 0);
}

function OpenDiv(div, endTop, endLeft, height, width, sensx, sensy, xwidth, xheight, handler, num)
{	
	div.style.top = endTop;
	div.style.left = endLeft;
	div.style.width = width;
	div.style.height = height;
	
	handler();
}

function RemoveDiv(id)
{
	var div = document.getElementById(id);
	if (div)
	{
		div.parentNode.removeChild(div);	
	}
}

function FindLeftEdge(img) 
{
    xPos = img.offsetLeft;

    tempEl = img.offsetParent;

    while (tempEl != null)
    {
        xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
    }

    return xPos;

}



function FindTopEdge(img) 
{
    yPos = img.offsetTop;

    tempEl = img.offsetParent;

    while (tempEl != null) 
    {
        yPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;

    }

    return yPos;

}

function CancelBubble(e)
{
	var evt = (e) ? e : window.event;
	evt.cancelBubble = true;
}

function getFlash()
{
	var tab = null;
	
	if (navigator.appName.indexOf("Microsoft") != -1) {
		tab = window["SAwale"];
    }
    else {
        tab = document["SAwale"];
    }

	if ((typeof(tab.length) != "undefined") && (tab.length > 0)) return tab[0];
	else return tab;
}


function CreateWindow(id, title, actionOnClose, sendHandler, w, h, backColor, parent)
{
	parent = (parent) ? parent : document.body;
	
	var div = document.getElementById(id);
	if (div)
	{
		if (div.style.zIndex != currentZIndex)
		{
			div.style.zIndex = ++currentZIndex;
		}
		
		return;
	}
	
	
	var div = document.createElement("div");
	div.id = id;
	div.style.position = "absolute";
	div.style.top = (document.body.clientHeight / 2) - (h / 2);
	div.style.left = (document.body.clientWidth / 2) - (w / 2);
	div.style.width = w;
	div.style.height = h;
	
	currentZIndex++;
	div.style.zIndex = currentZIndex;
	
	var table = document.createElement("table"); table.style.width = "100%"; table.style.height = "100%";
		table.cellPadding = 0; table.cellSpacing = 0; table.style.tableLayout = "fixed";
		var colGroup = document.createElement("colgroup");
		var col = document.createElement("col"); colGroup.appendChild(col);
		var col = document.createElement("col"); col.style.width = "80px"; colGroup.appendChild(col);
	table.appendChild(colGroup);
		
	var tr = table.insertRow(-1);
	var td = tr.insertCell(-1); td.style.height = "23px"; td.colSpan = 2;
		var headerTable = document.createElement("table");
		headerTable.style.width = "100%"; headerTable.style.height = "23px";
		headerTable.cellPadding = 0; headerTable.cellSpacing = 0;
		headerTable.style.tableLayout = "fixed";
		
		
		var hTr = headerTable.insertRow(-1);
		var hTd = hTr.insertCell(-1); hTd.style.fontSize = "1px"; hTd.innerHTML = "&nbsp;";
			hTd.style.width = "7px"; hTd.style.background = "url('img/window/h_1.png')"; 
		var hTd = hTr.insertCell(-1); hTd.style.fontSize = "1px"; hTd.innerHTML = "<img src=\"img/window/bt_close.png\" border=0 style=\"cursor:pointer;\" onclick=\"" + actionOnClose + "\">";
			hTd.style.width = "20px"; hTd.style.background = "url('img/window/h_2.png')"; hTd.style.paddingTop = "4px";
		var hTd = hTr.insertCell(-1); hTd.style.fontSize = "1px"; hTd.innerHTML = "<img src=\"img/window/bt_minimized.png\" border=0>";
			hTd.style.width = "20px"; hTd.style.background = "url('img/window/h_2.png')"; hTd.style.paddingTop = "4px";
		var hTd = hTr.insertCell(-1); hTd.innerHTML = title;
			hTd.style.background = "url('img/window/h_2.png')"; hTd.align = "center";
			hTd.setAttribute("target", id);
			hTd.style.cursor = "move";
		var hTd = hTr.insertCell(-1); hTd.style.fontSize = "1px"; hTd.innerHTML = "&nbsp;";
			hTd.style.width = "7px"; hTd.style.background = "url('img/window/h_3.png')";
	td.appendChild(headerTable);
	
	var tr = table.insertRow(-1);
	var content = tr.insertCell(-1); content.className = "GreyBorder"; content.colSpan = 2; 
	content.style.height = "100%";
	
	var tr = table.insertRow(-1);
	var td = tr.insertCell(-1); td.className = "GreyBorder"; td.style.borderBottom = "1px solid #9e9e9e"; td.colSpan = 2;
	td.style.fontSize = "1px"; td.innerHTML = "&nbsp;"; td.style.height = "2px";
	td.style.backgroundColor = backColor;
	
	div.appendChild(table);
	
	parent.appendChild(div);
	
	return content;
}

function getAlphaImg(path, width, height)
{
	if (!ie6)
	{
		var img = document.createElement("img");
		img.src = path;
		return img;
	}
	else
	{
		var div = document.createElement("div"); div.style.width = width; div.style.height = height;
		div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true src='" + path + "', sizingMethod='scale')";
		return div;
	}
}

function getAlphaImgText(path, width, height, id, oWidth, oHeight, className, action)
{
    var str = "";

	if (!ie6)
	{
        str = "<img src=\"" + path + "\" style=\"width:" + width + ";height:" + height + ";";
        
        if (action != null) {
            str += "cursor:pointer";
        }
        str += "\"";
        
        if (oWidth != null) {
            str += " oWidth=\"" + oWidth + "\"";
        }
        if (oHeight != null) {
            str += " oHeight=\"" + oHeight + "\"";
        }
        if (id != null) {
            str += " id=\"" + id + "\"";
        }
        if (className != null) {
            str += " class=\"" + className + "\"";
        }
        if (action != null) {
            str += " onclick=\"" + action + "\"";
        }
        
        str += " />";
	}
	else
	{
        str = "<div style=\"width:" + width + ";height:" + height + ";filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true src='" + path + "', sizingMethod='scale');";
        
        if (action != null) {
            str += "cursor:pointer";
        }
        str += "\"";
        
        if (oWidth != null) {
            str += " oWidth=\"" + oWidth + "\"";
        }
        if (oHeight != null) {
            str += " oHeight=\"" + oHeight + "\"";
        }
        if (id != null) {
            str += " id=\"" + id + "\"";
        }
        if (className != null) {
            str += " class=\"" + className + "\"";
        }
        if (action != null) {
            str += " onclick=\"" + action + "\"";
        }
		str += "></div>";
	}
    
    return str;
}