//<![CDATA[
JSoul.$(function(){
	//$("@input,type,text|password").initAutoFocus();
    $("#ShowBox").initShowBox();
    $("#HotRecommend").iniScrollBox($("#HotRecommendBar")).initHotItemList();
    $("#HotMenu").initHotMenu($("#HotContent"));
    $("#BannerMenu").initBannerMenu($("#BannerContent"));
    $("#EntryBox").initLoginForm().initLogoutForm();
});
/***JSoul prototype extend Method [Show Box]***/
JSoul.fn.extend({
    /**
     * 初始堂客秀
     * @return JSoul
     */
    initShowBox: function(){
        this.subs = this.find("@a,className,me").each(function(i){
            this.$ = $(this).initShowItem().bindEvent({
                onmouseover: function(){
                    this.$.showIn();
                },
                onmouseout: function(){
                    this.$.delayShowOut();
                }
            });
        });
        return this;
    },
    /**
     * 初始化show动画
     * @return JSoul
     */
    initShowItem: function(){
        if (this.infoBox = this.nextSibling()) {
            this.infoBox.initShowInfo()
        }
        this.initTimeIntervarEvent(50, 0.3).extend({
            start: 50,
            callback: function(selfer){
                var scaler = (selfer.end - selfer.start) / 50;
                var wh = selfer.endWH - (scaler * 30);
                var mg = (90 - wh) / 2;
                selfer.setAlpha(selfer.start);
                selfer.cssForPairs({
                    height: wh + "px",
                    width: wh + "px",
                    marginLeft: mg + "px",
                    marginTop: mg + "px"
                });
            },
            showIn: function(){
                this.clearTimeEvent();
                this.cssForPairs({
                    position: "absolute",
                    zIndex: 9
                });
                this.end = 100;
                this.endWH = 120;
                this.endCallback = function(){
                    this.infoShowIn();
                };
                this.timeIntervarEvent(true);
            },
            showOut: function(){
                this.infoShowOut();
                this.style("zIndex", 5);
                this.end = 50;
                this.endWH = 90;
                this.endCallback = function(){
                    this.style("position", "relative");
                };
                this.timeIntervarEvent(true);
            },
            delayShowOut: function(){
                this.timeOutEvent();
            },
            infoShowIn: function(){
                if (this.infoBox) {
                    this.infoBox.showIn();
                }
            },
            infoShowOut: function(){
                if (this.infoBox) {
                    this.infoBox.showOut();
                }
            }
        }).initTimeOutEvent(200, "showOut");
        return this;
    },
    /**
     * 初始化糖客秀信息
     * @return JSoul
     */
    initShowInfo: function(){
        this.setAlpha(0);
        this.initTimeIntervarEvent(50, 0.4).extend({
            start: 0,
            callback: function(selfer){
                selfer.setAlpha(selfer.start);
            },
            showIn: function(){
                this.show();
                this.end = 100;
                this.endCallback = null;
                this.timeIntervarEvent(true);
            },
            showOut: function(){
                this.end = 0;
                this.endCallback = function(){
                    this.hidden();
                };
                this.timeIntervarEvent(true);
            }
        });
        return this;
    }
});
/***JSoul prototype extend Method [Scroll bar]***/
JSoul.fn.extend({
    /**
     * 初始化定制滚动
     * @param {Object} scrollBar
     * @return JSoul
     */
    iniScrollBox: function(scrollBar){
        this.height = this.attr("offsetHeight");
        this.scrollHeight = this.attr("scrollHeight");
        if (this.scrollHeight > this.height) {
            this.bindScrollBoxEvent(scrollBar);
        }
        else {
            scrollBar.hidden();
        }
        return this;
    },
    /**
     * 绑定滚动事件
     * @param {Object} scrollBar
     * @return JSoul
     */
    bindScrollBoxEvent: function(scrollBar){
        this.bindEvent({
            $: this,
            onscroll: function(){
                this.$.provider.notifyRun(false, null);
            }
        }).extend({
            //得到滚动的比例
            scale: function(){
                return this.attr("scrollTop") / (this.attr("scrollHeight") - this.height);
            },
            //将滚动条距离转换为内容距离
            convert: function(space){
                return (this.attr("scrollHeight") - this.height) * space;
            },
            toScroll: function(space){
                this.clearTimeEvent();
                var scrollSize = this.convert(space);
                this.attr("scrollTop", scrollSize);
                return this;
            },
            callback: function(selfer){
                selfer.attr("scrollTop", selfer.start);
            },
            scrollInOut: function(space){
                this.start = this.attr("scrollTop");
                this.end = Math.ceil(this.convert(space));
                this.timeIntervarEvent(true);
            }
        }).initTimeIntervarEvent(50, 0.5);
        scrollBar.initScrollBar(this);
        this.provider = new $.IProvider(this);
        this.provider.register("scroll.bar", scrollBar, "barCallback");
        this.run("onscroll");
        return this
    },
    /**
     * 初始化滚动条
     * @param {Object} box
     * @return JSoul
     */
    initScrollBar: function(box){
        this.box = box;
        this.bar = this.find("a").bindEvent({
            Main: this
        }).Drag(function(e){
            this.clientY = e.clientY;
            this.marginTop = parseFloat(this.style.marginTop) || 0;
            return true;
        }, function(e){
            var space = (this.marginTop + e.clientY - this.clientY) / this.Main.maxMagin;
            this.Main.box.toScroll(space);
        }, null);
        
        this.bindEvent({
            $: this,
            onclick: function(e){
                var node = JSoul.$eNode(e);
                if (node != this.$.bar.get(0)) {
                    var space = (JSoul.$offsetY(e) - this.$.halfBarHeight) / this.$.maxMagin;
                    this.$.box.scrollInOut(space);
                }
            }
        }).extend({
            start: 255,
            callback: function(selfer){
                selfer.style("backgroundColor", "#FFFF" + selfer.start.toString(16));
            },
            showIn: function(){
                this.clearTimeEvent();
                this.end = 130;
                this.timeIntervarEvent(true);
            },
            showOut: function(){
                this.end = 255;
                this.timeIntervarEvent(true);
            }
        }).initTimeIntervarEvent(50, 0.1).mouseEdge(100, "showOut", "showIn");
        
        this.halfBarHeight = this.bar.attr("offsetHeight") / 2;
        this.maxMagin = box.height - (this.halfBarHeight * 2);
        return this;
    },
    /**
     * 滚动条事件
     * @param {Object} state
     * @param {Object} attchs
     */
    barCallback: function(state, attchs){
        var scrollTop = this.maxMagin * this.box.scale();
        this.bar.style("marginTop", scrollTop + "px");
    }
});
/***JSoul prototype extend Method [Hot Items]***/
JSoul.fn.extend({
    /**
     * 初始化热门推荐内容列表
     * @return JSoul
     */
    initHotItemList: function(){
        var _obj = this;
        this.items = this.find("li").each(function(i){
            this.$ = $(this).initHotItem(_obj);
        });
        return this;
    },
    /**
     * 初始化热门推荐内容
     * @param {JSoul} main
     * @return JSoul
     */
    initHotItem: function(main){
        var per = {
            boxW: (690 - 860) / 100,
            boxH: (140 - 60) / 100,
            imgW: (150 - 100) / 100,
            imgH: (90 - 60) / 100
        };
        this.oImg = this.find("@a,className,cover").firstChild().initHotItemDetail(this);
        this.oH5 = this.find("h5").firstChild().initHotItemDetail(this);
        this.initTimeIntervarEvent(50, 0.3).extend({
            start: this.containAttr("className", "choosed") == -1 ? 0 : 100,
            oGoto: this.find("@p,className,*goto"),
            callback: function(selfer){
                selfer.cssForPairs({
                    width: (860 + per.boxW * selfer.start) + "px",
                    height: (60 + per.boxH * selfer.start) + "px"
                });
                selfer.oImg.bindEvent({
                    width: (100 + per.imgW * selfer.start),
                    height: (60 + per.imgH * selfer.start)
                });
                selfer.syncScroll();
            },
            //同步滚动条
            syncScroll: function(){
                var total = this.innerTop() + this.attr("offsetHeight");
                if (total > main.attr("scrollTop") + main.height) {
                    main.attr("scrollTop", total - main.height);
                }
            },
            showIn: function(){
                this.clearTimeEvent();
                if (this.innerTop() < main.attr("scrollTop")) {
                    main.attr("scrollTop", this.innerTop());
                }
                this.oGoto.className("goto enable");
                this.end = 100;
                this.endCallback = null;
                this.timeIntervarEvent(true);
            },
            showOut: function(){
                this.end = 0;
                this.endCallback = function(){
                    this.oGoto.className("goto");
                };
                this.timeIntervarEvent(true);
            }
        });
        return this;
    },
    /**
     * 初始化热门推荐详细信息
     * @param {JSoul} main
     * @return JSoul
     */
    initHotItemDetail: function(main){
        this.bindEvent({
            onclick: function(e){
                if (main.start == 100) {
                    main.showOut();
                }
                else {
                    main.showIn();
                }
            }
        });
        return this;
    }
});
/***JSoul prototype extend Method [Hot Menu]***/
JSoul.fn.extend({
    /**
     * 初始化热门菜单
     * @param {JSoul} ctBox 内容对象
     * @return JSoul
     */
    initHotMenu: function(ctBox){
        ctBox.extend({
            start: 0,
            callback: function(selfer){
                selfer.style("marginLeft", -selfer.start + "px");
            },
            showIn: function(between){
                this.end = between;
                this.timeIntervarEvent(true);
            }
        }).initTimeIntervarEvent(50, 0.8);
        this.menus = this.find("a").bindEvent({
            Main: this,
            onmouseover: function(e){
                this.Main.choosed.className = "";
                this.className = "choosed";
                this.Main.choosed = this;
                ctBox.showIn(this.Num * 726);
            }
        }).each(function(i){
            if (this.className == "choosed") {
                this.Main.choosed = this;
            }
        });
        return this;
    }
});
/***JSoul prototype extend Method [Banner Menu]***/
JSoul.fn.extend({
    /**
     * 初始化热门菜单
     * @param {JSoul} brBox banner内容对象
     * @return JSoul
     */
    initBannerMenu: function(brBox){
        var menus = this.find("li");
        menus.bindEvent({
            onmouseover: function(e){
                menus.clearTimeEvent();
                menus.choosed.className = "";
                this.className = "choosed";
                menus.choosed = this;
                brBox.showIn(this.Num * 320, e);
            },
            onmouseout: function(e){
                menus.timeOutEvent();
            }
        }).each(function(i){
            if (i == 0) {
                menus.choosed = this;
            }
        }).extend({
            //自动切换广告
            autoChange: function(){
                var index = this.choosed.Num + 1;
                if (index >= this.length) {
                    index = 0;
                }
                this.get(index).onmouseover("auto");
            }
        }).initTimeOutEvent(3000, "autoChange").timeOutEvent();
		
        brBox.extend({
            start: 0,
            callback: function(selfer){
                selfer.style("marginTop", -selfer.start + "px");
            },
            showIn: function(between, e){
                this.end = between;
                this.endCallback = function(){
                    if (e == "auto") {
                        menus.timeOutEvent();
                    }
                };
                this.timeIntervarEvent(true);
            }
        }).initTimeIntervarEvent(50, 0.3);
        return this;
    }
});
/***JSoul prototype extend Method [Login Form]***/
JSoul.fn.extend({
    /**
     * 初始化登录
     * @return JSoul
     */
    initLoginForm: function(){
        var main = this.extend({
            MainInfo: this.find("#MainInfo"),
            PwdInfo: this.find("#PwdInfo"),
            Inputs: this.find("@input,type,text|password").bindEvent({
                onfocus: function(e){
                    this.parentNode.parentNode.className = "typed choosed";
                },
                onblur: function(e){
                    this.parentNode.parentNode.className = "typed";
                }
            }).setDisabled(false),
            LoginForm: this.find("#LoginForm").bindEvent({
                onsubmit: function(e){
                    var isMail = JSoul.rex.mail.test(this["email"].value);
                    var isPwd = (this["password"].value.trim().length > 5);
                    main.MainInfo.setVisible(!isMail, main.MainInfo.defaultInfo);
                    main.PwdInfo.setVisible(!isPwd, main.PwdInfo.defaultInfo);
                    if (isMail && isPwd) {
						main.className("entry entrying");
                        JSoul.$xJson(this.action, main.loginBack, JSoul.getQuery(this));
						main.Inputs.setDisabled(true);
                    }
                    return false;
                }
            }),
            loginBack: function(json){
				main.className("entry");
                if (json) {
                    if (json.isSucceed) {
                        main.innerHtml(json.loginContent);
                        main.initLogoutForm();
                    }
                    else {
						main.Inputs.setDisabled(false);
                        main[json.errorType].setVisible(true, json.errorInfo);
                    }
                }
            }
        });
        main.MainInfo.defaultInfo = main.MainInfo.attr("innerHTML");
        main.PwdInfo.defaultInfo = main.PwdInfo.attr("innerHTML");
        return this;
    },
    /**
     * 初始已登录
     * @return JSoul
     */
    initLogoutForm: function(){
        var main = this.extend({
            LogoutBtn: this.find("#LogoutBtn").bindEvent({
				onclick: function(e){
					main.className("entry entrying");
					JSoul.$xJson(this.href, main.logoutBack);
					return false;
				}
			}),
            logoutBack: function(json){
				main.className("entry");
                if (json && json.isSucceed) {
                   main.innerHtml(json.logoutContent);
                   main.initLoginForm();
                }
            }
        });
    }
});
//]]>
