//<![CDATA[
JSoul.$(function(){
	JSoul("#Menu").initMenuBox();
});
/*******************JSoul原型扩展方法 [导航效果]******************/
/**
 * JSoul原型扩展方法 [方法类型]
 */
JSoul.fn.extend({
    /**
     * 初始导航菜单
     * @return JSoul
     */
    initMenuBox: function(){
		var main = this;
        this.subs = this.find("dd").each(function(i){
			this.$ = JSoul(this).initMenuItem(main);
        });		
        return this;
    },
    /**
     * 初始化Menu动画
     * @param {JSoul} main 主对象
     * @return JSoul
     */
    initMenuItem: function(main){
		this.extend({
			defClass: this.attr("className"),
			infoBox: this.find("ul")
		});
		if(this.infoBox.length > 0){
			this.infoBox.cssForPairs({
				//width:(this.infoBox.attr("offsetWidth")) + "px",
				width:505 + "px",
				marginLeft: (main.attr("offsetLeft") - this.attr("offsetLeft")) + "px"
			});
		}
        this.initTimeIntervarEvent(50, 0.4).extend({
            start: 0,
			end: 100,
            callback: function(selfer){
				selfer.infoBox.setAlpha(selfer.start);
				var top = (selfer.start / 100 - 1) * 20
				selfer.infoBox.style("marginTop", top + "px");
            },
            showIn: function(){
				if(main.now != this){
					this.clearTimeEvent();
					if(main.now){
						main.now.showOut();
					}
					main.now = this;
					if(this.defClass != "choosed"){
						this.className("hover");
					}
					this.infoBox.setVisible(true);
					this.timeIntervarEvent(true);
				}
            },
            showOut: function(){
				this.clearTimeEvent();
                this.start = 0;
				this.infoBox.setAlpha(this.start);
				this.infoBox.style("marginTop", "-20px");
				this.infoBox.setVisible();
                this.className(this.defClass);
            },
			setMain: function(){
				if(this.defClass == "choosed"){
					main.now = this;
				}
				return this;
			}
        }).setMain().bindEvent({
			onmouseover: function(e){
				this.$.showIn();
			}
		});
        return this;
    }
});
/***JSoul prototype extend Method [search Form]***/
JSoul.fn.extend({
    /**
     * 搜索表单
     * @return JSoul
     */
    initsearchForm: function(){
		var main = this.extend({
			searchTyper: this.find("@dd,className,typer").initSearchTyper(this),
			keyBox: this.find(">keyword").extend({
				isPass: function(){
					var key = this.attr("value").trim();
					if (this.attr("value") != this.attr("defaultValue") && key.length > 0) {
						return true;
					}
					return false;
				}
			}).bindEvent({
				onfocus: function(e){
					if(!main.keyBox.isPass()){
						this.value = "";
						JSoul.style(this, "color", "#333");
					}
				},
				onblur: function(e){
					if (!main.keyBox.isPass()) {
						this.value = this.defaultValue;
						JSoul.style(this, "color", "#999");
					}
				}
			})
		}).bindEvent({
			onsubmit: function(e){
				if(main.keyBox.isPass()){
					return true;
				}
				main.keyBox.run("focus");
				alert("请填入关键词");
				return false;
			}
		});
    	return this;
    },
	/**
	 * 初始化搜索类型
	 * @param {JSoul} main
	 * @return JSoul
	 */
	initSearchTyper: function(main){
		var dd = this.extend({
			title: this.find("h3"),
			typers: this.find("@ul,className,typers"),
			options: this.find("a").bindEvent({
				onclick: function(e){
					main.attr("action", this.rel);
					dd.title.innerHtml(this.innerHTML);
					dd.typers.hidden();
				}
			})
		}).bindEvent({
			onmouseover: function(e){
				dd.typers.show();
			},
			onmouseout: function(e){
				dd.typers.hidden();
			}
		});
		return this;
	}
});
/***JSoul prototype extend Method [UpEffect Menu]***/
JSoul.fn.extend({
    /**
     * 初始化上线滚动特效
     * @param {JSoul} brBox banner内容对象
     * @param {String} tagName 菜单按钮名
     * @param {Number} step 步进高度
     * @param {Boolean} isAuto 是否自动执行
     * @return JSoul
     */
    initUpEffect: function(brBox, tagName, step, isAuto){
        var menus = this.find(tagName);
		if(menus.length < 2)return;
        menus.bindEvent({
            onmouseover: function(e){
                menus.clearTimeEvent();
                menus.choosed.className = "";
                this.className = "choosed";
                menus.choosed = this;
                brBox.showIn(this.Num * step, e);
            },
            onmouseout: function(e){
				if(isAuto)
                	menus.timeOutEvent();
            }
        }).each(function(i){
            if (i == 0) {
                menus.choosed = this;
            }
        });
		if (isAuto) {
			menus.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.2);
        return this;
    },
    /**
     * 初始化动态菜单
     * @param {JSoul} brBox banner内容对象
     * @param {Number} step 步进高度
     * @param {Boolean} isAuto 是否自动执行
     * @return JSoul
     */
	initTrendMenu: function(brBox, step, isAuto){
		var html = [];
		for(var i = 0, j = brBox.find("li").length; i< j; i++){
			html.push('<li><a href="javascript:void(0);"></a></li>');
		}
		this.innerHtml(html.join(''));
		this.initUpEffect(brBox, "a", step, isAuto);
	}
});
/***JSoul prototype extend Method [Flash]***/
JSoul.fn.extend({
	/**
	 * 插入FLASH
	 * @param {json} param 传递的json参数
	 * @param {String} vars 传递给FLASH的HTML变量
	 * @return JSoul flash包装对象
	 */
	insertFlash: function(param, vars){
		var defaultparam = {
				id:"previewItem",
				src:"common/player.swf",
				width:this.attr("offsetWidth") + "px",
				height:this.attr("offsetHeight") + "px",
				allowScriptAccess:"always",
				allowNetworking:"all",
				allowFullScreen:"true",
				wmode:"transparent",
				salign:"LT",
				scale:"noscale",
				quality:"autohigh",
				flashvars:""
			};
		if(param){
			for(k in defaultparam){
				if(param[k]){
					defaultparam[k] = param[k];
				}
			}
		}
		if(vars){
			defaultparam.flashvars = vars + "&stretching=exactfit";
		}else if(defaultparam.flashvars){
			defaultparam.flashvars = defaultparam.flashvars + "&stretching=exactfit";
		}
		if(!defaultparam.name){
			defaultparam.name = defaultparam.id;
		}
		this.innerHtml(JSoul.buildFlash(defaultparam, "10,0,0,0"));
		return this.find("#" + defaultparam.id);
	}
});
JSoul.extend({
	/**
	 * 建立FLASH代码
	 * @param {json} param 传递的json参数
	 * @param {Object} version 需要的版本
	 * @return Flash html代码
	 */
	buildFlash: function(param, version){
		var params=[],embedArgm=[],objArgm=[];
		for(k in param){
			switch(k){
				case "movie":
					continue;
					break;
				case "id":
				case "name":
				case "width":
				case "height":
				case "style":
					JSoul.flashKeyValue(objArgm, k, param[k]);
					JSoul.flashKeyValue(embedArgm, k, param[k]);
					break;
				default:
					JSoul.flashParams(params, k, param[k]);
					JSoul.flashKeyValue(embedArgm, k, param[k]);
			}
		}
		if(version){
			objArgm.push('codeBase=\'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=');
			objArgm.push(version);
			objArgm.push('\' ');
		}
		if (JSoul.browser.msie) {
			return '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ' + objArgm.join('') + '>' + params.join('') + '</object>';
		}else {
			return '<embed ' + embedArgm.join('') + ' pluginspage="http://www.adobe.com/go/getflashplayer_cn" type="application/x-shockwave-flash"></embed>';
		}
	},
	/**
	 * flash名值对
	 * @param {json} list
	 * @param {String} key
	 * @param {String} value
	 */
	flashKeyValue: function(list, key, value){
		list.push(key);
		list.push('=\'');
		list.push(value);
		list.push('\' ');
	},
	/**
	 * flash参数
	 * @param {json} list
	 * @param {String} key
	 * @param {String} value
	 */
	flashParams: function(list, key, value){
		list.push('<param name=\'');
		list.push((key =="src")?"movie":key);
		list.push('\' value=\'');
		list.push(value);
		list.push('\' />');
	}
});
/***JSoul prototype extend Method [Col Scroll]***/
JSoul.fn.extend({
    /**
     * 初始化横向滚动组
	 * @param {Integer} itemWidth 单个宽度
	 * @param {Integer} groupSize 每组数目
	 * @param {String} wrapSelector 内包装项搜索表达
	 * @param {String} itemSelector 单个项搜索表达式
	 * @param {String} nullSuffix [可选] 导向按钮到达最后呈现样式名后缀 prev => prevnull
     * @return JSoul
     */
    initColScrollGroup: function(itemWidth, groupSize, wrapSelector, itemSelector, nullSuffix){
		var main = this.extend({
			//当前所在组
			group: 0,
			//总共的组数
			groupCount: 0,
			//步进宽度
			step: itemWidth * groupSize,
			//内包装项
			wrap: this.find(wrapSelector),
			//所有项目
			items: this.find(itemSelector),
			//左右控制按钮
			controls: this.find("acronym").find("a").bindEvent({
				onclick: function(e){
					if(this.className != this.defaultClass){return;}
					switch(this.Num){
						case 0: return main.scrollToPrev(this);
						case 1: return main.scrollToNext(this);
						default: return;
					}
				}
			}).each(function(i){
				this.defaultClass = this.className;
			}),
			//初始化时没有数据
			nullInit: function(){
				this.controls.each(function(i){
					main.hiddenControl(this);
				});
			},
			//隐藏控制项
			hiddenControl: function(elem){
				if(!nullSuffix){
					JSoul.hidden(elem);
				}else{
					elem.className = elem.defaultClass + " " + elem.defaultClass + nullSuffix;
				}
			},
			//隐藏控制项
			showControl: function(elem){
				if(!nullSuffix){
					JSoul.show(elem);
				}else{
					elem.className = elem.defaultClass;
				}
			},
			//向前一组滚动
			scrollToPrev: function(elem){
				this.group--;
				this.goScroll(this.countCurrentGroup());
			},
			//向后一组滚动
			scrollToNext: function(elem){
				this.group++;
				this.goScroll(this.countCurrentGroup());
			},
			//计算当前组的距离
			countCurrentGroup: function(){
				if(this.group > 0){
					this.showControl(this.controls.get(0));
				}else{
					this.group = 0;	
					this.hiddenControl(this.controls.get(0));
				}
				if(this.group < this.groupCount - 1){
					this.showControl(this.controls.get(1));
				}else{
					this.group = this.groupCount - 1;
					this.hiddenControl(this.controls.get(1));
				}
				return this.group * this.step;
			},
			//初始化自己
			initSelf: function(){
				//如果项为空，或者小于等于每组个数，隐藏前后导航按钮，同时不初始化任何值
				if(this.items.length == 0 || this.items.length <= groupSize){this.nullInit();return;}
				this.groupCount = Math.ceil(this.items.length / groupSize);
				//设置内包装最大宽度，防止换行
				this.wrap.style("width", (this.groupCount * this.step) + "px");
				this.initTimeIntervarEvent(50, 0.4).countCurrentGroup();
			},
			start: 0,
			callback: function(selfer){
				selfer.wrap.style("marginLeft", -selfer.start + "px");
			},
			goScroll: function(endLeft){
				if(this.start == endLeft){return;}
				this.clearTimeEvent();
				this.end = endLeft;
				this.timeIntervarEvent(true);
			}
		});
		this.initSelf();
    	return this;
    }
});
/*******************JSoul原型扩展方法 [导航效果]******************/
/**
 * JSoul原型扩展方法 [方法类型]
 */
JSoul.fn.extend({
    /**
     * 导航切换
	 * @param {JSoul} lists 要切换的子项列表
     * @return JSoul
     */
    initNavChanger: function(lists){
		var main = this.menus = this.find("a");
		if(this.menus.length == 0 || this.menus.length != lists.length){
			return this;
		}
		this.menus.bindEvent({
			onclick: function(e){
				if(this != main.choosed){
					JSoul.hidden(lists.get(main.choosed.Num));
					main.choosed.className = "";
					var current = lists.get(this.Num);
					JSoul.show(current);
					if(current.showCallback){
						current.showCallback();
					}
					this.className = "choosed";
					main.choosed = this;
				}
			}
		}).each(function(i){
			if(this.className == "choosed"){
				main.choosed = this;
			}
		});
        return this;
    }
});
//]]>