﻿var placeHolder = $("#rotatingGamesPanel");
var latestAdditions = $(".lnkLatestAddition");
var latestgamesprecache = new Array();
for (i = 0; i < latestgames.length; i++) { var newImg = new Image(0,0); newImg.src = latestgames[i][1]; latestgamesprecache.push(newImg); }
//latestgames.sort(function() { return 0.5 - Math.random() });
window.latestGamesCurrentIndex = -1;

window.ChangeLatestGames = function() {
    if (window.latestGamesCurrentIndex < -1) { window.latestGamesCurrentIndex = -1; }
    for (i = 0; i < 7 && i < latestgames.length; i++) {
        window.latestGamesCurrentIndex++;
        if (window.latestGamesCurrentIndex > latestgames.length - 1) { window.latestGamesCurrentIndex = -1; }
        var z = window.latestGamesCurrentIndex;
        var game = latestAdditions.eq(i);
        game.attr('href', latestgames[z][2]);
        $('img', game).eq(0).attr('src', latestgames[z][1]);
        $('span', game).eq(0).get(0).innerHTML = latestgames[z][0];
    }
    window.latestGamesCurrentIndex = window.latestGamesCurrentIndex - 6;
}
window.ChangeLatestGames();
window.ChangeLatestGamesInterval = window.setInterval(ChangeLatestGames, 2000);

if (document.getElementById('rotatingGamesPanel')) {
    var rotatingFadeImages = $('#rotatingGamesPanel img');
    var restoreTimeout = null;
    function adjustImageFadePosition() {
        if (restoreTimeout) { window.clearTimeout(restoreTimeout); };
        if (window.ci != null)
            window.clearInterval(window.ChangeLatestGamesInterval);

        var centerImageIndex = (window.ci != null ? window.ci : 3);

        var opacity = 100;
        if (centerImageIndex > -1) {
            for (i = centerImageIndex; i < rotatingFadeImages.length; i++) { setElementOpacity(rotatingFadeImages.get(i), opacity); opacity = opacity - (opacity / 2.1); }
        }
        opacity = 100;
        for (i = centerImageIndex; i > -1; i--) { setElementOpacity(rotatingFadeImages.get(i), opacity); opacity = opacity - (opacity / 2.1); }
    }
    function restoreFadePosition() { window.ci = null; window.ChangeLatestGamesInterval = window.setInterval(ChangeLatestGames, 2000); if (restoreTimeout) { window.clearTimeout(restoreTimeout); }; restoreTimeout = window.setTimeout(adjustImageFadePosition, 200); }
    function setElementOpacity(el, ol) { el.style.opacity = (ol / 100).toString(); el.style.filter = 'alpha(opacity=' + ol + ')'; }
    adjustImageFadePosition(null);

}

function createSortable() {
    $("#dropContainer").sortable({
        'handle': '.ui-handle',
        items: '.dropArea',
        placeholder: 'ui-helper',
        containment: '#contentsWrapper',
        dropOnEmpty: true,
        update: function(event, ui) {
            var order = $(this).sortable("serialize");
            $('#layoutInfo').load('/default.aspx?setLayout=true&' + order);
        }
    });
}

var fPanels = new Array();
var currIndex = -1;
var tickerTimeout;

function setupTickers() {
    fPanels = new Array();
    currIndex = -1;
    $(".tipArea").css("display", "none");
    $(".tipArea").each(function(i) {
        fPanels.push(this);
    })
    fPanels.sort(randOrd);
    if (fPanels.length > 0)
        NextTicker();
}

function randOrd() {
    return (Math.round(Math.random()) - 0.5);
}

function HideTicker() {
    $(fPanels[currIndex]).fadeOut(1500, function() { NextTicker(); });

}

function NextTicker() {
    currIndex++;
    if (currIndex > fPanels.length - 1) currIndex = 0;
    $(fPanels[currIndex]).fadeIn(1500, function() {
        tickerTimeout = window.setTimeout(HideTicker, 6000);
    });
}

function ClearTickerTimeout() {
    $(".tipArea").css("display", "none");
    if (tickerTimeout)
        window.clearTimeout(tickerTimeout);

}

setupTickers();

$(document).ready(function() { createSortable() });
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(ClearTickerTimeout);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(createSortable);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(setupTickers);