﻿(function() {
    ZOOMIN.namespace("ZOOMIN.DealOfTheDay");
    var common = ZOOMIN.Common;
    $(document).ready(function() {
        $('.iwantone').click(function() {
            var CategoryID = $(this).attr("id");
            console.log(CategoryID);
            $.ajax({
                type: "GET",
                url: "/DealOfDay/SendPromoCode?CategoryID=" + CategoryID,
                cache: false,
                dataType: "json",
                success: function(json) {
                    console.log(json);
                    if (json.code == -1) {
                        location.href = json.message;
                    }
                    else {
                        showBoxy(json.code, CategoryID);
                    }
                }
            });
        });
    });
    // currency change raising the currencyChange for reloading the page
    $(CURRENCY).bind('currencyChange', function(args, data) {
        location.reload();
    });
})();

function emailcode(CategoryID) {
    $.ajax({
        type: "GET",
        url: "/DealOfDay/ReSendCodeEmail?CategoryID=" + CategoryID,
        cache: false,
        dataType: "json",
        success: function(json) {
            showBoxy(json.code);
        }
    });
}
function showBoxy(boxyType, CategoryID) {
    if (boxyType == 0) {
        new Boxy.alert("<div><p class='text'>Your coupon has been e-mailed to your registered email address. Your code is valid until midnight (India time) on the day of the Deal.</p><br/><br/><strong class='text'>Important:</strong><ul class='disclist'><li>Your code validity cannot be extended.</li><li>This code is only valid for one redemption per household.  Do not use multiple codes as you will get charged the full retail price.</li></ul></div>", null, {
            title: "Coupon Sent",
            modal: true,
            height: "auto", 
            width: "400px"
        });
    }
    if (boxyType == 1) {
        new Boxy.alert("<div><p class='text'>We have resent your coupon code to your registered email address. Your code is valid until midnight (India time) on the day of the Deal.</p><br/><br/><strong class='text'>Important:</strong><ul class='disclist'><li>Your code validity cannot be extended.</li><li>This code is only valid for one redemption per household.  Do not use multiple codes as you will get charged the full retail price.</li></ul></div>", null, {
            title: "Coupon Resent",
            modal: true,
            height: "auto",
            width: "400px"
        });
    }
    if (boxyType == -3) {
        new Boxy.alert("<div><p class='text'>It appears you have already claimed a code for this deal. " +
                        "Please check your e-mail for this Deal's code and remember to return on the " +
                        "day of the Deal to redeem it.</p>" +                        
                        "<p class='text center'><button id=email" + CategoryID + " type=button class='btn-orange'>" +
                        "Email me my code again</button>" +
                        "</p></div>", null, {
                            title: "Already claimed",
                            modal: true,
                            height: "auto",
                            width: "300px",
                            behaviours: function(r) {
                                $(r).find("button").click(function() {
                                    var catID = $(this).attr("id").replace("email", "");
                                    Boxy.get(this).hide();
                                    emailcode(catID);
                                });
                            }
                        });
    }
    if (boxyType == -2) {
        new Boxy.alert("<p class='text'>Our records indicate that you have registered after December 24th 2009.  We're sorry - but this promotion is valid for members who registered by December 24th only.  A brand new Deal of the Day just for you will be back soon - so stay tuned!</p>", null, {
            title: "Oops!",
            modal: true,
            height: "auto", width: "300px"
        });
    }
    if (boxyType == -4) {
        new Boxy.alert("<p class='text'>We could not locate your coupon code. Please try again or contact customer service for further assistance.</p>", null, {
            title: "Oops!",
            modal: true,
            height: "auto", width: "300px"
        });
    }
}

$("#loadingajax").ajaxStart(function() {
    $(this).css({
        opacity: 0.7,
        height: $("#main-content").height()
    });
    $(this).show();
});
$("#loadingajax").ajaxStop(function() {
    $(this).hide();
});
