//v1.0
//toplayer

function Toplayer(id, idDB, imageFile, url, flashFile, html, width, height, posX, posY, type) {
  this.debug = true;
  this.id = id;
  this.idDB = idDB;
  this.imageFile = imageFile;
  this.url = url;
  this.flashFile = flashFile;
  this.html = html;
  this.width = width?width:400;
  this.height = height?height:400;
  this.posX = posX;
  this.posY = posY;
  this.type = type;

  this.cookieName = 'toplayer';
  this.cookiePath = '/';
  this.cookieLifetime = 100;
  this.initialized = true;
  this.numToRemember = 10;

  this.nop = nop;
  this.init = init;
  this.destroy = destroy;
  this.setToplayerCookie = setToplayerCookie;
  
//nic
  function nop() {}

//inicjalizacja
  function init() {
    var cookie = getCookie(this.cookieName);
    if(!cookie || cookie.indexOf(','+this.idDB+',') == -1)
      try {
        var toplayer = document.createElement('div');
        toplayer.className = 'toplayer';
        toplayer.id = this.id;
        if(this.imageFile != '') {
          var img = document.createElement('img');
          img.src = imageFile;
          if(this.url != '') {
            var a = document.createElement('a');
            a.href = url;
            a.appendChild(img);
            toplayer.appendChild(a);
          } else {
            toplayer.appendChild(img);
          }
        } else if(this.flashFile != '') {
          var flash = document.createElement('div');
          flash.id = this.id+'Flash';
          toplayer.appendChild(flash);
          var flashvars = {};
          var params = {};
          params.menu = 'false';
          params.quality = 'autohigh';
          params.wmode = 'transparent';
          var attributes = {};
          attributes.id = flash.id;
        } else if(this.html != '') {
          toplayer.innerHTML = this.html;
        } else {
          this.initialized = false;
        }
        if(this.initialized) {
          if(this.width != '')
            toplayer.style.width = this.width+'px';
          if(this.height != '')
            toplayer.style.height = this.height+'px';
          document.body.appendChild(toplayer);
          if(this.posX != '')
            toplayer.style.left = this.posX+'px';
          else
            toplayer.style.left = ((document.all?document.documentElement.clientWidth:window.innerWidth) - toplayer.offsetWidth) / 2 + 'px';
          if(this.posY != '')
            toplayer.style.top = this.posY+'px';
          else
            toplayer.style.top = ((document.all?document.documentElement.clientHeight:window.innerHeight) - toplayer.offsetHeight) / 2 + 'px';
          if(this.flashFile != '')
            swfobject.embedSWF(this.flashFile, flash.id, this.width, this.height, '9.0.0', '/flash/expressinstall.swf', flashvars, params, attributes);
          toplayer.style.visibility = 'visible';
        }
      } catch(ex) {
        if(this.debug) alert(ex);
      }
  }

//likwidacja toplayera
  function destroy(f) {
    document.body.removeChild(document.getElementById(this.id));
    if(f != undefined && f) {
      if(this.type == 0 || this.type == 1)
        this.setToplayerCookie();
    } else {
      if(this.type == 0)
        this.setToplayerCookie();
    }
  }

//zapisywanie cookie toplayera
  function setToplayerCookie() {
    var now = new Date();
    var cookie = getCookie(this.cookieName);
    if(!cookie)
      cookie = ','+this.idDB+',';
    else if(cookie.indexOf(','+this.idDB+',') == -1)
      cookie += this.idDB+',';
    try {
      var ids = cookie.substr(1, cookie.length-2);
      if(ids.length > this.numToRemember) {
        cookie = ',';
        for(var i = ids.length - this.numToRemember; i < ids.length; i++)
          cookie += cookie += ids[i]+',';
      }
    } catch(ex) {}
    fixDate(now);
    now.setTime(now.getTime() + this.cookieLifetime*24*60*60*1000);
    setCookie(this.cookieName, cookie, now, this.cookiePath);
  }

}

