/********************************************
* 模块名称：jquery-extend
* 功能说明：jQuery扩展
* 创 建 人：邵先军
* 创建时间：2009-12-06
* ******************************************/

// 控件层级
jQuery.zIndex = 10000;

// 扩展jQuery对象
jQuery.extend({

    // 打开新窗口
    openWindow: function(width, height, url) {
        var top = (window.screen.height - height) / 2;
        var left = (window.screen.width - width) / 2;
        return window.open(url, "", "top=" + top + ", left=" + left + ", height=" + height + ", width=" + width + ",toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no");
    },

    // 打开模态窗体
    openDialog: function(width, height, url, para) {
        return window.showModalDialog(url, para, "dialogHeight:" + height + "px; dialogWidth:" + width + "px; center: yes; scroll: no; resizable: no; status: no;");
    },

    // 定时关闭窗体
    setTimeout: function(millisecond) {
        setTimeout("window.close()", millisecond);
        //setTimeout("self.close()", millisecond);
    },

    // 定时跳转到其它页面
    setTimeoutAndReturn: function(url, millisecond) {
        setTimeout("window.location = '" + url + "'", millisecond)
    },

    // 跳转到其它页面
    returnUrl: function(url) {
        window.location = url;
    },

    //获取Url参数
    getUrlPara: function(name) {
        var result = '';
        var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");

        if (reg.test(location.href)) {
            result = unescape(RegExp.$2.replace(/\+/g, " "));
        }
        return result;
    },
    
    // 产生随数
    S4: function() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    },

    // 产生GUID
    newGuid: function() {
        return (this.S4() + this.S4() + "-" + this.S4() + "-" + this.S4() + "-" + this.S4() + "-" + this.S4() + this.S4() + this.S4());
    },

    // 获取页面宽度、页面高度、窗口宽度、窗口高度
    getPageSize: function() {
        var scrW, scrH;
        if (window.innerHeight && window.scrollMaxY) {
            // Mozilla
            scrW = window.innerWidth + window.scrollMaxX;
            scrH = window.innerHeight + window.scrollMaxY;
        } else if (document.body.scrollHeight > document.body.offsetHeight) {
            // all but IE Mac    
            scrW = document.body.scrollWidth;
            scrH = document.body.scrollHeight;
        } else if (document.body) {
            // IE Mac
            scrW = document.body.offsetWidth;
            scrH = document.body.offsetHeight;
        }

        var winW, winH;
        if (window.innerHeight) {
            // all except IE
            winW = window.innerWidth;
            winH = window.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) {
            // IE 6 Strict Mode
            winW = document.documentElement.clientWidth;
            winH = document.documentElement.clientHeight;
        } else if (document.body) {
            // other    
            winW = document.body.clientWidth;
            winH = document.body.clientHeight;
        }

        // for small pages with total size less then the viewport  
        var pageW = (scrW < winW) ? winW : scrW;
        var pageH = (scrH < winH) ? winH : scrH;

        return { PageW: pageW, PageH: pageH, WinW: winW, WinH: winH };
    },

    // 获取滚动条水平位置和滚动条垂直位置
    getPageScroll: function() {
        var x, y;
        if (window.pageYOffset) {
            // all except IE
            y = window.pageYOffset;
            x = window.pageXOffset;
        } else if (document.documentElement && document.documentElement.scrollTop) {
            // IE 6 Strict
            y = document.documentElement.scrollTop;
            x = document.documentElement.scrollLeft;
        } else if (document.body) {
            // all other IE
            y = document.body.scrollTop;
            x = document.body.scrollLeft;
        }

        return { X: x, Y: y };
    },

    // 获取鼠标水平和垂直位置
    pointer: function() {
        return {
            X: event.pageX || (event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft)),
            Y: event.pageY || (event.clientY + (document.documentElement.scrollTop || document.body.scrollTop))
        };
    },

    // 获取控件左上坐标
    getPositionLT: function(o) {
        var offset = o.offset();
        return { X: offset.left, Y: offset.top };
    },

    // 获取控件左下坐标
    getPositionLB: function(o) {
        var offset = o.offset();
        return { X: offset.left, Y: offset.top + o.height() };
    },

    // 获取控件右上坐标
    getPositionRT: function(o) {
        var offset = o.offset();
        return { X: offset.left + o.width(), Y: offset.top };
    },

    // 获取控件右下坐标
    getPositionRB: function(o) {
        var offset = o.offset();
        return { X: offset.left + o.width(), Y: offset.top + o.height() };
    },

    // 获取窗口居中显示位置
    getCenterPosition: function(width, height) {
        var pageSize = this.getPageSize();
        var pageScroll = this.getPageScroll();
        var x = 0, y = 0;

        if (height != undefined) {
            x = (pageSize.WinW - width) / 2 + pageScroll.X;
        }

        if (width != undefined) {
            y = (pageSize.WinH - height) / 2 + pageScroll.Y;
        }

        return { X: x, Y: y };
    }
});

// tips 控件函数
jQuery.tips = function() {

    return {
        show: function(o, w, m) {
            
            if (o != null && o != undefined) {
                this.oGuid = 'jQuery.zoshow.tips';
                this.oElem = o;
                this.oWidth = w;
                this.oMessage = m;
                
                //格式化Tips宽度
                if (w == null || w < 160) {
                    this.oWidth = 160;
                }

                //获取控件的左上坐标
                this.oPositionLT = jQuery.getPositionLT($(this.oElem));
                
                //创建Tips
                this.createTips();
            }
            else {
                return false;
            }
        },
        createTips: function() {
            var html = this.renderTips();
           
            var oldDiv = document.getElementById(this.oGuid);

            if (oldDiv != null && oldDiv != undefined) {
                document.body.removeChild(oldDiv);
            }

            var newDiv = document.createElement('div');
            newDiv.id = this.oGuid;
            newDiv.style.top = parseInt(this.oPositionLT.Y) + 'px';
            newDiv.style.left = this.oPositionLT.X + Math.ceil(this.oElem.offsetWidth / 2) - Math.ceil(this.oWidth / 2) + (Math.ceil(this.oWidth / 2) - (40 + 7)) + 'px';
            newDiv.style.position = 'absolute';
            newDiv.style.zIndex = (jQuery.zIndex += 1);
            newDiv.innerHTML = html;
            document.body.appendChild(newDiv);
                
            // 设置Tips最终显示位置
            newDiv.style.top = newDiv.style.top.substring(0, newDiv.style.top.length - 2) - newDiv.offsetHeight + 'px'; 
        },
        renderTips: function() {
            return '<div class="openWin" style="width: ' + this.oWidth + 'px;*margin-left:' + (this.oWidth-160) + 'px!important;ie7;">'+
                       '<div class="topOW">'+
                           '<span></span><em></em>'+
                       '</div>'+
                       '<div class="cOW">'+ this.oMessage + '</div>'+
                       '<div class="botOW">'+
                           '<span></span><em></em>'+
                       '</div>'+
                   '</div>'
        },
        close: function() {
            var elem = document.getElementById(this.oGuid);
            if (elem != null && elem.parentNode != null) {
                elem.parentNode.removeChild(elem);
            }
        }
    }
} ();

// pops 控件函数
jQuery.pops = function() {

    return {
        show: function(o, w, m) {
            
            if (o != null && o != undefined) {
                this.oGuid = 'jQuery.zoshow.tips';
                this.oElem = o;
                this.oWidth = w;
                this.oMessage = m;
                
                //格式化Tips宽度
                if (w == null || w < 160) {
                    this.oWidth = 160;
                }

                //获取控件的左上坐标
                this.oPositionLT = jQuery.getPositionLT($(this.oElem));
                
                //创建Tips
                this.createPops();
            }
            else {
                return false;
            }
        },
        createPops: function() {
            var html = this.renderPops();

            var newDiv = document.createElement('div');
            newDiv.style.top = parseInt(this.oPositionLT.Y) + 'px';
            newDiv.style.left = this.oPositionLT.X + Math.ceil(this.oElem.offsetWidth / 2) - Math.ceil(this.oWidth / 2) + (Math.ceil(this.oWidth / 2) - (40 + 7)) + 'px';
            newDiv.style.position = 'absolute';
            newDiv.style.zIndex = (jQuery.zIndex += 1);
            newDiv.innerHTML = html;
            document.body.appendChild(newDiv);
                
            // 设置Tips最终显示位置
            newDiv.style.top = newDiv.style.top.substring(0, newDiv.style.top.length - 2) - newDiv.offsetHeight + 'px'; 
        },
        renderPops: function() {
            return '<div class="openWin" style="width: ' + this.oWidth + 'px;">'+
                       '<div class="topOW">'+
                           '<span></span><em></em>'+
                       '</div>'+
                       '<div class="cOW">'+ this.oMessage + '</div>'+
                       '<div class="botOW">'+
                           '<span></span><em></em>'+
                       '</div>'+
                   '</div>'
        }
    }
} ();

// confirm 控件函数
jQuery.confirm = function() {
  
    return {
        show: function(o, w, u, m) {
            
            if (o != null && o != undefined) {
                this.oGuid = 'jQuery.zoshow.confirm';
                this.oElem = o;
                this.oWidth = w;
                this.oUrl = u;
                this.oMessage = m;
                
                //格式化Tips宽度
                if (w == null || w < 160) {
                    this.oWidth = 160;
                }

                this.lWidth = Math.ceil((this.oWidth - 19) / 2);
                this.rWidth = this.oWidth - 19 - this.lWidth;

                //获取控件的左上坐标
                this.oPositionLT = jQuery.getPositionLT($(this.oElem));
                
                //创建Confirm
                this.createConfirm();
            }
            else {
                return false;
            }
        },
        createConfirm: function() {
            var html = this.renderConfirm();
           
            var oldDiv = document.getElementById(this.oGuid);

            if (oldDiv != null && oldDiv != undefined) {
                document.body.removeChild(oldDiv);
            }

            var newDiv = document.createElement('div');
            newDiv.id = this.oGuid;
            newDiv.style.top = parseInt(this.oPositionLT.Y) + 'px';
            newDiv.style.left = this.oPositionLT.X + Math.ceil(this.oElem.offsetWidth / 2) - Math.ceil(this.oWidth / 2) + (Math.ceil(this.oWidth / 2) - (40 + 7)) + 'px';
            newDiv.style.position = 'absolute';
            newDiv.style.zIndex = (jQuery.zIndex += 1);
            newDiv.innerHTML = html;
            document.body.appendChild(newDiv);
                
            // 设置Confirm最终显示位置
            newDiv.style.top = newDiv.style.top.substring(0, newDiv.style.top.length - 2) - newDiv.offsetHeight + 'px'; 
        },
        renderConfirm: function() {
            return '<div class="openWin" style="width: ' + this.oWidth + 'px;*margin-left:' + (this.oWidth-160) + 'px!important;ie7;">'+
                       '<div class="topOW">'+
                           '<span></span><em></em>'+
                       '</div>'+
                       '<div class="cOW">'+ this.oMessage + '<br /><a href="'+this.oUrl+'" class="padBoth">确定</a><a onclick="jQuery.confirm.close();" class="padBoth"   style="cursor:pointer;">取消</a></div>'+
                       '<div class="botOW">'+
                           '<span></span><em></em>'+
                       '</div>'+
                   '</div>'
        },
        close: function() {
            var elem = document.getElementById(this.oGuid);
            if (elem != null && elem.parentNode != null) {
                elem.parentNode.removeChild(elem);
            }
        }
    }
} ();

// cart 控件函数
jQuery.cart = function() {

    return {
        show: function(o, w, m) {
            
            if (o != null && o != undefined) {
                this.oGuid1 = 'jQuery.zoshow.cartTitle';
                this.oGuid2 = 'jQuery.zoshow.cartBody';
                this.oElem = o;
                this.oWidth = w;
                this.tWidth = 292;
                this.oMessage = m;
                
                //格式化Tips宽度
                if (w == null || w < 80) {
                    this.oWidth = 80;
                }

                //获取控件的左上坐标
                this.oPositionLT = jQuery.getPositionLT($(this.oElem));
                
                //创建Title
                this.createTitle();
                
                //创建Title
                this.createBody();
            }
            else {
                return false;
            }
        },
        createTitle: function() {
            var html = this.renderTitle();
           
            var oldDiv = document.getElementById(this.oGuid1);

            if (oldDiv != null && oldDiv != undefined) {
                document.body.removeChild(oldDiv);
            }

            var newDiv = document.createElement('div');
            newDiv.id = this.oGuid1;
            newDiv.style.top = parseInt(this.oPositionLT.Y) + 'px';
            newDiv.style.left = this.oPositionLT.X + 'px';
            newDiv.style.position = 'absolute';
            newDiv.style.zIndex = (jQuery.zIndex += 1);
            newDiv.innerHTML = html;
            document.body.appendChild(newDiv);
        },
        createBody: function() {
            var html = this.renderBody();

            var oldDiv = document.getElementById(this.oGuid2);

            if (oldDiv != null && oldDiv != undefined) {
                document.body.removeChild(oldDiv);
            }

            var oTitle = document.getElementById(this.oGuid1);

            var newDiv = document.createElement('div');
            newDiv.id = this.oGuid2;
            newDiv.style.top = parseInt(this.oPositionLT.Y + oTitle.offsetHeight) + 'px';
            newDiv.style.left = this.oPositionLT.X - Math.ceil((this.tWidth - oTitle.offsetWidth) / 2) + 'px';
            newDiv.style.position = 'absolute';
            newDiv.style.zIndex = (jQuery.zIndex -= 1);
            newDiv.innerHTML = html;
            document.body.appendChild(newDiv);
        },
        renderTitle: function() {
            return '<table border="0" cellspacing="0" cellpadding="0" class="winli">'+
                       '<tr>'+
                           '<td class="winli_td1">&nbsp;</td>'+
                           '<td class="winli_td2" id="pkgtitle" onclick="location=\'http://buy.xiu.com/user.php?act=flow\';" style="cursor:pointer;">购物袋(<strong>'+ this.oMessage + '</strong>)</td>'+
                           '<td class="winli_td3">&nbsp;</td>'+
                       '</tr>'+
                   '</table>'
        },
        renderBody: function() {
            return '<div class="winbox" id="pop_cart">'+
                       '<div class="twb"></div>'+
                       '<div class="cwb" id="pkgtext"><center><img src="http://images.xiu.com/popup/ajax-loader14-20.gif" border="0" /></center>'+
                       '</div>'+
                       '<div class="fwb"></div>'+
                   '</div>'
        },
        close: function() {
            var elem = document.getElementById(this.oGuid1);
            if (elem != null && elem.parentNode != null) {
                elem.parentNode.removeChild(elem);
            }
            
            var elem = document.getElementById(this.oGuid2);
            if (elem != null && elem.parentNode != null) {
                elem.parentNode.removeChild(elem);
            }
        }
    }
} ();

//我的走秀控件函数
jQuery.myxiu = function() {

    return {
        show: function(o, w, m) {
            jQuery.cart.close();
            if (o != null && o != undefined) {
                this.oGuid1 = 'jQuery.zoshow.myxiuTitle';
                this.oGuid2 = 'jQuery.zoshow.myxiuBody';
                this.oElem = o;
                this.oWidth = w;
                this.tWidth = 292;
                this.oMessage = m;
                
                //格式化Tips宽度
                if (w == null || w < 80) {
                    this.oWidth = 80;
                }

                //获取控件的左上坐标
                this.oPositionLT = jQuery.getPositionLT($(this.oElem));
                
                //创建Title
                //this.createTitle();
                
                //创建body
                this.createBody();
            }
            else {
                return false;
            }
        },
        createTitle: function() {
            var html = this.renderTitle();
           
            var oldDiv = document.getElementById(this.oGuid1);

            if (oldDiv != null && oldDiv != undefined) {
                document.body.removeChild(oldDiv);
            }

            var newDiv = document.createElement('div');
            newDiv.id = this.oGuid1;
            newDiv.style.top = parseInt(this.oPositionLT.Y) + 'px';
            newDiv.style.left = this.oPositionLT.X + 'px';
            newDiv.style.position = 'absolute';
            newDiv.style.zIndex = (jQuery.zIndex += 1);
            newDiv.innerHTML = html;
            document.body.appendChild(newDiv);
        },
        createBody: function() {
            var html = this.renderBody();

            var oldDiv = document.getElementById(this.oGuid2);

            if (oldDiv != null && oldDiv != undefined) {
                document.body.removeChild(oldDiv);
            }

            var oTitle = document.getElementById(this.oGuid1);

            var newDiv = document.createElement('div');
            newDiv.id = this.oGuid2;
            newDiv.style.top = parseInt(this.oPositionLT.Y ) - 5 + 'px';
            newDiv.style.left = parseInt(this.oPositionLT.X) - 5 + 'px';
            newDiv.style.position = 'absolute';
            newDiv.style.zIndex = (jQuery.zIndex -= 1);
            newDiv.innerHTML = html;
            document.body.appendChild(newDiv);
        },
        renderTitle: function() {
            return '<table border="0" cellspacing="0" cellpadding="0" class="winli">'+
                       '<tr>'+
                           '<td class="winli_td1">&nbsp;</td>'+
                           '<td class="winli_td2" id="pkgtitle" onclick="location=\'http://buy.xiu.com/user.php?act=flow\';" style="cursor:pointer;">购物袋(<strong>'+ this.oMessage + '</strong>)</td>'+
                           '<td class="winli_td3">&nbsp;</td>'+
                       '</tr>'+
                   '</table>'
        },
        renderBody: function() {
			return  '<div class="winbox2" id="winbox2" >' + 
					'<h3><a href="http://acc.xiu.com">我的走秀</a></h3>' + 
					'<ul>' +
					'<li><a href="http://acc.xiu.com/?f=myorder" >我的订单</a></li>' +
					'<li><a href="http://acc.xiu.com/?f=collection">我的收藏夹</a></li>' + 
					'</ul>' + 
					'</div>';
        },
        close: function() {
			/*
            var elem = document.getElementById(this.oGuid1);
            if (elem != null && elem.parentNode != null) {
                elem.parentNode.removeChild(elem);
            }
            */
            var elem = document.getElementById(this.oGuid2);
            if (elem != null && elem.parentNode != null) {
                elem.parentNode.removeChild(elem);
            }
        }
    }
} ();