﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Soft202");

Soft202.ShopCar = function(element) {
    Soft202.ShopCar.initializeBase(this, [element]);
    this.cookie = new Cookie();
    this._setPosstionDelegate = null;
    this._box;
    this._meID;
    this._productPage;
    this._numberInputDelegate = null;
}

Soft202.ShopCar.prototype = {
    initialize: function() {
        Soft202.ShopCar.callBaseMethod(this, 'initialize');
        if (this._setPosstionDelegate == null) {
            this._setPosstionDelegate = Function.createDelegate(this, this.SetPosstion);
        }
        if (this._numberInputDelegate == null) {
            this._numberInputDelegate = Function.createDelegate(this, this.NumberInputChange);

        }


        var box = $get(this._box);
        box.style.position = "absolute";
        box.style.top = "0px";
        var left = document.body.clientWidth - 200;
        box.style.left = left + "px";

        g_myBodyInstance = (document.documentElement ? document.documentElement : window);
        $addHandler(g_myBodyInstance, "scroll", this._setPosstionDelegate);
        $addHandler(window, "resize", this._setPosstionDelegate);
        setInterval(this._setPosstionDelegate, 100);
        this.Render();

        // 在此处添加自定义初始化
    },
    get_ProductPage: function() {
        return this._productPage;
    },
    set_ProductPage: function(value) {
        this._productPage = value;
    },
    get_MeID: function() {
        return this._meID;
    },
    set_MeID: function(value) {
        this._meID = value;
    },
    get_Box: function() {
        return this._box;
    }
    ,
    set_Box: function(value) {
        this._box = value;
    },
    SetPosstion: function() {
        var box = $get(this._box);
        var value = this.GetScroll();

        box.style.top = value + "px";
        var left = document.body.clientWidth - 200;
        box.style.left = left + "px";
    },
    DeleteProduct: function(pid) {

        var cValue = this.cookie.getCookie("shopCar");

        var items = cValue.split('*');
        var deleteValue = "";
        for (var i = 0; i < items.length; i++) {
            var v = items[i].split('$');
            if (v[0] != undefined) {
                if (v[0] == pid) {
                    deleteValue = items[i] + '*';
                }
            }
        }
        cValue = cValue.replace(deleteValue, "");
        this.cookie.setCookie("shopCar", cValue, 200);
        this.Render();
    },
    ClearProduct: function() {
        this.cookie.setCookie("shopCar", "", 200);

        this.Render();
    },
    AddProduct: function(pid, pName, number, price, color, remark) {

        if (color == undefined) {
            color = "";
        }

        if (remark == undefined) {
            remark = "";
        }
        var cValue = this.cookie.getCookie("shopCar");
        var items = cValue.split('*');
        var addIndex;
        for (var i = 0; i < items.length; i++) {
            var v = items[i].split('$');
            if (v[0] != undefined) {
                if (v[0] == pid) {
                    addIndex = i;
                }
            }
        }
        if (addIndex == undefined) {
            cValue += pid + "$" + pName + "$" + number + "$" + price + "$" + color + "$" + remark + "*";

        }
        else {
            var v = items[addIndex].split('$');
            var oldString = pid + "$" + pName + "$" + v[2] + "$" + price + "$" + color + "$" + remark + "*";
            var newNumber = Number(v[2]) + 1;
            var newString = pid + "$" + pName + "$" + newNumber + "$" + price + "$" + color + "$" + remark + "*";
            cValue = cValue.replace(oldString, newString);
        }
        this.cookie.setCookie("shopCar", cValue, 200);
        this.Render();
    },
    NumberInputChange: function(e) {

        var index = e.id.replace("carNumber", "");

        var cValue = this.cookie.getCookie("shopCar");
        var items = cValue.split('*');
        var v = items[index].split('$');
        var oldString = v[0] + "$" + v[1] + "$" + v[2];
        var newNumber = Number(v[2]) + 1;
        var newString = v[0] + "$" + v[1] + "$" + e.value;
        cValue = cValue.replace(oldString, newString);
        this.cookie.setCookie("shopCar", cValue, 200);
    },
    Render: function() {
        var box = $get(this._box);
        var renderValue = "";
        var cValue = this.cookie.getCookie("shopCar");
        var itemsString = cValue.split('*');
        var deleteValue = "";


        for (var i = 0; i < itemsString.length; i++) {
            var v = itemsString[i].split('$');
            if (v[1] != undefined) {
                renderValue += "<div class=\"carItem\">";
                renderValue += "<a class=\"proName\" href=\"" + this._productPage + "?id=" + v[0] + "\">" + v[1] + "</a>";
                renderValue += "<input id=\"carNumber" + i + "\" onKeypress=\"return (/[\\d.]/.test(String.fromCharCode(event.keyCode)))\" style=\"ime-mode:Disabled\" class=\"proNumber\" type=\"text\" value=\"" + v[2] + "\" maxlength=\"3\" onchange=\"" + this._meID + ".NumberInputChange(this);\" />";
                renderValue += "<a class=\"proDel\" href=\"#\" onclick=\"" + this._meID + ".DeleteProduct(" + v[0] + ");\">删除</a>";
                renderValue += "</div>";
            }

        }
        if (renderValue != "") {
            box.style.display = "block";

        }
        else {
            box.style.display = "none";
        }
        this._element.innerHTML = renderValue;
    },
    GetScroll: function() {

        var t, l, w, h;

        if (document.documentElement && document.documentElement.scrollTop) {

            t = document.documentElement.scrollTop;

            l = document.documentElement.scrollLeft;

            w = document.documentElement.scrollWidth;

            h = document.documentElement.scrollHeight;

        } else if (document.body) {

            t = document.body.scrollTop;

            l = document.body.scrollLeft;

            w = document.body.scrollWidth;

            h = document.body.scrollHeight;

        }

        return t;

    },

    dispose: function() {
        //在此处添加自定义释放操作
        Soft202.ShopCar.callBaseMethod(this, 'dispose');
    }
}
Soft202.ShopCar.registerClass('Soft202.ShopCar', Sys.UI.Behavior);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
