﻿/*------------------------------------------------------------------
    Author      :   Hotel.M.Hou
    Create Date :   2008-02-01
    Function    :   Banner Class
    NameSpace   :   Newegg.Xpert
-------------------------------------------------------------------*/

/*-------- Register Namespace (Start)------------------------------*/
var Newegg = ( ( "undefined" == typeof( Newegg ) ) ? {} : Newegg );
Newegg.Xpert = ( ( "undefined" == typeof( Newegg.Xpert ) ) ? {} : Newegg.Xpert );
/*-------- Register Namespace (End)------------------------------*/

var Xpert = Newegg.Xpert;

// The constructor of Banner Class   
Xpert.Banner = function( name,imgName, flashName, cookieName, rotatiOnRefresh, picAppearanceMethod ,TransPic)
   {
     this.transPic = TransPic;
     this.internalName = name;  
     this.bannerAD=new Array();
     this.bannerADlink=new Array();
     this.bannerADtarget=new Array();
     this.bannerDuration = new Array();
     this.bannerType = new Array();
     this.bannerWidth = new Array();
     this.bannerHeight = new Array();
     this.bannerText = new Array();
     this.adNum = -1;   
     this.cookieKeyName = cookieName; 
     //swap the picture only at refreshing
     this.rotationRefresh = rotatiOnRefresh; 
     //The picture appearance method : Random/Ordered
     this.picAppearanceMethod = picAppearanceMethod; 
     //this.preloadedimages=new Array();
     
     this.bannerImage = document.getElementById(imgName);
     this.bannerFlash = document.getElementById(flashName);
   }   

Xpert.Banner.prototype.setTransition =  function()
    {
       //Is IE broser. 
       if (document.all)
       {
          this.bannerImage.filters.revealTrans.Transition=12;
          this.bannerImage.filters.revealTrans.apply();
       }
    }

Xpert.Banner.prototype.playTransition = function()
    {
       if (document.all)
          this.bannerImage.filters.revealTrans.play();
    }

Xpert.Banner.prototype.nextAd =function()
    {  
       this.adNum = this.getPictureNumber();
          
       //It is a flash object
       if ( "SWF" == this.bannerType[this.adNum].toUpperCase())
       {
            var new_margin = parseInt(this.bannerHeight[this.adNum])+5;
            this.bannerImage.style.display = "none";        
            this.bannerFlash.innerHTML = "<embed src=" + this.bannerAD[this.adNum] +" width=" + this.bannerWidth[this.adNum] +" height=" + this.bannerHeight[this.adNum] +" type=application/x-shockwave-flash   wmode=Opaque></embed>";
            this.bannerFlash.innerHTML += "<img style='border:0;margin-top:-" + new_margin + "px; z-index:9999;opacity:0;' src="+this.transPic+" width=" + this.bannerWidth[this.adNum] + " height=" + this.bannerHeight[this.adNum]  + ">";
            this.bannerFlash.href = this.bannerADlink[this.adNum];
            this.bannerFlash.target = this.bannerADtarget[this.adNum];
                        
            this.bannerFlash.style.display = "block";   
       }
       else  //It is a picture object
       {   
           this.bannerImage.style.display = "block";
           this.bannerFlash.style.display = "none";
           this.bannerFlash.innerHTML ="";
           
           this.setTransition();
           this.bannerImage.src= this.bannerAD[this.adNum];
           this.bannerImage.width = this.bannerWidth[this.adNum];
           this.bannerImage.height = this.bannerHeight[this.adNum];
           this.bannerImage.title = this.bannerText[this.adNum];
           this.bannerImage.alt = this.bannerText[this.adNum];
           this.playTransition();
       } 
         
       if ( !this.rotationRefresh )
       {
            window.setTimeout(this.internalName+".nextAd()", this.bannerDuration[this.adNum] * 1000 );
        }
    }

Xpert.Banner.prototype.jump2url = function()
    {
       var jumpUrl = this.bannerADlink[this.adNum];
       var jumpTarget = this.bannerADtarget[this.adNum];
       
       if (jumpUrl && jumpUrl != '')
       {
          if (jumpTarget != '')window.open(jumpUrl,jumpTarget);
          else window.open(jumpUrl,"","");
       }
    }

Xpert.Banner.prototype.getPictureNumber = function()
    {
        var tmpNumber = this.adNum;
        
        if ( "ordered" == this.picAppearanceMethod.toLowerCase() )
        {
            if ( !this.rotationRefresh )
            {
                if( tmpNumber < this.bannerAD.length-1 ) 
                {
                    tmpNumber ++ ;
                 }
                else 
                {
                    tmpNumber = 0;
                 }
            }
            else
            {
                var Cookie = Newegg.Xpert.Cookie;
                if ( Cookie.exist( this.cookieKeyName ) )
                {
                    tmpNumber = parseInt( Cookie.getValue( this.cookieKeyName ) );
                    if( tmpNumber < this.bannerAD.length-1) 
                    {
                        tmpNumber ++ ;
                     }
                    else 
                    {
                        tmpNumber = 0;
                     }
                     var expireTime = new Date();
                     expireTime.setTime(expireTime.getTime()+ 60 * 60 * 24 * 10);
                     Cookie.update( this.cookieKeyName, ""+tmpNumber+"",expireTime); 
                }
                else
                {
                    if( tmpNumber < this.bannerAD.length-1) 
                    {
                        tmpNumber ++ ;
                     }
                    else 
                    {
                        tmpNumber = 0;
                     }
                    
                     var expire = new Date();
                     expire.setTime(expire.getTime()+ 60 * 60 * 24 * 10);
                     Cookie.add( this.cookieKeyName, ""+tmpNumber+"",expire);  
                }            
            } 
             
        }
        else
        {
            tmpNumber = this.getRandomNumber( this.bannerAD.length-1 );
        }
        
        return tmpNumber;
    }

Xpert.Banner.prototype.getRandomNumber = function( Num )
    {
       return  Math.round( Math.random() * Num , 0 );    
    }