//<![CDATA[
JSoul.$(function(){
    JSoul.Onresize = JSoul(window).initOnresize();
	JSoul.Mask = JSoul.buildMask();
});
/***JSoul prototype extend Method [window]***/
JSoul.fn.extend({
    /**
     * 初始window窗口改变事件
     * @return JSoul
     */
    initOnresize: function(){
		var provider = new JSoul.IProvider(this);
		var main = this.extend({
			register: function(groupName, obj, callBack){
				provider.register(groupName, obj, callBack);
			},
			/**
             * 通知状态改变
             */
            notify: function(){
                provider.notifyRun(false, this);
            }
		}).bindEvent({
			onresize: function(e){
				main.notify();
			},
			onscroll: function(e){
				main.notify();
			}
		});
    	return this;
    }
});
/***JSoul prototype extend Method [mask]***/
JSoul.extend({
    /**
     * 建立一个遮罩
     * @return JSoul
     */
    buildMask: function(){
		var mask = JSoul.$b("div", {className:"cover"});
		JSoul.insertFirst(document.body, mask);
		return JSoul(mask).initMask();
    }
});
JSoul.fn.extend({
    /**
     * 设置遮罩
     * @return JSoul
     */
    initMask: function(){
		var target = this.extend({
			defaultAction: this.attr("action"),
			MinWidth:1002,
			onresize: function(state, win){
				var root = JSoul.root;
				this.cssForPairs({
					width:(root.clientWidth < this.MinWidth ?  this.MinWidth : root.clientWidth) + "px",
					height:(root.clientHeight < root.scrollHeight ? root.scrollHeight : root.clientHeight) + "px"
				});
			},
			/**
			 * 遮罩点击要调用的方法
			 * @param {Object} clickback
			 */
			toShow: function(clickback){
				this.onresize();
				this.show();
				if(clickback){
					this.bindEvent({
						onclick: function(e){
							clickback();
						}
					});
				}
			}
		});
		JSoul.Onresize.register("main.cover", this, "onresize");
        return this;
    }
});
/***JSoul prototype extend Method [Fixed bar]***/
JSoul.fn.extend({
    /**
     * 初始化固定文档元素位置
     * @return JSoul
     */
    initFixed: function(){
        var obj = this;
        this.extend({
			end:0,
            callback: function(selfer){
				var scroller = document.body.scrollTop ? document.body : JSoul.root;
				scroller.scrollTop = selfer.start;
            },
			gotoTop: function(){
				this.start = document.body.scrollTop || JSoul.root.scrollTop;
				if(this.start > 0){
					this.timeIntervarEvent(true);
				}
			}
        }).initTimeIntervarEvent(50, 0.8);
        return this;
    },
    /**
     * 初始化固定顶部
     * @param {Object} top
     * @return JSoul
     */
    initFixedTop: function(topSize){
        this.extend({
            top: topSize,
            setPosition: function(top){
                this.style("top", (top + this.top) + "px");
            }
        }).initMoveSmooth();
        return this;
    },
    /**
     * 初始化固定底部
     * @param {Object} top
     * @return JSoul
     */
    initFixedBottom: function(bottomSize){
        this.extend({
            bottom: bottomSize,
            setPosition: function(top){
                this.style("top", (top + JSoul.root.clientHeight - this.attr("offsetHeight") - this.bottom) + "px");
            },
            fixedPosition: function(state, attchs){
                this.setPosition(JSoul.root.scrollTop);
            }
        });
        return this;
    },
    /**
     * 初始化平滑移动
     * @return JSoul
     */
    initMoveSmooth: function(){
        this.initTimeIntervarEvent(50, 0.5).extend({
            start: 0,
            callback: function(selfer){
                selfer.setPosition(selfer.start);
            },
            fixedPosition: function(state, attchs){
                this.end = JSoul.scrollTop();
                this.timeIntervarEvent(true);
            }
        });
        return this;
    }
});
/*******************JSoul原型扩展方法 [模式对话框]******************/
/**
 * JSoul原型扩展方法 [方法类型]
 */
JSoul.fn.extend({
    /**
     * 初始化默认对话框
	 * @param {JSoul} buttons 对话框显示按钮
     * @return JSoul
     */
    initdefaultDialog: function(buttons){
		var main = this.extend({
			closer: this.find("@a,className,closer").bindEvent({
				onclick: function(e){
					main.hiddenDialog();
				}
			}),
			/**
			 * 显示对话框
			 * @param {Element} elem
			 */
			showDialog: function(elem){
				this.show();
				JSoul.Mask.toShow(this.bindMaskEvent);
				this.showHook(elem);
			},
			hiddenDialog: function(){
				this.hidden();
				JSoul.Mask.hidden();
			},
			bindMaskEvent: function(){
				main.hiddenDialog();
			},
			/**
			 * 显示时的钩子函数,后面可以覆盖
			 * @param {Element} elem
			 */
			showHook: function(elem){return this;}
		});
		if (buttons) {
			buttons.bindEvent({
				onclick: function(e){
					main.showDialog(this);
					return false;
				}
			});
		}
        return this;
    }
});
/*******************JSoul原型扩展方法 [图片查看器]******************/
/**
 * JSoul原型扩展方法 [方法类型]
 */
JSoul.fn.extend({
    /**
     * 初始化图片查看器
	 * @param {JSoul} images 图片列表
     * @return JSoul
     */
    initLightBox: function(images){
		var main = this.initdefaultDialog(images).extend({
			acronym: this.find("acronym")
		});
		var groups = [];
		images.each(function(i){
			var attr = this.getAttribute("group");
			if(attr != null){
				if(groups[attr] == undefined){
					groups[attr] = [];
				}
				this.group = attr;
				this.sort = groups[attr].length;
				groups[attr][groups[attr].length] = this;
			}
		});
					
		this.initTimeIntervarEvent(50, 0.5).extend({
			//对话框标题
			title: this.find("span"),
			//图片包装对象
			wrap: this.find("@div,className,dwrap"),
			//当前选中的图片
			current: null,
			//当前选中图片的组序列
			sort: 0,
			//左右控制按钮
			controls: this.acronym.find("a").bindEvent({
				onclick: function(e){
					switch(this.Num){
						case 0: return main.showToPrev(this);
						case 1: return main.showToNext(this);
						default: return;
					}
				}
			}),
			//显示前一张
			showToPrev: function(elem){
				this.sort--;
				this.showImage(this.countCurrentGroup());
			},
			//显示后一张
			showToNext: function(elem){
				this.sort++;
				this.showImage(this.countCurrentGroup());
			},
			/**
			 * 计算图片组
			 * @return Image
			 */
			countCurrentGroup: function(){
				if(this.sort > 0){
					JSoul.show(this.controls.get(0));
				}else{
					this.sort = 0;	
					JSoul.hidden(this.controls.get(0));
				}
				var groupLength = this.getGroupLength();
				if(this.sort < groupLength - 1){
					JSoul.show(this.controls.get(1));
				}else{
					this.sort = groupLength - 1;
					JSoul.hidden(this.controls.get(1));
				}
				return this.getInGroup();
			},
			/**
			 * 得到当前图片组总数
			 * @return Integer
			 */
			getGroupLength: function(){
				if(this.current.group == null){
					return 1;
				}
				return groups[this.current.group].length;
			},
			/**
			 * 得到组里面的对象
			 * @return Image
			 */
			getInGroup: function(){
				if (this.current.group == null) {
					return null;
				}
				return groups[this.current.group][this.sort];
			},
			/**
			 * 设置标题
			 * @param {Image} elem
			 */
			setTitle: function(elem){
				if(elem.alt != null && elem.alt != ""){
					this.title.innerHtml(elem.alt);
				}else{
					this.title.innerHtml("图片浏览");
				}
			},
			/**
			 * 得到图像路径
			 * @param {Image} elem
			 */
			getImagePath: function(elem){
				if(elem.src.match(/^.*(\.sthumb\.jpg|\.bthumb\.jpg)$/ig)){
					return elem.src.replace(/\.sthumb\.jpg|\.bthumb\.jpg/ig, "");
				}
				return elem.src;
			},
			/**
			 * 显示时的钩子函数
			 * @param {Image} elem
			 */
			showHook:function(elem){
				this.current = elem;
				this.sort = (elem.group != null) ? elem.sort : 0;
				this.countCurrentGroup();
				this.showImage(elem);
			},
			showImage: function(elem){
				if(elem != null){
					this.wrap.innerHtml("");
					this.setTitle(elem);
					JSoul.loadImage(this.getImagePath(elem),function(img){
						main.setImage(img);
					});
				}
			},
			/**
			 * 设置图片
			 * @param {Image} img
			 */
			setImage: function(img){
				this.detailImg = img;
				this.setWidth();
			},
			width: 300,
			height: 300,
			start: 0,
			detailImg: null,
			/**
			 * 设置图片宽度
			 */
			setWidth: function(){
				this.clearTimeEvent();
				this.start = this.width;
				this.end = this.detailImg.width;
				this.callback = this.setWidthTyper;
                this.endCallback = function(){
                    this.setHeight();
                };
				this.timeIntervarEvent(true);
			},
			/**
			 * 动态设置宽度方法
			 * @param {JSoul} selfer
			 */
			setWidthTyper: function(selfer){
			    selfer.style("width", (selfer.start + 2) + "px");
				selfer.style("marginLeft", (984 - selfer.start)/2 + "px");
				selfer.acronym.style("width", (selfer.start + 2) + "px")
				selfer.width = selfer.start;
			},
			/**
			 * 设置图片高度
			 */
			setHeight:function(){
				this.start = this.height;
				this.end = this.detailImg.height;
				this.callback = this.setHeightTyper;
                this.endCallback = function(){
                    this.setDisplayImg();
                };
				this.timeIntervarEvent(true);
			},
			/**
			 * 动态设置高度方法
			 * @param {JSoul} selfer
			 */
			setHeightTyper: function(selfer){
				selfer.wrap.style("height", selfer.start + "px");
			    selfer.acronym.style("marginTop", (selfer.start - 30) / 2 + "px");
				selfer.height = selfer.start;
			},
			/**
			 * 设置显示图片
			 */
			setDisplayImg:function(){
				this.start = 0;
				this.end = 100;
				this.callback = this.setDisplayImgTyper;
				JSoul.setAlpha(this.detailImg, 0);
				this.wrap.insertLast(this.detailImg);
                this.endCallback = function(){
					this.typer = null;
                };
				this.timeIntervarEvent(true);
			},
			/**
			 * 动态设置显示方法
			 * @param {JSoul} selfer
			 */
			setDisplayImgTyper: function(selfer){
				JSoul.setAlpha(selfer.detailImg, selfer.start);
			}
		});
        return this;
    }
});
//]]>