var tabs={
    obj:".tabs",
    init:function(opt,callback){ //重置外部参数
        if(typeof(opt)=='object'){
        for(var o in opt)
            tabs[o]=opt[o];
        }
        $(tabs.obj).each(function(){
            var obj=this;
            var first=$.trim($(obj).attr("rel")); /* 指定切换到默认的tab */
			var vb=$.trim($(obj).attr("rev"));
			if(first!='-1'){
				first=first==""?0:(first-1);
				$(".menu li",obj).removeClass("active").eq(first).addClass("active");
				$(".tabpage",obj).hide(0).eq(first).show(0);
			}
            $(".menu li",obj).click(function(e){
                var str=$.trim($(this).attr("rev")) //广告地址
                if(str!=""){
                    window.open(str);
                }
				if($(this).hasClass("active") && vb==1){
					$(this).removeClass("active");
					$(".tabpage",obj).hide(0)
					return false;
				}
				$(".menu li",obj).removeClass("active");
				$(this).addClass("active");
				$(".tabpage",obj).hide(0).eq($(this).attr("rel")-1).show(0);
				if($.browser.msie && $.browser.version==6)
					$(".tabpage",obj).eq($(this).attr("rel")-1).css("overflow","visible")
				changeSildeShow();
				$(e.target).blur();
				return false;
            });
        });
        if(typeof(callback)=='function'){
            callback.call();
        }
    },
    turn:function(obj,pos,callback){
        $(".menu li",obj).removeClass("active").eq(pos-1).addClass("active");
        $(".tabpage",obj).hide(0).eq(pos-1).show(0);
        if(typeof(callback)=='function'){
            callback.call();
        }
    }
}

var lab={
    obj:".lab",
    init:function(opt,callback){ //重置外部参数
        if(typeof(opt)=='object'){
        for(var o in opt)
            lab[o]=opt[o];
        }
        $(lab.obj).each(function(){
            var obj=this;
            var first=$.trim($(obj).attr("rel")); /* 指定切换到默认的tab */
			if(first!='-1'){
				first=first==""?0:(first-1);
				$("li",obj).eq(first).addClass("active");
			}
            $(obj).children("li").click(function(e){
                var str=$.trim($(this).attr("rev")) //广告地址
                if(str!=""){
                    window.open(str);
                }
				$("li.active",obj).removeClass("active");
                $(this).addClass("active");
				$(e.target).blur();
				if(e.target.className!='title')
					return false;
				
            });
        });
        if(typeof(callback)=='function'){
            callback.call();
        }
    }
}

var lab2={
    obj:".lab2",
    init:function(opt,callback){ //重置外部参数
        if(typeof(opt)=='object'){
        for(var o in opt)
            lab[o]=opt[o];
        }
        $(lab2.obj).each(function(){
            var obj=this;
            $("li",obj).mouseover(function(){
				$("li.active",obj).removeClass("active");
                $(this).addClass("active");
				return false;
            });
        });
        if(typeof(callback)=='function'){
            callback.call();
        }
    }
}

var focusPics={
    interVal:4000,
    obj:".focusPics",
	maxLen:480,
	
    init:function(obj,callback){
        if(typeof(opt)=='object'){
            for(o in opt)
                focusPics[o]=opt[o];
        }
        $(focusPics.obj).each(function(){
            var count=$("ul.pics li",this).length;
            var str=[];
            var timeout=null;
            var obj=this;
            var pos=0;
			var ml=Math.floor(focusPics.maxLen/count);
			 $("ul.pages li",obj).css("width",ml).eq(count-1).css("width",focusPics.maxLen-(count-1)*ml);
			
            $("ul.pages li",obj).bind("click",function(){
				if(!$(this).hasClass("active")){
					next($(this).index("ul.pages li"));
					clearInterval(timeout);
					timeout=setInterval(function(){next()},focusPics.interVal);
				}
			});
            next();
            timeout=setInterval(function(){next()},focusPics.interVal);
            function next(opt){
                if(opt>=0){
                    pos=opt;
                }
                if(pos>=count){
                    pos=0;
                }
                $("ul.pics li",obj).hide(0).eq(pos+0).fadeIn(200);
                $("ul.pages li",obj).removeClass("active").eq(pos+0).addClass("active");
                pos++;
            }
        });
        if(typeof(callback)=='function'){
            callback.call();
        }
    }   
}

var carousel={
    obj:'.carousel',
    init:function(opt,callback){
        if(typeof(opt)=='object'){
            for(var o in opt)
                carousel[o]=opt[o];
        }
        $(carousel.obj).each(function(){
            var obj=this;
            var rel=$.trim($(obj).attr("rel")); /* 每次滚动的个数：开始位置 */
            var once=3,first=0;
            if(rel!=""){
                rel=rel.split(":");
                once=parseInt(rel[0]);
                first=parseInt(rel[1]-1);
            }
            var width=$("li",obj).outerWidth("true");
            var len=$("li",obj).length;
			
            var maxWidth=len*width;
            var scrollWidth=once*width;
            var left=-first*width;
            var right=left-scrollWidth;
            if(once>=len) //判定是否需要滚动，如果图片的数量少于每次滚动的个数，那么就不执行滚动。
                return;
            $(".clip",obj).css("width",$(".clip",obj).width());
            $("ul",obj).css("width",maxWidth).animate({left:left},50,function(){
																			 
                $(this).css("left",0).children("li").slice(0,first).appendTo($(this));
            });
            $(".next",obj).bind("click",function(){
												  
                $("ul",obj).animate({left:-scrollWidth},300,function(){
                    $(this).css("left",0).children("li").slice(0,once).appendTo($(this));
                });
				return false
            });
            $(".prev",obj).bind("click",function(){
                var l=len-once;
                $("ul",obj).css("left",-scrollWidth).children("li").slice(l,len).prependTo($("ul",obj));
                $("ul",obj).animate({left:0},300,function(){})
				return false
            });
        });
        if(typeof(callback)=='function'){
            callback.call();
        }
    }
}


var tipAlbums={
    obj:".tipAlbums",
	delay:3000,
    init:function(opt,callback){ //重置外部参数
        if(typeof(opt)=='object'){
        for(var o in opt)
            tipAlbums[o]=opt[o];
        }
		$(tipAlbums.obj).each(function(i){
			var r=i;
			var curID=0;
			var tmout=1;
			//var r=parseInt(Math.random()*100000);
			//var p=$(this).attr("rel").split(";");
			var p;
			var id=$(this).attr("rel")
			
			$.getJSON("/hotel/GetPicList/"+id,function(data){
				p=data;
			});
			p=[{ImageUrl:"1.jpg"},{ImageUrl:"2.jpg"}]
			var inplay=false;
			function goto(op){
				if(!inplay){
					inplay=true;
					if(op==1){
						curID=0;
					}else if(op=="" || op=="next" || op==undefined){
						curID++;
						if(curID>=p.length){
							curID=0;
						}
					}else{
						curID--;
						if(curID<0){
							curID=p.length-1;
						}
					}
					$("#tipAlbumsDiv"+r).find(".photo li").hide().eq(curID).fadeIn(500,function(){inplay=false});
					$("#tipAlbumsDiv"+r).find(".control span").text(curID+1);
				}
			}
			
			
			
			$(this).click(function(e){
				var obj=this
				$(".tipAlbumsDiv").hide();
				$(".albumszz").hide();
				$(this).parents("dt").find(".albumszz").show();
				changeSildeShow();
				if($("#tipAlbumsDiv"+r)[0]){
					$("#tipAlbumsDiv"+r).show();
					clearInterval(tmout);
					if($("#tipAlbumsDiv"+r).find(".control a").eq(1).text()=='stop')
						tmout=setInterval(function(){goto()},tipAlbums.delay);
				}else{
					
					
					var str='<div class="tipAlbumsDiv" id="tipAlbumsDiv'+r+'" style="left:'+($(this).offset().left+200)+'px;top:'+($(this).offset().top-70)+'px"><div class="corner3s"><div class="out"></div><div class="inner"></div></div><div class="head"><a href="" class="close"><span>关闭</span></a>'+$(this).parents("dt").next().find("h3 b").text()+'</div><table class="photo"><tr><td><ul>';
					for(var i in p)
						str+='<li><img src="'+p[i].ImageUrl+'" width=300 height=237 /></li>';
					str+='</ul></td></tr></table><div class="control"><ul><li><a href="" class="prev">prev</a></li><li><a href="" class="stop">stop</a></li><li><a href="" class="next">next</a></li></ul><span>1</span> / '+p.length+'</div></div>';
					$("body").append(str);
					goto(1);
					tmout=setInterval(function(){goto()},tipAlbums.delay);
					
					$("#tipAlbumsDiv"+r).hover(function(){},function(){
						$(this).hide();
						$(this).parent().find(".albumszz").hide();
						clearInterval(tmout);
					}).find(".close").click(function(){
						$("#tipAlbumsDiv"+r).hide();
						$("#tipAlbumsDiv"+r).parent().find(".albumszz").hide();
						return false;
						clearInterval(tmout);
					})
					$("#tipAlbumsDiv"+r).find(".control a").click(function(){
						var t=$(this).text();
						clearInterval(tmout);
						if(t=="stop"){
							$(this).text("start").get(0).className="start";
						}else if(t=="start"){
							tmout=setInterval(function(){goto()},tipAlbums.delay);
							$(this).text("stop").get(0).className="stop";
						}else{
							goto(t);
							tmout=setInterval(function(){goto()},tipAlbums.delay);
						}
						return false;
					});
				}
				return false;
			})
		});
		
		$(window).bind("scroll",function(){
			changeSildeShow();
		});
		$(window).click(function(e){
			if(!$(e.target).parents(".tipAlbumsDiv")[0]){
				$(".tipAlbumsDiv").hide();
				$(".albumszz").hide();
			}
		})
		if(typeof(callback)=='function'){
            callback.call();
        }
    }
}

function changeSildeShow(){
	var obj=$(".tipAlbumsDiv:visible")[0]
	if(obj){
		var id=obj.id.substr(-1,1);
		$(".tipAlbumsDiv:visible").css("top",$(".tipAlbums").eq(id).offset().top-70);
	}
}

var marquee={
    obj:".marquee",
    oventime:2300,
    init:function(opt,callback){
        if(typeof(opt)=='object'){
        for(var o in opt)
            marquee[o]=opt[o];
        }
        $(marquee.obj).each(function(){
            var obj=this;
            var end=$(this).attr("rel"); /*每次滚动的li条数*/
            end=$.trim(end)==""?1:end;
            function auto(){
                $(obj).find("ul").animate({top:-48},1500,function(){
                    $(this).css("top",0).children("li").slice(0,end).appendTo($(this));
                });   
            }
            var timeout=setInterval(auto,marquee.oventime);
            $(obj).hover(function(){
              clearInterval(timeout);
            },function(){
                timeout=setInterval(auto,marquee.oventime);
            });
        });
        if(typeof(callback)=='function'){
            callback.call();
        }
    }
}

function tipDiv(sw,sh){
	var w=document.documentElement.clientWidth;
	var h=document.documentElement.clientHeight;
	var t=document.documentElement.scrollTop+document.body.scrollTop;
	var l=document.documentElement.scrollLeft+document.body.scrollLeft;
	var zh=$(document).height();	
	var s='<div id="zz" style="position:absolute;z-index:1000;left:0;top:0;width:'+w+'px;height:'+zh+'px;background:#000;filter:alpha(opacity=30);opacity:0.3"></div>';
	if(!$("#zz")[0]){
		$("body").append(s);
		$("#tipDiv").show().css({"top":+(h-sh)/2+t,"left":(w-sw)/2+l,"width":sw,"height":sh});
	}else{
		$("#zz").show();	
		$("#tipDiv").show().css({"top":+(h-sh)/2+t,"left":(w-sw)/2+l,"width":sw,"height":sh});
	}

	$(window).unbind("scroll").bind("scroll",function(){
		var t=document.documentElement.scrollTop+document.body.scrollTop;
		var l=document.documentElement.scrollLeft+document.body.scrollLeft;
		var w=document.documentElement.clientWidth;
		var h=document.documentElement.clientHeight;
		$("#tipDiv").show().css({"top":+(h-sh)/2+t,"left":(w-sw)/2+l});
	}).unbind("resize").bind("resize",function(){
		var t=document.documentElement.scrollTop+document.body.scrollTop;
		var l=document.documentElement.scrollLeft+document.body.scrollLeft;
		var w=document.documentElement.clientWidth;
		var h=document.documentElement.clientHeight;
		$("#tipDiv").show().css({"top":+(h-sh)/2+t,"left":(w-sw)/2+l});
		$("#zz").css("width",$(document).width())
	})
	
	$("#zz").click(function(){
		$(this).hide();
		$("#tipDiv").hide();
		$(window).unbind("scroll").unbind("resize");
	});
	
}


var tips=function(o,w){
		o=o || ".headTips";
		$(o).each(function(){
			var obj=this;
			var str=""
			var i=parseInt((Math.random()*1000000));
			if(w>1){
				str=";width:"+w+"px"
			}
			var val=$(obj).attr("rel")||$(obj).attr("rev")
			$('<div class="tipConent" id="tipConentText'+i+'" style="display:none'+str+'">'+val+'</div>').css("position","absolute").appendTo("body");
			$(obj).mousemove(function(e){
				w=w||$("#tipConentText"+i).width();
				if(e.pageX+w+45>document.documentElement.clientWidth){
					$("#tipConentText"+i).show(0).css({"left":e.pageX-w-25,"top":e.pageY+15});
				}else{
					$("#tipConentText"+i).show(0).css({"left":e.pageX+25,"top":e.pageY+15});
				}			
			}).mouseout(function(){
				$("#tipConentText"+i).hide(0);
		});				 
		});
	}
$(function(){
	tips();	
})
