// themes/Dreamy/dreamy.js 
//-------------------------------------------------------------------------
//Site:    http://leotheme.cn/
//Version: 3.0
//Date:    2009/11/28
//Author:  Await(等待)
//Email:   yltfy2008@gmail.com
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
//控制滚动条滚动插件
//-------------------------------------------------------------------------
jQuery.fn.scrollTo = function(speed) {
	var targetOffset = $(this).offset().top;
	$('html,body').stop().animate({scrollTop: targetOffset}, speed);
	return this;
}; 
///-------------------------------------------------------------------------
//jQuery弹出窗口 By Await [2009-11-22]
//--------------------------------------------------------------------------
/*参数：[可选参数在调用时可写可不写,其他为必写]
----------------------------------------------------------------------------
    title:	窗口标题
  content:  内容(可选内容为){ text | id | img | url | iframe }
    width:	内容宽度
   height:	内容高度
	 drag:  是否可以拖动(ture为是,false为否)
     time:	自动关闭等待的时间，为空是则不自动关闭
   showbg:	[可选参数]设置是否显示遮罩层(0为不显示,1为显示)
  cssName:  [可选参数]附加class名称
------------------------------------------------------------------------*/
//示例:
//simpleWindown("例子","text:例子","500","400","true","3000","0","exa")
//------------------------------------------------------------------------
//
function tipsWindown(title,content,width,height,drag,time,showbg,cssName) {
	$("#windown-box").remove(); //请除内容
	var width = width>= 950?this.width=950:this.width=width;	    //设置最大窗口宽度
	var height = height>= 527?this.height=527:this.height=height;  //设置最大窗口高度
	if(showWindown == true) {
		var simpleWindown_html = new String;
			simpleWindown_html = "<div id=\"windownbg\" style=\"height:"+$(document).height()+"px;filter:alpha(opacity=0);opacity:0;z-index: 999901\"></div>";
			simpleWindown_html += "<div id=\"windown-box\">";
			simpleWindown_html += "<div id=\"windown-title\"><h2></h2><span id=\"windown-close\">关闭</span></div>";
			simpleWindown_html += "<div id=\"windown-content-border\"><div id=\"windown-content\"></div></div>"; 
			simpleWindown_html += "</div>";
			$("body").append(simpleWindown_html);
			show = false;
	}
	contentType = content.substring(0,content.indexOf(":"));
	content = content.substring(content.indexOf(":")+1,content.length);
	switch(contentType) {
		case "text":
		$("#windown-content").html(content);
		break;
		case "id":
		$("#windown-content").html($("#"+content+"").html());
		break;
		case "img":
		$("#windown-content").ajaxStart(function() {
			$(this).html("<img src='"+templateSrc+"/images/loading.gif' class='loading' />");
		});
		$.ajax({
			error:function(){
				$("#windown-content").html("<p class='windown-error'>加载数据出错...</p>");
			},
			success:function(html){
				$("#windown-content").html("<img src="+content+" alt='' />");
			}
		});
		break;
		case "url":
		var content_array=content.split("?");
		$("#windown-content").ajaxStart(function(){
			$(this).html("<img src='"+templateSrc+"/images/loading.gif' class='loading' />");
		});
		$.ajax({
			type:content_array[0],
			url:content_array[1],
			data:content_array[2],
			error:function(){
				$("#windown-content").html("<p class='windown-error'>加载数据出错...</p>");
			},
			success:function(html){
				$("#windown-content").html(html);
			}
		});
		break;
		case "iframe":
		$("#windown-content").ajaxStart(function(){
			$(this).html("<img src='"+templateSrc+"/images/loading.gif' class='loading' />");
		});
		$.ajax({
			error:function(){
				$("#windown-content").html("<p class='windown-error'>加载数据出错...</p>");
			},
			success:function(html){
				$("#windown-content").html("<iframe src=\""+content+"\" width=\"100%\" height=\""+parseInt(height)+"px"+"\" scrolling=\"auto\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\"></iframe>");
			}
		});
	}
	$("#windown-title h2").html(title);
	if(showbg == "true") {$("#windownbg").show();}else {$("#windownbg").remove();};
	$("#windownbg").animate({opacity:"0.5"},"normal");//设置透明度
	$("#windown-box").show();
	if( height >= 527 ) {
		$("#windown-title").css({width:(parseInt(width)+22)+"px"});
		$("#windown-content").css({width:(parseInt(width)+17)+"px",height:height+"px"});
	}else {
		$("#windown-title").css({width:(parseInt(width)+10)+"px"});
		$("#windown-content").css({width:width+"px",height:height+"px"});
	}
	var	cw = document.documentElement.clientWidth,ch = document.documentElement.clientHeight,est = document.documentElement.scrollTop; 
	var _version = $.browser.version;
	if ( _version == 6.0 ) {
		$("#windown-box").css({left:"50%",top:(parseInt((ch)/2)+est)+"px",marginTop: -((parseInt(height)+53)/2)+"px",marginLeft:-((parseInt(width)+32)/2)+"px",zIndex: "999999"});
	}else {
		$("#windown-box").css({left:"50%",top:"50%",marginTop:-((parseInt(height)+53)/2)+"px",marginLeft:-((parseInt(width)+32)/2)+"px",zIndex: "999999"});
	};
	var Drag_ID = document.getElementById("windown-box"),DragHead = document.getElementById("windown-title");
		
	var moveX = 0,moveY = 0,moveTop,moveLeft = 0,moveable = false;
		if ( _version == 6.0 ) {
			moveTop = est;
		}else {
			moveTop = 0;
		}
	var	sw = Drag_ID.scrollWidth,sh = Drag_ID.scrollHeight;
		DragHead.onmouseover = function(e) {
			if(drag == "true"){DragHead.style.cursor = "move";}else{DragHead.style.cursor = "default";}
		};
		DragHead.onmousedown = function(e) {
		if(drag == "true"){moveable = true;}else{moveable = false;}
		e = window.event?window.event:e;
		var ol = Drag_ID.offsetLeft, ot = Drag_ID.offsetTop-moveTop;
		moveX = e.clientX-ol;
		moveY = e.clientY-ot;
		document.onmousemove = function(e) {
				if (moveable) {
				e = window.event?window.event:e;
				var x = e.clientX - moveX;
				var y = e.clientY - moveY;
					if ( x > 0 &&( x + sw < cw) && y > 0 && (y + sh < ch) ) {
						Drag_ID.style.left = x + "px";
						Drag_ID.style.top = parseInt(y+moveTop) + "px";
						Drag_ID.style.margin = "auto";
						}
					}
				}
		document.onmouseup = function () {moveable = false;};
		Drag_ID.onselectstart = function(e){return false;}
	}
	$("#windown-content").attr("class","windown-"+cssName);
	var closeWindown = function() {
		$("#windownbg").remove();
		$("#windown-box").fadeOut("slow",function(){$(this).remove();});
	}
	if( time == "" || typeof(time) == "undefined") {
		$("#windown-close").click(function() {
			$("#windownbg").remove();
			$("#windown-box").fadeOut("slow",function(){$(this).remove();});
		});
	}else { 
		setTimeout(closeWindown,time);
	}
}

//-------------------------------------------------------------------------
//图片缩放
//-------------------------------------------------------------------------
function AutoImgSize(obj){
	var n=$(obj).length;
	for(i=0;i<n;i++){
		var objt=$(obj).eq(i);
		if ($(objt).width() > Img_Maxwidth){
			var height=Img_Maxwidth/($(objt).width()/$(objt).height());
			$(objt).width(Img_Maxwidth+'px');
			$(objt).height(height+'px');
			$(objt).parent("a").attr("target","_blank");
			if (typeof($(objt).parent().attr('href'))!='' && typeof($(objt).parent().attr('href'))!='undefined'){objt=$(objt).parent()};
			$(objt).wrap("<div class='entryimgbox'></div>");
			$('<a href="'+$(obj).eq(i).attr('src')+'" rel="inlinks" style="position:absolute;left:0;bottom:0px;width:100%;height:20px;line-height:20px;padding:0 0 3px 5px;background:#fff;" class="showimgbtn">查看大图</a>').insertAfter($(objt));
			$('.entryimgbox').height(height+'px');
			$('.showimgbtn').click(function() {
				var Img_src = $(this).attr('href');
				//alert("src="+Img_src+"alt="+Img_alt);
				var Img='<img src="'+Img_src+'" alt="" class="theImg" style="display: none;" />';
				$("body").append(Img);
				var Img_width = $(".theImg").width();
				var Img_height = $(".theImg").height();
				var Img_name = $(".theImg").attr('alt');
				$(".theImg").remove();
				tipsWindown("图片："+Img_name,"img:"+Img_src,Img_width,Img_height,"true","","false");
				return false;
			});
		}
	}
}
///---------------------------------------------------------------------
//jQuery焦点图切换 By Await [2010-09-18 00：54]
//----------------------------------------------------------------------
/*参数：
------------------------------------------------------------------------
      obj:	要切换的对像[ID,Class]
animation:  设置切换方式 [left(向左滚动),top(向上滚动),fade(淡入淡出)]
     time:	设置每次动画执行间隔时间 [单位毫秒)]
    speed:	设置每次动画执行时间 [单位毫秒]
     auto:	设置是否显自动切换 [可选值为:true,false]
  cssName:  附加Class名称 [自定义焦点图容器样式,可以不填]
-------------------------------------------------------------------------
//示例:
-------------------------------------------------------------------------
<div id="ifocus">
	<ul class="ifocus_list">
		<li><a href="#1"><img src="images/01.png" alt="图片1" /></a></li>
		<li><a href="#2"><img src="images/02.png" alt="图片2" /></a></li>
		<li><a href="#3"><img src="images/03.png" alt="图片3" /></a></li>
	</ul>
</div>
-------------------------------------------------------------------------
//example1.jQFocus("#ifocus","left",2000,500,true);
-------------------------------------------------------------------------
//example2.jQFocus("#ifocus","top",2000,500,true,"border_red");
------------------------------------------------------------------------*/

function jQFocus(obj,animation,time,speed,auto,cssName) {
	var t = n =0,li=$(obj+" li"),count = $(obj+" li").size();
		_width = $(obj).width(),_height = $(obj).height();
		$(obj).css({width: (_width)+"px",height:_height+"px"}).attr("class",cssName);
		$("<div class='ifocus_info'></div>").appendTo(obj);
		$("<span class='ifocus_title'></span>").appendTo(obj).html($(".ifocus_list li:first-child").find("img").attr('alt'));
	var num='<ul class="ifocus_control">';
		for(var i=0;i<count;i++){num+='<li>'+(i+1)+'</li>'};
		num+='</ul>';
		$(obj).append(num);
		$(obj+" .ifocus_control li:first-child").addClass("current");
		$(".ifocus_title").click(function(){window.open($(".ifocus_list a:first-child").attr('href'), "_blank")});
	$(obj+" .ifocus_control li").click(function() {
			var i = $(this).text() - 1;
				n = i;
				if (i >= count) return;
				animation = animation.substring();
				switch(animation) {
					case "left":
						$(obj+" .ifocus_list").css({width:(_width*count)+"px",height:_height+"px"});
						$(obj+" .ifocus_list li").css({float:"left",height:_height+"px"});
						$(obj+" .ifocus_list").animate({left:-(_width*i)},speed);
					break;
					case "top":
						$(obj+" .ifocus_list").animate({top:-(_height*i)},speed);
					break;
					case "fade":
						$(obj+" .ifocus_list li").filter(":visible").fadeOut(speed).parent().children().eq(i).fadeIn(speed);
				}
				$(".ifocus_title").html($(".ifocus_list a").eq(i).find("img").attr('alt'));
				$(this).addClass("current").siblings().removeClass("current");
		});
		if(auto==true ) {
			showAuto = function (){
				n = n >= (count - 1) ? 0 : ++n;
				$(obj+" .ifocus_control li").eq(n).trigger('click');
			}
			t = setInterval("showAuto()", time);
			$(obj).hover(function(){clearInterval(t)}, function(){t = setInterval("showAuto()", time);});	
		}
}
