﻿$(function () {
    if (document.getElementById("hour1") != undefined && location.href.indexOf('entrycomplete') == -1) {
        UpdateTime2(); setInterval("UpdateTime2()", 1000);
    }
    if (document.getElementById("hour1q") != undefined && location.href.indexOf('entrycomplete') == -1) {
        UpdateTime3(); setInterval("UpdateTime3()", 1000);
    }
    if (document.getElementById("hours") != undefined && location.href.indexOf('entrycomplete') == -1 && location.href.indexOf('dailyprize') == -1) {
        UpdateTime(); setInterval("UpdateTime()", 1000);
    }
    if (typeof hyphenate !== "undefined") {
        $(".top-prize p").hyphenate();
    }
    AlignContent();
});

// Language change links
$(function () {
    $(".language").click(function () {
        $("#fldLanguage").val($(this).next().val());
    });
});

function UpdatePrize() {
    if (spanPr != undefined) {
        //reached last item so reset
        if (currentItem == items - 1) {
            currentItem = 0;
        }
        else {
            currentItem += 1;
        }
        spanPr.innerHTML = "<a href=\"" + competitionUrl + prizes[currentItem].url + "\">" + prizes[currentItem].callToAction + "</a>";
        ScrollIn();
    }
}

function ScrollOut() {
    var x = parseInt(spanPr.style.left);
    x -= 3;
    spanPr.style.left = x + "px";

    if (x > -284) {
        if (timeoutTime > 30) {
            timeoutTime -= 5;
        }
        setTimeout("ScrollOut()", timeoutTime);
    } else {
        //set position to right
        spanPr.style.left = "284px";

        UpdatePrize();
    }
}

function ScrollIn() {
    var x = parseInt(spanPr.style.left);
    //set x to be offset to the right
    x -= 3;

    spanPr.style.left = x + "px";

    if (x > 0) {
        if (timeoutTime < 100 && x < 50) {
            timeoutTime += 2;
        }
        setTimeout("ScrollIn()", timeoutTime);
    } else {
        timeoutTime = 100;
        spanPr.style.left = "0px";
        window.setTimeout("ScrollOut()", 5000);
    }
}

var newwindow;
function popup(url) {
    newwindow = window.open(url, 'name', 'height=700,width=700');
    if (window.focus) { newwindow.focus() }
}

//new users
function UpdateTime2() {
    ss -= 1;
    if (ss < 0) {
        ss = 59; mm -= 1;
    }
    if (mm < 0) {
        mm = 59; hh -= 1;
    }
    if (hh < 0) {
        hh = 23;
    }
    var hour1 = document.getElementById("hour1");
    var hour2 = document.getElementById("hour2");
    var minute1 = document.getElementById("minute1");
    var minute2 = document.getElementById("minute2");
    var second1 = document.getElementById("second1");
    var second2 = document.getElementById("second2");

    if (hour1 != undefined) {
        hour1.style.backgroundPosition = -20 * getNumber(hh, 0) + "px";
        hour2.style.backgroundPosition = -20 * getNumber(hh, 1) + "px";
        minute1.style.backgroundPosition = -20 * getNumber(mm, 0) + "px";
        minute2.style.backgroundPosition = -20 * getNumber(mm, 1) + "px";
        second1.style.backgroundPosition = -20 * getNumber(ss, 0) + "px";
        second2.style.backgroundPosition = -20 * getNumber(ss, 1) + "px";
    }
}

//Quiz Timer
function UpdateTime3() {
    ss -= 1;
    if (ss < 0) {
        ss = 59; mm -= 1;
    }
    if (mm < 0) {
        mm = 59; hh -= 1;
    }
    if (hh < 0) {
        hh = 23;
    }
    var hour1q = document.getElementById("hour1q");
    var hour2q = document.getElementById("hour2q");
    var minute1q = document.getElementById("minute1q");
    var minute2q = document.getElementById("minute2q");
    var second1q = document.getElementById("second1q");
    var second2q = document.getElementById("second2q");

    if (hour1q != undefined) {
        hour1q.style.backgroundPosition = -25 * getNumber(hh, 0) + "px";
        hour2q.style.backgroundPosition = -25 * getNumber(hh, 1) + "px";
        minute1q.style.backgroundPosition = -25 * getNumber(mm, 0) + "px";
        minute2q.style.backgroundPosition = -25 * getNumber(mm, 1) + "px";
        second1q.style.backgroundPosition = -25 * getNumber(ss, 0) + "px";
        second2q.style.backgroundPosition = -25 * getNumber(ss, 1) + "px";
    }
}

function getNumber(str, pos) {
    var i = parseInt(str);
    if (pos == 0 && i < 10) {
        return 0;
    }
    else if (i < 10) {
        return i;
    }

    var num = parseInt((str + ' ').substr(pos, 1));

    return isNaN(num) ? 0 : num;
}

//existing
function UpdateTime() {
    ss -= 1;
    if (ss < 0) { ss = 59; mm -= 1; }
    if (mm < 0) { mm = 59; hh -= 1; }
    if (hh < 0) { hh = 23; }

    var hours = document.getElementById("hours");
    var minutes = document.getElementById("minutes");
    var seconds = document.getElementById("seconds");
    if (hours != undefined) {
        if (hh < 10) {
            hours.innerHTML = "0";
            hours.innerHTML += hh;
        }
        else {
            hours.innerHTML = hh;
        }
        if (mm < 10) {
            minutes.innerHTML = "0";
            minutes.innerHTML += mm;
        }
        else {
            minutes.innerHTML = mm;
        }
        if (ss < 10) {
            seconds.innerHTML = "0";
            seconds.innerHTML += ss;
        }
        else {
            seconds.innerHTML = ss;
        }
    }
}

// Setup image radio buttons
$(function () {
    $(".customRad").addClass("customEnabled");
    $(".customRad input").after("<div></div>");
    var radClick = function () {
        $(this).parent().parent().children().children("div").removeClass("checked");
        $(this).parent().children("div").addClass("checked");
        $(this).parent().children("input").attr('checked', 'checked');
    };
    $(".customRad div").click(radClick);
    $(".customRad label").click(radClick);
});

$(document).ready(function () {
    $.extend($.expr[':'], {
        currentPage: function (el) {
            return el.href && (el.href === window.location.href || (el.href.match(/-competitions$/) && window.location.href.match(/-competitions$/)));
        }
    });

    $('.nav-wrap ul li a:currentPage').addClass('selected');
});

function AlignContent() {
    var columns = $(".col");
    var maxHeight = 0;

    for (var i = 0; i < columns.length; i++) {
        if (columns.eq(i).height() > maxHeight) {
            maxHeight = columns.eq(i).height() + parseInt(columns.eq(i).css("padding-top")) + parseInt(columns.eq(i).css("padding-bottom"));
        }
    }

    for (var i = 0; i < columns.length; i++) {
        columns.eq(i).css("min-height", maxHeight - parseInt(columns.eq(i).css("padding-top")) - parseInt(columns.eq(i).css("padding-bottom")));
    }
}

//function displayFacebookFlag() {
//    var disabled = false;
//    var cookies = document.cookie.split(';');
//    for (var i = 0; i < cookies.length; i++) {
//        var key = cookies[i].split('=')[0];
//        if (key.replace(' ', '') == 'disablefbflag')
//            disabled = true;
//    }

//    if (!disabled) {
//        $("<div class='fb-flag'><a href='http://apps.facebook.com/offerx_competitions/?a=OX&cid=OX_HP'><img width='217' height='177' src='http://www.offerx.co.uk/im/ox5-1/home/facebook-app-flag.png' alt='Check out our new app on Facebook!' /></a></div>").appendTo("body").animate({ left: 0 }, 2000);
//        $("<a class='hide-fb-flag' href='#'><img src='http://www.offerx.co.uk/im/ox5-1/home/close-facebook-app-flag.png' width='16' height='16' alt='Hide' /></a>").click(function (e) {
//            e.preventDefault();
//            $(this).parent().animate({ left: -217 }, 2000, function () {
//                $(".fb-flag").remove();
//                var d = new Date();
//                document.cookie = 'disablefbflag=true;expires=' + FormatDate(new Date(d.setTime(d.getTime() + (1 * 24 * 60 * 60 * 1000)))) + ";path=/";
//            });
//        }).appendTo('.fb-flag');
//    }
//}

function FormatDate(d) {
    return d.getDate() + "/" + (d.getMonth() + 1) + "/" + d.getYear();
}
