﻿$("#sodContainer #closebtn a").live( "click", function() {
    SetCookie("sodoff","1");
    $("#sodContainer").slideUp();
    return false;
});

function showStealOfTheDay()
{
    jQuery("#sodContainer").html("<div style='margin-top: 1in'>" + ytimesSOD.Loading + "</div>").slideDown();
    $.getJSON("http://site.finnstyle.com/steal/getSteal.php?store=" + storeId + "&jsonp=?",
        function(data){
            if (data.result)
            {
                var source = data.product_id + ".html";
                var qty = data.qty;
                var timeLeft = data.timeLeft;
                if (!ytimesSOD.alternateLoad)
                {
                    source += " #sodContent";
                    jQuery("#ajax").load(source, function(responseText,textStatus,XMLHttpRequest) {
                        var html = $("#sodContainer").html().replace(/<.*?>/gi,'');

                        var htmlResult = "";
                        for (j = 0; j < html.length; j +=2)
                        {
                            var h1 = html.charAt(j);
                            var h2 = html.charAt(j+1);
                            htmlResult += (String.fromCharCode(parseInt(h1 + h2,16)));
                        }
                        jQuery("#sodContainer").html(htmlResult);
                        $("#sodContainer #remainingQuan span").text(qty);
                    });
                }
                else
                {
                    jQuery.get(source, function(data) {
                        var htmlResult = "";
                        var resp = data.split("<!-- SOD -->");
                        if (resp && resp.length > 0)
                        {
                            if (typeof(resp[1]) != 'undefined')
                            {
                                html = resp[1].replace(/<.*?>/gi,'');

                                for (j = 0; j < html.length; j +=2)
                                {
                                    var h1 = html.charAt(j);
                                    var h2 = html.charAt(j+1);
                                    htmlResult += (String.fromCharCode(parseInt(h1 + h2,16)));
                                }
                            }
                            else
                            {
                                htmlResult = "<center>Sorry, could not retrieve more information on that item. Please visit the item page instead.</center>";
                            }
                            jQuery("#sodContainer").html(htmlResult);
                            $("#sodContainer #remainingQuan span").text(qty);
                            $("#sodEnds").text(timeLeft);
                        }
                    });
                }
            }
        });
    return(false);
}

$("#stealOfTheDay").click( showStealOfTheDay );

if (GetCookie("sodoff") == null || GetCookie("sodoff") != "1")
{
    $(document).ready( showStealOfTheDay );
}