﻿//cookie操作插件
(function (jQuery){
	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;
		}
	};
	jQuery.cookies=function(name,value,options,callback){
		name=name||{};
		value=value||{};
		options=options||{};
		callback=callback||function(){};
		if(typeof value=='object'){
			options=value;
			callback=options;
		};
		if(jQuery.isFunction(options)){
			callback=options;
			options=null;
		};
		var defaults={
			dataType:'string'
		};
		var dataType=options.dataType||defaults.dataType;
		if(typeof value=='string'){
			document.cookie=name+'='+value;
		}else{
			var c=document.cookie.split(';');
			for(var i in c){
				if(c[i].indexOf('=')>0){
					var v=c[i].split('=');
					if($.trim(v[0])==name){
						if(dataType=='json')return eval(decodeURIComponent(v[1]));
						return decodeURIComponent(v[1]);
					};
				};
			};
		};
		callback.apply(this);
	};
	jQuery.htmlHeight=function(){
		if ($.browser.msie && $.browser.version<7){
			var scrollHeight=Math.max(document.documentElement.scrollHeight,document.body.scrollHeight);
			var offsetHeight=Math.max(document.documentElement.offsetHeight,document.body.offsetHeight);
			if(scrollHeight<offsetHeight){
				return $(window).height();
			}else{
				return scrollHeight;
			};
		}else{
			return $(document).height();
		};
	};
	jQuery.htmlWidth=function(){
		if ($.browser.msie&&$.browser.version<7) {
			var scrollWidth=Math.max(document.documentElement.scrollWidth,document.body.scrollWidth);
			var offsetWidth=Math.max(document.documentElement.offsetWidth,document.body.offsetWidth);
			if (scrollWidth<offsetWidth){
				return $(window).width();
			}else{
				return scrollWidth;
			};
		} else {
			return $(document).width();
		};
	};
	jQuery.clientHeight=function(){
		return document.documentElement.clientHeight;
	};
	jQuery.getScrollTop=function(){
		IeTrueBody=function(){
			return (document.compatMode&&document.compatMode!="BackCompat")?document.documentElement:document.body;
		};
		return $.browser.msie?IeTrueBody().scrollTop:window.pageYOffset;
	};
})(jQuery);
jQuery.fn.extend({
	center:function(){
		var me=$(this);
		var w=me.width();
		var h=me.height();
		var cliHeight=$.clientHeight();
		var docHeight=(cliHeight/2-h/2)+$.getScrollTop();
		var docWidth=$.htmlWidth()/2-w/2;
		me.css({position:'absolute',top:docHeight,left:docWidth}).show();
	},
	imgshow:function(options){
		options=options||{};
		var defaults={
			src:$(this).attr('src'),
			bgcolor:'#ffffff',
			mask:false,
			maskcolor:'#000000',
			maskalpha:0.3,
			borderColor:'#ffffff',
			borderWidth:6,
			maxWidth:800,
			maxHeight:800
		};
		var src=options.src||defaults.src;
		var bgcolor=options.bgcolor||defaults.bgcolor;
		var mask=options.mask||defaults.mask;
		var maskcolor=options.maskcolor||defaults.maskcolor;
		var maskalpha=options.maskalpha||defaults.maskalpha;
		var borderColor=options.borderColor||defaults.borderColor;
		var borderWidth=options.borderWidth||defaults.borderWidth;
		var maxWidth=options.maxWidth||defaults.maxWidth;
		var maxHeight=options.maxHeight||defaults.maxHeight;
		var me=$(this);
		var w=me.width();
		var h=me.height();
		var offset=me.offset();
		$('.imgshow_jq,._mask').fadeOut();
		var imgobj=new Image();
			imgobj.src=src;
		var test=setInterval(function(){
				if(imgobj.width!=0&&imgobj.height!=0){
					if(mask){
						var maskdiv=$('<div class="_mask"></div>')
							.css({
								position:'absolute',
								backgroundColor:maskcolor,
								opacity:maskalpha,
								width:$.htmlWidth(),
								height:$.htmlHeight(),
								left:0,
								top:0,
								zIndex:99998
							})
							.appendTo('body');
					};
					var width=imgobj.width;
					var height=imgobj.height;
					if(width>maxWidth){
						height=height*(maxWidth/width);
						width=maxWidth;
					};
					if(height>maxHeight){
						width=width*(maxHeight/height);
						height=maxHeight;
					};
					var left=($.htmlWidth()-width)/2;
					var top=($.clientHeight()-height)/2+$.getScrollTop();
					var imgbox=$('<div class="imgshow_jq"></div>')
						.css({position:'absolute',backgroundColor:bgcolor,opacity:0,width:w,height:h,left:offset.left,top:offset.top,zIndex:99999})
						.appendTo('body')
						.animate({opacity:1,width:width,height:height,left:left,top:top},600,function(){
							$(window).scroll(function(){
								imgbox.css({top:($.clientHeight()-height)/2+$.getScrollTop()});
							});
							imgbox.css({borderColor:borderColor,borderStyle:'solid',borderWidth:borderWidth,left:parseInt(imgbox.css('left'))-borderWidth,top:parseInt(imgbox.css('top'))-borderWidth});
							$(imgobj)
							.css({width:width,height:height})
							.hide()
							.appendTo(imgbox)
							.fadeIn()
							.click(function(){
								$(this).fadeOut(600,function(){
									imgbox.css({borderWidth:0,left:left,top:top}).animate({opacity:0,width:w,height:h,left:offset.left,top:offset.top},600,function(){
										$('._mask').fadeOut('fast');
										$(this).remove();
										$(window).unbind('scroll');
									});
								});
							});
						});
					clearInterval(test);
				};
			},100);
	}
});
