//**********************************************
//  信息栏组件
//  @author：xingshikang
//  @v1.0  2011-03-18
//  版权归焦点科技所有
//**********************************************
if (!!window.jQuery) {
    (function($) {
        function tipBasket() {
            this.debug = true;
            this.location = "RB";
            this.extend = true;
            this.smallBasket = {
                obj: ".smallBasket",
                url: ""
            };
            this.bigBasket = {
                obj: ".bigBasket",
                url: ""
            }
        };
        $.extend(tipBasket.prototype, {
            init: function() {
                var $this = $(this.basket);
                var style = this._locDiv();
                $(this.basket).css(style);
                $this.bgiframe();
                this._extend();
            },
            _locDiv: function(options) {
                var $this = $(this.basket);
                var style = $.extend({
                    position: 'fixed',
                    zIndex: 95,
                    marginTop: 0,
                    marginLeft: 0,
                    relative: true
                }, options ||
                {});
                var position = {};
                this.location = this.location.toUpperCase();
                if ((new RegExp("RB|RT|LB|LT")).test(this.location)) {
                    switch (this.location.substr(0, 1)) {
                        case "R":{
                            position.left = "100%";
                            position.marginLeft = style.outerWidth != null ? (-style.outerWidth) : (-$this.outerWidth());
                        }
break;
                        case "L":{
                            position.left = "0";
                            position.marginLeft = 0;
                        }
break;
                    }
                    switch (this.location.substr(1, 1)) {
                        case "B":{
                            position.top = "100%";
                            position.marginTop = style.outerHeight != null ? (-style.outerHeight) : (-$this.outerHeight());
                        }
break;
                        case "T":{
                            position.top = "0";
                            position.marginTop = 0;
                        }
break;
                    }
                }
                style = $.extend(style, position);
                if (style.relative && !$this.parent().is('body') && $this.parent().css('position') == 'static') 
                    $this.parent().css('position', 'relative');
                delete style.relative;
                if (style.position == 'fixed' && $.browser.version == '6.0') {
                    var _marginTop = style.marginTop;
                    var _marginLeft = style.marginLeft;					
                    style.marginTop = document.documentElement.scrollTop+_marginTop;
                    style.marginLeft = document.documentElement.scrollLeft+_marginLeft;
                    style.position = 'absolute';
                    $(window).scroll(function() {
                        $this.stop().animate({
                            marginTop: document.documentElement.scrollTop + _marginTop,
                            marginLeft: document.documentElement.scrollLeft + _marginLeft
                        }, 1);
                    });
                }
                return style;
            },
            _extend: function() {
                var _this = this;
                if (this.extend) {
                    if (!($(_this.bigBasket.obj).length != 0 && _this._contains($(_this.basket).get(0), $(_this.bigBasket.obj).get(0)) && $(_this.smallBasket.obj).length != 0 && _this._contains($(_this.basket).get(0), $(_this.smallBasket.obj).get(0)))) {
                        _this._debug("error:check smallBasket or bigBasket's obj setting!");
                        return;
                    }
                    if ($(_this.smallBasket.obj).is(":visible") && $(_this.bigBasket.obj).is(":visible")) {
                        $(_this.bigBasket.obj).hide();
                        _this._debug("warnning:all baskets are visible!")
                    }
                    var initclass = $(_this.smallBasket.obj).is(":visible") ? "arrow-b" : "arrow-s";
                    if ($.cookie("arrow_sb") == "b") {
                        initclass = "arrow-s";
                    }
                    else if ($.cookie("arrow_sb") == "s") {
                        initclass = "arrow-b";
                    }
                    if ($(_this.basket).find("div[class^='arrow']").length == 0) {
                        if ($(_this.basket).find("#btn_extend").length == 0) {
                            $(_this.basket).append("<div id=\"btn_extend\" class=\"" + initclass + "\"></div>");
                        }
                        else {
                            $(_this.basket).find("#btn_extend").attr("class", initclass);
                        }
                    }
                    else {
                        $(_this.basket).find("div[class^='arrow']").attr("id", "btn_extend").attr("class", initclass);
                    }
                    $(_this.basket).find("#btn_extend").bind("click", function() {
                        if ($(_this.smallBasket.obj).is(":visible")) {
                            $(this).attr("class", "arrow-s");
                            _this.toBasket = _this.bigBasket;
                            _this.fromBasket = _this.smallBasket;
                            arrowCookie = "b";
                        }
                        else {
                            $(this).attr("class", "arrow-b");
                            _this.toBasket = _this.smallBasket;
                            _this.fromBasket = _this.bigBasket;
                            arrowCookie = "s";
                        }
                        jQuery.cookie("arrow_sb", escape(arrowCookie), {
                            path: '/',
                            expires: '-1'
                        });
                        _this._contentChange();
                    });
                }
                else {
                    $(_this.basket).find("div[class^='arrow']").remove();
                }
            },
            _contentChange: function() {
                var _this = this;
                function _switch() {
                    if (!_this.offset) {
                        _this.offset = {};
                        _this.offset.width = $(_this.basket).outerWidth() - $(_this.fromBasket.obj).outerWidth();
                        _this.offset.height = $(_this.basket).outerHeight() - $(_this.fromBasket.obj).outerHeight();
                    }
                    var style = _this._locDiv({
                        outerWidth: $(_this.toBasket.obj).outerWidth() + _this.offset.width,
                        outerHeight: $(_this.toBasket.obj).outerHeight() + _this.offset.height
                    });
                    $(_this.basket).stop().animate({
                        left: style.left,
                        top: style.top,
                        marginLeft: style.marginLeft,
                        marginTop: style.marginTop,
                        height: $(_this.toBasket.obj).outerHeight(),
                        width: $(_this.toBasket.obj).outerWidth()+2
                    }, 200);
                    $(_this.fromBasket.obj).stop().animate({
                        opacity: 'toggle'
                    }, 1);
                    $(_this.toBasket.obj).stop().animate({
                        opacity: 'toggle'
                    }, 1);
                }
                _this.toBasket.url != "" ? (_this._getData(_this.toBasket.url, _switch)) : _switch();
            },
            _getData: function(url, _fn) {
                var _this = this;
                $.ajax({
                    type: "GET",
                    url: url,
                    dataType: "html",
                    success: function(result) {
                        $(_this.toBasket.obj).html(result);
                        _fn.call(_this);
                    },
                    error: function() {
                        _this._debug("error:ajax getdata error!")
                    }
                });
            },
            _contains: function(a, b) {
                return a.contains ? a != b && a.contains(b) : !!(a.compareDocumentPosition(b) & 16);
            },
            _debug: function(error) {
                if (this.debug) {
                    window.console ? (console.log(error)) : (alert(error));
                }
                else {
                    return;
                }
            },
            reSet: function(options) {
                var $this = $(this.basket);
                $.extend(true, this, options);
                this.offset = null;
                $this.css("width", "").css("height", "").css("left","").css("top","").css("marginLeft","").css("marginTop","");
                var style = this._locDiv();
                $(this.basket).css(style);
            }
        });
        $.fn.tipBasket = function(options) {
            var _tipBasket = new tipBasket();
            _tipBasket.basket = this;
            $.extend(true, _tipBasket, options);
            _tipBasket.init();
            return _tipBasket;
        };
        $.tipBasket = {};
        $.tipBasket.version = "1.0.0";
    })(jQuery);
    (function($) {
        $.fn.bgiframe = ($.browser.msie && /msie 6\.0/i.test(navigator.userAgent) ? function(s) {
            s = $.extend({
                top: 'auto',
                left: 'auto',
                width: 'auto',
                height: 'auto',
                opacity: true,
                src: 'javascript:false;'
            }, s);
            var html = '<iframe class="bgiframe"frameborder="0"tabindex="-1"src="' + s.src + '"' + 'style="display:block;position:absolute;z-index:-1;' +
            (s.opacity !== false ? 'filter:Alpha(Opacity=\'0\');' : '') +
            'top:' +
            (s.top == 'auto' ? 'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')' : prop(s.top)) +
            ';' +
            'left:' +
            (s.left == 'auto' ? 'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')' : prop(s.left)) +
            ';' +
            'width:' +
            (s.width == 'auto' ? 'expression(this.parentNode.offsetWidth+\'px\')' : prop(s.width)) +
            ';' +
            'height:' +
            (s.height == 'auto' ? 'expression(this.parentNode.offsetHeight+\'px\')' : prop(s.height)) +
            ';' +
            '"/>';
            return this.each(function() {
                if ($(this).children('iframe.bgiframe').length === 0) 
                    this.insertBefore(document.createElement(html), this.firstChild);
            });
        }
 : function() {
            return this;
        });
        $.fn.bgIframe = $.fn.bgiframe;
        function prop(n) {
            return n && n.constructor === Number ? n + 'px' : n;
        }
    })(jQuery);
}

