﻿
/**
* Masque JS Library 0.0.1.0
* Copyright(c) 2008, 青岛领创网络科技有限公司
* @author Lookingon Team
*/
if (window.Masque)
    _Masque = window.Masque;
var Masque = window.Masque = {
    version: '0.0.1',
    supportW3C: (document.getElementById) ? true : false,
    isOpera: window.opera && navigator.userAgent.match(/opera/gi) ? true : false,
    isIE: !this.isOpera && document.all && navigator.userAgent.match(/msie/gi) ? true : false,
    isIE5: (this.supportW3C && this.isIE) ? true : false,
    isIE6: navigator.userAgent.toLowerCase().indexOf('msie 6') > -1,
    isNS: (navigator.appName == "Netscape") ? true : false,
    isSafari: !this.isIE && navigator.userAgent.match(/safari/gi) ? true : false,
    isGecko: !this.isIE && navigator.userAgent.match(/gecko/gi) ? true : false,
    isFirefox: !this.isIE && navigator.userAgent.match(/firefox/gi) ? true : false,
    isFF3: !this.isIE && navigator.userAgent.match(/Firefox\/3/gi) ? true : false,
    $Temp: {},
    V: {
        isElement: function(object) {
            return object && object.nodeType == 1;
        },
        isFunction: function(object) {
            return typeof object == "function";
        },

        isString: function(object) {
            return typeof object == "string";
        },

        isNumber: function(object) {
            return typeof object == "number";
        },

        isUndefined: function(object) {
            return typeof object == "undefined";
        },
        isObject: function(object) {
            return typeof object == "object";
        }
    },
    TR: {
        GetValue: function(_result, _name) {
            if (_result == null || typeof (_result) != "string")
                return "";
            if (_name == null || typeof (_name) != "string")
                return "";
            if (_result.length > _name.length + 8) {
                if (_result.indexOf("<!--" + _name + ":") != -1) {
                    var startIndex = _result.indexOf("<!--" + _name + ":") + _name.length + 5;
                    return _result.substring(startIndex, _result.indexOf("-->", startIndex));
                }
            }
            return "";
        }
    },
    URL: {
        noAnchorTopLocation: function() {
            return window.location.href.replace(window.location.hash, '')
        },
        getQueryString: function(name) {
            var url;
            if (arguments.length > 1) {
                url = arguments[1];
            }
            else {
                url = location + "";
            }
            var reg = new RegExp("(^|&|\\?)" + name + "=([^&]*)(&|$)", 'i');
            var r = url.match(reg);
            if (r != null)
                return unescape(r[2]);
            return null;

        }

    }

};

Object.extend = function(destination, source) {
    for (var property in source)
        destination[property] = source[property];
    return destination;
};

var Class = function() {
    var _class = function() {

        return Object.extend(this, _class).initialize.apply(this, arguments);
    };
    for (i = 0; i < arguments.length; i++) {
        superClass = arguments[i];
        _class.extend(superClass.prototype);
    }
    Object.extend(_class, {
        child: function() {
            return new Class(this);
        },
        initialize: function(options) {
            this.SetOption(options);
        },
        extend: function(f) {
            Object.extend(this, f);
        },
        option: {},
        SetOption: function(options) {
            Object.extend(this.option, options || {});
        }
    });
    return _class;
};

/************************************************************************/
/* MCElementScroll                                                                     */
/************************************************************************/
if (!Masque.L) {
    Masque.L = {};
}
if (!Masque.L.MoveDirection) {
    Masque.L.MoveDirection = {};
}
Object.extend(Masque.L.MoveDirection, {
    UpDown: 100, //上下滚动
    LeftRight: 101 //左右滚动
});

Masque.L.ElementScrollor = Class();
Masque.L.ElementScrollor.extend({
    option: {
        Speed: 500, //滚动效果时间。int类型
        Interval: 0, //滚动间隔。
        Direction: Masque.L.MoveDirection.LeftRight, //滚动方向， 枚举自Masque.L.MoveDirection。也可以为0-3的整数。
        Roll: false, //是否循环滚动（仿marquee），可以调用SetRoll控制。bool类型
        OneStep: true, //在鼠标移到到容器时，是否自动停止滚动,可以调用SetAutoStop进行控制。bool类型
        PrveCSS: "", //向前滚动按钮可用时的样式。
        PrveDisableCSS: "", //向前滚动按钮不可用时的样式。
        NextCSS: "", //向后滚动按钮不可用时的样式。
        NextDisableCSS: "", //向后滚动按钮不可用时的样式。
        SeparatorWidth: 0 //分隔符宽度
    },
    initialize: function(ScrollBox, PrveButton, NextButton, options) {
        this.ScrollBox = $(ScrollBox);
        this.__ElementBox = this.ScrollBox.children(":first");
        this.PrveButton = $(PrveButton);
        this.NextButton = $(NextButton);
        this.option.PrveCSS = this.PrveButton.attr("class");
        this.option.NextCSS = this.NextButton.attr("class");
        this.SetOption(options);
        this.__bindEvents();

        //左右按钮的状态
        this.__calcButtonState();
    },
    __bindEvents: function() {
        var me = this;
        if (this.option.OneStep) { //只移动一个
            if (this.PrveButton != null)
                this.PrveButton.click(function() { me.__MovePrve(); });
            if (this.NextButton != null)
                this.NextButton.click(function() { me.__MoveNext(); });
        }
        else { //连续移动
            if (this.PrveButton != null) {
                this.PrveButton.mousedown(me.__RollMovePrve);
                this.PrveButton.mouseup(me.__StopRollMovePrve);
            }
            if (this.NextButton != null) {
                this.NextButton.mousedown(me.__RollMoveNext);
                this.NextButton.mouseup(me.__RollMovePrve);
            }
        }
    },
    __calcButtonState: function() {
        if (this.__CanMoveNext()) {
            this.NextButton.attr("class", this.option.NextCSS);
        } else {
            this.NextButton.attr("class", this.option.NextDisableCSS);
        }
        if (this.__CanMovePrve()) {
            this.PrveButton.attr("class", this.option.PrveCSS);
        } else {
            this.PrveButton.attr("class", this.option.PrveDisableCSS);
        }
    },
    __MoveList: new Array(),
    __MovePrve: function() {
        this.__MoveList.push(1);
        this.__DoMove();
    },
    __IsMoveing: false,
    __DoMove: function() {
        if (!this.__IsMoveing) {
            if (this.__MoveList.length > 0) {
                this.__IsMoveing = true;
                var d = this.__MoveList.pop();
                if (d > 0) {
                    this.____MovePrve();
                } else if (d < 0) {
                    this.____MoveNext();
                }
            }
        }
    },
    ____MovePrve: function() {
        if (this.__CanMovePrve()) {
            var me = this;
            var _mleft = 0;
            var _moveStep = 0;
            if (this.option.Direction == Masque.L.MoveDirection.UpDown) {
                _mleft = this.__ElementBox.css("top").replace("px", "");
                _moveStep = this.__ElementBox.children("[ishidden!='true']:first").attr("scrollWidth");
            }
            else if (this.option.Direction == Masque.L.MoveDirection.LeftRight) {
                _mleft = this.__ElementBox.css("left").replace("px", "");
                _moveStep = this.__ElementBox.children("[ishidden!='true']:first").attr("scrollWidth");
            }
            if (isNaN(_mleft))
                _mleft = 0;
            this.__ElementBox.animate({ left: _mleft - _moveStep - this.option.SeparatorWidth }, me.option.Speed, function() {
                me.__ElementBox.children("[ishidden!='true']:first").attr("ishidden", "true");
                me.__calcButtonState();
                me.__IsMoveing = false;
                me.__DoMove();
            });
        } else {
            this.__IsMoveing = false;

            this.__calcButtonState();
        }
    },
    __CanMoveNext: function() {
        var _mleft = 0;
        var _boxWidth = 0;
        var _contentWidth = 0;
        if (this.option.Direction == Masque.L.MoveDirection.UpDown) {
            _mleft = this.__ElementBox.css("top").replace("px", "");
            //            _boxWidth = this.ScrollBox.outerHeight(false);
            //            _contentWidth = this.__ElementBox.outerHeight(false);
        }
        else if (this.option.Direction == Masque.L.MoveDirection.LeftRight) {
            _mleft = this.__ElementBox.css("left").replace("px", "");
            //            _boxWidth = this.ScrollBox.outerWidth(false);
            //            _contentWidth = this.__ElementBox.outerWidth(false);
        }
        if (isNaN(_mleft))
            _mleft = 0;
        return _mleft < 0;
    },
    __CanMovePrve: function() {
        var _mleft = 0;
        var _boxWidth = 0;
        var _contentWidth = 0;
        if (this.option.Direction == Masque.L.MoveDirection.UpDown) {
            _mleft = this.__ElementBox.css("top").replace("px", "");
            _boxWidth = this.ScrollBox.outerHeight(false);
            _contentWidth = this.__ElementBox.outerHeight(false);
        }
        else if (this.option.Direction == Masque.L.MoveDirection.LeftRight) {
            _mleft = this.__ElementBox.css("left").replace("px", "");
            _boxWidth = this.ScrollBox.attr("clientWidth");
            _contentWidth = this.__ElementBox.attr("scrollWidth");
        }
        if (isNaN(_mleft))
            _mleft = 0;

        return Math.abs(_mleft) < _contentWidth - _boxWidth;
    },
    __MoveNext: function() {
        this.__MoveList.push(-1);
        this.__DoMove();
    },
    ____MoveNext: function() {

        if (this.__CanMoveNext()) {
            var me = this;
            var _mleft = 0;
            var _moveStep = 0;
            if (this.option.Direction == Masque.L.MoveDirection.UpDown) {
                _mleft = parseInt(this.__ElementBox.css("top").replace("px", ""));
                _moveStep = this.__ElementBox.children("[ishidden='true']:last").outerHeight(false);
            }
            else if (this.option.Direction == Masque.L.MoveDirection.LeftRight) {
                _mleft = parseInt(this.__ElementBox.css("left").replace("px", ""));
                _moveStep = this.__ElementBox.children("[ishidden='true']:last").outerWidth(false);
            }
            if (isNaN(_mleft))
                _mleft = 0;
            this.__ElementBox.animate({ left: _mleft + _moveStep + this.option.SeparatorWidth }, me.option.Speed, function() {
                me.__ElementBox.children("[ishidden='true']:last").attr("ishidden", "false");
                me.__calcButtonState();
                me.__IsMoveing = false;
                me.__DoMove();
            });
        } else {
            this.__IsMoveing = false;

            this.__calcButtonState();
        }

    },
    __RollMovePrve: function() {
    },
    __StopRollMovePrve: function() {
    },
    __RollMoveNext: function() {
    },
    __StopRollMoveNext: function() {
    }
});

/************************************************************************/
/* MCImgRating                                                                     */
/************************************************************************/
if (!Masque.L) {
    Masque.L = {};
}

Masque.L.ImageRating = Class();
Masque.L.ImageRating.extend({
    initialize: function(RatingBox, options) {
        this.option = {
            Filter: "img", //图片筛选器
            LightImage: "/images/Rating1.jpg", //变亮时的图片。
            DarkImage: "/images/Rating0.jpg", //变暗时的图片
            Outer: "",
            DefaultText: "",
            Event: "mouseover",
            TitleFormat: "",
            Reselect: true,
            OnSelect: null,
            SelectedIndex: -1,
            IsApprise: false
        };
        this.RatingBox = $(RatingBox);
        this.option.DarkImage = this.RatingBox.find(this.option.Filter).eq(0).attr("src");
        this.SetOption(options);
        if (!this.option.DefaultText || this.option.DefaultText == "") {
            if (this.option.Outer) {
                this.option.DefaultText = $(this.option.Outer).html();
            }
        }

        this.__bindEvents();
        this.__setState();
        if (this.option.SelectedIndex > -1) {
            var i = this.option.SelectedIndex;
            this.option.SelectedIndex = -1;
            this.____Hover(i);
            this.option.SelectedIndex = i;
        }
    },
    __bindEvents: function(first) {
        var event;
        var me = this;
        var sts = this.RatingBox.find(this.option.Filter);
        if (this.option.Event.toLowerCase() == "mouseover") {
            sts.bind("mouseover", me, me.__EventHover).bind("mouseout", me, me.__EventClearHover);
        }
        sts.bind("click", me, me.__EventSelect);
    },
    __setState: function() {
        var me = this;
        var test = ["非常不满意", "不满意", "一般", "满意", "非常满意"];
        this.RatingBox.find(this.option.Filter).each(function(index, el) {
            var e = $(el);

            if (me.option.TitleFormat != "") {
                if (me.option.IsApprise == "True") {
                    e.attr("title", me.option.TitleFormat.replace('{0}', test[index]));
                }
                else if (me.option.IsApprise == "False") {
                    e.attr("title", me.option.TitleFormat.replace('{0}', (index + 1) + '星'));
                }

                //e.attr("title", me.option.TitleFormat.replace('{0}', index + 1));

            }
            e.attr("src", me.option.DarkImage);
        });
        this.__SetOuter(this.option.DefaultText);

    },
    __EventSelect: function(handler) {

        var rating = handler.data;

        if (rating.option.Reselect != true && rating.option.SelectedIndex >= 0)
            return;
        rating.option.SelectedIndex = -1;
        var index = rating.RatingBox.find(rating.option.Filter).index(this);

        rating.____Hover(index);
        rating.option.SelectedIndex = index;
        if (rating.option.OnSelect != null) {
            rating.option.OnSelect(index, rating);
        }
        if (rating.option.IsApprise) {//评价页面
            rating.RatingBox.parent().attr("style", "width:auto;");
        }

    },
    __SetOuter: function(str) {
        if (this.option.Outer) {
            $(this.option.Outer).html(str);
        }

    },
    ____Hover: function(index) {
        if (this.option.SelectedIndex >= 0)
        { return; }


        if (isNaN(index))
            index = -1;
        var me = this;
        this.RatingBox.find(this.option.Filter).each(function(idx, el) {
            e = $(el);
            if (idx <= index) {
                e.attr("src", me.option.LightImage);
                if (idx == index) {
                    me.__SetOuter(e.attr("title"));
                }
            }
            else
                e.attr("src", me.option.DarkImage);
        });
        if (index < 0) {
            this.__SetOuter(this.option.DefaultText);
        }
    },
    __EventHover: function(handler) {
        var rating = handler.data;
        var index = rating.RatingBox.find(rating.option.Filter).index(this);

        rating.____Hover(index);

        if (rating.option.IsApprise) {//评价页面
            rating.RatingBox.parent().attr("style", "width:auto;");
        }
    },
    __EventClearHover: function(handler) {
        var rating = handler.data;
        rating.____Hover(rating.option.SelectedIndex);
        if (rating.option.SelectedIndex < 0) {
            if (rating.option.IsApprise) {//评价页面
                rating.RatingBox.parent().removeAttr("style");
            }
        }

    }, Hover: function(index) {
        this.____Hovert(index);
    },
    Reset: function(option) {

        this.SelectIndex == -1;
        this.Hover(-1);
    },
    Value: function() {
        return this.option.SelectedIndex + 1;
    }
});
