﻿Cookie=window.Cookie||{};

Cookie.set=function(name, value, expires, path, domain, secure) {
        if(expires){
        	expires = new Date(new Date().getTime()+expires*1000*60*60).toGMTString();
		}

        document.cookie = name + "=" + encodeURIComponent(value) + ";"
		+ ((expires) ? " expires="+expires+";" : "")
		+ ((path) ? "path="+path+";" : "")
		+ ((domain) ? "domain="+domain+";" : "")
		+ ((secure && secure != 0) ? "secure" : "");
};

Cookie.get=function(name) {
        var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));

        if (arr != null) {
            return decodeURIComponent(arr[2]);
        }
        return null;
};

//改变页面内某一元素的宽高，使页面内容尺寸接近或等于可视区域宽高
function autoWidth(ele) {
	var ele = $(ele);
	var isQuirks = document.compatMode === 'BackCompat';
	var viewport = isQuirks ? document.body : document.documentElement;
	var page = isQuirks ? document.documentElement : document.body;
	var viewportW = pageW = minW = maxW = 0;
	return {
		init:function(o){
			minW = o.minWidth||0;
			maxW = o.maxWidth||9999;
			this.changeSize(true);
			var self=this;
			$(document).ready(this.changeSize);
			$(window).bind("load",function(){self.changeSize();});
			$(window).resize(this.changeSize);
		},
		changeSize:function(cookie){
			var toW,elW=ele.width();
			if(cookie===true){
				var cookie_toW=parseInt(Cookie.get('autoWidth'),10);
				if(cookie_toW && cookie_toW>minW && cookie_toW<maxW){
					ele.width(cookie_toW);
				}
				return cookie_toW;
			}

			viewportW = viewport.clientWidth;
			pageW = page.offsetWidth;
			if(viewportW < minW){
				toW=minW;
			}else if(viewportW > maxW){
				toW=maxW;
			}else{
				toW=viewportW;
			}
			if(toW != elW){
				ele.width(toW);
				if(cookie){
					Cookie.set('autoWidth',toW,24*30);
				}
				return toW;
			}

		}
	}
}
function autoHeight(ele) {
	var ele = $(ele);
	var isQuirks = document.compatMode === 'BackCompat';
	var viewport = isQuirks ? document.body : document.documentElement;
	var page = isQuirks ? document.documentElement : document.body;
	var viewportH = pageH = minH = maxH = 0;
	var resetTop;
	return {
		init:function(o){
			minH = o.minHeight||0;
			maxH = o.maxHeight||99999;
			resetTop=o.resetTop;
			if(resetTop){
				resetTop = $(resetTop);
			}
			this.changeSize(true);
			var self=this;
			$(document).ready(this.changeSize);
			$(window).bind("load",function(){self.changeSize();});
			$(window).resize(this.changeSize);
		},
		changeSize:function(cookie){
			var toH, elH=ele.height(),toT;
			if(cookie===true){
				var cookie_toH=parseInt(Cookie.get('autoHeight'),10);
				if(cookie_toH && cookie_toH>minH && cookie_toH<maxH){
					ele.height(cookie_toH);
					if(resetTop){
						toT=parseInt(resetTop.css('top'),10)+(cookie_toH-elH)/2;
						if(resetTop && toT<0){
							resetTop.css('top',toT);
						}else{
							resetTop.css('top',0);
						}
					}
					return cookie_toH;
				}
			}
			
			viewportH = viewport.clientHeight;
			pageH = page.offsetHeight;
			if(viewportH < pageH){
					toH=Math.max(minH, elH-pageH+viewportH);
			}else if(viewportH > pageH){
					toH=Math.min(maxH,elH-pageH+viewportH);
			}else{
					toH=elH-pageH+viewportH;
			}
			if(toH != elH){
				ele.css('overflow','hidden');
				ele.height(toH);
				//alert([viewportH,pageH,elH,toH,ele.height()]);
				if(resetTop){
					toT=parseInt(resetTop.css('top'),10)+(toH-elH)/2;
					if(resetTop && toT<0){
						resetTop.css('top',toT);
					}else{
						resetTop.css('top',toT);
					}
				}
				if(cookie){
					Cookie.set('autoHeight',toH,24*30);
				}

				return toH;
			}
			
		}
	}
}

function autoFill4Img(img,w,h){
	if(img.naturalWidth){
		if(img.naturalWidth/img.naturalHeight > w/h){
			img.height=h;
			img.width=h*img.naturalWidth/img.naturalHeight;
		}
	}else{
		var scale=img.offsetWidth/img.offsetHeight;
		if(scale > w/h){
			//alert([img.height,img.width])
			img.height=h;
			img.width=h*scale;
		}
	};
}
function autoWidth4Img(img,w,h){
	if(img.naturalWidth){
		if(img.naturalHeight/img.naturalWidth > h/w){
			img.height=h;
			img.width=h*img.naturalWidth/img.naturalHeight;
		}
	}else{
		var scale=img.offsetWidth/img.offsetHeight;
		if(img.offsetHeight/img.offsetWidth > h/w){
			//alert([img.height,img.width])
			img.height=h;
			img.width=h*scale;
		}
	};
}
function autoHeight4Img(img,w,h){
	if(img.naturalWidth){
		if(img.naturalWidth/img.naturalHeight > w/h){
			img.width=w;
			img.height=w*img.naturalHeight/img.naturalWidth;
		}
	}else{
		var scale=img.offsetHeight/img.offsetWidth;
		if(img.offsetWidth/img.offsetHeight > w/h){
			//alert([img.height,img.width])
			img.width=w;
			img.height=w*scale;
		}
	};
}
//输入框提示
function inputTips(o,val){
	var o = $(o);
	o.val(val).focus(function(){
		if(o.val(val)){
			o.val("");
		}
	})
	o.blur(function(){
		if(o.val() == ""){
			o.val(val);
		}
	})
}

//切换导航展示二级
function showNav(t,c){
	var t = $(t);
	var c = $(c);
	var curr = $(".nav.on")
	t.each(function(i){
		$(this).hover(function(){
			$(this).addClass("on").siblings().removeClass("on");
			c.eq(i).show();
			if($.browser.msie && $.browser.version=='6.0'){
				$(document.body).addClass('hideSelect');	
			}
			
		},function(){
			curr.addClass("on").siblings().removeClass("on");
			$(this).removeClass("on");
			c.eq(i).hide();
			if($.browser.msie && $.browser.version=='6.0'){
				$(document.body).removeClass('hideSelect');	
			}
		})	
	})	
}

//切换选项卡
function showTab(t,c,curClass){
	var t = $(t);
	var c = $(c);
	t.hover(function(){
		$(this).addClass(curClass).siblings().removeClass(curClass);
		c.eq($(this).index()).show().siblings().hide();
	})
}

