// JavaScript Document
// 调用方法
/*	
$(function(){
	$("#autoimg img").autoImage(700,600);
});
*/

//$ 				自动调整图片大小
//$("#autoimg img") 	$图片对象集合
//	w 				:	宽度上限
//	h				:	高度上限
$.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);				
					});	
				}
			}
		});
	}
});
//函数结束

//全选反选函数，参数：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)});
		});
	}
//函数结束

//自动切换标签内容函数,提供参数: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.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 = "#336699"; // 提示窗口的边框颜色 
	var titlecolor = "#FFFFFF"; // 窗口标题颜色 
	var titlebgcolor = "#336699"; // 窗口标题背景色
	var bgcolor = "#FFFFFF"; // 提示内容的背景色
	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);
}

//自滚动代码,提供参数: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);
}
//函数结束

//修复IE6 PNG 不透明函数
function fixPng() {
  var arVersion = navigator.appVersion.split("MSIE")
  var version = parseFloat(arVersion[1])

  if ((version >= 5.5 && version < 7.0) && (document.body.filters)) {
    for(var i=0; i<document.images.length; i++) {
      var img = document.images[i];
      var imgName = img.src.toUpperCase();
      if (imgName.indexOf(".PNG") > 0) {
        var width = img.width;
        var height = img.height;
        var sizingMethod = (img.className.toLowerCase().indexOf("scale") >= 0)? "scale" : "image"; 
        img.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + img.src.replace('%23', '%2523').replace("'", "%27") + "', sizingMethod='" + sizingMethod + "')";
        img.src = "Manage/images/blank.gif";
        img.width = width;
        img.height = height;
        }
      }
    }
  }

//$(function(){fixPng();});//执行
//函数结束

//jQuery png插入和背景透明插件 pngFix
/* --------------------------------------------------------------------
 * jQuery-Plugin "pngFix"
 * Version: 1.2, 09.03.2009
 * by Andreas Eberhard, andreas.eberhard@gmail.com
 *http://jQuery.andreaseberhard.de/
 *
 * Copyright (c) 2007 Andreas Eberhard
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 *
 * Changelog:
 *    09.03.2009 Version 1.2
 *    - Update for $ 1.3.x, removed @ from selectors
 *    11.09.2007 Version 1.1
 *    - removed noConflict
 *    - added png-support for input type=image
 *    - 01.08.2007 CSS background-image support extension added by Scott Jehl, scott@filamentgroup.com, http://www.filamentgroup.com
 *    31.05.2007 initial Version 1.0
 * --------------------------------------------------------------------
 * @example $(function(){$(document).pngFix();});
 * @desc Fixes all PNG's in the document on document.ready
 *
 * $(function(){$(document).pngFix();});
 * @desc Fixes all PNG's in the document on document.ready when using noConflict
 *
 * @example $(function(){$('div.examples').pngFix();});
 * @desc Fixes all PNG's within div with class examples
 *
 * @example $(function(){$('div.examples').pngFix( { blankgif:'ext.gif' } );});
 * @desc Fixes all PNG's within div with class examples, provides blank gif for input with png
 * --------------------------------------------------------------------
 */
(function(jQuery){
jQuery.fn.pngFix = function(settings) {
	// Settings
	settings = $.extend({
		blankgif: 'Manage/images/blank.gif'
	}, settings);
	
	var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
	var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

	if ($.browser.msie && (ie55 || ie6)) {
		//fix images with png-source
		$(this).find("img[src$=.png]").each(function() {
			$(this).attr('width',$(this).width());
			$(this).attr('height',$(this).height());

			var prevStyle = '';
			var strNewHTML = '';
			var imgId = ($(this).attr('id')) ? 'id="' + $(this).attr('id') + '" ' : '';
			var imgClass = ($(this).attr('class')) ? 'class="' + $(this).attr('class') + '" ' : '';
			var imgTitle = ($(this).attr('title')) ? 'title="' + $(this).attr('title') + '" ' : '';
			var imgAlt = ($(this).attr('alt')) ? 'alt="' + $(this).attr('alt') + '" ' : '';
			var imgAlign = ($(this).attr('align')) ? 'float:' + $(this).attr('align') + ';' : '';
			var imgHand = ($(this).parent().attr('href')) ? 'cursor:hand;' : '';
			if (this.style.border) {
				prevStyle += 'border:'+this.style.border+';';
				this.style.border = '';
			}
			if (this.style.padding) {
				prevStyle += 'padding:'+this.style.padding+';';
				this.style.padding = '';
			}
			if (this.style.margin) {
				prevStyle += 'margin:'+this.style.margin+';';
				this.style.margin = '';
			}
			var imgStyle = (this.style.cssText);

			strNewHTML += '<span '+imgId+imgClass+imgTitle+imgAlt;
			strNewHTML += 'style="position:relative;white-space:pre-line;display:inline-block;background:transparent;'+imgAlign+imgHand;
			strNewHTML += 'width:' + $(this).width() + 'px;' + 'height:' + $(this).height() + 'px;';
			strNewHTML += 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + $(this).attr('src') + '\', sizingMethod=\'scale\');';
			strNewHTML += imgStyle+'"></span>';
			if (prevStyle != ''){
				strNewHTML = '<span style="position:relative;display:inline-block;'+prevStyle+imgHand+'width:' + $(this).width() + 'px;' + 'height:' + $(this).height() + 'px;'+'">' + strNewHTML + '</span>';
			}

			$(this).hide();
			$(this).after(strNewHTML);
		});

		// fix css background pngs
		$(this).find("*").each(function(){
			var bgIMG = $(this).css('background-image');
			if(bgIMG.indexOf(".png")!=-1){
				var iebg = bgIMG.split('url("')[1].split('")')[0];
				$(this).css({'background-image':'none','cursor':'pointer'});
				$(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='scale')";
			}
		});
		
		//fix input with png-source
		$(this).find("input[src$=.png]").each(function() {
			var bgIMG = $(this).attr('src');
			$(this).get(0).runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + bgIMG + '\', sizingMethod=\'scale\');';
   		$(this).attr('src', settings.blankgif)
		});	
	}	
	return jQuery;
};
})(jQuery);
//png插入和背景透明插件 pngFix 结束

//启用pngFix插件
$(function(){$(document).pngFix();});


/*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];
	}
