var BannerTypeDetector = {

  banner: null,
  
  setBanner: function(banner)
  {     
	  this.currentBanner = banner;			
  },
    
  detect: function()
  {  	 
    if (!this.currentBanner) 
      return;
	   
    if ((typeof jtv_advertising_network_id == 'string' || typeof jtv_advertising_original_url == 'string') && (this.currentBanner.position == 'lp_video')) 
      this.detectAdvertisingCom();
    else if (typeof jtv_preroll_url == 'string') 
      this.detectCustomPreroll();
    else if (typeof jtv_banner_image == 'string') 
      this.detectCustomImage();
    else if (typeof jtv_banner_flash == 'string') 
      this.detectCustomFlash();
    else if (this.currentBanner.type == 'preroll') 
      this.detectEmptyPreroll();
    else 
      this.currentBanner.showIframe();
  },
  
  detectOnPageLoad: function()
  {
  	if (!this.currentBanner) 
      return;
	  
   if (typeof jtv_banner_image == 'string') 
      this.detectCustomImage();
   else if (typeof jtv_banner_flash == 'string') 
      this.detectCustomFlash();	   
	  
   jtv_banner_image = null;
   jtv_banner_flash = null;	  
  }, 
  
  detectCustomFlash: function()
  {
    var id = this.currentBanner.params.pos;
    var width = this.currentBanner.params['width'];
    var height = this.currentBanner.params['height'];
    var wmode = 'transparent';
    var bgcolor = '#ffffff';
    
    if (jtv_banner_flash_wigth && jtv_banner_flash_height) {
      width = jtv_banner_flash_wigth;
      height = jtv_banner_flash_height;
    }
    
    if (jtv_banner_flash_bg_color) {
      wmode = 'window';
      bgcolor = jtv_banner_flash_bg_color;
    }
    
    var objectProperties = {
      classid: "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000",
      codebase: "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0",
      width: width,
      height: height,
      id: id,
      align: "middle"
    };
    
    var objectParams = {
      allowScriptAccess: "sameDomain",
      allowFullScreen: "false",
      movie: jtv_banner_flash + "?clickTag=" + jtv_banner_url,
      width: width,
      height: height,
      quality: "high",
      wmode: wmode,
      bgcolor: bgcolor
    };
    
    var embedProperties = {
      src: jtv_banner_flash + "?clickTag=" + jtv_banner_url,
      quality: "high",
      wmode: wmode,
      swliveconnect: "TRUE",
      bgcolor: bgcolor,
      type: "application/x-shockwave-flash",
      allowscriptaccess: "never",
      width: width,
      height: height
    };
    
    var content = '';
    var objectTag = '';
    
    if (jtv_banner_flash_alternative_image) {
      if (!Helper.isFlashSupported()) {
        var size = 'width="' + width + '" height="' + height + '"';
        content = '<a href="' + jtv_banner_url + '" target="_blank"><img src="' + jtv_banner_flash_alternative_image + '" ' + size + ' alt=""></a>';
        this.currentBanner.showDiv(content);
        return false;
      }
    }
    
    if (Helper.getBrowser() == "IE") {
      for (var paramName in objectParams) 
        content += '<param name="' + paramName + '" value="' + eval('objectParams.' + paramName) + '" />';
      for (var propertyName in objectProperties) 
        objectTag += ' ' + propertyName + '="' + eval('objectProperties.' + propertyName) + '"';
      
      content = "<object" + objectTag + ">" + content + "</object>";
    }
    else {
      for (var propertyName in embedProperties) 
        objectTag += ' ' + propertyName + '="' + eval('embedProperties.' + propertyName) + '"';
      
      content = "<embed" + objectTag + " />";
    }
    content = this.wrapWithValignDivs(content, this.currentBanner.div.style.width, this.currentBanner.div.style.height);
    this.currentBanner.showDiv(content);
  },
  
  detectCustomImage: function()
  {
    var width = this.currentBanner.params['width'];
    var height = this.currentBanner.params['height'];
    
    var size = "";
    if (jtv_banner_image_wigth && jtv_banner_image_height) 
      size = 'width="' + jtv_banner_image_wigth + '" height="' + jtv_banner_image_height + '"';
    
    var content = '<a href="' + jtv_banner_url + '" target="_blank"><img src="' + jtv_banner_image + '" ' + size + ' alt=""></a>';
    content = this.wrapWithValignDivs(content, this.currentBanner.div.style.width, this.currentBanner.div.style.height);
    
    var additionalStyles = "";
    if (typeof jtv_banner_image_bg_color == 'string' && jtv_banner_image_bg_color) 
      var additionalStyles = "background:" + jtv_banner_image_bg_color;
    
    if (additionalStyles) 
      content = "<div style='" + additionalStyles + "'>" + content + "</div>";
    
    this.currentBanner.showDiv(content);
  },
  
  detectCustomPreroll: function()
  {  	
    parent.PreRollBannerManager.showPreroll({
      type: "custom",
      url: jtv_preroll_url
    });
  },
  
  detectAdvertisingCom: function()
  {
  	this.currentBanner.div.style.display = 'block';
    parent.PreRollBannerManager.showPreroll({
      type: "advertising.com",
      nid: jtv_advertising_network_id,
      level: jtv_advertising_default_level,
      format: jtv_advertising_media_format
    });
  },
  
  detectEmptyPreroll: function()
  {
    parent.PreRollBannerManager.showPreroll({
      type: "empty"
    });
  },
  
  wrapWithValignDivs: function(content, width, height)
  {
    return '<div class="valignWrap" align="center" style="height:' + height + '"><div class="valignValignCenter" style="width:' + width + '">' + content + '</div><div class="valignJustForIE"></div></div>';
  }
  
};
