/*
==轮播{对象|对象属性}==
对象属性{宽度|高度|文字大小|自动切换时间}
*/
function dk_slideplayer(object,config){
	this.obj = object;
	this.config = config ? config : {width:"300px",height:"200px",fontsize:"12px",right:"10px",bottom:"10px",time:"5000"};
	this.pause = false;
	var _this = this;
	if(!this.config.right){
		this.config.right = "0px"
	}
	if(!this.config.bottom){
		this.config.bottom = "3px"
	}
	if(this.config.fontsize == "12px" || !this.config.fontsize){
		this.size = "12px";
		this.height = "21px";
		this.right = "6px";
		this.bottom = "10px";
	}else if(this.config.fontsize == "14px"){
		this.size = "14px";
		this.height = "23px";
		this.right = "6px";
		this.bottom = "15px";
	}
	this.count = jQuery(this.obj + " li").size();
	this.n =0;
	this.j =0;
	var t;
	this.factory = function(){
		jQuery(this.obj).css({position:"relative",zIndex:"0",margin:"0",padding:"0",width:this.config.width,height:this.config.height,overflow:"hidden"})
		jQuery(this.obj).prepend("<div style='position:absolute;z-index:20;right:"+this.config.right+";bottom:"+this.config.bottom+"'></div>");
		jQuery(this.obj + " li").css({width:"100%",height:"100%",overflow:"hidden"}).each(function(i){jQuery(_this.obj + " div").append("<a>"+(i+1)+"</a>")});

		jQuery(this.obj + " img").css({border:"none",width:"100%",height:"100%"})

		this.resetclass(this.obj + " div a",0);

		jQuery(this.obj + " p").each(function(i){			
			jQuery(this).parent().append(jQuery(this).clone(true));
			jQuery(this).html("");
			jQuery(this).css({position:"absolute",margin:"0",padding:"0",zIndex:"1",bottom:"0",left:"0",height:_this.height,width:"100%",background:"#000",opacity:"0.4",overflow:"hidden"})
			jQuery(this).next().css({position:"absolute",margin:"0",padding:"0",zIndex:"2",bottom:"0",left:"0",height:_this.height,lineHeight:_this.height,textIndent:"5px",width:"100%",textDecoration:"none",fontSize:_this.size,color:"#FFFFFF",background:"none",zIndex:"1",opacity:"1",overflow:"hidden"})
			if(i!= 0){jQuery(this).hide().next().hide()}
		});

		this.slide();
		this.addhover();
		t = setInterval(this.autoplay,this.config.time);
	}
	
	this.slide = function(){
		jQuery(this.obj + " div a").mouseover(function(){
			_this.j = jQuery(this).text() - 1;
			_this.n = _this.j;
			if (_this.j >= _this.count){return;}
			jQuery(_this.obj + " li").hide();
			jQuery(_this.obj + " p").hide();
			jQuery(_this.obj + " li").eq(_this.j).fadeIn("slow");
			jQuery(_this.obj + " li").eq(_this.j).find("p").show();
			_this.resetclass(_this.obj + " div a",_this.j);
		});
	}

	this.addhover = function(){
		jQuery(this.obj).hover(function(){clearInterval(t);}, function(){t = setInterval(_this.autoplay,_this.config.time)});
	}
	
	this.autoplay = function(){
		_this.n = _this.n >= (_this.count - 1) ? 0 : ++_this.n;
		jQuery(_this.obj + " div a").eq(_this.n).triggerHandler('mouseover');
	}
	
	this.resetclass =function(obj,i){
		jQuery(obj).css({float:"left",marginRight:"3px",width:"15px",height:"14px",lineHeight:"15px",textAlign:"center",fontWeight:"800",fontSize:"12px",color:"#000",background:"#FFFFFF",cursor:"pointer"});
		jQuery(obj).eq(i).css({color:"#FFFFFF",background:"#FF7D01",textDecoration:"none"});
	}

	this.factory();
}
/*
选项卡|显示隐藏层
选项卡属性设置: {mobj:"#m1",mchild:"li",mclass:"tds",eobj:"#m2",echild:"div",first:"1"};
	      mobj: 触发鼠标事件
*/
function optioncard(config){
	this.config	 = config;
	var _this = this;
	
	this.resetall = function(i){
		if(!this.config.mchild && !this.config.echild){
			if(i==0){
				jQuery(_this.config.mobj).removeClass(_this.config.mclass);
				jQuery(_this.config.eobj).hide();
				return;
			}
			if(i==1){
				jQuery(_this.config.mobj).addClass(_this.config.mclass);
				jQuery(_this.config.eobj).show();
				return;
			}
		}else{
			jQuery(this.config.mobj).find(this.config.mchild).removeClass(this.config.mclass);
			jQuery(this.config.eobj).find(this.config.echild).hide();
			jQuery(this.config.mobj).find(this.config.mchild).eq(i).addClass(this.config.mclass);
			jQuery(this.config.eobj).find(this.config.echild).hide().eq(i).show();
		}
	}

	if(this.config.mchild && this.config.echild){
		if(!this.config.first) this.config.first = 1;
		this.resetall(this.config.first-1);
		jQuery(this.config.mobj).find(this.config.mchild).each(function(i){
			if("click" == _this.config.events){
				jQuery(this).click(function(){_this.resetall(i);return false;})
			}else{
				jQuery(this).mouseover(function(){_this.resetall(i);})
			}
		})
	}else{
		jQuery(this.config.mobj).hover(function(){_this.resetall(1)},function(){_this.resetall(0)});
		jQuery(this.config.eobj).hover(function(){_this.resetall(1)},function(){_this.resetall(0)});
	}	
}

/* dk_lightbox {原slightbox}*/
(function(jQuery) {
	jQuery.dk_lightbox = function(object, options) {
		options = jQuery.extend({},options);
		var $jqobj = jQuery(object);
		var ie6		= (jQuery.browser.msie && jQuery.browser.version < 7);
		var $body	= jQuery(document.body);
		var $window	= jQuery(window);
		var $document	= jQuery(document);

		if(ie6){
				jQuery('select').css('visibility','hidden');
		}
		
		if(options.opacity){
			$ojbfade = jQuery("<div class=\"dk_lightboxfade\" id=\"dk_lightboxfade\"></div>").appendTo($body);
		}

		var ie6scroll = function(){
			$jqobj.css({top:$window.scrollTop()+($window.height() - $jqobj.height())/2});
		};

		var positionLightBox = function(){
			$jqobj.css({
				position: (ie6) ? "absolute" : "fixed",
				top: (ie6) ? ($window.height() - $jqobj.height())/2+$window.scrollTop() : ($window.height() - $jqobj.height())/2,
				left: "50%",
				zIndex:"328",
				marginLeft: (($jqobj.outerWidth()/2)*-1)
			});
			if(options.opacity){
				$ojbfade.css({
					position: "absolute",
					zIndex: 327,
					height: $document.height(),
					width: "100%",
					background: (options.background) ? options.background : "#000000",
					opacity: options.opacity,
					top: 0,
					left: 0,
					right: 0,
					bottom: 0
				});
			}
		};

		var removePrompt = function(){
			if(options.opacity){$ojbfade.hide();}
			$jqobj.hide();
			if(ie6) {$body.unbind('scroll',ie6scroll);}
			$window.unbind('resize',positionLightBox);
			$jqobj.fadeOut('fast',function(){
				jQuery('select').css('visibility','visible');
			});
		};

		positionLightBox();
		
		if(ie6) {
			$window.scroll(ie6scroll);
		}
		$window.resize(positionLightBox);
	
		if(options.closed){
			$jqobj.find(options.closed).click(function(){removePrompt()});
		}
		
		if(options.closedtime){
			setTimeout(removePrompt,options.closedtime);
		}
		
		if(options.closedobj){
			jQuery(options.closedobj).click(function(){removePrompt()});
		}
		$jqobj.show();
	};
})(jQuery);

//simplescroll
(function($){
	$.fn.dk_simplescroll = function(options){
		var opt = $.extend({},$.fn.dk_simplescroll.defaults,options);
		var $this = $(this);
		var scrollPause = false //是否暂停自动播放
		var scrollWrapId = "scrollWrap_"+$this.attr('id');//包裹对象id
		var $scrollWrap;//包裹对象
		var ChildSize = $this.children().size(); //获得子节点数量
		var singleSize = opt.type == "x" ? $this.children().outerWidth() : $this.children().outerHeight(); //获得单个子节点尺寸
		var overPlus = opt.type == "x" ? $this.width() - ChildSize* singleSize : $this.height() - ChildSize* singleSize;
		
		if(overPlus >= 0) {return false} //内容不够多时候自动退出

		if(opt.auto){
			opt.cycle = true;
			opt.action = "single";
		}
		
		//初始化
		var init = function(){
			$this.css({position:"relative",zIndex:0});
			
			if(opt.cycle){ //循环播放时将类容复制3份
				opt.action = "single";
				$this.html("<div id='"+scrollWrapId+"' style='position:absolute;z-index:0;'>"+$this.html() + $this.html() + $this.html()+"</div>");
			}else{
				$this.html("<div id='"+scrollWrapId+"' style='position:absolute;z-index:0;'>"+$this.html()+"</div>");
			}
			$scrollWrap = $this.find("#"+scrollWrapId);

			if(opt.type == "x"){
				$scrollWrap.width(singleSize*$scrollWrap.children().size()).css({left:0,top:0});
				if(opt.cycle){
					$scrollWrap.css("left",singleSize*ChildSize*(-1));
				}
			}else{
				$scrollWrap.height(singleSize*$scrollWrap.children().size()).css({top:0,left:0});
				if(opt.cycle){
					$scrollWrap.css("top",singleSize*ChildSize*(-1));
				}
			}
			
			//绑定事件
			if(opt.nextId){
				$("#" + opt.nextId).click(function(){
					if(opt.auto) {clearInterval(timeout);}
					if(opt.action == "single"){
						doanimate(-1);
					}else	if(opt.action == "group"){
						if(opt.type == "x"){
							doanimate(0-$this.width()/singleSize);
						}else{
							doanimate(0-$this.height()/singleSize);
						}
					}
					return false;
				});
			};
			if(opt.prevId){
				$("#" + opt.prevId).click(function(){
					if(opt.auto) {clearInterval(timeout);}
					if(opt.action == "single"){
						doanimate(1);
					}else	if(opt.action == "group"){
						if(opt.type == "x"){
							doanimate($this.width()/singleSize);
						}else{
							doanimate($this.height()/singleSize);
						}
					}
					return false;
				});
			}
			if(opt.auto){
				$this.hover(function(){
						clearInterval(timeout);
						timeout = "mmover";
					},
					function(){
						timeout = window.setInterval(function(){
							doanimate(-1);
						},opt.autotime)
					}
				)
			}
		}

		//移动事件
		var doanimate = function(n){
			if(scrollPause){return false;}
			scrollPause = true;
			var tmpPosVal = opt.type == "x" ? parseInt($scrollWrap.css("left")) : parseInt($scrollWrap.css("top"));
			if(tmpPosVal == 0  && n >0){scrollPause = false;return false;}
			if(tmpPosVal <= overPlus && !opt.cycle && n<0){scrollPause = false;return false;}
			if(opt.type == "x"){
				$scrollWrap.animate({left:tmpPosVal + n*singleSize},opt.speed,function(){
					checkPosition(tmpPosVal + n*singleSize,"x");
				});
			}else{
				$scrollWrap.animate({top:tmpPosVal + n*singleSize},opt.speed,function(){
					checkPosition(tmpPosVal + n*singleSize,"y");
				});
			}
		}
		
		//检测
		var checkPosition = function(v,p){
			scrollPause = false;
			if(opt.cycle){
				if(v == 0){
					if(p == "x"){
						$scrollWrap.css("left",singleSize*ChildSize*(-1));
					}else{
						$scrollWrap.css("top",singleSize*ChildSize*(-1));
					}
				}
				if(v <= singleSize*ChildSize*(-2)){
					if(p == "x"){
						$scrollWrap.css({left:singleSize*ChildSize*(-1)});
					}else{
						$scrollWrap.css({top:singleSize*ChildSize*(-1)});
					}
				}
			}
			if(opt.auto && timeout != "mmover"){
				clearInterval(timeout);
				timeout = window.setInterval(function(){
					doanimate(-1);
				},opt.autotime)
			}
		}

		if(opt.auto){
			var timeout = window.setInterval(function(){
				doanimate(-1);
			},opt.autotime)
		}
		
		init();

	}

	$.fn.dk_simplescroll.defaults = {
		type:   "x",         //滚动方向{x|y} 横向、纵向 默认x
		action: "single",    //滚动方式{single|group}单张、组 默认single
		cycle:  false,       //是否循环 默认false
		auto:  false,        //是否自动播放，自动播放时cycle默认为true 默认false
		autotime: 3000,      //自动播放间隔时间 默认为3000毫秒(3秒)
		speed: 500           //速度 300 默认500毫秒(5秒)
	}

	
})(jQuery);


//$("#cycle").dk_cycle({relateId:"relate",active:"active",type:"x||y||opacity",time:3000,duration:300});
(function($){
	$.fn.dk_cycle = function(options){
		var $obj, $wrap, opt, $objChild, $relateChild, pause, time, current,delayno, presize, init, play;
		$obj = this;
		opt = $.extend({},$.fn.dk_cycle.defaults,options);
		$objChild = $obj.children();
		$relateChild = $("#"+opt.relateId).children();
		
		//初始化事件
		init = function(){
			if($relateChild.size() != $objChild.size()){
				return false;
			}

			$obj.css({position:"relative",zIndex:0});
			if(opt.type == "opacity"){
				$objChild.each(function(i){
					$objChild.eq(i).css({position:"absolute",zIndex:$objChild.size() - i})
				})
			}else{
				$obj.html("<div id=wrap_"+ $obj.attr("id") +">"+$obj.html()+$obj.html()+"</div");
				$wrap = $obj.find("#wrap_"+$obj.attr("id"));
				$wrap.css({position:"absolute",overflow:"hidden"});
				if(opt.type == "x"){
					presize = $obj.width();
					$wrap.css({left:0,height:$obj.height()+"px",width:($wrap.children().size()* presize)+"px"});
				}
				if(opt.type == "y"){
					presize = $obj.height();
					$wrap.css({top:0,width:$obj.width()+"px",height:($wrap.children().size()* presize)+"px"});
				}
			}

			current = 0;
			$relateChild.eq(0).addClass(opt.active);
			
			//绑定事件
			$relateChild.each(function(i){
				$relateChild.eq(i).unbind().bind("mouseover",function(){
					clearInterval(time)
					play(i);
				}).bind("mouseout",function(){
					autoplay();
				});
			})

			//绑定事件
			$obj.unbind().bind("mouseover",function(){
				clearInterval(time)
			}).bind("mouseout",function(){
				autoplay();
			});

			autoplay();
		}
		
		play = function(i){
			if(i == current){return false;}
			if(pause){delayno = i;return false;}
			pause = true;
			if(i == $objChild.size()){
				$relateChild.removeClass(opt.active).eq(0).addClass(opt.active);
			}else{
				$relateChild.removeClass(opt.active).eq(i).addClass(opt.active);
			}
			
			switch(opt.type){
				case "opacity":
					$objChild.eq(current).css("z-index",10).end().eq(i).css("z-index",9).end().eq(current).css("z-index",10).fadeOut(opt.duration,function(){
						$(this).css({display:"block",zIndex:current});
						unsetpause(i);
					});
					break;
				case "x":
					$wrap.animate({left:presize * (0-i)},opt.duration,function(){unsetpause(i);});
					break;
				case "y":
					$wrap.animate({top:presize * (0-i)},opt.duration,function(){unsetpause(i);});
					break;
					break;
			}
		}
		
		unsetpause = function(i){
			pause = false;
			current = i;
			if(i == $objChild.size()){
				if(opt.type == "x"){
					$wrap.css("left",0);
					current = 0;
				}
				if(opt.type == "y"){
					$wrap.css("top",0);
					current = 0;
				}
			}
			
			if(opt.callback){
				opt.callback();
			}
				
			if(delayno != null){
				play(delayno);
				delayno = null;
			}
		}

		autoplay = function(){
			clearInterval(time)
			time = setInterval(function(){
				if(current == ($relateChild.size() -1) && opt.type == "opacity"){
					play(0);
				}else{
					play(current+1)
				}
			},opt.time);
		}
		
		init();
	}
	$.fn.dk_cycle.defaults = {
		type:"opacity",
		time:3000,
		duration:300,
		callback:function(){}
	}
})(jQuery)