/*
  Banners Version 1.0
  Copyright (C) 2009, Andres Melguizo Velez.

  Language    : JScript
  Description : Banners handle.
  Author      : Andres Melguizo Velez
  Created     : 20090617
  Modified    : 20090820
*/

// Global scope
var glbBannerScope = new Array();

Banner = function(idBanner) {

  this.idBanner = idBanner;
  this.list = null;
  this.id = null;
  this.hTimer = null;
  this.iItem = -1;
  this.szPath = "";
  glbBannerScope[idBanner] = this; // Save this to global scope

  this.setId = function(id) {
    this.id = id;
  }

  this.setList = function(lst) {
    this.list = lst;
  }

  this.setPath = function(szPath) {
    this.szPath = szPath;
  }

  this.start = function() {
    if(this.list.length == 0)
      return;
    this.iItem++;
    if(this.iItem > this.list.length - 1)
      this.iItem = 0;
    var iTimer = this.list[this.iItem].time * 1000; // Convert to seconds
    if(iTimer != 0)
      this.hTimer = setTimeout("glbBannerScope[" + this.idBanner + "].start()", iTimer);
    this.show();
  }

  this.stop = function() {
    if(this.hTimer != null) {
      clearTimeout(this.hTimer);
      this.hTimer = null;
    }
    this.iItem = -1;
  }

  this.show = function() {
    if(this.id == null)
      return;
    var oItem = this.list[this.iItem];
    //var szFile = "file.php?file=banners/" + oItem.img;
    var szFile = this.szPath + oItem.img;
    var szImg = "";
    var szLink = oItem.link;
    if(oItem.type == 1) // Image
      szImg = "<img src='" + szFile + "'>";
    else if(oItem.type == 2) // Flash
      szImg = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" width=\"99%\" height=\"99%\"><param name=\"allowScriptAccess\" value=\"sameDomain\" /><param name=\"movie\" value=\"" + szFile + "\" /><param name=\"quality\" value=\"high\" /><param name=\"scale\" value=\"noborder\" /><param name=\"bgcolor\" value=\"#ffffff\" /><embed src=\"" + szFile + "\" quality=\"high\" scale=\"noborder\" bgcolor=\"#ffffff\" width=\"99%\" height=\"99%\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" /></object>";
    this.id.innerHTML = szImg;
    //var s = oItem.img + ", " + this.iItem + " of " + this.list.length + ", time: " + oItem.time;
    //this.id.innerHTML = s;
    if(oItem.link != "") {
      this.id.onclick = function() {
        var code = this.code;
        var oItem = code.list[code.iItem];
        //alert(oItem.img);
        if(oItem.blank == 1) {
          //alert("Open link in new window: " + oItem.link);
          window.open(oItem.link);
        }
        else {
          //alert("Open link in same window: " + oItem.link);
          location = oItem.link;
        }
      }
      this.id.style.cursor = "pointer";
      this.id.code = this;
    }
    else {
      this.id.onclick = null;
      this.id.style.cursor = "default";
      this.id.code = null;
    }
  }

}

