﻿(function(){
    Letao.Page.Index = {
        RollImages : (function(){
            var __ImageArray__ = {};
            return {
                Set : function(index,arr){
                    __ImageArray__[index] = arr;
                },
                Get : function(index){
                    return __ImageArray__[index] || [];
                }
            }
        })()
    };
	var rollImage = function(imgdata,outer,hover,uuid,fnpre,fnload){
		if(!rollImage.init){
			//如果还没有执行过函数，就设定init方法，构造所有需要用到的函数
			rollImage.init = (function(){
				var F = function(imgdata,outer,hover,uuid,fnPreload,fnLoad){
					//起码得有imagedata,outer,hover三个定义参数
					if(arguments.callee.length < 3){
						return null;
					}
					//如果没有唯一索引，就给一个随机值
					if(typeof uuid!="string" || typeof uuid!="number"){
						uuid = Math.floor(new Date().getTime() / 1000);
					}
					//特权函数，取得UUID的值
					this.getUUID = function(){
						return uuid;
					}
					this.images = imgdata || [];
					this.outer = outer;
					this.hover = hover;
					this.fnPreload = fnPreload || null;
					this.fnLoad = fnLoad || function(){};
					this._index = 0;
					this._loaded = {};
					this._hovers = {};
				}
				
				F.prototype.loadImage = function(index){
					//如果已经读取过这个图片了，就返回这个图片
					if(this._loaded.hasOwnProperty(index) && this._loaded.index!="preload"){
					    if(typeof this._loaded[index]!="undefined" && this._loaded[index].length!=0 && typeof this._loaded[index].fadeIn!="undefined"){
						    this._loaded[index].fadeIn(200,this.fnLoad);
						}
					}
					else{
						var imgid = "rollimage_" + this.getUUID() + "_" + index, _this = this;
						this.outer.append("<img id=\"" + imgid + "\" src=\"/images/e.gif\" style=\"display:none\" />");
						var imgobj = $("#" + imgid);
						//将图片对象添加到loaded对象中，并设置标志位表示正在准备读取图片
						this._loaded[index] = "preload";
						var img = new Image();
						img.src = this.images[index];
						if(img.complete){
							imgobj.attr("src",img.src);
							if(this._index == index){
								this._loaded[index] = imgobj;
								this.fnLoad(this);
								imgobj.fadeIn(200);
							}
						}
						else{
							img.onload = function(){
								imgobj.attr("src",img.src);
								if(_this._index == index){
									_this._loaded[index] = imgobj;
									_this.fnLoad(_this);
									imgobj.fadeIn(200);
								}
							}
						}
					}
				}
				F.prototype.hideImage = function(){
					//停掉当前的图像动画效果并且隐藏掉
					var imgid = "rollimage_" + this.getUUID() + "_" + this._index;
					$("#" + imgid).stop(true,true).hide();
				}
				F.prototype.initFirstImage = function(){
					var images = this.outer.find("img");
					if(images.length == 1){
						images.attr("id","rollimage_" + this.getUUID() + "_0");
						this._loaded["0"] = images;
					}
				}
				F.prototype.run = function(){
					var _this = this;
					this.hover.each(function(i){
						var index = i;
						_this._hovers[index] = $(this);
						$(this).hover(function(){
							if(_this._index != index){
								//在hover的时候执行Preload事件
								_this.fnPreload && _this.fnPreload(_this,$(this),index);
								//把前一个图片hide掉
								_this.hideImage();
								//设置当前的图片索引
								_this._index = index;
								_this.loadImage(index);
							}
						},function(){});
					});
				}
				return F;
			})();
		}
		return new rollImage.init(imgdata,outer,hover,uuid,fnpre,fnload);
	}
	
    $(function(){
	    var r = rollImage(Letao.Page.Index.RollImages.Get("topRollImage"),$("#indexImageOuter"),$("#indexImageRoll li"),null,function(){
		    var o = arguments[0],
			    hover = arguments[1],
			    index = arguments[2];
			//改变链接
			o.outer.attr("href",hover.children("a").attr("href"));
		    o._hovers[o._index].removeClass("imgfocus").find("img").removeClass("nobd");
		    hover.addClass("imgfocus").find("img").addClass("nobd");
		    if(typeof o._loaded[index]=="undefined"){
			    o.outer.parent().css("background-position","50% 50%");
		    }
	    },function(){
		    if(typeof arguments[0]!="undefined"){
			    var o = arguments[0];
			    o.outer.parent().css("background-position","-1000px -1000px");
		    }
	    });
	    r.initFirstImage();
	    r.run();
    })
})();



