﻿// JScript File
 if(typeof MX == "undefined")
{
    MX = {};
    MX.ToArray = function(l,s,e){var a=[];s=s||0;e=e||l.length;if(s<=e)for(var i=s;i<e;++i)a.push(l[i]);return a;};
    MX.Delegate = function(){var Q=MX.ToArray(arguments,2);var I=arguments[0],bo=arguments[1];return function(){var aC=MX.ToArray(arguments);return bo.apply(I,Q.concat(aC))}};
}

AdsManager = function(json,link)
{        
    // this classID is stand for adobe
    this.embed = (document.all)?('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="#w#" height="#h#" ><param name="movie" value="$" /><param name="wmode" value="transparent" /><param name="autostart" value="1" /><param name="quality" value="high" /><param name="flashvars" value="MMplayerType=ActiveX" /></object>'):('<embed type="application/x-shockwave-flash" src="$" width="#w#" height="#h#" wmode="transparent" autostart="1" quality="high" flashvars="MMplayerType=PlugIn"/>');    
    this.Initialize.apply(this,arguments);
}
AdsManager.prototype = 
{
    Initialize : function(json,link)
    {           
        this.showInterval = MX.Delegate(this,this.Show);
        this.Items = [];
        for(var i=0,n=json.items.length;i<n;++i)
        {
            this.Items.push(json.items[i]);
        }
        this.current=-1;
        this.link = document.getElementById(link);
        this.link.target = "_blank";    
        this.arrImg = [];
        this.flag = [];              
        for(var i=0,n=this.Items.length;i<n;++i)
        {
            if(this.Items[i].type == "image")
            {
                this.arrImg.push(null);              
                this.flag.push(0);
            }
            else
            {
                this.arrImg.push("$");
                this.flag.push(-1);
            }                      
        }            
        this.player = document.createElement("div");   
        this.html = document.createElement("div");
        this.link.appendChild(this.player);
        this.link.appendChild(this.html);
        this.Show();
    }
    , CreateImage : function(item)
    {
        var img = document.createElement("img");
        img.src = item.src;
        img.style.cssText = "border:none; width:"+item.width+"px; height:"+item.height+"px;";
        this.link.appendChild(img);
        this.arrImg[this.current] = img;
    }
    , Show : function()
    {       
        this.Reset();
        this.current++;
        if(this.current>=this.Items.length) this.current = 0;
        
        var item = this.Items[this.current];        
        this.link.href = item.link;
        
        if(item.type == "image")
        {
            if(this.flag[this.current]==0)  
            {        
                this.CreateImage(item);
                this.flag[this.current]=1;
            }
            else if(this.flag[this.current]==1)  
            {
                this.arrImg[this.current].style.display = "block";
            }              
        }                
        else if(item.type == "flash")
        {
            this.player.style.display = "block";
            this.player.innerHTML = this.BuildPlayer(item.src,item.width,item.height);
        }
        else if(item.type == "html")
        {
            this.html.style.display = "block";
            this.html.innerHTML = item.src;
        }
        else
        {
            // unknow item type
        }
        window.setTimeout(this.showInterval,item.period);        
    }
    , Reset : function()
    {   
        if(this.flag[this.current]==1)
        {
            this.arrImg[this.current].style.display = "none"; 
        }
        this.player.style.display = "none";
        this.player.innerHTML = "";
        this.html.style.display = "none";    
        
    }
    , BuildPlayer : function(src,width,height)
    {
        var s = this.embed.replace("$",src).replace("#w#",width).replace("#h#",height);             
        return s;
    }
}