$(document).ready(function() {
	if($.browser.msie)
		$('.menu_item').css("height", "109px");
	else
		$('.menu_item').css("height", "59px");
		
    var p = $("#baseimg");
    var position = p.position()

    $('#bg_block').css({
        'top':position.top,
        'left':position.left
        });

    $('#pagemask_front').css({
        'left':position.left,
        'top':position.top+285
        });
	$('#pagemask_front2').css({
        'left':position.left+468,
        'top':position.top+420
        });
    $('#yogi').animate({
        'left':position.left,
        'top':position.top+590
        }, 0);
    $('#yogi').animate({
        'left':1050,
        'top':position.top+590
        }, 8000).hide(100);

    p = $("#maintemplate_12");
    position = p.position()

    $('#eventscontent').css({
                'top':position.top-40,
                'left':position.left+45});

    doBGAnimate();
});

$(window).resize(function() {
    var p = $("#baseimg");
    var position = p.position()

    $('#bg_block').css({
        'top':position.top,
        'left':position.left
        });
    $('#pagemask').css({
        'left':position.left,
        'top':position.top+395
        });
    $('#pagemask_front').css({
        'left':position.left,
        'top':position.top+285
        });
	$('#pagemask_front2').css({
        'left':position.left+468,
        'top':position.top+420
        });		
    p = $("#maintemplate_12");
    position = p.position()

    $('#eventscontent').css({
                'top':position.top,
                'left':position.left+45});
});


// Used to calculate the value of of a curve between
// two colors based on the current time value.
//
// Colors must be in HEX, and time should be between 0 and 1.
//
// Example found at: http://stackoverflow.com/questions/4580105/programmatic-gradient-stops-with-javascript
function calcGrad(start, end, current_time)
{
    // Create some local variables for the calculations.
    var r,g,b;

    r1 = HexToR(start);
    g1 = HexToG(start);
    b1 = HexToB(start);
    r2 = HexToR(end);
    g2 = HexToG(end);
    b2 = HexToB(end);

    // Calculate the current value based on the time
    // and the start and end colors.
    r = current_time * HexToR(start) + (1-current_time) * HexToR(end);
    g = current_time * HexToG(start) + (1-current_time) * HexToG(end);
    b = current_time * HexToB(start) + (1-current_time) * HexToB(end);

    return new Array(Math.round(r),Math.round(g),Math.round(b));
}

// Credit for these functions to: http://www.javascripter.net/faq/hextorgb.htm
function HexToR(h) {
    return parseInt((cutHex(h)).substring(0,2),16)
    }
function HexToG(h) {
    return parseInt((cutHex(h)).substring(2,4),16)
    }
function HexToB(h) {
    return parseInt((cutHex(h)).substring(4,6),16)
    }
function cutHex(h) {
    return (h.charAt(0)=="#") ? h.substring(1,7):h
    }

// All credits to R0bb13, found at: http://markmail.org/message/hilbsejsl4zxwlv6#query:+page:1+mid:hilbsejsl4zxwlv6+state:results
//generates the hex-digits for a colour.
function hex(x) {
    hexDigits = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B",
        "C", "D", "E", "F");
    return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
}
function rgb2hex(rgb) {
    return "" + hex(rgb[0]) + hex(rgb[1]) + hex(rgb[2]);
}

function doBGAnimate()
{
    setTimeout(function(){
        doBGAnimate();
    }, 60000);
    var d = new Date();
    var mins = (d.getHours()*60) + d.getMinutes();

    if(mins > 1440)
        mins = 1;

    time = (mins/(1440));

    // Night time
    if (mins >= 0 && mins <= 240)
    {
        mins_in_cycle = (240);
        time = Math.abs(240 - mins) / mins_in_cycle;
        c2 = calcGrad("#1155EF","#F978EB" , time);
        c = calcGrad("#010F4A", "#FCE57B"  , time);
    }

    if (mins >= 240 && mins <= 420)
    {
        mins_in_cycle = (420-240);
        time = Math.abs(420 - mins) / mins_in_cycle;
        c2 = calcGrad("#F978EB", "#AEFAFD",  time);
        c = calcGrad("#FCE57B","#1FD7D1"  ,  time);
    }
	
    if (mins >= 420 && mins <= 840)
    {
        mins_in_cycle = (840-420);
        time = Math.abs(420 - mins) / mins_in_cycle;
        c2 = calcGrad("#86F0FD" ,"#AEFAFD",  time);
        c = calcGrad("#08B5C5"  , "#1FD7D1", time);
    }

    if (mins >= 840 && mins <= 1080)
    {
        mins_in_cycle = (1080-840);
        time = Math.abs(840 - mins) / mins_in_cycle;
        c2 = calcGrad("#46DCEE","#86F0FD"  , time);
        c = calcGrad("#117C86","#08B5C5"  , time);
    }

    if (mins >= 840 && mins <= 1020)
    {
        mins_in_cycle = (1020-840);
        time = Math.abs(840 - mins) / mins_in_cycle;
        c2 = calcGrad("#46DCEE", "#86F0FD" , time);
        c = calcGrad("#117C86","#08B5C5"  , time);
    }

    if (mins >= 1020 && mins <= 1140)
    {
        mins_in_cycle = (1140-1020);
        time = Math.abs(1020 - mins) / mins_in_cycle;
        c2 = calcGrad("#F186B8","#46DCEE" , time);
        c = calcGrad("#FFC600","#117C86"  , time);
    }

    if (mins >= 1020 && mins <= 1140)
    {
        mins_in_cycle = (1140-1020);
        time = Math.abs(1020 - mins) / mins_in_cycle;
        c2 = calcGrad("#F186B8","#46DCEE" , time);
        c = calcGrad("#FFC600","#117C86"  , time);
    }

    if (mins >= 1140 && mins <= 1260)
    {
        mins_in_cycle = (1260-1140);
        time = Math.abs(1140 - mins) / mins_in_cycle;
        c2 = calcGrad("#1155EF","#F186B8" , time);
        c = calcGrad("#010F4A","#FFC600"  , time);
    }

    if (mins >= 1260 && mins <= 1440)
    {
        mins_in_cycle = (1440-1260);
        time = Math.abs(1260 - mins) / mins_in_cycle;
        c2 = calcGrad("#1155EF","#F186B8" , time);
        c = calcGrad("#1155EF","#010F4A"  , time);
    }	

    // Add gradient to the background.
    $('#bg_block').gradient({
        from:      rgb2hex(c),
        to:        rgb2hex(c2)
    });
}


