$(document).ready(function()
{
    //alert('I Love JQuery');
    
    $(".sel-show-signup").live("click", function()
    {
        $find("modalSignup").show();
        tc_log('/Talking500/Signup');
    });

    $(".sel-show-login").click(function()
    {
        $find("modalLogin").show();
        tc_log('/Talking500/Login');
    });

    $(".sel-show-from-email").live("click", function() {
        $find("modalLoggedIn").show();
        //tc_log('/Talking500/Login');
    });
    
    $(".sel-hide-signup").click(function()
    {
        $find("modalSignup").hide();
    });

    $(".sel-hide-login").click(function()
    {
        $find("modalLogin").hide();
    });

    $(".sel-hide-from-email").click(function() {
        $find("modalLoggedIn").hide();
    });
    
    $(".sel-hide-signup-show-login").click(function()
    {
        $find("modalSignup").hide();
        $find("modalLogin").show();
        tc_log('/Talking500/Login');
    });
    
    
    

//#############################################################
/*
Table sorter
*/
//#############################################################

    if($("#sel-stats").length > 0){
    $("#sel-stats").tablesorter({
        sortList:[[0,1]]
    });}

    if ($("#sel-guess").length > 0){
    $("#sel-guess").tablesorter({
        sortList: [[1, 1]]
    });}

    if ($("#sel-winner").length > 0){
    $("#sel-winner").tablesorter({
        sortList: [[2, 1]]
    });}
    

//#############################################################
/*
    Ticker on the guess page
*/
//#############################################################
    var d1, d2, h1, h2, m1, m2, s1, s2;
    var height = 24;

    function startTimer()
    {
        readTime();

        var t ='' + getTime(d1, 'd1') + getTime(d2, 'd2') +
            ' ' + getTime(h1, 'h1') + getTime(h2, 'h2') +
            ' ' + getTime(m1, 'm1') + getTime(m2, 'm2') +
            ' ' + getTime(s1, 's1') + getTime(s2, 's2');

        $(".ticks").html(t);
        
        $("span.tick").each(function()
        {
            var val = $(this).attr("data-val") * height;
            $(this).css({ backgroundPosition: '0px 0px' });
            $(this).animate({ backgroundPosition: '0px -' + val + 'px' }, { duration: 200 });
        });

        setTimeout(timerTick, 1000);
    }

    function readTime()
    {
        var time = $(".target").html();

        d = caltime(time, 86400, 100000);
        h = caltime(time, 3600, 24);
        m = caltime(time, 60, 60);
        s = caltime(time, 1, 60);

        if ((s + '').length > 1) { s1 = s.substring(0, 1); s2 = s.substring(1, 2); } else { s1 = 0; s2 = s; }
        if ((m + '').length > 1) { m1 = m.substring(0, 1); m2 = m.substring(1, 2); } else { m1 = 0; m2 = m; }
        if ((h + '').length > 1) { h1 = h.substring(0, 1); h2 = h.substring(1, 2); } else { h1 = 0; h2 = h; }
        if ((d + '').length > 1) { d1 = d.substring(0, 1); d2 = d.substring(1, 2); } else { d1 = 0; d2 = d; }

        $(".target").html(time - 1);
    }
    
    function timerTick()
    {
        dx1 = d1;
        dx2 = d2;
        hx1 = h1;
        hx2 = h2;
        mx1 = m1;
        mx2 = m2;
        sx1 = s1;
        sx2 = s2;

        readTime();

        if (s2 != sx2) { ticked("s2", s2); }
        if (s1 != sx1) { ticked("s1", s1); }
        if (m2 != mx2) { ticked("m2", m2); }
        if (m1 != mx1) { ticked("m1", m1); }
        if (h2 != hx2) { ticked("h2", h2); }
        if (h1 != hx1) { ticked("h1", h1); }
        if (d2 != dx2) { ticked("d2", d2); }
        if (d1 != dx1) { ticked("d1", d1); }
        
        setTimeout(timerTick, 1000);
    }

    function ticked(type, val)
    {
        $("." + type).attr('data-val', val);
        $("." + type).animate({ backgroundPosition: '0px -' + val * height + 'px' }, { duration: 200 });
    }

    function padLeft(s)
    {
        for (i = 0; (s + '').length <= 1; i++)
        {
            s = "0" + s;
        }
        return s;
    }

    function getTime(entity, type)
    {
        return "<span class='tick " + type + "' data-val=" + entity + ">&nbsp;</span>";
    }

    function caltime(secs, turn, count)
    {
        s = ((Math.floor(secs / turn)) % count).toString();
        return padLeft(s);
    }

    startTimer();

});


/**
* @author Alexander Farkas
* v. 1.21
* for dynamically changing the position of a background-image
*/


(function($)
{
    if (!document.defaultView || !document.defaultView.getComputedStyle)
    { // IE6-IE8
        var oldCurCSS = jQuery.curCSS;
        jQuery.curCSS = function(elem, name, force)
        {
            if (name === 'background-position')
            {
                name = 'backgroundPosition';
            }
            if (name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[name])
            {
                return oldCurCSS.apply(this, arguments);
            }
            var style = elem.style;
            if (!force && style && style[name])
            {
                return style[name];
            }
            return oldCurCSS(elem, 'backgroundPositionX', force) + ' ' + oldCurCSS(elem, 'backgroundPositionY', force);
        };
    }

    var oldAnim = $.fn.animate;
    $.fn.animate = function(prop)
    {
        if ('background-position' in prop)
        {
            prop.backgroundPosition = prop['background-position'];
            delete prop['background-position'];
        }
        if ('backgroundPosition' in prop)
        {
            prop.backgroundPosition = '(' + prop.backgroundPosition;
        }
        return oldAnim.apply(this, arguments);
    };

    function toArray(strg)
    {
        strg = strg.replace(/left|top/g, '0px');
        strg = strg.replace(/right|bottom/g, '100%');
        strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g, "$1px$2");
        var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
        return [parseFloat(res[1], 10), res[2], parseFloat(res[3], 10), res[4]];
    }

    $.fx.step.backgroundPosition = function(fx)
    {
        if (!fx.bgPosReady)
        {
            var start = $.curCSS(fx.elem, 'backgroundPosition');

            if (!start)
            {//FF2 no inline-style fallback
                start = '0px 0px';
            }

            start = toArray(start);

            fx.start = [start[0], start[2]];

            var end = toArray(fx.options.curAnim.backgroundPosition);
            fx.end = [end[0], end[2]];

            fx.unit = [end[1], end[3]];
            fx.bgPosReady = true;
        }
        //return;
        var nowPosX = [];
        nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
        nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
        fx.elem.style.backgroundPosition = nowPosX[0] + ' ' + nowPosX[1];

    };
})(jQuery);

//These buttons perform all call the following Javascript function 'openSpecialWindow' and pass in either:
//BOOK , REQUEST or CONFIGURE

function openSpecialWindow(sType) {
    //alert(sType);
    switch (sType) {
        case 'BOOK':
            //window.location = "http://www.fiat.co.uk/contactconsole/ArrangeTestDrive.aspx?eddb=true&mediaCode=106&hash=showroom/home";
            window.open("http://www.fiat.co.uk/contactconsole/ArrangeTestDrive.aspx?eddb=true&mediaCode=106&hash=showroom/home", sType, 'location=0,status=0,scrollbars=1,height=685,width=575,resizable=1');
            break;
        case 'REGISTER':
            //window.location = "http://www.fiat.co.uk/contactconsole/requestbrochure.aspx?mediaCode=106&hash=showroom/home";
            window.open("http://www.fiat.co.uk/contactconsole/requestbrochure.aspx?mediaCode=106&hash=showroom/home", sType, 'location=0,status=0,scrollbars=1,height=685,width=575,resizable=1');
            break;
        case 'CONFIGURE':
            //window.location = "http://www.fiat.co.uk/Showroom/#showroom/configurator/configurator_home";
            window.open("http://www.fiat.co.uk/Showroom/#showroom/configurator/configurator_home", sType, 'location=0,status=0,scrollbars=1,height=610,width=1010,resizable=1');
            break;
        default: alert('error');
    }
}
