/**********************************
    Shopping Cart
***********************************/
var onMouseOutTimeOut = 300;
var cartEffectsDuration = 300;
function addToCart(SkuId, Skucount) {

    var count = Skucount;
    if (!Skucount)
        count = $("#" + SkuId).val();
    $.ajax({
        type: "POST",
        url: "/Cart/AddSku/" + SkuId + "/" + count,
        data: "",
        dataType: "json",
        success: ChangeBasket,
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
            alert("Add to cart failed\nReason: " + textStatus + "\nHTTP status: " + XMLHttpRequest.status + " " + XMLHttpRequest.statusText);
        }
    });
}
function ChangeBasket(msg) {
    $("#TotalPrice").html('' + msg.TotalPrice);
    $("#CountItems").html('' + msg.TotalCount);
}

function quantityChanged(source, skuId)
{
    var qty = parseInt($(source).val());
    if (!isNaN(qty) && isFinite(qty))
        updateQty(skuId, qty);
    else
        alert("Incorrect number!");
}

function updateQty(skuId, quantity)
{
    $.ajax({
        type: "POST",
        url: "/Cart/UpdateSku/" + skuId + "?quantity=" + quantity,
        data: "",
        dataType: "json",
        success: function(msg)
        {
            $("#ViewCartControl").html(msg.htmlViewCart);

            ChangeBasket(msg);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
            alert("Update from cart failed\nReason: " + textStatus + "\nHTTP status: " + XMLHttpRequest.status + " " + XMLHttpRequest.statusText);
        }
    });
}

function removeFromCart(skuId)
{
    $.ajax({
        type: "POST",
        url: "/Cart/RemoveSku/" + skuId,
        data: "",
        dataType: "json",
        success: function(msg)
        {
            $("#ViewCartControl").html(msg.htmlViewCart);

            ChangeBasket(msg);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
            alert("Remove from cart failed\nReason: " + textStatus + "\nHTTP status: " + XMLHttpRequest.status + " " + XMLHttpRequest.statusText);
        }
    });
}

$(document).ready(
  function()
  {
    //hover states on the static widgets
    $('a.ui-state-default').hover(
	    function() { $(this).addClass('ui-state-hover'); }, 
	    function() { $(this).removeClass('ui-state-hover'); }
    );
  }
);

function CountTime(HomeUrl)
{
    var IsRun = false;
    
    for (var i = 0; i < TRValues.length; i++)
    {
        var ts = TRValues[i].ts;

        if(ts > 0)
        {
            IsRun = true;
            
            ts = ts - 1;
            TRValues[i].ts = ts;

            var strTime = 'Time Left: <br/> ';
            strTime += '<table class="tblTimer">';
            strTime += '<tr>';
            strTime += '<th>Days</th>';
            strTime += '<th>Hrs</th>';
            strTime += '<th>Mins</th>';
            strTime += '<th>Secs</th>';
            strTime += '</tr>';
            strTime += '<tr>';
            strTime += '<td>' + Math.floor(ts / 86400) + '</td>';
            strTime += '<td>' + Math.floor(ts/3600)%24 + '</td>';
            strTime += '<td>' + Math.floor(ts/60)%60 + '</td>';
            strTime += '<td>' + (ts%60) + '</td>';
            strTime += '</tr>';
            strTime += '</table>';
            
            var spanRem = $("span.time_" + TRValues[i].id);
            spanRem.html(strTime);
            
            if (ts == 0)
            {
                window.location = HomeUrl;
            }
        }
    }
        
    if(IsRun)
        setTimeout("CountTime('" + HomeUrl + "')", 1000 );
}