////////////////////////////////////////////////////////////
//                                                        //
//  WebSpiker.js                                          //
//                                                        //
////////////////////////////////////////////////////////////
//                                                        //
//  Objekt-kontener dla WebSpikerów                       //
//                                                        //
//  http://webspikerzy.pl/                                //
//  Wszystkie prawa zastrzeżone - IRIS IN                 //
//                                                        //
////////////////////////////////////////////////////////////
//                                                        //
//  1.0.001 - 2011.12.25                                  //
//                                                        //
////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////
//  Funkcje pomocnicze                                    //
////////////////////////////////////////////////////////////

function delegate(that, thatMethod) {
  return function(e) { return thatMethod.call(that, e); }
}

////////////////////////////////////////////////////////////
//  Definicja prototypu obiektu-kontenera WebSpiker 
//  Konstruktor
////////////////////////////////////////////////////////////

function WebSpiker() {
  this.parseBrowser();

  // Ustaw parametry domyslne
  this.baseURL = "/";
  this.sizeX = 300;
  this.sizeY = 400;  

  this.positionHorizontalFrom = 'fromLeft';
  this.locationX = 0;
  this.anchoredToX = null;

  this.positionVerticalFrom = 'fromBottom';
  this.locationY = 0;
  this.AnchoredToY = null;
  
  this.autoStart = this.AUTO_START.play;
}

////////////////////////////////////////////////////////////
//  Definicja prototypu obiektu-kontenera WebSpiker 
//  Część publiczna definicji
////////////////////////////////////////////////////////////

WebSpiker.prototype.AUTO_START = {
  none : 0,
  show : 1,
  showNavigationOnly: 2,
  play : 3,
  playOnlyOnce : 4,
  playOnlyOnceInSession : 5
}

WebSpiker.prototype.setBaseURL = function(baseURL) {
  // Ustawia URL bazowy dla plików, do których odwołuje się obiekt WebSpiker
  this.baseURL = baseURL;
}

WebSpiker.prototype.setMovie = function(movieFileName, sizeX, sizeY) {
  // Ustawia komponent na wyswietlanie zadanego pliku
  this.movieFileName = movieFileName;
  this.sizeX = sizeX;
  this.sizeY = sizeY;
}

WebSpiker.prototype.setPositionX = function(positionFrom, anchoredTo, offset) {
  // Ustawia webspikera horyzontalnie w określonej pozycji (offset punktów od...) względem określonego obiektu
  this.locationX = offset;
  this.positionHorizontalFrom = positionFrom;
  this.anchoredToX = anchoredTo;
}

WebSpiker.prototype.setPositionY = function(positionFrom, anchoredTo, offset) {
  // Ustawia webspikera wertykalnie w określonej pozycji (offset punktów od...) względem określonego obiektu
  this.locationY = offset;
  this.positionVerticalFrom = positionFrom;
  this.anchoredToY = anchoredTo;
}

WebSpiker.prototype.setAutoStart = function(autoStart) {
  // Ustawia URL bazowy dla plików, do których odwołuje się obiekt WebSpiker
  this.autoStart = autoStart;
}

WebSpiker.prototype.initialize = function() {
  // Konczy inicjalizację obiektu WebSpiker i uruchamia WebSpikera (opcjonalnie, w zależności od ustawień autostartu)
  //
  // Ta funkcja powinna byc uruchamiana tylko po pelnym zaladowaniu DOM (czyli np. w window.onload)
  // oraz po ustawieniu podstawowych parametrów, takich jak rozmiar filmu
  
  var S4 = function() { return (((1+Math.random())*0x10000)|0).toString(16).substring(1); };

  if (this.mainDIV == null) {
    this.mainDIV = document.createElement('div');
    this.mainDIV.style.display='block';
    this.mainDIV.style.zIndex = "1000"; 
    this.mainDIV.style.border = "0px"; 
    this.mainDIV.style.width = this.sizeX + "px"; 
    this.mainDIV.style.height = this.sizeY + "px";
    this.mainDIV.style.position = "fixed"; 
    this.reposition();  
    this.mainDIV.style.visibility='hidden';  
    
    document.body.appendChild(this.mainDIV);
  }
  
  if (this.movieObject == null) {
    this.movieObject = document.createElement('div');
    this.movieObject.id = 'webSpikerMovie_' + S4 + '_' + S4 + '_' + S4 + '_' + S4; // Random ID
    this.mainDIV.appendChild(this.movieObject);

    var flashvars = {};
    var params = { quality: "high", menu: "false", wmode: "transparent", allowScriptAccess: "always" };
    var attributes = {};

    swfobject.embedSWF(this.baseURL + "WebSpiker.swf", this.movieObject.id, this.sizeX, this.sizeY, "9.0.0", "expressInstall.swf", flashvars, params, attributes, delegate(this, this.onFlashLoaded));
  } else {
    this.onFlashLoadedAndReady();
  }
      
  if (this.resizeEventsAttached != true) {     
    if (this.isIE) {    
      window.attachEvent('onload', delegate(this, this.reposition));
      window.attachEvent('onresize', delegate(this, this.reposition));
    } else {
      window.addEventListener('load', delegate(this, this.reposition), false);
      window.addEventListener('resize', delegate(this, this.reposition), false);
    }
    this.resizeEventsAttached = true;
  }    
}

WebSpiker.prototype.initializeOnLoad = function() {
   if (this.isIE) {    
     window.attachEvent('onload', delegate(this, this.initialize));
   } else {
     window.addEventListener('load', delegate(this, this.initialize), false);
   }
}


WebSpiker.prototype.show = function() {
  // Pokazuje obiekt webspikera
  this.movieObject.setSize(this.sizeX, this.sizeY);
  this.mainDIV.style.width = this.sizeX + "px"; 
  this.mainDIV.style.height = this.sizeY + "px";
    
  this.mainDIV.style.display='block';
  this.mainDIV.style.visibility = 'visible';
}

WebSpiker.prototype.showNavigationOnly = function() {
  // Pokazuje obiekt webspikera - tylko pasek nawigacji
  this.movieObject.setSize(this.sizeX, this.sizeY);
  this.mainDIV.style.width = "100px"; 
  this.mainDIV.style.height = "45px";
    
  this.mainDIV.style.display='block';
  this.mainDIV.style.visibility = 'visible';
}

WebSpiker.prototype.hide = function() {
  // Ukrywa obiekt webspikera
  this.mainDIV.style.display='none';
  this.mainDIV.style.visibility = 'hidden';
}

WebSpiker.prototype.play = function() {
  // Uruchamia odtwarzanie webspikera
  this.show();
  this.logPlayInCookies();
  this.movieObject.loadAndStartWS(this.movieFileName); 
}

WebSpiker.prototype.stop = function() {
  // Zatrzymuje odtwarzanie webspikera
  this.movieObject.stopWS();
}

WebSpiker.prototype.getPlayCount = function() {
  var playCount = this.getCookie(this.getCookieNamePlayCount());
  if (playCount != null) {
  	return parseInt(playCount);
  } else {
    return 0;
  }
}

WebSpiker.prototype.getPlayCountSession = function() {
  var playCount = this.getCookie(this.getCookieNamePlayCountSession());
  if (playCount != null) {
  	return parseInt(playCount);
  } else {
    return 0;
  }
}

WebSpiker.prototype.setShowPoweredBy = function(showPoweredBy) {
  this.movieObject.setShowPoweredBy(showPoweredBy);
}

////////////////////////////////////////////////////////////
//  Definicja prototypu obiektu-kontenera WebSpiker 
//  Część prywatna definicji
////////////////////////////////////////////////////////////

WebSpiker.prototype.parseBrowser = function() {
  this.isNetscape=false;
  this.isFirefox=false;
  this.isIE=false;
  this.isOpera=false;
  this.isSafari=false;
  
  this.appName=navigator.appName;
  this.appVersion=navigator.appVersion;
  if (this.appName=="Netscape"&&parseFloat(this.appVersion)>4.7) {
    var uai=navigator.userAgent.indexOf("Firefox");
    if (uai!=-1) {
      this.isFirefox=true;
      this.browserVersionMaj=parseInt(navigator.userAgent.charAt(uai+8));
    } else {
      this.isNetscape=true;
      this.browserVersionMaj=parseFloat(this.appVersion);
    }
  } else {
    var uai=navigator.appVersion.indexOf("MSIE");
    if(uai!=-1) {
      this.isIE=true;
      var temp=navigator.appVersion.split("MSIE")
      this.appVersion=parseFloat(temp[1]);
      this.browserVersionMaj=parseInt(this.appVersion);
    } else {
      var uai=navigator.userAgent.indexOf("Opera");
      if(uai!=-1){
        this.isOpera=true;
        this.browserVersionMaj=parseInt(navigator.userAgent.charAt(uai+6));
      }
    }
  }
  
  if (this.isIE) {
    this.browserIsCompatible = ( (this.appVersion >= 7) && (document.compatMode == 'CSS1Compat') );
  } else if (this.isFirefox) {
    this.browserIsCompatible = (this.browserVersionMaj>=2);
  } else {
    this.browserIsCompatible = true;
  } 
}

WebSpiker.prototype.repositionX=function() {
  var absoluteX = this.locationX; 

  var o = this.anchoredToX;
  while (o!=null) {
    absoluteX += o.offsetLeft;
    o = o.offsetParent;
  } 

  if (this.positionHorizontalFrom=='fromLeft') {
    if (absoluteX < 0) { absoluteX = 0; }
    this.mainDIV.style.left = absoluteX + 'px';
  } else {
    this.mainDIV.style.right = absoluteX + "px";
  }
}

WebSpiker.prototype.repositionY = function () {
  if (this.positionVerticalFrom=='fromBottom') {
    this.mainDIV.style.bottom = this.locationY + "px";
  } else {
    this.mainDIV.style.top = this.locationY + "px";
  }
}

WebSpiker.prototype.reposition = function () {
  this.repositionX();
  this.repositionY();
}

WebSpiker.prototype.onFlashLoadedAndReady=function() {
  try {
  	this.movieObject.setSize(this.sizeX, this.sizeY);
  } catch(e) {
    setTimeout(delegate(this, this.onFlashLoadedAndReady), 100);  
    return;	
  }
	
  if (this.autoStart == this.AUTO_START.show) {
    this.show();
    this.movieObject.loadWS(this.movieFileName);    
  } else if (this.autoStart == this.AUTO_START.showNavigationOnly) {
    this.showNavigationOnly();
  } else if (this.autoStart == this.AUTO_START.play) {
    this.play();
  } else if (this.autoStart == this.AUTO_START.playOnlyOnce) {
    if (this.getPlayCount == 0) {
      this.play();
    } else {
      this.showNavigationOnly();
    }
  } else if (this.autoStart == this.AUTO_START.playOnlyOnceInSession) {
    if (this.getPlayCountSession() == 0) {
      this.play();
    } else {
      this.showNavigationOnly();
    }
  }
}

WebSpiker.prototype.onFlashLoaded=function(e) {
  if (e.success) {
    this.movieObject = e.ref;
    setTimeout(delegate(this, this.onFlashLoadedAndReady), 100);
  } else {
    this.movieObject = null;
  }
}

WebSpiker.prototype.setCookie = function (name, value, expires, path, domain, secure) {
  var today = new Date();
  today.setTime( today.getTime() );
  if ( expires ) { expires = expires * 1000 * 60 * 60 * 24; }
  var expires_date = new Date( today.getTime() + (expires) );
  document.cookie = name + "=" +escape( value ) + ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + ( ( path ) ? ";path=" + path : "" ) + ( ( domain ) ? ";domain=" + domain : "" ) + ( ( secure ) ? ";secure" : "" );
}

WebSpiker.prototype.getCookie = function (check_name) {
  var a_all_cookies = document.cookie.split( ';' );
  var a_temp_cookie = '';
  var cookie_name = '';
  var cookie_value = '';
  var b_cookie_found = false;

  for ( i = 0; i < a_all_cookies.length; i++ ) {
    a_temp_cookie = a_all_cookies[i].split( '=' );
    cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

    if ( cookie_name == check_name ) {
      b_cookie_found = true;
      if ( a_temp_cookie.length > 1 ) { cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') ); }
      return cookie_value;
      break;
    }
    a_temp_cookie = null;
    cookie_name = '';
  }

  if ( !b_cookie_found ) { return null; }
}

WebSpiker.prototype.getCookieNamePlayCount = function() { return "webspiker_pc_" + escape(this.movieFileName); }
WebSpiker.prototype.getCookieNamePlayCountSession = function() { return "webspiker_pcs_" + escape(this.movieFileName); }

WebSpiker.prototype.logPlayInCookies = function () {
  this.setCookie (this.getCookieNamePlayCount(), this.getPlayCount() + 1, 10000, '/', '', '');
  this.setCookie (this.getCookieNamePlayCountSession(), this.getPlayCountSession() + 1, '', '/', '', '');
}

