var arrBox = function(){
	return {
		posterArr : function(){
			r = {
				"wrap"   : ".poster",			//盒子
				"move"   : ".poster_img li",	//展现列表大图
				"list"   : ".poster_list li",	//展现列表小图
				"onthis" : "onthis",			//展现列表小图，当前状态。 onthis 为自定义的class
				"auto"   : true,				//是否自动
				"t"	     : 3000					//轮播时间
			};
			
			return r;
		},
		
		maskArr : function(){
			r = {
				"wrap" : "#maskWrap",			//遮罩层总背景
				"url"  : "",					//url传值
				"urlID": "#returnUrl",			//url获取
				"move" : ".move",				//拖拽
				"closeBox" : ".close",			//关闭遮罩层
				"touch" : ".maskLogin",			//触发遮罩层按钮
				"box"  : ".maskBox",			//遮罩层
				"pm"   :  5,					//拖动层由于父层 padding 、margin 、 position引起的间差,目前设置为手动调节
				"t"    :  300,					//移动速度
				"o"    :  0.50,					//遮罩透明度
				"k"    :  0.70,					//回弹速度
				"n"    :  7						//回弹次数
			};
			
			return r;
		},
		
		scroArr : function(){
			r = {
				"next" : "a#scronext",			//点击触发
				"prev" : "a#scroprev",			//点击触发
				"move" : ".scroinfo ul",		//左右移动层
			    "list" : ".scrolist li",		//移动位置
				"wrap" : ".scroBox",			//盒子
				"auto" : true,					//是否自动轮播
				"pm"   : 30,					//左右按钮位置，一般以postion来定位隐藏值
				"dfpm" : 1,						//左右按钮位置，一般以postion来定位显示值
				"t"    : 5000,					//轮播时间
				"msh" : true,					//左右按钮显示或者mouseover来显示隐藏
				"change" : true,				//按钮是否改变
				"retn"  : true,					//是否重复
				"ms"  : true					//轮播状况，目前为两种
			};
			
			return r;
		}
	};
}();

function $Fun(arr)
{
	this.timer = null;
	this.auto = true;
	this.msh = true;
	this.ms = true;
	this.change = true;
	this.retn = true;
	this.i = 0;
	this.wrap = arr.wrap;
	this.url = arr.url;
	this.urlID = arr.urlID;
	this.move = arr.move;
	this.list = arr.list;
	this.box = arr.box;
	this.prev = arr.prev;
	this.next = arr.next;
	this.onthis = arr.onthis;
	this.closeBox = arr.closeBox;
	this.touch = arr.touch;
	this.dfpm = arr.dfpm;
	this.pm = arr.pm;
	this.k = arr.k;
	this.n = arr.n;
	this.o = arr.o;
	this.t = arr.t;
	
	this.pn = function(arr, fn, l){
		
		function autoShow(){
			
			++arr.i;
			
			if( arr.i > l ){ arr.i = 0; };
			
			fn(arr.i);
			
			arr.timer = setTimeout(autoShow, arr.t);
		};
		
		if(arr.auto === true){
			if(!arr.timer){ arr.timer = setTimeout(autoShow, arr.t); };
		};
		
		if(arr.msh === true){
			$(arr.next).css("right", -arr.pm + 'px');
			$(arr.prev).css("left", -arr.pm + 'px');
		};
		
		$(arr.wrap).hover(function(){
			if(arr.msh === true){
				$(arr.next).stop(true, false).animate({"right": arr.dfpm + 'px'}, 300);
				$(arr.prev).stop(true, false).animate({"left": arr.dfpm + 'px'}, 300);
			};
			
			if(arr.auto === true){
				clearTimeout(arr.timer);
				arr.timer = null;
			};
		},function(){
			if(arr.msh === true){
				$(arr.next).stop(true, false).animate({"right": -arr.pm  + 'px'}, 300);
				$(arr.prev).stop(true, false).animate({"left": -arr.pm + 'px'}, 300);
			};
			
			if(arr.auto === true){
				arr.timer = setTimeout(autoShow, arr.t);
			};
		});
	};
};

$Fun.prototype = {
	constructor : $Fun,
	
	maskShow : function(){
		if(arguments.length == 0){
			this.arr = arr;
		} else {
		
			var arr = arguments[0];
			
			for(a in arrBox.maskArr()){
				if(arr[a] !== undefined){ this[a] = arr[a]; };
			};
		};
		
		var _closeBox  =  $(this.closeBox),
		    _wrap  =  $(this.wrap),
			_move  =  $(this.move),
			_touch  =  $(this.touch),
			_box  =  $(this.box),
			_urlID  =  $(this.urlID),
			url  =  this.url,
			pm =  this.pm,
		    t  =  this.t,
			k  =  this.k,
			o  =  this.o,
			n  =  this.n;
		
		var H = $(window).height(),
			W = $(window).width(),
			w = _box.width(),
			h = _box.height(),
		    v = $(document).height();
		
		window.onresize = function(){
			H = $(window).height(),
			W = $(window).width(),
			v = $(document).height();
		};
			
		var flag = false,
			time = null,
			zd = {x:0, y:0},
			md = {x:0, y:0};
		
		_move.mousedown(function(e){
			var p = _move.offset();
			md.x = e.pageX - p.left;
			md.y = e.pageY - p.top;
			flag = true;
		});
		
		$(document).mousemove(function(e){
			
			if(flag){
				time = setTimeout(function(){
					z(e.pageX, e.pageY);
				}, 30);
				
				return false;
			};
			
		}).mouseup(function(){ flag = false; });
		
		function z(X, Y){
			
			zd.x = X - md.x - pm;
			zd.y = Y - md.y - pm;
			
			if(X - md.x <= 0){ zd.x = 0};
			if(Y - md.y <= 0){ zd.y = 0};
			if(zd.x >= W - w - pm*2){ zd.x = W - w - pm*2};
			if(zd.y >= v - h - pm*2){ zd.y = v - h - pm*2};
			if(X <= 0 || Y <= 0 || X >= W || Y >= v){ flag = false; };
			
			_box.css({"left": zd.x + 'px', "top": zd.y + 'px'});
		};
		
		_touch.click(function(){
			var cH = H/2 - h/2 + $(window).scrollTop();
			v = $(document).height();
			
			_urlID.attr("value", url);
			
			_wrap.show().css("height", v + 'px').stop(true, false).animate({"opacity": o}, t, function(){
				_box.show().css("top", cH + h/2 + 'px')
				.css({"left": W/2 - w/2 + 'px', "height": 0})
				.stop(true, false)
				.animate({"top": cH + 'px', "height": h + 'px'}, t);
			});
			
			_wrap.clearQueue();
			
			return false;
		});
		
		_closeBox.click(function(){
			var cH = H/2 - h/2 + $(window).scrollTop();
			_box.stop(true, false).animate({"top": cH + h/2 + 'px', "height": 0},{duration: t, queue: false})
			.delay(t*1.5, "del")
			.queue("del", function(next){
				$(this).hide();
				_wrap.animate({"opacity": 0}, t, function(){ $(this).hide(); });
				next();
			}).dequeue("del");
			
			_box.clearQueue();
			
			return false;
		});
	},
	
	posterShow : function()
	{
		if(arguments.length == 0){
			this.arr = arr;
		} else {
			
		    var arr = arguments[0];
			
			for(a in arrBox.posterArr()){
				if(arr[a] !== undefined){ this[a] = arr[a]; };
			};
		};
		
		var wrap   =  $(this.wrap),
			list   =  $(this.list),
		    move   =  $(this.move),
			onthis =  this.onthis,
			   t   =  this.t,
			   l   =  list.length - 1,
			   i   =  this.i;
			
		var n  =  0,
			pre  =  0;
		
		var _this = this;
			   
		move.hide().eq(0).show().css("zIndex",2);
		list.eq(0).addClass(onthis);
		
		function show(i){
			list.removeClass(onthis);
			
			move.eq(pre).show().css("zIndex",2).stop(true,false).animate({opacity:0}, 380, function(){$(this).css("zIndex", 0).hide();});
			move.eq(i).show().css("zIndex",1).stop(true,false).animate({opacity:1}, 180);
			list.eq(i).addClass(onthis);
			pre = i;
		};
		
		this.pn(this, show, l);
		
		list.mouseover(function(){
			_this.i = list.index(this);
			list.eq(_this.i).addClass(onthis);
			if(pre == _this.i){ return false};
			show(_this.i);
			return false;
		});
	},
	
	scroShow : function()
	{
		if(arguments.length == 0){
			this.arr = arr;
		} else {
		
			var arr = arguments[0];
			
			for(a in arrBox.scroArr()){
				if(arr[a] !== undefined){ this[a] = arr[a]; };
			};
		};
		
		var next  =  $(this.next),
			prev  =  $(this.prev),
			list  =  $(this.list),
			move  =  $(this.move),
			wrap  =  $(this.wrap),
			dfpm  =  this.dfpm,
			 msh  =  this.msh,
			  pm  =  this.pm,
			   t  =  this.t,
			   w  =  move.children().width(),
			   l  =  move.children().length - 1,
			   i  =  this.i,
			   m  =  0;
		
		var _this = this;
		
		move.css("width", (l+1) * w + 'px');
		
		function show(i){
			if(_this.change){ list.removeClass().eq(i).addClass("onthis"); };
			
			m = -(i * w);
			
			if(_this.ms){
				move.stop(true, false).animate({ "left": m + 'px'},{duration: 1500, queue: false})
				.delay(150,"ms")
				.queue("ms", function(next){
					$(this).stop(true, false).animate({ "left": m + 'px'}, 220, showBtn);
					next();
				}).dequeue("ms");
			} else {
				move.stop(true, false).animate({ "left": m + 'px'}, 400, showBtn);
			};
		};
		
		function showBtn(){
			if(_this.change){
				if( _this.i >= l ){
					setTimeout(function(){
						next.removeClass().addClass("nextup");
						prev.removeClass().addClass("prevdown");
					}, 200);
				} else if( _this.i <= 0 ){
					setTimeout(function(){
						prev.removeClass().addClass("prevup");
						next.removeClass().addClass("nextdown");
					}, 200);
				} else {
					setTimeout(function(){
						next.removeClass().addClass("nextdown");
						prev.removeClass().addClass("prevdown");
					}, 200);
				};
			} else {
				return;
			};
		};
		
		this.pn(this, show, l);
		
		list.click(function(){
			
			_this.i = list.index(this);
			
			show(_this.i);
			
			return false;
			
		});
		
		next.click(function(){
			
			++_this.i;
		
			if( _this.i > l ){ if( _this.retn ){ _this.i = l; return false; } else { _this.i = 0; }; };
			
			show(_this.i);
			
			return false;
			
		});
		
		prev.click(function(){
		
			--_this.i;
			
			if( _this.i < 0 ){ if( _this.retn ){ _this.i = 0; return false; } else { _this.i = l; }; };
			
			show(_this.i);
			
			return false;
			
		});
	}
};

var $poster = new $Fun(arrBox.posterArr());
var $mask = new $Fun(arrBox.maskArr());
var $scro = new $Fun(arrBox.scroArr());

