﻿//String类的扩展函数．功能：清除字符串两端的空格．
String.prototype.trim = function()
{
	var returnString = this;
    for(;returnString .charCodeAt(0) == 0x20;returnString = returnString .substr(1));
    for(;returnString .charCodeAt(returnString .length-1) == 0x20;returnString = returnString .substr(0,returnString.length-1));
    return returnString ;    
}

//格式化字符串
String.prototype.format = function(params)
{
    var str = this;
    for(var i=0;i<arguments.length;i++)
        str = str.replace("{"+i+"}",arguments[i].toString());
        
    return str;
    
}

//缩放图片
function zoomImg(img,maxWidth,maxHeight,isAlignMiddle)
{
    var actualImg = new Image();
    actualImg.attachEvent("onload",function(){
                    var finalHeight,finalWidth,actualWidth,actualHeight;
                    actualWidth = actualImg.width;
                    actualHeight = actualImg.height;
                    
                    if(maxWidth > actualWidth && maxHeight > actualHeight)
                    {
                        finalHeight = actualHeight;
                        finalWidth = actualWidth;
                    }
                    else if (maxWidth / maxHeight > actualWidth / actualHeight)
                    {
                        finalHeight = maxHeight;
                        finalWidth = Math.round(maxHeight * actualWidth / actualHeight);
                    }
                    else if (maxWidth / maxHeight < actualWidth / actualHeight)
                    {
                        finalHeight = Math.round(maxWidth * actualHeight / actualWidth);
                        finalWidth = maxWidth;
                    }
                    else
                    {
                        finalHeight = maxHeight;
                        finalWidth = maxWidth;
                    }
                    
                    if(isAlignMiddle)
                    {
                        img.style.position = "absolute";
                        img.style.top = (maxHeight-finalHeight)/2 + "px";
                        img.style.left = (maxWidth - finalWidth)/2 + "px";
                    }
                                        
                    img.style.width = img.width = finalWidth + "px";
                    img.style.height = img.height = finalHeight + "px";
                    delete actualImg;
                    actualImg = null;

                   }
                 );
     actualImg.src = img.src;
     
}

//判断当前浏览器是否是IE
var isIE = navigator.appName == "Microsoft Internet Explorer"?true:false;
var isIE6 = navigator.userAgent.indexOf("MSIE 6.0")> 0;
var isIE7 = navigator.userAgent.indexOf("MSIE 7.0")> 0;
	
	/////////////////////////////////////////////////////////////////////////////
	function WaitingUI(parent,innerHTML,opacity,backColor,top,left,backImg,zIndex)
	{
	    if(innerHTML == null)
	        innerHTML = "<img src=\"/images/waiting2.gif\"/>";
	    
	    if(opacity == null)
	        opacity = 2;
	
	    this.opacityDiv;
	    this.parentNode = typeof(parent)=="string"?$(parent):parent;
		this.opacityDiv = document.createElement("div");
		this.cDiv = document.createElement("div");	
		
		this.opacityDiv.style.width = "100%";
		this.opacityDiv.style.height = this.parentNode.scrollHeight+"px";
		this.cDiv.style.position = this.opacityDiv.style.position = "absolute";
		this.opacityDiv.style.left = "0px";
		this.opacityDiv.style.top  = "0px";
		
		if(opacity>0 && opacity<=10)
		{
			this.opacityDiv.style.display = "block";
			if(isIE)
				this.opacityDiv.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+opacity*10+")";
			else
				this.opacityDiv.style.opacity = "0."+opacity;
		}
		else
		{
			this.opacityDiv.style.display = "none";
		}
		
		this.cDiv.innerHTML = innerHTML;
		
		if(left == null)
		    left = (this.parentNode.scrollWidth - this.cDiv.scrollWidth)/2;
		this.cDiv.style.left = left + "px";
	
		if(top == null)
		    top = (this.parentNode.scrollHeight - this.cDiv.scrollHeight)/2;
		    
		this.cDiv.style.top = top + "px";
		    
		if(zIndex)
		{
		    this.opacityDiv.style.zIndex = zIndex;
		    this.cDiv.style.zIndex = zIndex+1;
		}
		
		this.parentNode.setAttribute("positionB",this.parentNode.style.position);
		this.parentNode.style.position = "relative";
		this.parentNode.appendChild(this.opacityDiv);
		this.parentNode.appendChild(this.cDiv);
		
		if(backImg)
		    this.opacityDiv.style.backgroundImage = "url("+backImg+")";
		if(backColor)
		    this.opacityDiv.style.backgroundColor = backColor;
	    else
	        this.opacityDiv.style.backgroundColor = "#fff";
		
	}
	
	WaitingUI.prototype.dispose = function()
	{
	    this.parentNode.removeChild(this.opacityDiv);
	    this.parentNode.removeChild(this.cDiv);
	    this.parentNode.style.position = this.parentNode.getAttributeNode("positionB").value;
	    delete this.opacityDiv;
	    delete this.cDiv;
	    this.opacityDiv = null;
	    this.cDiv = null;
	}
	///////////////////////////////////////////////////////////
	
///////////////////	
//左右滚动内容
//参数说明:e:要进行滚动的对象;width:要滚动的距离;p:滚动的速度参数,该值在1-100范围内;fun:滚动完成后调用的函数
//////////////////
function scrollList(e,width,p,fun)
{             
    e.scrollLeft += p*width/100;
    
    if((width < 0 && e.scrollLeft == 0) || (width > 0 && (e.scrollLeft + parseInt(e.style.width)) == e.scrollWidth))
    {
        if(fun)
           fun(width>0?1:-1);
        return;
    }
    
    if(Math.abs(width) <= 1)
    {
        e.scrollLeft += width;
        
        if(fun)
        {
            if((width < 0 && e.scrollLeft == 0) || (width > 0 && (e.scrollLeft + parseInt(e.style.width)) == e.scrollWidth))
                fun(width>0?1:-1);
            else
                fun(0);
        }
        return;
    }
    
    setTimeout(function(){scrollList(e,(1-p/100)*width,p,fun);},80);
}


//获取当前页面地址中search部分包含的参数值
function getQueryValue(key)
{
    key = key.toLowerCase();
    var href = location.search.toLowerCase();
    var arr  = href.match("[?,&]"+key+"=([^&]*)&?");
    if(arr != null)
        return arr[1];
    return null;
}

//显示全屏半透明遮盖
function showFullScreenCover(isDisplay)
{
    if(isDisplay == true)
    {
        $("coverDiv").className = "display";
        $("coverDiv").style.height = document.documentElement.scrollHeight + "px";
        $("coverDiv").style.width = document.documentElement.scrollWidth + "px";
    }
    else
        $("coverDiv").className = "undisplay";
}