﻿//countdown timer 
function CountDownTimer(arguments) {
    var defaults = {
        outputEle: $("body").get(0),
        year: 0,
        month: 0,
        day: 0,
        hour: 0,
        minute: 0,
        second: 0,
        milliseconds: 0
    };
    var args = $.extend(defaults, arguments);
    
    this.OutputEle = args.outputEle;
    this.EndDate = new Date(
        args.year, 
        args.month, 
        args.day, 
        args.hour, 
        args.minute, 
        args.second, 
        args.milliseconds
    );
}
CountDownTimer.prototype.showTimeLeft = function(outputFormat) {
    var beginning = new Date(0);
    var now = new Date();
    var diff = new Date(this.EndDate - now);
    if (diff < 0) {
        $(this.OutputEle).html("Now Ended");
        return;
    }
    //get the differences
    var years = diff.getFullYear() - beginning.getFullYear();
    var months = diff.getMonth() - beginning.getMonth();
    var days = diff.getDate() - beginning.getDate();
    var hours = diff.getHours() - beginning.getHours();
    var minutes = diff.getMinutes() - beginning.getMinutes();
    var seconds = diff.getSeconds() - beginning.getSeconds();
    var milliseconds = diff.getMilliseconds() - beginning.getMilliseconds();
    //output to specified ele using chosen format
    //if no format specified, show all except 0's on years, months and days, and dont bother with milliseconds
    var str = "";
    if (outputFormat == null) {
        if (years != 0) str += (years + " year" + ((years == 1) ? "" : "s") + ", ");
        if (months != 0) str += months + " month" + ((months == 1) ? "" : "s") + ", ";
        if (days != 0) str += days + " day" + ((days == 1) ? "" : "s") + ", ";
        str += hours + " hour" + ((hours == 1) ? "" : "s") + ", ";
        str += minutes + " minute" + ((minutes == 1) ? "" : "s") + ", ";
        str += seconds + " second" + ((seconds == 1) ? "" : "s");
    }
    $(this.OutputEle).html(str);
}

function showPromotionExplainedPopup(promoID) {
    triggerMacBox({
        boxID: "PromoExplained",
        width: 541,
        ajaxUrl: "/global/promotions/PromoExplainedAJAX.aspx?promoID=" + promoID + "&date=" + new Date()
    });
}

function show28DayMBGPopup() {
    triggerMacBox({
        boxID: "_28DayMBGExplained",
        width: 600,
        ajaxUrl: "/global/promotions/28day-mbg/28day-explained-ajax.aspx",
        borderType: "round"
    });
}

$(document).ready(function() {
    //make all delivery popup links link to the erm... delivery popup
    $(".delivery-cutoff-link").click(function(e) {
        e.preventDefault();
        triggerMacBox({
            boxID: "delivery-cutoff-explained",
            width: 500,
            ajaxUrl: "/global/promotions/DeliveryCutOff/DeliveryCutOffExplained.aspx",
            borderType: "round"
        });
    });
    //all BANK5 popup links need to link up
    $(".bank5-explained").click(function(e) {
        e.preventDefault();
        triggerMacBox({
            boxID: "bank4-explained",
            width: 500,
            ajaxUrl: "/global/promotions/BANK5-26082010/BANK5-Explained.aspx",
            borderType: "round"
        });
    });
    //popup for delivery-before-weekend
    $(".delivery-before-weekend").click(function(e) {
        e.preventDefault();
        triggerMacBox({
            boxID: "DeliveryBeforeWeekend",
            width: 450,
            ajaxUrl: "/global/promotions/DeliveryBeforeWeekend/DeliveryBeforeWeekend.aspx"
        });
    });
    //popup for pick your own delivery date
    $(".pyo-delivery-date").click(function(e) {
        e.preventDefault();
        triggerMacBox({
            boxID: "PYODeliveryDate",
            width: 450,
            ajaxUrl: "/global/promotions/PYODeliveryDate/PYODeliveryDate.aspx"
        });
    });
    //promotions explained popup
    $(".promo-explained").click(function(e) {
        e.preventDefault();
        var promoID = $(this).attr("rel");
        showPromotionExplainedPopup(promoID);
    });

    //28 day money back guarantee
    $(".28day-mbg-explained").click(function(e) {
        e.preventDefault();
        show28DayMBGPopup();
    });

    //set up our general hover bubbles
    $(".hoverbubble-anchor").each(function() {
        var h = "<img src=\"" + $(this).attr("data-image") + "\" alt=\"\" />";
        h += "<p>" + $(this).attr("data-text") + "</p>";
        var r = $(this).attr("data-rel");
        var w = $(this).attr("data-width");
        $(this).hoverbubble({
            html: h,
            rel: r,
            width: w
        });
    });
});
