﻿

/// <reference path="../MsAjax/MicrosoftAjax.js" />

//202软件工程有限公司  
//魏建 2009年5月12日
Type.registerNamespace("Soft202");
Soft202.CompareProduct = function(element) {
    Soft202.CompareProduct.initializeBase(this, [element]);
    this.cookie = new Cookie();
    this._setPosstionDelegate = null;
    this._box;
    this._meID;
}

Soft202.CompareProduct.prototype = {
    initialize: function() {
        Soft202.CompareProduct.callBaseMethod(this, 'initialize');
        if (this._setPosstionDelegate == null) {
            this._setPosstionDelegate = Function.createDelegate(this, this.SetPosstion);
        }


        var box = $get(this._box);

        box.style.position = "absolute";

        box.style.top = "300px";
        var left = document.body.clientWidth - 200;
        box.style.left = left + "px";

        g_myBodyInstance = (document.documentElement ? document.documentElement : window);
        g_myBodyInstance.onscroll = this._setPosstionDelegate;
        window.onresize = this._setPosstionDelegate;
        if (Sys.Browser.name == "Firefox") {
            setInterval(this.SetPosstion, 100);
        }
        this.RenderItems();
        // 在此处添加自定义初始化
    },
    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;
    },
    AddProduct: function(id, name, img, price) {


        var cValue = this.cookie.getCookie("Compare");
        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] == id) {
                    addIndex = i;
                }
            }
        }
        if (addIndex == undefined) {
            cValue += id + "$" + name + "$" + img + "$" + price + "*";

        }

        this.cookie.setCookie("Compare", cValue, 200);
        this.RenderItems();
    },
    DeleteProdcut: function(id) {
        var cValue = this.cookie.getCookie("Compare");

        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] == id) {
                    deleteValue = items[i] + '*';
                }
            }
        }
        cValue = cValue.replace(deleteValue, "");
        this.cookie.setCookie("Compare", cValue, 200);
        this.RenderItems();

    },
    ClearProduct: function() {

        this.cookie.setCookie("Compare", "", 200);

        this.RenderItems();
    },
    RenderItems: function() {
        var box = $get(this._box);
        var renderValue = "";
        var cValue = this.cookie.getCookie("Compare");
        var itemsString = cValue.split('*');
        var deleteValue = "";


        for (var i = 0; i < itemsString.length; i++) {
            var v = itemsString[i].split('$');
            if (v[1] != undefined) {
                renderValue += this.RenderItem(v[0], v[1], v[2], v[3]);
            }

        }
        if (renderValue != "") {
            box.style.display = "block";

        }
        else {
            box.style.display = "none";
        }
        this._element.innerHTML = renderValue;
    }
    ,

    RenderItem: function(id, name, img, price) {
        var item = "<div class=\"img\">";
        item += "<ul>";
        item += "<li class=\"l1\"><img alt=\"" + name + "\" src=\"" + img + "\"/></li>";
        item += "<li class=\"l2 overflowH\">" + name + "</li>";
        item += "<li class=\"l2\">";
        item += "<input type=\"button\" value=\"删除\" class=\"i1\" onclick=\"" + this._meID + ".DeleteProdcut(" + id + ")\"/></li>";
        item += "</ul>";
        item += "</div>";
        return item;
    }
    ,
    Compare: function() {
        var paramers = "";
        var cValue = this.cookie.getCookie("Compare");
        var itemsString = cValue.split('*');
        for (var i = 0; i < itemsString.length; i++) {
            var v = itemsString[i].split('$');
            if (v[1] != undefined) {
                paramers += v[0] + ",";
            }

        }
        window.location.href = "Compare.aspx?Products=" + paramers;
    },
    SetPosstion: function() {
        var box = $get(this._box);
        var value =this. GetScroll();
        value += 300;
        box.style.top = value + "px";
        var left = document.body.clientWidth - 200;
        box.style.left = left + "px";
    },


    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.CompareProduct.callBaseMethod(this, 'dispose');
    }

}
Soft202.CompareProduct.registerClass('Soft202.CompareProduct', Sys.UI.Behavior);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
