﻿/*
* Deal(S) of the Day Library
* http://www.logomark.com/
*
* Author:		Witt
* Create date:  2010-01-21
* Description:	
*/


var picturenotfound = "site/themes/images/detail-photonotfound.gif";
var pathstandardimage = "site/images/standard/";
var pathfilmstripimage = "site/images/filmstrip/";
var paththumbnailimage = "site/images/thumbnail/";
var defaultpageurl = "http://www.99centpromos.com/ecommerce/CatalogSubCategoryDisplay.aspx?SearchType=3&PP=1";
var nodealoftheday = "Kapow!! Today's 99centpromos.com <br /> Deal Of The Day is sold out. <br />  Here are more incredible deals <br /> for 99cents or less!";

function redirectdefault(msg) {
    messageboxrd(msg, "Sold out", defaultpageurl);
}

function loaddeal() {
    // IE caches ajax call result
    var myRand = parseInt(Math.random() * 99999999);
    $.ajax({
        type: "GET",
        url: "/Comm/dealoftheday.aspx",
        data: "Rand=" + myRand,
        success: function(responce) {

            if ((!responce) || (responce == "")) {
                redirectdefault(nodealoftheday);
                return;
            }

            // this will give us an array of objects
            var info = JSON.parse(responce);

            if (!info || !info.Deals) {
                redirectdefault(nodealoftheday);
                return;
            }

            // Deal Interval
            if (info.From) {
                $(".dealintervalfrom").text(info.From.toString());
            }
            if (info.To) {
                $(".dealintervalto").text(info.To.toString());
            }
            if (true == info.DealTime) {
                $(".dealtimebuynow").show();
            }
            else {
                $(".dealtimebuynow").hide();
            }

            populatedeals(info.Deals);
        }
    });
}

function populatedeals(deals) {
    if ((!deals) || (deals.length <= 0)) {
        redirectdefault(nodealoftheday);
        return;
    }

    for (var i = 0; i < deals.length; i++) {
        if (!deals[i])
            break;

        loadeachdeal(deals[i]);
    }
    // Scrollable panel
    $(".scrollabletop").scrollable({ size: 1, loop: true });
    $(".scrollablebott").scrollable({ size: 1, loop: true });
}

function loadeachdeal(deal) {
    if (!deal)
        return;

    var dealid = deal.DealTypeID.toString();

    // /////////////////////////////////////////////////////////////////////////
    // Today: [Buy Now] Button
    $(".buynow_butt" + dealid + " a").attr("href", "CatalogItemDetail.aspx?IID=" + deal.IID.toString());

    // Today: Limited Quantity Available
    // !!! Ignore the quantity carted
    var nqty = deal.QtyLimited - deal.QtyOrdered;
    // var nqty = deal.QtyLimited - deal.QtyCarted - deal.QtyOrdered;
    $(".today.deal_inventory" + dealid + " .numlimited").text(FormatNumberBy3(nqty));

    // Today: Product Image
    if ((!deal.IMG) || (deal.IMG == "")) {
        $(".today.product_info" + dealid + " a").attr("href", picturenotfound);
        $(".today.product_info" + dealid + " a img").attr("src", picturenotfound);
    }
    else {
        $(".today.product_info" + dealid + " a").attr("href", pathstandardimage + deal.IMG);
        $(".today.product_info" + dealid + " a img").attr("src", pathfilmstripimage + deal.IMG);
        $(".today.product_info" + dealid + " a").flyout();
    }

    // Today: Tomorrow's Product Image
    if (deal.DealTypeID == 1) {
        if ((!deal.IMGTom) || (deal.IMGTom == "")) {
            $(".today.TomorrowImageBox img").attr("src", picturenotfound);
        }
        else {
            $(".today.TomorrowImageBox img").attr("src", paththumbnailimage + deal.IMGTom);
        }
    }
    else {
        $(".DealBottomContainer").show();
        $(".moredealssplitter").show();
    }

    // Today: Product SKU, Name
    $(".today.product_info" + dealid + " .deal_productsku").text(deal.Headline1 + "  -  " + deal.StockCode);
    $(".today.product_info" + dealid + " .deal_productname").text(deal.Headline2);
    if (deal.QtyMin > 0) {
        $(".today.product_info" + dealid + " .deal_productavail").text(deal.QtyMin.toString() + " piece min").show();
    }

    // Today: Sold out
    if ((nqty <= 0) || (deal.QtyLimited < deal.QtyMin) || (nqty < deal.QtyMin)) {
        $(".buynow_butt" + dealid + " a").hide();
        $("#DealBottomOverlay" + dealid).show();
        $(".today.deal_inventory" + dealid + " .numlimited").text(FormatNumberBy3(0));
    }

    // Long Descript of the product
    $(".longdesc.today").html(deal.LongDescription);

    // /////////////////////////////////////////////////////////////////////////
    // Tomorrow: Limited Quantity Available
    $(".tomorrow.deal_inventory" + dealid + " .numlimited").text(FormatNumberBy3(deal.QtyLimitedTom));

    // Tomorrow: Product Image
    if ((!deal.IMGTom) || (deal.IMGTom == "")) {
        $(".tomorrow.product_info" + dealid + " a").attr("href", picturenotfound);
        $(".tomorrow.product_info" + dealid + " a img").attr("src", picturenotfound);
    }
    else {
        $(".tomorrow.product_info" + dealid + " a").attr("href", pathstandardimage + deal.IMGTom);
        $(".tomorrow.product_info" + dealid + " a img").attr("src", pathfilmstripimage + deal.IMGTom);
        $(".tomorrow.product_info" + dealid + " a").flyout();
    }

    // Tomorrow: Today's Product Image
    if (deal.DealTypeID == 1) {
        if ((!deal.IMG) || (deal.IMG == "")) {
            $(".tomorrow.TomorrowImageBox img").attr("src", picturenotfound);
        }
        else {
            $(".tomorrow.TomorrowImageBox img").attr("src", paththumbnailimage + deal.IMG);
        }
    }

    // Tomorrow: Product SKU, Name
    $(".tomorrow.product_info" + dealid + " .deal_productsku").text(deal.Headline1Tom + "  -  " + deal.StockCodeTom);
    $(".tomorrow.product_info" + dealid + " .deal_productname").text(deal.Headline2Tom);
    if (deal.QtyMinTom > 0) {
        $(".tomorrow.product_info" + dealid + " .deal_productavail").text(deal.QtyMinTom.toString() + " piece min").show();
    }

    // Long Descript of the product
    $(".longdesc.tomorrow").html(deal.LongDescriptionTom);

    // Deal panel
    $(".dealpanel" + dealid).show();
}
