﻿Array.prototype.remove = function(dx) {
    if (isNaN(dx) || dx > this.length) { return false; }
    for (var i = 0, n = 0; i < this.length; i++) {
        if (this[i] != this[dx]) {
            this[n++] = this[i]
        }
    }
    this.length -= 1;
}

function LoadCusBro() {
    $("#viewed").load($("#appPath").val() + "Style/CustBrowseStyle.aspx?" + Math.random() * 10000, function(responseText, textStatus, XMLHttpRequest) {

        if (textStatus != "success") {
            $("#viewed").empty();
        }
        else {
            $("#viewed").empty();
            $("#viewed").append(responseText);
            $("#viewed .handle").click(function() {
                $.cookie('LatestViewedItem', null);
                LoadCusBro();
                return false;
            });
        }
    });
}


function ViewStyle(styleID, stylePrice, styleLargeMapImagePath, styleName, styleBrand, styleType) {

    var currentCookieValue = $.cookie('LatestViewedItem');
    var canAddValue = true;

    if (currentCookieValue != null) {

        var newCookieValue = "";

        cookieValues = currentCookieValue.split("$");

        if (cookieValues.length > 0) {

            if (cookieValues.length >= 5) {
                cookieValues.remove(cookieValues.length - 1);
            }

            for (i = 0; i <= cookieValues.length - 1; i++) {

                cookieItems = cookieValues[i].split("^");

                if (cookieItems[0] == styleID) {
                    canAddValue = false;
                }
            }
            

            if (canAddValue) {
                $.cookie('LatestViewedItem', styleID + "^" + stylePrice + "^" + styleLargeMapImagePath + "^" + styleName + "^" + styleBrand + "^" + styleType + "$" + cookieValues.join("$"), { expires: 5 });
            }
        }
        else {
            $.cookie('LatestViewedItem', styleID + "^" + stylePrice + "^" + styleLargeMapImagePath + "^" + styleName + "^" + styleBrand + "^" + styleType, { expires: 5 });
        }

    } else {
        $.cookie('LatestViewedItem', styleID + "^" + stylePrice + "^" + styleLargeMapImagePath + "^" + styleName + "^" + styleBrand + "^" + styleType, { expires: 5 });
    }
}



$(document).ready(function() {
    LoadCusBro();
});
