/**
 * TGD Epaper
 **/
tgdEPaperObject = function()
{
   var idFlashLayer = "TGD_epaperFlashLayer";
   var idStatsImage = "TGD_epaperStatsImage";
   var idFlashLayerInner = "TGD_epaperFlashLayerInner";
   var fl;
   var fo;
   var si;
   var isInit = false;
   var isLoad = false;
   this.init = function()
   {
      fl = document.getElementById(idFlashLayer);
      fi = document.getElementById(idFlashLayerInner);
      si = document.getElementById(idStatsImage);
      if (!fi)
      {
         fi = document.createElement("div");
         fi.id = idFlashLayerInner;
         fi.style['backgroundColor'] = '#fff';
         fi.style['textAlign'] = 'center';
         fi.style['fontSize'] = '10px';
         fi.style['color'] = 'black';
         fi.style['border'] = '1px black solid';
         fi.style['padding'] = '4px';
         fi.style['width'] = '200px';
         fi.style['margin'] = '0 auto';
         fi.innerHTML = '<strong>This page requires FlashPlayer 9</strong><br/><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_adobe_flash_player.png" alt="Get Adobe Flash player" border="0"/></a><br/><a href="javascript:tgdEPaper.close();">close</a>';
      }
      if (!fl) {
         fl = document.createElement("div");
         fl.id = idFlashLayer;
         //fl.style['zIndex'] = '1001';
         fl.style['position'] = 'absolute';
         fl.style['left'] = '0';
         fl.style['top'] = '0';
         fl.appendChild(fi);
      }
      if (!si) {
         si = document.createElement("img");
         si.id = idStatsImage;
         si.style['display'] = 'none';
         si.src = '';
      }
      isInit = true;
   };

   this.showFlashObjects = function()
   {
      var flashObjects = document.getElementsByTagName("object");
      for (i = 0; i < flashObjects.length; i++) {
         flashObjects[i].style.visibility = "visible";
      }
      var flashEmbeds = document.getElementsByTagName("embed");
      for (i = 0; i < flashEmbeds.length; i++) {
         flashEmbeds[i].style.visibility = "visible";
      }
   };

   this.close = function()
   {
      EpaperStats.stop();
      this.showFlashObjects();
      document.body.removeChild(fl);
   };

   this.hideFlashObjects = function()
   {
      var flashObjects = document.getElementsByTagName("object");
      for (i = 0; i < flashObjects.length; i++) {
         flashObjects[i].style.visibility = "hidden";
      }
      var flashEmbeds = document.getElementsByTagName("embed");
      for (i = 0; i < flashEmbeds.length; i++) {
         flashEmbeds[i].style.visibility = "hidden";
      }
   };

   this.load = function(xmlPath, epaperId, xmlFile, policyFile, swfPath)
   {
      if (!isInit) this.init();
      if (isLoad) this.close();
      this.hideFlashObjects();
      if (!swfPath) swfPath = 'swf/';
      if (!xmlFile) xmlFile = 'epaper.xml';
      self.scrollTo(0,0);
      document.body.appendChild(fl);
      var flashWidth = document.viewport.getWidth();
      var flashHeight = document.viewport.getHeight();
      swfobject.embedSWF(
         swfPath + "ePaper.swf", idFlashLayerInner, flashWidth, flashHeight, "9.0.115", swfPath + "expressInstall.swf",
         {  policyFile: policyFile,
            xmlFile: xmlFile,
            basePath: xmlPath,
            uiPath: "ePaper.controller.swf"
         }, {
            allowScriptAccess: "always",
            bgcolor: "#eeeeee",
            allowFullscreen: "true"
         },
         {
            id : idFlashLayerInner
         }
      );
      resizeFlash();
      EpaperStats.setOptions({
         epaperId: epaperId,
         currentPage: 0,
         statUrl: 'http://epaper.1000grad.com/html/stats'
      });
      EpaperStats.start();
   };

   this.teaser = function(preview, layerToMapID, xmlFile, xmlPath, policyFile, swfPath, width, height, bgcolor)
   {
      if (!swfPath) swfPath = 'swf/';
      if (!xmlPath) xmlPath = xmlFile.replace(/epaper.xml/, '');
      if (!width) width = "150";
      if (!height) height = "200";
      if (!bgcolor) bgcolor = "#FFFFFF";
      swfobject.embedSWF(
         swfPath + "ePaper.teaser.swf", layerToMapID, width, height, "9.0.115", swfPath + "expressInstall.swf",
         {  policyFile: policyFile,
            cover: preview,
            action: "tgdEPaper.load('"+xmlFile+"', '"+xmlPath+"', '"+policyFile+"', '"+swfPath+"')"
         }, {
            allowScriptAccess: "always",
            //wmode: "transparent",
            bgcolor: bgcolor,
            allowFullscreen: "true"
         },
         {
            id : layerToMapID
         }
      );
   }
   
   this.setStatsUrl = function(url)
   {
      if (si)
      {
         si.src = url;
      }
   }
};

var tgdEPaper = new tgdEPaperObject();

function closeEPaper() {
   tgdEPaper.close();
}

// Passt Flashfilm Container an aktuellen Fensterbreite/hoehe an
function resizeFlash()
{
   var flashCon = $("TGD_epaperFlashLayerInner");
   if (flashCon)
   {
      newWidth = document.viewport.getWidth();
      if (newWidth<970) newWidth = 970;
      flashCon.style.width = newWidth + 'px';
      newHeight = document.viewport.getHeight();
      if (newHeight<500) newHeight = 500;
      flashCon.style.height = newHeight + 'px';
      // Fuer den Mozilla, 50 ms spaeter nochmal checken
      // (Sonst Fehler an Stellen an denen Scrollbars waren)
      if (navigator.appName.indexOf("Netscape")!=-1)
         setTimeout(resizeFlash, 150);
   }
}

// Initialisiert Shadowbox
function initShadowbox()
{
   Shadowbox.loadSkin('classic', 'js/shadowbox/skin');
   Shadowbox.loadLanguage('en', 'js/shadowbox/lang');
   Shadowbox.loadPlayer(['flv', 'html', 'iframe', 'img', 'qt', 'swf', 'wmp'], 'js/shadowbox/player');
}

// Zeigt ein Bilder-Array als Galerie in Shadowbox
function startShadowbox(imgs)
{
   var options = {
      displayNav: true,
      handleUnsupported: 'remove',
      autoplayMovies: false,
      continuous:     true,
      initialHeight: 600,
      initialWidth: 400
   };
   if ('content' in imgs)
   {
      if (imgs.player == 'flv')
      {
         if (imgs.content.indexOf("flv") == -1) imgs.player = 'iframe';
      }
   }
   Shadowbox.init(options);
   Shadowbox.open(imgs);
};

EpaperStats = (function ()
{
   var epaperId, currentPage = 0, statUrl, pe;

   function getHash() {
      var href = top.location.href, i = href.indexOf("#");
      return i >= 0 ? href.substr(i + 1) : null;
   }

   function checkToken()
   {
      var hash = getHash();
      newHash = 0;
      if (hash) newHash = hash.replace(/\//, '');
      newHash = newHash == 0 ? 1 : newHash;
      if(newHash && newHash != currentPage)
      {
         currentPage = newHash;
         sendStats();
      }
   };

   function sendStats()
   {
      tgdEPaper.setStatsUrl(statUrl + '/unique_id/' + epaperId + '/page_nr/' + currentPage);
   }

   return {
      setOptions : function(options)
      {
         epaperId = options.epaperId;
         currentPage = options.currentPage;
         statUrl = options.statUrl;
      },
      stop: function()
      {
         if(pe)
            pe.stop()
      },
      start: function()
      {
         currentPage = 0;
         if(!this.pe)
            pe = new PeriodicalExecuter(checkToken.bind(this), 2);
         else
            pe.registerCallback();
         sendStats();
      }
   }
})();
