var g_d = 0;
var g_tm = 0;
var game;
var loaded;
var erroneous;
var g_intWaitBigScreen;
var g_timeWaitBigScreen = 10000;

window.onload = init;
g_tm = setInterval('animateSpinner("loading")', 30);

function $(id) {return document.getElementById(id);}
function init()
{
	localize();
}

function initFlyout(gameObj, isWrong)
{
	
	game = gameObj;
	
	erroneous = 0;
	loaded = 0;
	
	clearWaitBigScreen();
	
	$('title').innerHTML = game.name
	
	if(isWrong)
	{
		showGettingData();
		
		$('descr').innerHTML = game.descr
		// укорачивание описания
		while($('descr').clientHeight > 30)
		{
			$('descr').innerHTML = $('descr').innerHTML.substring(0, $('descr').innerHTML.lastIndexOf(' '))+'...';
		}
		$('err_content').style.display = 'none'
		$('cats').innerHTML = game.cats;
		$('screen_1').style.visibility = 'hidden';
		$('screen_2').style.visibility = 'hidden';
		//$('block').style.visibility = 'hidden';
		
		$('screen_1').onload = loadedScreen_ok;
		$('screen_1').onerror = loadedScreen_err;
		$('screen_2').onload = loadedScreen_ok;
		$('screen_2').onerror = loadedScreen_err;
		
		$('screen_1').src = 'http://www.myplaycity.com/gn/screen_110x83/'+game.id+'_screen_1_110x83.jpg';
		$('screen_2').src = 'http://www.myplaycity.com/gn/screen_110x83/'+game.id+'_screen_2_110x83.jpg';
		$('screen_1').onclick = open_screen;
		$('screen_2').onclick = open_screen;
		
		$('screen_big').onclick = function(){clearWaitBigScreen();}
		
		hideGettingData();
		
	}
	else
	{
		showWrong();
	}
}

function loadedScreen_ok(){
	loaded++;
	this.style.visibility = 'visible';
	$('block').style.visibility = 'visible';
	if(loaded == 2)
	{
		hideGettingData();
	}
}

function loadedScreen_err(){
	erroneous++;
	{
		showWrong();
	}
}

function showWrong()
{
	clearInterval(g_tm);
	// $('title').innerHTML = '';
	$('loading').style.visibility = 'hidden';
	$('err_content').style.display = 'block';
	$('block').style.visibility = 'hidden';
}

function open_screen(){
	showGettingData();
    //$('screen_big').src = '';
    var img = this.src;
    img = img.replace('110x83','640x480');
    img = img.replace('110x83','640x480');
    $('screen_big').onload = function(){
		clearTimeout(g_intWaitBigScreen);
        hideGettingData();
		this.title = this.title1;
		this.style.display = 'block';
		this.style.filter="alpha(opacity=100)";
    }
	$('screen_big').onerror = function()
	{
		clearWaitBigScreen();
	}
	$('screen_big').style.filter="alpha(opacity=0)";
    $('screen_big').style.display = 'block';
	$('screen_big').title = $('screen_big').title2;
    $('screen_big').src = img;
	g_intWaitBigScreen = setTimeout( clearWaitBigScreen, g_timeWaitBigScreen);
}

function showGettingData()
{
    $('block').style.filter="alpha(opacity="+20+")";
	$('block').style.opacity = 0.2;
    clearInterval(g_tm);
    g_tm = setInterval('animateSpinner("loading")', 30);
    $('loading').style.visibility = 'visible';
}

function hideGettingData()
{
	clearInterval(g_tm);
	$('loading').style.visibility = 'hidden';
	$('block').style.visibility = 'visible';
	$('block').style.filter="alpha(opacity="+100+")";
	$('block').style.opacity = 1;
}

function clearWaitBigScreen()
{
	clearTimeout(g_intWaitBigScreen);
	// $('screen_big').src = '';
	$('screen_big').onload = null;
	$('screen_big').style.display = 'none';
	hideGettingData();
}

function animateSpinner(spinner){
    var spinnerDiv = $(spinner);
    if(g_d < 272)g_d = g_d + 16; else g_d = 0
	spinnerDiv.style.backgroundPosition = '0 -'+g_d+'px';
}

