
var g_tm = 0;
var g_d = 0;

var g_currentGame = -1;
var g_lastShowedGame;

var g_gettingDataTimerHandler;
var g_gettingDataTimeInterval = 30000;

var g_showFlyoutTimerHandler;
var g_showFlyoutTimeInterval = 200;

var g_isWrong = true;
var g_isWaitFlyout = false;

var analytics = true;

// window.onload = init;
function $(id) {return document.getElementById(id);}
function init()
{
	g_tm = setInterval('animateSpinner("loading")', 50)
	localize();
	setup();
}

function setAction()
{
	$('btn_l').onmouseover = function(){this.style.backgroundPosition = '-10px'}
    $('btn_l').onmouseout = function(){this.style.backgroundPosition = '0 0'}
    $('btn_l').onmousedown = function(){this.style.backgroundPosition = '-20px'}
    $('btn_l').onmouseup = function(){this.style.backgroundPosition = '-10px'}
    
    $('btn_r').onmouseover = function(){this.style.backgroundPosition = '-10px'}
    $('btn_r').onmouseout = function(){this.style.backgroundPosition = '0 0'}
    $('btn_r').onmousedown = function(){this.style.backgroundPosition = '-20px'}
    $('btn_r').onmouseup = function(){this.style.backgroundPosition = '-10px'}
    
    $('btn').onmouseover = function(){this.style.backgroundPosition = '0 -22px'}
    $('btn').onmouseout = function(){this.style.backgroundPosition = '0 0'}
    $('btn').onmousedown = function(){this.blur; this.style.backgroundPosition = '0 -44px'}
    $('btn').onmouseup = function(){this.style.backgroundPosition = '0 -22px'}
    
	$('img_link').onmouseover = function(){ beginWaitFlyout(g_showFlyoutTimeInterval); }
	$('img_link').onmouseout = function(){ breakWaitFlyout(); }
	$('img_link').onclick = gotoHref;
	
    $('btn_l').onclick = slide_prev;
    $('btn_r').onclick = slide_next;
    
    $('img').onload = show_ok;
	$('img').onerror = show_wrong;
	
	$('all').href = 'http://www.myplaycity.com/';
	$('logo').href = 'http://www.myplaycity.com/';
	$('head').href = 'http://www.myplaycity.com/new_games/';
	if(analytics)
	{
		$('all').href += '?utm_source=dgadget01_allbestgames&utm_medium=dgadget01_links';
		$('logo').href += '?utm_source=dgadget01_logo&utm_medium=dgadget01_links';
		$('head').href += '?utm_source=dgadget01_title&utm_medium=dgadget01_links';
	}
	$('logo').onclick = gotoHref;
	$('head').onclick = gotoHref;
	$('title').onclick = gotoHref;
	
	
	spinner_start();
    g_currentGame = 0;
	setInformation();
	
	// slide_next();
	
    settingsChanged();
    
    // System.Gadget.settingsUI = "settings.html";   // This tells the gadget the HTML file to launch for settings
    // System.Gadget.onSettingsClosed = settingsClosed;     //wire ourselves to settings closed event to save them 
    System.Gadget.visibilityChanged = visibilityChanged;  // stop working if we are not visible 
    // System.Gadget.onUndock = chkdockedState;  //let me know if I get undocked so I can look different 
    // System.Gadget.onDock = chkdockedState;
    // System.Gadget.onShowSettings = showSettings ; // let me know if settings are being shown 
    
    System.Gadget.Flyout.onHide = flyoutHiding;   //let me know if flyout is showing/hiding. I can pass data to it via document property
    System.Gadget.Flyout.onShow = flyoutShowing;  
	
	chkdockedState();
	
}

function slide_prev(){
    spinner_start()
    if(g_currentGame > 0)g_currentGame--; else g_currentGame = games.length-1
	setInformation();
}

function slide_next(){
    spinner_start()
    if(g_currentGame < games.length-1)g_currentGame++; else g_currentGame = 0
	setInformation();
}

function setInformation()
{
	$('img').src = 'http://www.myplaycity.com/g/90x67/'+ games[g_currentGame].id +'_90x67.jpg'
	$('title').innerHTML = games[g_currentGame].name;
	// $('title').title = games[g_currentGame].name;
	while($('title').clientHeight > 23)
	{
		$('title').innerHTML = $('title').innerHTML.substring(0, $('title').innerHTML.lastIndexOf(' '))+'...';
	}
	var game_href = 'http://www.myplaycity.com/'+games[g_currentGame].lgp+'/';
	$('title').href = game_href;
	$('more_link').href = game_href;
	$('img_link').href = game_href;
	if(analytics)
	{
		$('title').href += '?utm_source=dgadget01_gamename&utm_medium=dgadget01_links';	
		$('more_link').href += '?utm_source=dgadget01_learnmore&utm_medium=dgadget01_links';
		$('img_link').href += '?utm_source=dgadget01_image&utm_medium=dgadget01_links';
	}
}

function spinner_start(){
    $('err_content').style.display = 'none'
	$('spinner').style.display = 'block';
    $('img').style.visibility = 'hidden';
    clearInterval(g_tm);
	g_tm = setInterval('animateSpinner("spinner")', 30);
}

function spinner_stop(){
	$('spinner').style.display = 'none';
    $('loading').style.display = 'none';
	$('err_content').style.display = 'none'
    $('img').style.visibility = 'visible';
	$('content').style.display = 'block';
	$('img_link').style.cursor = 'pointer';
	clearInterval(g_tm);
	beginWaitFlyout(0);
}

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';
}

function show_ok()
{
	g_isWrong = true;
	spinner_stop();
}

function show_wrong(){
	g_isWrong = false;
	$('spinner').style.display = 'none';
	$('loading').style.display = 'none'
	$('err_content').style.display = 'block'
    $('more_link').href = 'javascript:void(0)';
    $('img_link').href = 'javascript:void(0)';
	$('img_link').style.cursor = 'default';
	beginWaitFlyout(0);
}

function gotoHref()
{
	if(this.href)
	{
		location.href = this.href;
	}
}

function showSettings ( ) 
{ 

}
 
function settingsClosed(event)
{
     
    if (event.closeAction == event.Action.commit)
    {
        settingsChanged();
    }
    
}

function settingsChanged()
{ 

} 

function visibilityChanged()
{
	if (!System.Gadget.visible)
    {
        
    }
    else
    {
        
    }
} 

function chkdockedState()
{
    if (!System.Gadget.docked)
    {
        
    }
    else
    {	
	
    }
}

    
function flyoutShowing ( ) 
{ 
	
} 

function flyoutHiding ( ) 
{
	
	g_isWaitFlyout = false;
} 

function beginWaitFlyout ( timeout )
{
	if(!g_isWaitFlyout)
	{
		if ( System.Gadget.Flyout.show == false && timeout > 0 ) 
		{
			g_isWaitFlyout = true;
			g_showFlyoutTimerHandler = setTimeout( showFlyout, timeout);
			
		} 
		else if(g_currentGame != g_lastShowedGame && System.Gadget.Flyout.show)
		{
			g_lastShowedGame = g_currentGame;
			addContentToFlyout();
		}
	}
}

function breakWaitFlyout ( )
{
	
	g_isWaitFlyout = false;
	clearTimeout(g_showFlyoutTimerHandler);
}

function showFlyout ( ) 
{
	System.Gadget.Flyout.file = "flyout.html";      
	System.Gadget.Flyout.show = true;
	System.Gadget.Flyout.onShow = function()
	{
		g_lastShowedGame = g_currentGame;
		addContentToFlyout();
	}
} 

function hideFlyout ( ) 
{ 
	System.Gadget.Flyout.show = false ;
} 

////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function addContentToFlyout()
{
	try
	{
		if(System.Gadget.Flyout.show)
		{
			var flyoutDiv = System.Gadget.Flyout.document.parentWindow;
			try
			{
				// список категорий
				// flyoutDiv.getElementById('title').innerText = "asdasda";
				flyoutDiv.initFlyout(games[g_currentGame], g_isWrong);
			}
			catch(e)
			{
				
			}
		}
	}
	catch(e)
	{
		//catch slow flyout - no div object will be available.
		
	}
}
