/*ASP.net adRotator组件 JScript 版

XML 文件结构
<Advertisements>
  <Ad>
    <ImageUrl>AD_Files/info_468x60_Banner_AQU.gif</ImageUrl>
    <NavigateUrl>ShowProductList.aspx?sid=38</NavigateUrl>
    <AlternateText>AQU</AlternateText>
    <Keyword>468x60</Keyword>
    <Impressions>2</Impressions>
  </Ad>
</Advertisements>

HTML 例(1):

<script type="text/javascript" src="XMLadRotator.js"></script>
<html>
	<body onload="">
		<a href="http://pws.paic.com.cn" name="adURL1" id="adURL1"><img src="" name="adImg1" id="adImg1" width="468" height="60"></a>
		<a href="http://pws.paic.com.cn" name="adURL2" id="adURL2"><img src="" name="adImg2" id="adImg2" width="468" height="60"></a>
	
		<script lang="javascript">
			XMLadRotator('Crystal_ADs.XML','adURL1','adImg1','468x60',3000);
			XMLadRotator('Crystal_ADs.XML','adURL2','adImg2','468x60',2000);
		</script>
	</body>
</html>

HTML 例(2):
<script type="text/javascript" src="XMLadRotator.js"></script>
<html>
	<body onload="javascript:XMLadRotator('Crystal_ADs.XML','adURL1','adImg1','468x60',3000);XMLadRotator('Crystal_ADs.XML','adURL2','adImg2','468x60',2000);">
		<a href="http://pws.paic.com.cn" name="adURL1" id="adURL1"><img src="" name="adImg1" id="adImg1" width="468" height="60"></a>
		<a href="http://pws.paic.com.cn" name="adURL2" id="adURL2"><img src="" name="adImg2" id="adImg2" width="468" height="60"></a>
	</body>
</html>

*/



String.prototype.Trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

function checkImages(oPreLoadimage){
	if(typeof oPreLoadimage!='object'){
		alert(oPreLoadimage + " 不是一个对象！");
	}
	if (oPreLoadimage.readyState=="complete") { 
	//alert("complete"); 
	} else { 
	window.setTimeout("checkImages(" + oPreLoadimage + ")",1000); 
	} 
} 



/*
 * xmlFile        : XML文件名
 * HREFid         : <a href="" id="HREFid" ></a> 标记中 id值
 * SRCid          : <img src="" id="SRCid" > 标记中 id值
 * sKeyword       : 分类关键字
 * slideShowSpeed : 更换图片间隔时间
 */
function XMLadRotator(xmlFile,HREFid,SRCid,sKeyword,slideShowSpeed)
{
	var i;
	var xmlDoc;
	var moz,ie;
		
	//判断是否为Firefox或IE
	moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined');
	ie = (typeof window.ActiveXObject != 'undefined');

 	if (moz) {
	  	//创建FIREFOX下XML文档对象
			xmlDoc = document.implementation.createDocument("", "", null);
			}
	else if (ie){
			//创建IE下XML文档对象
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async = false;
	}

  xmlDoc.load(xmlFile);
  
	if (moz){
		//FireFox 未调试完整
		if(xmlDoc.selectNodes("Keyword")(0).text == sKeyword){
			 var ad = xmlDoc.getElementsByTagName("Ad")(0);
			 var sImageUrl;
			 var _sImageUrl = ad.getElementsByTagName("ImageUrl")[i].firstChild.nodeValue;
			 if (_sImageUrl) sImageUrl = _sImageUrl.Trim();
		
			 //得到ImageUrl,NavigateUrl节点下的值
			 var sNavigateUrl;
			 var _sNavigateUrl=ad.getElementsByTagName("NavigateUrl")[i].firstChild.nodeValue;
	
			 if(_sNavigateUrl){
				 sNavigateUrl = _sNavigateUrl.Trim();
				 document.getElementById(HREFid).href = sImageUrl;
				 document.getElementById(SRCid).src = sNavigateUrl;
			 }
	 	}
	}
	else if (ie)
	{
		//获得根接点 
		var nodes = xmlDoc.documentElement.childNodes;
    var NavigateUrls = new Array;
    var ImageUrls = new Array;
    var j = 0;
    var r = 0;
    
		//得到根接点下共有子接点个数，并循环 
		for(var i=0; i<nodes.length; i++) { 
			//如果接点名为 "Ad" 并且 "Keyword" 的值等于变更 sKeyword 中的字符
			if ((nodes(i).nodeName == "Ad")&&(nodes(i).selectNodes("Keyword")(0).text == sKeyword)) { 
					NavigateUrls[j] = nodes(i).selectNodes("NavigateUrl")(0).text;
					ImageUrls[j] = nodes(i).selectNodes("ImageUrl")(0).text;
					j++;
			} 
		}
		
		//生成随机数 r 取得范围以 j 的值为准
		r = parseInt(Math.random()*(j-1))+1;
		_preLoadimage = new Image();
		_preLoadimage.src = ImageUrls[r]; 
		checkImages(_preLoadimage);
		
		document.getElementById(HREFid).href = NavigateUrls[r];
		//document.getElementById(SRCid).src =  ImageUrls[r];		
		document.getElementById(SRCid).src = _preLoadimage.src;
	}
	setTimeout("XMLadRotator('"+ xmlFile + "','" + HREFid + "','" + SRCid + "','" + sKeyword +"',"+parseInt(slideShowSpeed)+")",parseInt(slideShowSpeed)); 
}


