// JavaScript Document
//*****************************************************************************
// Do not remove this notice.
//
// Copyright 2007 by whb147（苦乐随缘）.

// 特别感谢CSDN的兄弟大力支持
//*****************************************************************************

// Determine browser and version.

function Browser() {
 
  var ua, s, i;

  this.isIE    = false;  // Internet Explorer
  this.isNS    = false;  // Netscape
  this.version = null;
  this.name = "" ;
  
  this.getBrowserName = getBrowserName;
  
  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    this.name = s;
 return; 
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
 this.name = s;
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
 this.name = s;
    return;
  }
  
  s = "Opera";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = 6.1;
 this.name = s;
    return;
  }
}

function getBrowserName()
{
 
 return this.name; 
}

function Option(html, value, css, selected) { 
 this.text = html; 
 this.value = value; 
 this.css = css; 
 this.isSelected = selected; 
}
function selectWhb(name)
{
 this.name = name;
 this.id = name;
 this.width = 125;
 this.height = 25;
 this.height_2 = 100;
 this.fontSize = 10;
 this.isOpened = false ;
 this.bigid=0;
 this.smallid=0;
 this.xid=0;
 this.dataArray = new Array(); 
 this.browser = new Browser();
 
 this.init = selectInit;
 this.writeCss = writeCssSlt;
 this.writeObject = writeSelect;
 this.onClickBtn = onClickSlt;
 this.onSelected = onSelectedSlt;
 this.onClickOption = onClickOption;
 this.hiddenDiv = hiddenOption;
 this.changeColor = changeOptionColor;
 this.changeColorBack = changeOptionColorBack;
 this.onChange=function(){};
 
 this.onLeaveSelect = onLeaveSelect;
 this.isLeave = isLeaveSelect;
 
 this.addOption = addSelectOption;
 
 this.getTextByte = getTextByte;
 this.subTextByteString = subTextByteString;

}

function addSelectOption(text,value,css,selected)
{
	 this.dataArray[this.dataArray.length] = new Option(text,value,css, selected);
}

function selectInit() 
{
 this.writeCss(this);
 this.writeObject(this);
}
//控制样式
function writeCssSlt()
{
 var textWight = this.width-17 ;  //显示的宽度
 var optiontWight = this.width-2; //列表的宽度
 document.write("<style type=\"text/css\">");
 document.write("#sltWhb"+this.name+" li{list-style-type:none;margin:0px; width:110px;}");
 document.write("#sltWhb"+this.name+"{width:"+this.width+"px;z-index:1;font-size:"+this.fontSize+"pt;border:0px solid red;padding:0px;margin:0px;text-algin:left;}");
  document.write("#sltText"+this.name+"{ width:"+this.width+"px;height:20px;");
  document.write("padding-top:4px;padding-bottom:0px;z-index:2;color:#333333;overflow:hidden;cursor:default; }");
 document.write("#sltOption"+this.name+"{border:1px solid #7F9DB9;  cursor:default; width:"+optiontWight+"px; z-index:3;display: none; padding:0px;margin:0px;position:absolute;background:#ffffff;}");
 document.write(".pCss"+this.name+"{list-style-type:none;cursor:hand; margin:0px;font-size:12px;overflow:hidden;width:"+optiontWight+"px;height:14px;line-height:14px;border:0px solid red;margin:0px;word-break:keep-all;padding:2px;}");
 document.write(".btnIE(float:left;width:20px;height:14px;)");
 document.write(".btnNS(width:20px;height:14px;)");
 document.write("</style>");
}


function writeSelect()
{
 //需要修改的地方

document.write("<div id=\"sltWhb"+this.name+"\" onmouseout =\""+this.name+".onLeaveSelect(event)\"   style=\"margin:0px;border:0px;height:25px; background:url(/images/select_bg_1.jpg) no-repeat; font-size:13px;font-family:\"宋体\";\">");
document.write("<input type=\"hidden\" name=\""+this.name+"3\"  id=\""+this.id+"3\" value=\""+this.dataArray[0].value+"\" />");
document.write("<ul style=\"margin:0px;padding:0px;\">");
document.write("<li id=\"sltText"+this.name+"\"   onClick=\""+this.name+".onClickBtn('sltOption"+this.name+"');\" style=\"text-align:left;padding-left:5px;padding-right:17px;margin-top:-3px; overflow:hidden;\" >"+this.dataArray[0].text+"</li>");
document.write("<li id=\"sltOption"+this.name+"\" onmouseleave=\""+this.name+".onClickOption('sltOption"+this.name+"')\"  onClick=\""+this.name+".onClickOption('sltOption"+this.name+"')\" style=\"text-align:left;padding-left:5px;width:"+(this.width-7)+"px;margin-top:0px;\">");
document.write("<ul style=\"margin:0px;padding:0px; HEIGHT: "+this.height_2+"px; OVERFLOW: scroll; overflow-x:hidden;  scrollbar-face-color: #9EBFE8; scrollbar-shadow-color: #FFFFFF; scrollbar-highlight-color: #FFFFFF; scrollbar-3dlight-color: #9EBFE8; scrollbar-darkshadow-color: #9EBFE8; scrollbar-track-color: #FFFFFF; scrollbar-arrow-color: #FFFFFF;\" id=\""+this.id+"_ul\">");

for(var i=0 ;i<this.dataArray.length;i++)
 {
	
  if(this.dataArray[i].isSelected)
  {
    document.getElementById("sltText"+this.name).innerHTML = this.subTextByteString(this.dataArray[i].text,(this.width)/8);
    document.getElementById("sltText"+this.name).title = this.dataArray[i].text; 
    document.getElementById(this.id+"3").value = this.dataArray[i].value;
 
  }
  document.write("<li  style=\"font-size:13px;font-family:'宋体'; overflow:hidden;\" onClick=\""+this.name+".onSelected(this,'"+this.dataArray[i].value+"');"+this.name+".onChange();\" onMouseMove='"+this.name+".changeColor(this)'  onMouseOut='"+this.name+".changeColorBack(this)' class='pCss"+this.name+"'" );
  document.write("  title=\""+this.dataArray[i].text+"\">"+this.dataArray[i].text+"</li>"); 
 }

document.write("</ul>");
document.write("</li>");
document.write("</ul>");
document.write("</div>"); 
}


function onChangeBig()
{
	ChangeBig(this.bigid,this.smallid,this.xid);
}


function onChangeSmall()
{
	
	ChangeSmall(this.bigid,this.smallid);
}

function onSelectedSlt(obj,value)
{
 document.getElementById("sltText"+this.name).innerHTML=this.subTextByteString(obj.innerHTML,(this.width)/8); 
 document.getElementById("sltText"+this.name).title=obj.innerHTML; 
 document.getElementById(this.id+"3").value =  value;
 }

function onClickSlt(obj)
{
  if (this.isOpened==false)
  {
   document.getElementById(obj).style.display="block";
   this.isOpened =true;
  }
  else
  {
   document.getElementById(obj).style.display="none";
   this.isOpened =false;
  }
}

function onClickOption(obj)
{ 
  //alert(obj);
 document.getElementById(obj).style.display="none";
 this.isOpened =false;
}

function hiddenOption()
{
 if (this.isOpened==true)
  document.getElementById('sltOption').style.display="block";
}
//鼠标移上列表样式
function changeOptionColor(obj)
{
 obj.style.backgroundColor="#666699";
 obj.style.color="#ffffff";
}
//鼠标移走样式
function changeOptionColorBack(obj)
{
 obj.style.backgroundColor="";
 obj.style.color="#000000";
}

function onLeaveSelect(e)
{

 if(this.isLeave(e))
 {
  //alert(11);
 }
 else
 {
  document.getElementById("sltOption"+this.name).style.display="none";
  this.isOpened =false;
 }
}

function isLeaveSelect(e)
{
 if(!this.isOpened)
 {
  return false; 
 }
 
 var x,y;
 
 //这个地方需要兼容浏览器，以后再说，呵呵，这里只是测试IE

 if (this.browser.isIE)
 {
  x = window.event.x;
  y = window.event.y;
 }
 if (this.browser.isNS) 
 {
  x = e.pageX;
  y = e.pageY;  
 }
 
 
 var left = 0;
 var top = 0;
 var right = 0;
 var bottom = 0;
 
 left = document.getElementById("sltOption"+this.name).offsetLeft;
 top = document.getElementById("sltOption"+this.name).offsetTop-2;
 right = document.getElementById("sltOption"+this.name).offsetLeft+document.getElementById("sltOption"+this.name).offsetWidth;
 bottom = document.getElementById("sltOption"+this.name).offsetTop+document.getElementById("sltOption"+this.name).offsetHeight;

 if((x>left) &&(x<right)&&(y>top)&&(y<bottom))
 {
  return true
 }

 return false;
}

function getTextByte(str)
{
    tempstr=str.replace(/[\x00-\xff]/g, "a");
    tempstr=tempstr.replace(/[\uff61-\uff9f]/g, "a");//半角片假文
    tempstr=tempstr.replace(/[\u3041-\u309f]/g, "aa");//全角平假文
    tempstr=tempstr.replace(/[\u30a0-\u30ff]/g, "aa");//全角片假文
    tempstr=tempstr.replace(/[^\x61-\x61]/g, "aa");
    return tempstr.length;
}

function subTextByteString(str, len)
{
    var i = 0;
    var rtnStr = "";
    var subStr = "";
    for(i = 0; i < getTextByte(str); i++)
    {
   subStr = str.substr(i, 1);
   if(getTextByte(subStr) == 2 && len >= 2)
   {
    rtnStr = rtnStr + subStr;
    len = len - 2;
   }
   else if (getTextByte(subStr) == 1 && len >= 1)
   {
    rtnStr = rtnStr + subStr;
    len = len - 1;
   }
   else if (getTextByte(subStr) == 2 && len < 2)
   {
    break;
   } 
   else if (getTextByte(subStr) == 1 && len < 1)
   {
    break;
   }
  }
    return rtnStr
}



 function ReSizeImg(objImg,maxWidth,maxHeight)
    {
       /* var image=new Image(); 
        image.src=Img.src; 
        if(image.width>width||image.height>height)    
        {
            w=image.width/width;
            h=image.height/height;
            if(w>h)
            {
                Img.width=width;
                Img.height=image.height/w;
            }
            else
            {
                Img.height=height;
                Img.width=image.width/h;
            }
        }
		if(Img.height<height)
		{
			
			Img.style.marginTop=(((height-Img.height)/2)+5)+"px";
			Img.style.marginBottom=(((height-Img.height)/2)+5)+"px";
		//	alert(Img.style.marginBottom)
		}
		
		if(Img.width<width)
		{
			Img.style.marginLeft=(((width-Img.width)/2)+5)+"px";
			Img.style.marginRight=(((width-Img.width)/2)+5)+"px";
		}*/


		var img = new Image();
		img.src = objImg.src;
		var hRatio;
		var wRatio;
		var Ratio = 1;
		var w = img.width;
		var h = img.height;
		wRatio = maxWidth / w;
		hRatio = maxHeight / h;
		if (maxWidth ==0 && maxHeight==0){
		Ratio = 1;
		}else if (maxWidth==0){//
		if (hRatio<1) Ratio = hRatio;
		}else if (maxHeight==0){
		if (wRatio<1) Ratio = wRatio;
		}else if (wRatio<1 || hRatio<1){
		Ratio = (wRatio<=hRatio?wRatio:hRatio);
		}
		if (Ratio<1){
		w = w * Ratio;
		h = h * Ratio;
		}
		objImg.height = h;
		objImg.width = w;
	 
	 
}
	
	
