﻿// JScript 文件

(function(){
    
    var AD = window.AD = function(xmlDocument){
        this.xmlDocument = xmlDocument;
    }
    AD.prototype = {

        hasChild : function(cn){// 是否有子节点
            return cn && cn.childNodes;
        },
        isAttrArray : function(r){ //
            return r && typeof r.length == 'number';
        },
        isDocument : function(cn){ //元素节点?
            return cn && cn.nodeType == 1;
        },
        each : function(array,fn,scope){ //遍历数组对象
            if(typeof array.length == 'undefined' || typeof array == 'string' ){
                arry = [array];
            }
            for(var i = 0;i<array.length;i++){
                if(fn.call(scope || array[i], array[i], i, array) === false){return i};
                
                //fn的执行结果返回false将会中断遍历并返回当前索引
            }
        },
        add : function(target,name){//target:容器id ,name: xml标识
              var d = document
              ,$ = function(id){return d.getElementById(id)};
              
              this.tg = $(target);
              this.data = this.getDataByName(name);
              if(this[this.data.type]){//如果type是未知的,将什么也不会发生
              
                    this[this.data.type](); //type已知, 根据type类型执行 img或flash
              }
              return this;
        },
        flash : function(){ //向容器插入flash AD
            var str ='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="'+ this.data.width +'" height="'+this.data.height+'">';
                        str+='<param name="movie" value="'+ this.data.url +'">';
                        str+='<param name="quality" value="high">';
                        str+='<param name="wmode" value="transparent">';
                str+='</object>';
            this.tg.innerHTML = str;
        },
        img : function(){ //插入img AD
            var str='<a href="'+ this.data.href +'" target="'+ this.data.target +'"><img style="border:0" src="'+ this.data.url +'" width="'+ this.data.width +'" height="'+this.data.height+'"';
            if(this.data.alt){
                str+= 'alt="'+ this.data.alt +'"';
            }
            str +=  '/></a>';
            this.tg.innerHTML = str;
        },
        getDataByName : function(n){ //根据已知 name 返回 该节点数据
            if(!n)return;
            if(!this.hasChild(this.xmlDocument)) return;
            var c = this.xmlDocument.childNodes;
            var result = {};
            this.each(c,function(item){
                if(this.isDocument(item)){
                    if(this.isAttrArray(item.attributes)){
                        var a = item.attributes.getNamedItem('name');
                        if(a.value == n){
                            this.each(item.attributes,function(c){
                            
                                result[c.name] = c.value;
                                
                            });
                        }
                    }
                }
            },this);
            return result;
        }
    }
})();