

var g_dataUrl = "http://www.myplaycity.com/gadget/data/data_en.xml?" + Math.random();
// var g_dataUrl = "http://192.168.0.10/alexsab/gadget/data_en.xml?" + Math.random();
var g_installUrl = "http://t.myplaycity.com/installgadget/";
var g_xmlTimeInterval;
var g_xmlTimerHandler;
var reqObj;
var regXML;
var games = [];
var cats = [];
var g_wrongLoadXml = false;

function loadXMLDoc(url, what)
{
    reqObj = null;
    if (window.XMLHttpRequest) {
        try {
            reqObj = new XMLHttpRequest();
        } catch (e){}
    } else if (window.ActiveXObject) {
        try {
            reqObj = new ActiveXObject('Msxml2.XMLHTTP');
        } catch (e){
            try {
                reqObj = new ActiveXObject('Microsoft.XMLHTTP');
            } catch (e){}
        }
    }
 
    if (reqObj) {       
        reqObj.open("GET", url, true);
		switch(what)
		{
			case 0:
				reqObj.onreadystatechange = processReqChange;
				break;
			case 1:
				reqObj.onreadystatechange = processReqInstallChange;
				break;
		}
        reqObj.send(null);
    }
}

function processReqChange() 
{   
    // document.form1.state.value = stat(reqObj.readyState); 
    
    var ab = window.setTimeout("reqObj.abort();", 5000);
    try {
		if (reqObj.readyState == 4) {
			clearTimeout(ab);
			
			// document.form1.statusnum.value = reqObj.status; 
			// document.form1.status.value = reqObj.statusText; 
			
			// only if "OK"
			if (reqObj.status == 200) {
				g_wrongLoadXml = false;
				regXML = reqObj.responseXML;
				// regXML = reqObj.responseText;
				parseXML();
			} else {
				g_wrongLoadXml = true;
				
				show_wrong();
				// loading.innerText = "Ошибка соединения...";
			}
		} else {
           //loading.innerText = "Загрузка...";
        }
	}
	catch( e ) {
		g_wrongLoadXml = true;

		show_wrong();
	}
	setWaitRequestXml();
}

// Парсит в массив аттрибуты XML-ноды
function getAttributes(node)
{
    var ret = new Object();
    if(node.attributes)
      for(var i=0; i<node.attributes.length; i++)
      {
          var attr = node.attributes[i];
          ret[attr.name] = attr.value;
      }
    return ret;
}

// function $$(name) {return document.getElementsByTagName(name);}

function parseXML() 
{
	var catsArrXml = regXML.getElementsByTagName("data")[0].getElementsByTagName("cats")[0].getElementsByTagName("cat");
	for(var num = 0; num < catsArrXml.length; num++)
	{
		var aboutGame = {
			id: catsArrXml[num].getElementsByTagName("id")[0].text,
			name: catsArrXml[num].getElementsByTagName("name")[0].text,
			lgp: catsArrXml[num].getElementsByTagName("lgp")[0].text
		};
		cats.push(aboutGame);
	}
	
	
	var gamesArrXml = regXML.getElementsByTagName("data")[0].getElementsByTagName("games")[0].getElementsByTagName("game");
	for(var num = 0; num < gamesArrXml.length; num++)
	{
		var aboutGame = {
			id: gamesArrXml[num].getElementsByTagName("id")[0].text,
			name: gamesArrXml[num].getElementsByTagName("name")[0].text,
			lgp: gamesArrXml[num].getElementsByTagName("lgp")[0].text,
			cats: gamesArrXml[num].getElementsByTagName("cats")[0].text,
			descr: gamesArrXml[num].getElementsByTagName("descr")[0].text
		};
		var cats_str = '';
		var catsArr = aboutGame.cats.split(',');
		for(var i = 0; i < catsArr.length; i++)
		{
			if(cats_str > '')cats_str += ', '
			for(var j = 0; j < cats.length; j++)
			{
				if(catsArr[i] == cats[j].id)
				{
					var cat_href = 'http://www.myplaycity.com/'+cats[j].lgp+'/';
					if(analytics)
					{
						cat_href += '?utm_source=dgadget01_category&utm_medium=dgadget01_links';
					}
					cats_str += '<a href="'+cat_href+'">'+cats[j].name+'</a>'
				}
			}
		}
		aboutGame.cats = cats_str;
		games.push(aboutGame);
	}
	
	
	
	chechFirstStart();
	
	setAction();
}

function setup() 
{
	
	loadXMLDoc(g_dataUrl, 0);
	setWaitRequestXml();
}

function setWaitRequestXml()
{
	if(!g_wrongLoadXml)
	{
		g_xmlTimeInterval = 3600000;
	}
	else
	{
		g_xmlTimeInterval = 60000;
	}	
	clearInterval(g_xmlTimerHandler);
	g_xmlTimerHandler = setInterval('loadXMLDoc("'+g_dataUrl+'")', g_xmlTimeInterval);	
}

function chechFirstStart()
{
	getPersistentSetting("FirstStart", getCallback);
}

function getCallback( value )
{
	
	if(value)
	{
		
	}
	else
	{
		loadXMLDoc(g_installUrl, 1);
	}
}

function processReqInstallChange() 
{   
    // document.form1.state.value = stat(reqObj.readyState); 
    
    var ab1 = window.setTimeout("reqObj.abort();", 5000);
    try {
		if (reqObj.readyState == 4) {
			clearTimeout(ab1);
			
			// document.form1.statusnum.value = reqObj.status; 
			// document.form1.status.value = reqObj.statusText; 
			
			// only if "OK"
			if (reqObj.status == 200) {
				setPersistentSetting("FirstStart", true, setCallback );
			} else {
				
				// loading.innerText = "Ошибка соединения...";
			}
		} else {
           //loading.innerText = "Загрузка...";
        }
	}
	catch( e ) {
		
		show_wrong();
	}
}

function setCallback()
{
	
}


