// JavaScript Document
// 调用方法
/*	
$(function(){
	$("#autoimg img").autoImage(700,600);
});
*/

//$ 				自动调整图片大小
//$("#autoimg img") 	$图片对象集合
//	w 				:	宽度上限
//	h				:	高度上限
function killerrors(){ 
	return true; 
} 
window.onerror = killerrors;

$.fn.extend({
	autoImage : function(w,h){
		var wl = isNaN(w) ? 400 : w;
		var hl = isNaN(h) ? 300 : h;
		var _thumb=$('#thumb');
		if(_thumb.length>0){
			var _img=_thumb.children('img');
		}
		this.each(function(){
			var imgObj = $(this);
			var wa = imgObj.width();
			var ha = imgObj.height();
			var scale_w = wl / wa;
			var scale_h = hl / ha;
			var scale = scale_w>scale_h ? scale_h : scale_w;
			if(wa > wl || ha > hl){
				var ws = parseInt(wa * scale);
				var hs = parseInt(ha * scale);
				imgObj.width(ws);
				imgObj.height(hs);
				imgObj.css({"cursor":"pointer"});
				imgObj.attr("title","单击放大 / 缩小");
				if(_thumb.length>0){
					imgObj.click(function(){
						_img.attr({'src':imgObj.attr('src'),'width':wa,'height':ha});
						_thumb.show().click(function(){
							_thumb.hide();
						});
						return false;
					});	
				}else{
					imgObj.toggle(function(){
						imgObj.width(wa);
						imgObj.height(ha);
					},function(){
						imgObj.width(ws);
						imgObj.height(hs);				
					});	
				}
			}
		});
	}
});
//函数结束

function referrer(){
	var ref=document.referrer;
	if(!ref){
		try{
			if(window.opener){//IE下如果跨域则抛出权限异常
				referrer=window.opener.location.href;//Safari和Chrome下window.opener.location没有任何属性
				}
			}catch(e){}
		}
	return ref;
	}

//全选反选函数，参数：jqexp：checkbox Jquery表达式；[flag：0-反选 1-全选]，若无flag参数，则返回所有已勾选的checkbox值，用逗号连接
function selectAll(jqexp,flag){
	if(arguments.length>1){
		$(jqexp).each(function(){
			this.checked=flag==1?true:!this.checked;
			});	
		}
	else{
		var ids='';
		$(jqexp).each(function(){
			ids+=ids==''?this.value:','+this.value;
			});	
		}
		return ids;
	}
//函数结束

//图片相册切换函数，参数：thid-缩略图容器ID，shid-展示容器ID
function imageThumb(thid,shid){
	$('#'+thid+" img").mouseover(function(){
		var thumb=this;
		$('#'+shid+" img").fadeTo(300,0.1,function(){$(this).attr('src',thumb.src).fadeTo(500,1)});
		});
	}
//函数结束

//图片相册动画展示函数，参数：id-List.ID，title-窗口标题
function imagePreview(id,title){	
	$.getJSON('Manage/Ajax.asp?Page=FileID&ID='+id,function(json){
		var titleheight = "24"; // 窗口标题高度 
		var bordercolor = "#369"; // 提示窗口的边框颜色 
		var titlecolor = "#fff"; // 窗口标题颜色 
		var titlebgcolor = "#369"; // 窗口标题背景色
		var bgcolor = "#fff"; // 提示内容的背景色
		var w=650;
		var h=610;
		
		var iWidth = document.body.scrollWidth; 
		var iHeight = document.body.scrollHeight;
		bgObj = document.createElement("div"); 
		bgObj.style.cssText = "position:absolute;left:0px;top:0px;width:"+iWidth+"px;height:"+iHeight+"px;filter:Alpha(Opacity=30);opacity:0.3;background-color:#000000;z-index:101;";
		document.body.appendChild(bgObj); 
		
		msgObj=document.createElement("div");
		msgObj.style.cssText = "position:absolute;top:"+((document.body.scrollTop?document.body.scrollTop:document.documentElement.scrollTop)+(document.documentElement.clientHeight-h)/2)+"px;left:"+(iWidth-w)/2+"px;width:"+w+"px;height:"+h+"px;text-align:center;border:1px solid "+bordercolor+";background-color:"+bgcolor+";padding:1px;z-index:102;overflow:hidden";
		document.body.appendChild(msgObj);
		
		var titleBar = document.createElement("div");
		titleBar.style.cssText = "width:100%;float:left;height:"+titleheight+"px;line-height:"+titleheight+"px;text-align:left;margin:0px;font-weight:bold;font-size:13px;color:"+titlecolor+";background-color:" + titlebgcolor;
		
		var titleCon = document.createElement("div");
		titleCon.style.cssText='float:left; padding-left:10px;';
		titleCon.innerHTML = title;
		
		var closeBtn = document.createElement("div");
		closeBtn.style.cssText = "cursor:pointer; float:right; padding-right:5px;";
		closeBtn.innerHTML = '<span title="关闭" style="font-size:15pt; color:'+titlecolor+';">×</span>';
		closeBtn.onclick = hide;
		
		titleBar.appendChild(titleCon);
		titleBar.appendChild(closeBtn);
		msgObj.appendChild(titleBar);
		
		function hide(){
			document.body.removeChild(bgObj); 
			document.body.removeChild(msgObj); 
		}
		
		var ul=document.createElement("ul");
		ul.className='ad-thumb-list';
		for(var i=0;i<json.length;i++){
			var li=document.createElement("li");
			li.innerHTML='<a href="'+json[i]+'"><img src="'+json[i]+'" title="'+title+'-'+(i+1)+'" /></a>';
			ul.appendChild(li);
			}		
		var ad_thumbs=document.createElement("div");
		ad_thumbs.className='ad-thumbs';
		ad_thumbs.appendChild(ul);
		
		var ad_nav=document.createElement("div");
		ad_nav.className='ad-nav';
		ad_nav.appendChild(ad_thumbs);
		
		var ad_controls=document.createElement("div");
		ad_controls.className='ad-controls';
		
		var ad_image_wrapper=document.createElement("div");
		ad_image_wrapper.className='ad-image-wrapper';
		
		var ad_gallery=document.createElement("div");
		ad_gallery.className='ad-gallery';
		ad_gallery.id='gallery';
		ad_gallery.appendChild(ad_image_wrapper);	
		ad_gallery.appendChild(ad_controls);
		ad_gallery.appendChild(ad_nav);			
		msgObj.appendChild(ad_gallery);
		
		var galleries = $('.ad-gallery').adGallery();		
		galleries[0].settings.effect = 'resize';//'slide-hori','slide-vert','resize','fade',''
		$('#toggle-slideshow').click(
		  function() {
			galleries[0].slideshow.toggle();
			return false;
		  });
		$('#toggle-description').click(
		  function() {
			if(!galleries[0].settings.description_wrapper) {
			  galleries[0].settings.description_wrapper = $('#descriptions');
			} else {
			  galleries[0].settings.description_wrapper = false;
			}
			return false;
		  });	
		});
	}
//函数结束

//自动切换标签内容函数,提供参数:Tab和Content的$ ID名
//autoTab(tabs,contents)
function autoTab(tabs,contents){
	$(tabs+'>li').mouseover(function(){
		var tab=$(this);
		var content=$(contents+'>li[title="'+this.title+'"]');
		tab.css('backgroundImage','url(Images/'+this.id+'_active.gif)');
		tab.siblings().each(function(){
			$(this).css('backgroundImage','url(Images/'+this.id.replace('_active','')+'.gif)');
			});
		//tab.addClass('active').siblings().removeClass('active');
		content.addClass('active').siblings().removeClass('active');
		});
	}
//函数结束

//弹出一个提示框,页面变灰,绝对居中显示.提供参数:标题,内容html,宽度,高度	
//showTip(title,content,width,height);
function showTip(title, msg, ww, hh){ 
	var titleheight = "25"; // 窗口标题高度 
	var bordercolor = "#369"; // 提示窗口的边框颜色 
	var titlecolor = "#fff"; // 窗口标题颜色 
	var titlebgcolor = "#369"; // 窗口标题背景色
	var bgcolor = "#fff"; // 提示内容的背景色
	var w=isNaN(ww) ? 400 : ww;
	var h=isNaN(hh) ? 300 : hh;
	
	var iWidth = document.body.scrollWidth; 
	var iHeight = document.body.scrollHeight;
	bgObj = document.createElement("div"); 
	bgObj.style.cssText = "position:absolute;left:0px;top:0px;width:"+iWidth+"px;height:"+iHeight+"px;filter:Alpha(Opacity=30);opacity:0.3;background-color:#000000;z-index:101;";
	document.body.appendChild(bgObj); 
	
	msgObj=document.createElement("div");
	msgObj.style.cssText = "position:absolute;top:"+((document.body.scrollTop?document.body.scrollTop:document.documentElement.scrollTop)+(document.documentElement.clientHeight-h)/2)+"px;left:"+(iWidth-w)/2+"px;width:"+w+"px;height:"+h+"px;text-align:center;border:1px solid "+bordercolor+";background-color:"+bgcolor+";padding:1px;z-index:102;overflow:hidden";
	document.body.appendChild(msgObj);
	
	var conObj = document.createElement("div");
	conObj.style.cssText = "margin:0px;border:0px;padding:0px;width:100%;overflow:hidden";
	var titleBar = document.createElement("div");
	titleBar.style.cssText = "width:100%;float:left;height:"+titleheight+"px;line-height:"+titleheight+"px;text-align:left;margin:0px;font-weight:bold;font-size:13px;color:"+titlecolor+";cursor:move;background-color:" + titlebgcolor;
	var titleCon = document.createElement("div");
	titleCon.style.cssText='float:left; padding-left:10px;';
	titleCon.innerHTML = title;
	var moveX = 0;
	var moveY = 0;
	var moveTop = 0;
	var moveLeft = 0;
	var moveable = false;
	var docMouseMoveEvent = document.onmousemove;
	var docMouseUpEvent = document.onmouseup;
	titleBar.onmousedown = function() {
		var evt = getEvent();
		moveable = true; 
		moveX = evt.clientX;
		moveY = evt.clientY;
		moveTop = parseInt(msgObj.style.top);
		moveLeft = parseInt(msgObj.style.left);
		
		document.onmousemove = function() {
			if (moveable) {
				var evt = getEvent();
				var x = moveLeft + evt.clientX - moveX;
				var y = moveTop + evt.clientY - moveY;
				if ( x > 0 &&( x + w < iWidth) && y > 0 && (y + h < iHeight) ) {
					msgObj.style.left = x + "px";
					msgObj.style.top = y + "px";
				}
			}	
		};
		document.onmouseup = function () { 
			if (moveable) { 
				document.onmousemove = docMouseMoveEvent;
				document.onmouseup = docMouseUpEvent;
				moveable = false; 
				moveX = 0;
				moveY = 0;
				moveTop = 0;
				moveLeft = 0;
			} 
		};
	}
	
	var closeBtn = document.createElement("div");
	closeBtn.style.cssText = "cursor:pointer; float:right; padding-right:5px;";
	closeBtn.innerHTML = '<span title="关闭" style="font-size:15pt; color:'+titlecolor+';">×</span>';
	closeBtn.onclick = hide;
	var msgBox = document.createElement("div");
	msgBox.style.cssText = "cursor:pointer; float:left; overflow:hidden; line-height:200%; text-align:left; padding:5px; width:"+(ww-14)+"px; height:"+(hh-titleheight)+"px;";
	msgBox.title = '单击关闭';
	msgBox.onclick = hide;
	msgBox.innerHTML = msg;
	
	titleBar.appendChild(titleCon);
	titleBar.appendChild(closeBtn);
	conObj.appendChild(titleBar);
	conObj.appendChild(msgBox);
	msgObj.appendChild(conObj);
	
    // 获得Event对象，用于兼容IE和FireFox
    function getEvent() {
	    return window.event || arguments.callee.caller.arguments[0];
    }
	function hide(){
		document.body.removeChild(bgObj); 
		document.body.removeChild(msgObj); 
	}
	this.autoHide=function(){
		setTimeout('document.body.removeChild(bgObj);document.body.removeChild(msgObj)',1500);
		}
}
//函数结束

//图片预览函数,提供参数:1-标题;2-图片src;3-预览窗口宽度;4-预览窗口高度
function showPreview(title,src,w,h){
	showTip(title,'<img src="'+src+'" width="'+(w-14)+'" />',w,h);
}

//打印日期时间函数：lan-0中文；-1英文
function printDate(lan,id){
	var str='';
	var d=new Date();
	var arrcn=['年','月','日','时','分','秒'];
	var arren=['/','/',':',':'];
	var weekcn=['星期日','星期一','星期二','星期三','星期四','星期五','星期六'];
	var weeken=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Satday'];
	var daycn=['上午好','下午好'];
	var dayen=['Good morning','Good afternoon'];
	if(lan==0){
		str=d.getFullYear()+arrcn[0]+(d.getMonth()+1)+arrcn[1]+d.getDate()+arrcn[2]//+' '+d.getHours()+arrcn[3]+d.getMinutes()+arrcn[4]+d.getSeconds()+arrcn[5];
		str+=' '+weekcn[d.getDay()];
		str+=' '+daycn[d.getHours()<12?0:1];
		}
	else{
		str=d.getFullYear()+arren[0]+(d.getMonth()+1)+arren[1]+d.getDate()//+' '+d.getHours()+arren[2]+d.getMinutes()+arren[3]+d.getSeconds();
		str+=' '+weeken[d.getDay()];
		str+=' '+dayen[d.getHours()<12?0:1];			
		}
	$('#'+id).html(str);
	}	
//打印日期时间函数，lan-语言；id-对象id

//自滚动代码,提供参数:1-左方向id;2-右方向id;3-滚动容器id,全部为DOM id
function autoScroll(leftid,rightid,contentid){
	var browser = getBrowser();
	var MyMar;
	var speed = 4; //速度，越大越慢
	var speed = browser.chrome ? speed*4 : speed;//适配Chrome浏览器,速度减慢
	var spec = 3; //每次滚动的间距, 越大滚动越快
	var leftobj=_$(leftid);
	var rightobj=_$(rightid);
	var contentobj=_$(contentid);
	function goleft() {contentobj.scrollLeft -= spec;}
	function goright() {contentobj.scrollLeft += spec;}
	leftobj.onmouseover = function() {MyMar=setInterval(goleft, speed);}
	leftobj.onmouseout = function() {clearInterval(MyMar);}
	rightobj.onmouseover = function() {MyMar=setInterval(goright,speed);}
	rightobj.onmouseout = function() {clearInterval(MyMar);}	
}
//函数结束

//按步长自循环滚动代码,提供参数:1-滚动容器id;2-滚动步长,id为DOM id
function autoScrollStep(id,step){
	var sobj=_$(id);
	var rdirection=true;
	var ldirection=false;
	var interval=setInterval(function(){
			if(rdirection){if((sobj.scrollLeft+step)>=sobj.scrollWidth){rdirection=false;ldirection=true;}sobj.scrollLeft += step;}
			if(ldirection){sobj.scrollLeft -= step;if(sobj.scrollLeft<=0){rdirection=true;ldirection=false;}}
		},3000);
}
//函数结束

/*jQuery操作cookie的插件,大概的使用方法如下
example $.cookie('the_cookie', 'the_value');
设置cookie的值
example $.cookie('the_cookie', 'the_value', {expires: 7, path: '/', domain: 'jquery.com', secure: true});
新建一个cookie 包括有效期 路径 域名等
example $.cookie('the_cookie', 'the_value');
新建cookie
example $.cookie('the_cookie', null);
删除一个cookie*/
jQuery.cookie = function(name, value, options) { 
    if (typeof value != 'undefined') { // name and value given, set cookie 
        options = options || {}; 
        if (value === null) { 
            value = ''; 
            options.expires = -1; 
        } 
        var expires = ''; 
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { 
            var date; 
            if (typeof options.expires == 'number') { 
                date = new Date(); 
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); 
            } else { 
                date = options.expires; 
            } 
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE 
        } 
        var path = options.path ? '; path=' + options.path : ''; 
        var domain = options.domain ? '; domain=' + options.domain : ''; 
        var secure = options.secure ? '; secure' : ''; 
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); 
    } else { // only name given, get cookie 
        var cookieValue = null; 
        if (document.cookie && document.cookie != '') { 
            var cookies = document.cookie.split(';'); 
            for (var i = 0; i < cookies.length; i++) { 
                var cookie = jQuery.trim(cookies[i]); 
                // Does this cookie string begin with the name we want? 
                if (cookie.substring(0, name.length + 1) == (name + '=')) { 
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break; 
                } 
            } 
        } 
        return cookieValue; 
    } 
};

//页面缩放函数
function zoom(f){
	var browser=getBrowser();
	if(!browser.firefox){
		var zscale=parseInt(document.body.style.zoom.replace('%',''));
		zscale=isNaN(zscale)?100:zscale;
		f==1?document.body.style.zoom=zscale+10+'%':document.body.style.zoom=zscale-10+'%';
		}
	else{f==1?document.body.style.cssText+= ';-moz-transform:scale(1.2);':document.body.style.cssText+= ';-moz-transform:scale(1.0);';
		}
	}
//函数结束

//改变字体大小函数,IE6会死掉,参数:1-顶级jquery对象,2-标志,1-加大1号,-1减小1号
function fontSize(jqobj,f){
	jqobj.children().each(function(){
		var obj=$(this);
		var tag=this.tagName.toLowerCase();
		var size=parseInt(obj.css('fontSize'));
		if(!isNaN(size)&&tag!='img'&&tag!='input'&&tag!='textarea'&&tag!='img'&&tag!='select'&&tag!='option'){
			obj.css('fontSize',(size+f)+'px');
			}
		fontSize(obj,f);
		});
	}
//函数结束

//改变字体大小函数,所有浏览器兼容,参数:1-标志,1-加大1号,-1减小1号
var defVal=-1;
var sizeVal=new Array('10px','11px','12px','14px','16px','18px','20px');
function changeFont(f){	
	defVal=defVal==-1?2:defVal;
	defVal+=f;
	if(defVal<0){defVal=0;}
	if(defVal>6){defVal=6;}
	document.body.style.fontSize=sizeVal[defVal];
	}
