﻿var IMGH = {
    ImgArry: [],
    Index: 0,
    Timout: null
};

function $H(obj) {
    if (typeof obj == 'string') {
        return document.getElementById(obj);
    }
    return obj;
}

function ShowControl(obj) {
    $H(obj).style.display = '';
}

function LoadedLinks(arry2) {
    var isNew;
    if (arry2.length > 0) {
        isNew = arry2.length > 2;
        ShowControl('promo');
        $H('promo').innerHTML = '<a onclick="_gaq.push([\'_trackPageview\',\'' + arry2[1] + '\']);" href="' + arry2[1] + '">' + arry2[0] + '</a>' + (isNew ? '<span class="new">New!</span>' : '');
        _gaq.push(['_trackEvent', 'Links', 'Show', arry2[1]]);
    }
}

function ShowPicture() {
    if (IMGH.Index > IMGH.ImgArry.length - 1) {
        IMGH.Index = 0;
    }
    var nextIndex = IMGH.Index + 1;
    if (nextIndex > IMGH.ImgArry.length - 1) {
        nextIndex = 0;
    }
    $H('NextButton').innerHTML = '<b class="next">&gt;&gt;</b> ' + IMGH.ImgArry[nextIndex][0];
    ShowControl('divProductions');
    var arry = IMGH.ImgArry[IMGH.Index];
    var arryPic = [];
    arryPic.push('<table class="items"><tr><td class="imgL"><a href="' + arry[2] + '" onclick="_gaq.push([\'_trackEvent\',\'Apps\',\'Click\',\'' + arry[0] + '\']);"><div>');
    arryPic.push('<img class="img" src="' + arry[1] + '" alt="" /><div class="free">' + arry[4] + '</div><b class="button download">Get app</b></div></a></td><td class="blocks"><div>');
    arryPic.push('<label>' + arry[0] + '</label><div class="time">' + arry[6] + '</div>');
    arryPic.push('<div class="content">' + arry[3] + '<a href="' + arry[5] + '" class="details">[Details]</a></div>');
    arryPic.push('</td></tr></table>');
    ShowControl('appDiv');
    $H('appDiv').innerHTML = arryPic.join('');
    IMGH.Index++;
    if (IMGH.ImgArry.length > 1) {
        window.clearTimeout(IMGH.Timout);
        IMGH.Timout = window.setTimeout(function() {
            ShowPicture();
        }, 15000);
    }
}

function requestAppsAndLink() {
    var xCon = new XHConn();
    xCon.connect('Home.ashx', 'GET', 'type=0', function(res) {
        var arry = eval(res.responseText);
        LoadedLinks(arry);
    });
    var xCon2 = new XHConn();
    xCon2.connect('Home.ashx', 'GET', 'type=1', function(res) {
        IMGH.ImgArry = eval(res.responseText);
        if (IMGH.ImgArry.length > 0) {
            ShowPicture();
        }
    });
}
