﻿/// <reference path="../jquery-1.5.1-vsdoc.js" />

// These can be changed
var imageRotateInitialTime = 9000;
var imageRotateInterval = 6000;
var imageRotateAfterClick = 10000;
var imageAnimationSpeed = 300;
var textAnimationSpeed = 150;
var featuresRotateInterval = 4000;

// Replacement function for setinterval and settimeout that work with new 
// browser behaviour of arseing about the the timer speeds when tab inactive
// requestAnimationFrame() shim by Paul Irish
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
window.requestAnimFrame = (function ()
{
    return window.requestAnimationFrame ||
           window.webkitRequestAnimationFrame ||
           window.mozRequestAnimationFrame || 
           window.oRequestAnimationFrame ||
           window.msRequestAnimationFrame ||
           function (/* function */callback, /* DOMElement */element)
           {
               window.setTimeout(callback, 1000 / 60);
           };
})();
window.requestTimeout = function (fn, delay)
{
    if (!window.requestAnimationFrame && !window.webkitRequestAnimationFrame && !window.mozRequestAnimationFrame && !window.oRequestAnimationFrame && !window.msRequestAnimationFrame)
        return window.setTimeout(fn, delay);

    var start = new Date().getTime(),
    handle = new Object();

    function loop()
    {
        var current = new Date().getTime(),
        delta = current - start;

        delta >= delay ? fn.call() : handle.value = requestAnimFrame(loop);
    };

    handle.value = requestAnimFrame(loop);
    return handle;
};
window.requestInterval = function (fn, delay)
{
    if (!window.requestAnimationFrame &&
!window.webkitRequestAnimationFrame &&
!window.mozRequestAnimationFrame &&
!window.oRequestAnimationFrame &&
!window.msRequestAnimationFrame)
        return window.setInterval(fn, delay);
    var start = new Date().getTime(),
handle = new Object();
    function loop()
    {
        var current = new Date().getTime(),
delta = current - start;
        if (delta >= delay)
        {
            fn.call();
            start = new Date().getTime();
        }
        handle.value = requestAnimFrame(loop);
    };
    handle.value = requestAnimFrame(loop);
    return handle;
}
/**
* Behaves the same as clearInterval except uses cancelRequestAnimationFrame() where possible for better performance
* @param {int|object} fn The callback function
*/
window.clearRequestInterval = function (handle)
{
    window.cancelAnimationFrame ? window.cancelAnimationFrame(handle.value) :
    window.webkitCancelRequestAnimationFrame ? window.webkitCancelRequestAnimationFrame(handle.value) :
    window.mozCancelRequestAnimationFrame ? window.mozCancelRequestAnimationFrame(handle.value) :
    window.oCancelRequestAnimationFrame ? window.oCancelRequestAnimationFrame(handle.value) :
    window.msCancelRequestAnimationFrame ? msCancelRequestAnimationFrame(handle.value) :
    clearInterval(handle);
};
/**
* Behaves the same as clearInterval except uses cancelRequestAnimationFrame() where possible for better performance
* @param {int|object} fn The callback function
*/
window.clearRequestTimeout = function (handle)
{
    window.cancelAnimationFrame ? window.cancelAnimationFrame(handle.value) :
    window.webkitCancelRequestAnimationFrame ? window.webkitCancelRequestAnimationFrame(handle.value) :
    window.mozCancelRequestAnimationFrame ? window.mozCancelRequestAnimationFrame(handle.value) :
    window.oCancelRequestAnimationFrame ? window.oCancelRequestAnimationFrame(handle.value) :
    window.msCancelRequestAnimationFrame ? msCancelRequestAnimationFrame(handle.value) :
    clearTimeout(handle);
};

var imageCount;
var currentImage = 0;
var rollingActive = true;
var t = 0;
var x = 0;
var fea = 0;
var rollFeatures = true;
var currentFeature = 2;

$(document).ready(function ()
{
    imageCount = $("#imageRotator img").length;

    $("#imageRotatorNext").click(function ()
    {
        if (rollingActive)
        {
            rollingActive = false;
            clearRequestTimeout(x);
            clearRequestInterval(t);
            RotateImages(1, true);
        }
    });

    $("#imageRotatorPrev").click(function ()
    {
        if (rollingActive)
        {
            rollingActive = false;
            clearRequestTimeout(x);
            clearRequestInterval(t);
            RotateImages(imageCount - 1, true);
        }
    });

    clearRequestInterval(t);
    t = requestInterval(function () { RotateImages(1, false); }, imageRotateInterval);

    // Features rotator
    $("#proRight li").hover(
    function ()
    {
        rollFeatures = false;

        $("#proRight li").removeClass("proactive");
        $(this).addClass("proactive");

        var id = $(this).attr("id");
        id = id.replace("pro", "");

        currentFeature = id;

        $("#proLeftImage img").hide();
        $("#proimg" + id).show();
    },
    function ()
    {
        rollFeatures = true;
    });

    clearRequestInterval(fea);
    fea = requestInterval(function () { RotateFeatures(); }, featuresRotateInterval);
});

function RotateFeatures()
{
    if (!rollFeatures)
        return;

    var previous = (currentFeature + $("#proLeftImage img").length - 1) % $("#proLeftImage img").length;
    if (previous == 0)
        previous = 5;

    $("#proRight li").removeClass("proactive");
    $("#pro" + currentFeature).addClass("proactive");

    var c = currentFeature;
    $("#proimg").hide();
    $("#proimg" + previous).show();
    $("#proimg" + previous).fadeOut("fast", function ()
    {
        $("#proimg" + c).fadeIn();
    });
    
    currentFeature = ((Number(currentFeature)) % $("#proLeftImage img").length) + 1;    
}

function RotateImages(amount, restart)
{
    if (restart)
    {
        clearRequestInterval(t);
        clearRequestTimeout(x);
        x = requestTimeout(function ()
        {
            t = requestInterval(function () { RotateImages(1, false); }, imageRotateInterval);
        }, 
        imageRotateAfterClick);        
    }

    var prevImage = currentImage;
    currentImage += amount;
    currentImage %= imageCount;

    $("#ir" + prevImage).fadeOut(textAnimationSpeed, function ()
    {       
        $("#ir" + currentImage).fadeIn(textAnimationSpeed);
    });

    $("#irimg" + prevImage).fadeOut(imageAnimationSpeed, function ()
    {
        $("#irimg" + currentImage).fadeIn(imageAnimationSpeed, function ()
        {
            rollingActive = true;
        });
    });
}

