﻿var _mainBoxTimeout;
var _winnersFeedTimeout;
var _isAnimating = false;
var homeSlideLink;

$(document).ready(function () {

    //############################################
    // Promo Slider
    //############################################

    $("#mainBoxImages img").each(function (i) {
        $(this).attr("id", "promoImage_" + i);
        if (i == 0) $(this).css("left", 0).attr("class", "active");
        else $(this).css("left", $(this).width());
    });

    _mainBoxTimeout = setTimeout("callPromoImage()", 3000);

    //############################################
    // Winners Feed
    //############################################

    $("#mainBoxFeed .item").each(function (i) {
        $(this).attr("id", "feedItem_" + i);
        if (i == 0) $(this).attr("rel", "active").show();
    })

    //setInterval("winnersFeedTick()", 3000);
    _winnersFeedTimeout = setTimeout("winnersFeedTick()", 3000);

    //############################################
    // Top Games Scroller
    //############################################

    var distance = $("#topGamesImages img").height();
    $("#topGamesImages").prepend($("#topGamesImages img:last"));
    $("#topGamesImages img:first").css("marginTop", -distance);

    scrollTopGames();

    //############################################
    // PJP Ticker
    //############################################

    $.get("/utils/wagersharejpproxy.aspx", function (data) {

        var d = $.parseXML(data);
        var bingoTotals;

        $("name", d).each(function (i) {
            if ($(this).text() == "Bingo Totals") {
                bingoTotals = $(this).next().text();
            }
        })

        PJPtotalTick(bingoTotals);
    })

    //############################################
    // Scratch Cards Scroller
    //############################################

    $(".footerPromoImg img").each(function (i) {
        var top = $(this).parent().height() / 2 - $(this).height() / 2;
        var left = $(this).parent().width() / 2 - $(this).width() / 2 - 10;
        $(this).css({ top: top, left: left });
        if (i == 0) $(this).attr("class", "active");
        else $(this).hide();
    })

    setInterval("scrollScratchCards()", 3000);

})

//############################################
// Image scroller
//############################################

function callPromoImage(id) {

    clearTimeout(_mainBoxTimeout);

    var activeImage = $("#mainBoxImages img[class='active']");
    var defaultId = activeImage.next().is("img") ? activeImage.next().attr("id").replace("promoImage_", "") : 0;
    id = id == undefined ? defaultId : id;
    var nextImage = $("#promoImage_" + id);

    //QV 20120116 As per A.Battikh the 3rd slide will take the player to the specials, if this needs to be stopped simply delete '/specials/' from the next couple of lines
    if (id == '3') {
        homeSlideLink = '/specials/';
    } else {
    homeSlideLink = '';
    }
    

    if (nextImage.attr("id") == activeImage.attr("id") || _isAnimating) return;

    var imageWidth = activeImage.width();

    _isAnimating = true;
    activeImage.attr("class", "");
    nextImage.attr("class", "active").css("left", imageWidth);
    activeImage.animate({ left: -imageWidth }, 400);
    nextImage.animate({ left: 0 }, 400, function () { _isAnimating = false; });

    _mainBoxTimeout = setTimeout("callPromoImage()", 5000);
}

//############################################
// Winners feed
//############################################

function winnersFeedTick() {

    clearTimeout(_winnersFeedTimeout);

    var activeItem = $("#mainBoxFeed .item[rel='active']");
    var nextItem = activeItem.next().attr("class") == "item" ? activeItem.next() : $("#feedItem_0");

    activeItem.fadeOut(200);

    $("#mainBoxFeedBg").animate({
        marginTop: 10
    }, 200, function () {
        $("#mainBoxFeedBg").animate({
            marginTop: 0
        }, 200, function () {
            nextItem.fadeIn(200)
        })
    })

    activeItem.attr("rel", "");
    nextItem.attr("rel", "active");

    _winnersFeedTimeout = setTimeout("winnersFeedTick()", 3000);
}

//############################################
// Top Games Scroller
//############################################

function scrollTopGames() {
    var headImage = $("#topGamesImages img:first");
    var newHeadImage = $("#topGamesImages img:first").next();
    var distance = newHeadImage.height() + 5;
    headImage.css("marginTop", 0).appendTo($("#topGamesImages"));
    newHeadImage.animate({ marginTop: -distance }, 3000, "linear", scrollTopGames);
}

//############################################
// PJP Ticker
//############################################

function PJPtotalTick(val) {
    $("#pjpTicker").html("£" + val);
    var numVal = Number(val.replace(/\,/g, ""));
    var newVal = currencyFormat(numVal += 0.73, ",");
    setTimeout("PJPtotalTick('"+newVal+"')", 1000);
}

function currencyFormat(value, separator, currency) {
    currency = currency || "";
    separator = separator || ",";
    var parts = String(value).split(".");
    var numbers = parts[0].split("").reverse();
    var decimal = parts[1] == undefined ? "00" : parts[1].substr(0, 2);
    if (decimal.length == 1) decimal += "0";
    var main = "";
    for (var i = 0; i < numbers.length; i++) {
        var c = numbers[i];
        if (i % 3 == 0 && i > 0) main = separator + main;
        main = c + main;
    }
    return currency + main + "." + decimal;
}

//############################################
// Scratch Cards Scroller
//############################################

function scrollScratchCards() {
    var active = $(".footerPromoImg img[class='active']");
    var next = $(".footerPromoImg img[class='active']").next();
    if (!next.is("img")) next = $(".footerPromoImg img:first");
    active.fadeOut(1000).attr("class", "");
    next.fadeIn(1000).attr("class", "active");
}

//############################################
// homepage scroller multiple content link
//############################################

function hpContentLink() {
    if (homeSlideLink == '') {
        register();
    } else {
    document.location = homeSlideLink;
    }

}
