//******************************************************************************************
var isIE= (navigator.appName == "Microsoft Internet Explorer");
var isFireFox= ( navigator.appName == "Netscape");
if (isFireFox){
 XMLDocument.prototype.loadXML = function(xmlString)
 {
 var childNodes = this.childNodes;
 for (var i = childNodes.length - 1; i >= 0; i--)
 this.removeChild(childNodes[i]);
 var dp = new DOMParser();
 var newDOM = dp.parseFromString(xmlString, "text/xml");
 var newElt = this.importNode(newDOM.documentElement, true);
 this.appendChild(newElt);
 };
 if( document.implementation.hasFeature("XPath", "3.0") )
 {
 XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
 {
 if( !xNode ) { xNode = this; }
 var oNSResolver = this.createNSResolver(this.documentElement)
 var aItems = this.evaluate(cXPathString, xNode, oNSResolver,
 XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
 var aResult = [];
 for( var i = 0; i < aItems.snapshotLength; i++)
 {
 aResult[i] = aItems.snapshotItem(i);
 }
 return aResult;
 }
 Element.prototype.selectNodes = function(cXPathString)
 {
 if(this.ownerDocument.selectNodes)
 {
 return this.ownerDocument.selectNodes(cXPathString, this);
 }
 else{throw "For XML Elements Only";}
 }
 }
 if( document.implementation.hasFeature("XPath", "3.0") )
 {
 XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
 {
 if( !xNode ) { xNode = this; }
 var xItems = this.selectNodes(cXPathString, xNode);
 if( xItems.length > 0 )
 {
 return xItems[0];
 }
 else
 {
 return null;
 }
 }
 Element.prototype.selectSingleNode = function(cXPathString)
 {
 if(this.ownerDocument.selectSingleNode)
 {
 return this.ownerDocument.selectSingleNode(cXPathString, this);
 }
 else{throw "For XML Elements Only";}
 }
 }
}
var _h = function(){
 this._data = new Object();
}
 function _h$add(key,value){
 if(!key) return;
 if(typeof(value) === 'undefined') return;
 this._data[key] = value;
 }
 function _h$addRange(h){
 if(!h || !h._data) return;
 for(var key in h._data){
 var value = h._data[key];
 this.add(key,value);
 }
 }
 function _h$remove(key){
 if(!key) return null;
 var value = this._data[key];
 delete this._data[key];
 return value;
 }
 function _h$removeAt(index){
 if(isNaN(index)) return null;
 var i = 0;
 for(var key in this._data){
 if(i == index){
 return this.remove(key);
 }
 i++;
 }
 return null;
 }
 function _h$removeRange(startIndex,endIndex){
 if(isNaN(startIndex) || isNaN(endIndex)) return null;
 if(startIndex > endIndex)return null;
 var i = 0;
 var h = new _h();
 for(var key in this._data){
 if(i>=startIndex && i<= endIndex){
 h.add(key,this.remove(key));
 }else if(i>endIndex){
 break;
 }
 i++;
 }
 return h;
 }
 function _h$getCount(){
 var i = 0;
 for(var key in this._data) i++;
 return i;
 }
 function _h$find(key){
 if(!key) return null;
 return this._data[key];
 }
 function _h$forEach(method,instance){
 var i = 0;
 for(var key in this._data){
 var value = this._data[key];
 if(typeof(value) !== 'undefined'){
 var r = method.call(instance, key, value, i, this);
 if(r == false) break;
 i++;
 }
 }
 }
 function _h$getKeys(){
 var arr = new Array();
 for (var key in this._data){
 arr.push(key);
 }
 return arr;
 }
 function _h$getValues(){
 var arr = new Array();
 for (var key in this._data){
 arr.push(this._data[key]);
 }
 return arr;
 }
function _h$getItem(key){
if(!key) return undefined;
var item = this._data[key];
if (typeof(item) !== 'undefined'){
return item;
}else{
delete this._data[key];
return undefined; }
}
_h.prototype = {
 _data : null,
 add : _h$add,
 addRange: _h$addRange,
 remove : _h$remove,
 removeAt : _h$removeAt,
 removeRange : _h$removeRange,
 getCount : _h$getCount,
 find : _h$find,
 forEach : _h$forEach,
 getKeys : _h$getKeys,
 getValues : _h$getValues,
 getItem: _h$getItem
}
_h.__typeName = '_h';
/******************************************************************************************
** Created By Zhangzy on 2008-04-16
** Defined all adum classes using JS
** The ADUM engine will genarate the static xml file which contains the AD resource used by a position .
** THIS JS will get and parse the info. with AJAX.
** After then THIS JS will call DHTML method to force the AD resource to display in HTML page.
** Also THIS JS contains the function which will be raised by the AD click event.
** 2008-04-18,Zhangzy
** Provide an another method to fetch AD because AJAX can not call url cross domains.
** So we implement to get a javascript from server using DHTML.
** 2008-04-22,Zhangzy
** Add exception processoptimize script
** 2008-04-22,Zhangzy
** Add debug method,Add CHINESE support tricks
** 2008-5-8,Zhangzy
** Add Flash fscommand supportsuggested
*******************************************************************************************/
function _aU()
{
if(typeof _aU._initialized =='undefined')
{
_aU.prototype.debug=function(info)
{
var de = document.getElementById('adumDebugInfo');
if (de)
de.value=de.value +"\n" + info;
}
_aU.prototype.addURLParam=function(sURL,sParamName,sParamValue)
{
sURL += (sURL.indexOf("?")==-1?"?":"&");
sURL += encodeURIComponent(sParamName)+"=" + encodeURIComponent(sParamValue);
return sURL;
}
_aU.prototype.setInnerHTML = function (el, htmlCode) {
 var ua = navigator.userAgent.toLowerCase();
 if (ua.indexOf('msie') >= 0 && ua.indexOf('opera') < 0) {
 htmlCode = '<div style="display:none">for IE</div>' + htmlCode;
 htmlCode = htmlCode.replace(/<script language="JavaScript([^>]*)>/gi,'<script language="JavaScript$1 defer="defer">');
 el.innerHTML = htmlCode;
 el.removeChild(el.firstChild);
 } else {
 var el_next = el.nextSibling;
 var el_parent = el.parentNode;
 el_parent.removeChild(el);
 el.innerHTML = htmlCode;
 if (el_next) {
 el_parent.insertBefore(el, el_next)
 } else {
 el_parent.appendChild(el);
 }
 }
}
_aU._initialized=true;
}
}
function _aI(name,url)
{
this.name=name;
this.url=url;
}
function _aR(resCode,beginHour,endHour,htmlCode)
{
this.beginHour=beginHour;
this.endHour = endHour;
this.resCode=resCode;
this.htmlCode = htmlCode;
this.adInfoArray = new _h();
if(typeof _aR._initialized =='undefined')
{
_aR.prototype.addAdInfo=function(info)
{
this.adInfoArray.add(info.name,info);
}
_aR._initialized = true;
}
}
function _aS(schCode,schType)
{
this._mU = new _aU();
this.schCode = schCode;
this.schType = schType;
this.hashAdResource = new _h();
this.activeResource=null;
if(typeof _aS._initialized =='undefined')
{
_aS.prototype.addResource=function(res)
{
this.hashAdResource.add(res.resCode,res);
}
_aS.prototype.chooseResource=function()
{
switch(this.schType)
{
case '0':
var keys = this.hashAdResource.getKeys();
if (keys.length>0){
this.activeResource = this.hashAdResource.getItem(keys[0]);
}
break;
case '1':
var keys = this.hashAdResource.getKeys();
if(keys.length>0){
this.activeResource = this.hashAdResource.getItem(keys[0]);
}
break;
case '2':
{
var objFind=null;
var hh = new Date().getHours();
this.hashAdResource.forEach(function(key,item,i,obj){
if(item.beginHour<=hh && hh <= item.endHour){
objFind=item;
return false;
}
});
this.activeResource=objFind;
}
break;
case '3':
var keys = this.hashAdResource.getKeys();
if (keys.length>0){
var r = Math.floor(Math.random()*keys.length);
this.activeResource = this.hashAdResource.getItem(keys[r]);
}
break;
}
return this.activeResource;
}
_aS._initialized = true;
}
}
function _aP(pcode,sch,divId)
{
this.posCode = pcode;
this.schObject = sch;
this.divId = divId;
};
function _aPP()
{
this.allPosition = new _h();
if(typeof _aPP._initialized == "undefined"){
_aPP.prototype.addPosition=function(pos)
{
this.allPosition.add(pos.posCode,pos);
}
_aPP.prototype.getPosition=function(pcode)
{
return this.allPosition.getItem(pcode);
}
_aPP._initialized=true;
}
}
var m_pagePosition = new _aPP();
var m_AdumRootUrl = "http://www.bj.10086.cn/adum";
var m_AdumClickLogCallbackUrl = "http://www.bj.10086.cn/www/adum/adumevent.jsp";
var m_AdumJSInfo = new _h();
var _mU = new _aU();
function fetchAdByJS(id,pcode)
{
m_AdumJSInfo.add(pcode,id);
var d = new Date();
var url=m_AdumRootUrl +"/" + pcode +"/adum.js";
url = _mU.addURLParam(url,"a", d.getYear()+"-"+d.getMonth()+1+"-"+d.getDate() +"-" + d.getHours() +"-" + d.getMinutes());
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
 script.type= 'text/javascript';
head.appendChild(script);
script.src=url;
}
function AdumJSLoaded( pcode,xml)
{
_mU.debug('AdumJSLoaded() pcode='+pcode+',xml=\n' + xml);
var objId = m_AdumJSInfo.getItem(pcode);
var xmlDoc;
if(window.DOMParser)
{
domParser = new DOMParser();
xmlDoc = domParser.parseFromString(xml,"text/xml");
}
else if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
xmlDoc.async = false;
xmlDoc.loadXML(xml);
}
AdumParseXml(xmlDoc,objId);
}
function AdumFlashCommand(pcode,rcode,cmd,param)
{
_mU.debug('Enter AdumFlashCommand(' + pcode+"," + rcode+"," + cmd +"," + param);
if (cmd.toLowerCase()=="adum")
{
AdumClickAction(pcode,rcode,param,"");
}
else
{
if(typeof(On_AdumFSCommand)=="function")
{
var pos = m_pagePosition.getPosition(pcode);
if (pos)
On_AdumFSCommand(pos.divId,pcode,rcode,cmd,param);
else
On_AdumFSCommand(null,pcode,rcode,cmd,param);
}
}
}
function AdumClickImage(pcode,rcode,adName)
{
var pos=m_pagePosition.allPosition.getItem(pcode);
var res= pos.schObject.activeResource;
var info = res.adInfoArray.getItem(adName);
if(info)
{
openWindow(info.url);
AdumClickAction(pos.posCode,res.resCode,info.name,"");
}
}
function openWindow(site) {
window.open(site,'_blank', "height=500, width=800, top=0,left=0,toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes, location=yes, status=yes");
}
function AdumClickAction(pcode,rcode,adName,adUrl)
{
_mU.debug("enter AdumClickAction()");
var url = m_AdumClickLogCallbackUrl;
url = _mU.addURLParam(url,"type","js");
url = _mU.addURLParam(url,"pcode",pcode);
url = _mU.addURLParam(url,"rcode",rcode);
url = _mU.addURLParam(url,"iname",adName);
url = _mU.addURLParam(url,"iurl",adUrl);
url = _mU.addURLParam(url,"rand",Math.random());
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.type= 'text/javascript';
head.appendChild(script);
script.src=url;
_mU.debug("AdumClickAction:"+url);
}
function AdumParseXml(xmlDoc,objId)
{
_mU.debug('****Enter AdumParseXml():' + xmlDoc +',' + objId);
var xmlPos = xmlDoc.selectSingleNode( "/adum/position" );
var adPos = new _aP();
adPos.posCode = xmlPos.getAttribute("code");
adPos.divId = objId;
_mU.debug('adPos.posCode=' + adPos.posCode);
var xmlSch = xmlDoc.selectSingleNode( "/adum/schedule" );
var adSch = new _aS();
adSch.schCode = xmlSch.getAttribute("code");
adSch.schType = xmlSch.getAttribute("type");
_mU.debug('adSch.schCode='+ adSch.schCode);
adPos.schObject = adSch;
var allRes= xmlDoc.getElementsByTagName( "resource" );
_mU.debug('allRes.length=' + allRes.length);
for ( var i=0;i<allRes.length ;i++){
var code = allRes[i].getAttribute("code");
var begin = allRes[i].getAttribute("begin");
var end = allRes[i].getAttribute("end");
var htmlCode ;
if(window.XMLHttpRequest){
htmlCode = allRes[i].selectSingleNode("htmlCode").childNodes[0].nodeValue;
}
else{
htmlCode = allRes[i].selectSingleNode("htmlCode").text;
}
_mU.debug('htmlCode=' + htmlCode);
var adRes = new _aR(code,begin,end,htmlCode);
var allInfo = allRes[i].getElementsByTagName( "info" );
for(var j=0;j<allInfo.length;j++)
{
var info = new _aI(allInfo[j].getAttribute("name"),allInfo[j].getAttribute("url"));
adRes.addAdInfo(info);
}
adSch.addResource(adRes);
}
m_pagePosition.addPosition(adPos);
var activeRes=adSch.chooseResource();
_mU.debug('activeRes='+ activeRes);
if(activeRes)
_mU.setInnerHTML(document.getElementById(objId), activeRes.htmlCode);
}
