/*
Copyright 2005-2007 Netvibes Ltd.
All rights reserved.
*/

/*
Class: Netvibes.UI.FlashPlayer

Flash player with full screen capabilities. Currently used by video module.

Example:
	>var flashPlayer = new Netvibes.UI.FlashPlayer(moduleContent);
	>
	>flashPlayer.show('http://video.google.com/googleplayer.swf?docId=39288702201978303');
	>
	>flashPlayer.setFullScreenMode(true);
*/
Netvibes.UI.FlashPlayer = Class.create();
Netvibes.UI.FlashPlayer._maxzIndex = 9010;
Netvibes.UI.FlashPlayer.prototype = {
  setOptions: function(options) {
      this.options = { flashVars: '', flashUrl: '', width: 320, height: 240,
                       wmode: "opaque", showToolbar: true, showFullScreenLink: true,
                       fixModuleId: 0 }
      Object.extend(this.options, options || {});
  },
  initialize: function(parent, options) {
      this.setOptions(options);
      this.parent = parent;
      this.container = null;
      this.linuxIframe = null;
      this.isFullScreen = false;
  },
  show: function(flashUrl, flashVars, options) {
      if (typeof options == 'undefined') options = {}
      this.options.flashUrl = flashUrl; // ie, googleplayer.swf
      
      if (Browser.isIE) { // Force cache refresh for IE
          if (this.options.flashUrl.indexOf('?') != -1)
              this.options.flashUrl += ('&' + Math.round(Math.random() * 100));
      }
      
      this.options.flashVars = flashVars;

      if (!this.container) {
          this.container = document.createElement("div");
          this.container.className = "flashplayer";
          this.container.align = 'center';
          this.container.style.position = "absolute";
          this.container.style.zIndex = '8000';
          this.container.style.background = "#ffffff";
          this.parent.appendChild(this.container);

          if (!Browser.isIE) {
              // IE is buggy with position: absolute|relative (block may disappear)
              this.container.style.position = "absolute";

              // Preserve space for absolute positionned container
              this.placeHolder = document.createElement("div");
              this.placeHolder.style.height = this.options.height+"px";
              this.parent.appendChild(this.placeHolder);
          }
      } else {
          this.container.innerHTML = "";
      }

      var playerHtml = '';
      playerHtml += '<object id="seesmicrecorder" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+this.options.width+'" height="'+this.options.height+'">';
      playerHtml +=   '<param name="allowScriptAccess" value="always" />';
      playerHtml +=   '<param name="movie" value="'+this.options.flashUrl+'" />';
      playerHtml +=   '<param name="quality" value="high" />';
      playerHtml +=   '<param name="wmode" value="'+this.options.wmode+'" />';
      playerHtml +=   '<param name="bgcolor" value="#ffffff" />';
      playerHtml +=   '<param value="true" name="allowfullscreen"/>';
      playerHtml +=   '<param name="flashVars" value="'+this.options.flashVars+'" />';
      playerHtml +=   '<embed wmode="'+this.options.wmode+'" src="'+this.options.flashUrl+'" quality="high" bgcolor="#ffffff" width="'+this.options.width+'" height="'+this.options.height+'" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashVars="'+this.options.flashVars+'" allowfullscreen="true" id="seesmicrecorder" />';
      playerHtml += '</object>';

      this.container.innerHTML = playerHtml;

      // Fix useless because the object is inserted by JavaScript
      //if (Browser.isIE) {
          // ByPass IE: "click to activate and use this control" (KB912945)
          //var object = this.parent.getElementsByTagName("object")[0];
          //object.outerHTML = object.outerHTML; // warning: this will de-reference any reference
      //}
      this.element = this.parent.getElementsByTagName((Browser.isIE || Browser.isSafari)? "object" : "embed")[0];

      if (this.options.showToolbar) this._createLinks(options.mediaLinkUrl, options.mediaLinkText);

      Element.show(this.container);
      if (this.placeHolder) Element.show(this.placeHolder);
      this.resize();
  },
  hide: function() {
      if (this.container) {
          this.container.innerHTML = ''; // Needed for IE in order to stop playing
          Element.hide(this.container);
          if (this.placeHolder) Element.hide(this.placeHolder);
      }
      if (this.hide && this.links) {
          Element.hide(this.links);
      }
	  // UWA callback with checks (widget may be undefined or a non-Netvibes object in special UWA platforms)
	  if (typeof widget == 'object' && typeof widget.callback == 'function') { widget.callback("onUpdateBody"); }
      return false;
  },
  setFullScreenMode: function(mode) {
      this.isFullScreen = (typeof(mode)!='undefined' && mode) ? true : false;
      this._fixAdjacentModules();

      if (this.isFullScreen) {
          window.scrollTo(0, 0);
          if (Browser.isIE) this.container.style.position = 'absolute';
          this.container.style.top = "0px";
          this.container.style.left = "0px";
          this.container.style.marginLeft = '0';

          // always make the full screen video as the highest zIndex
          // preserves others videos with the same zIndex the overlay it
          this.container.style.zIndex = ++Netvibes.UI.FlashPlayer._maxzIndex;
          this._createCloseButton();

          // ByPass App 'resize' notifications limiter
          Event.observe(window, 'resize', this.resize.bindAsEventListener(this));
      } else {
          Element.remove(this.closeButton);
          if (Browser.isLinux && Browser.isFirefox) Element.remove(this.linuxIframe);
          Event.stopObserving(window, 'resize', this.resize);
      }
      // Computes size
      this.resize();

      // Hack for Firefox on Linux to show the "Back to your page" button
      if (Browser.isLinux && Browser.isFirefox) {
          this.linuxIframe = document.createElement("iframe");
	      this.linuxIframe.style.position = "absolute";
          this.linuxIframe.style.width = this.closeButton.offsetWidth+"px";
          this.linuxIframe.style.height = this.closeButton.offsetHeight+"px";
	      this.linuxIframe.style.left = this.closeButton.offsetLeft+"px";
          this.linuxIframe.style.top = this.closeButton.offsetTop+"px";
	      this.linuxIframe.setAttribute("frameborder", "0");
	      this.linuxIframe.setAttribute("scrolling", "no");
          this.linuxIframe.style.zIndex = parseInt(this.container.style.zIndex)+1;
          document.body.appendChild(this.linuxIframe);
      }
      
      return false;
  },
  resize: function(w, h) {

      if (!this.container) return;

      if (this.isFullScreen) {
          if (this.container.style.width != Browser.getDocumentWidth()) {

              // Helps to computes the correct document width
              this.container.style.width = '1px';
              this.container.style.height = '1px';
              this.element.style.width = '1px';

              this.container.style.width = Browser.getDocumentWidth()+'px';
              this.container.style.height = Browser.getDocumentHeight()+'px';
              this.element.style.height = Browser.getClientHeight()-(Browser.isFirefox? 3 : 0)+'px';
              this.element.style.width = Browser.getDocumentWidth()+'px';
          }
          return;
      }

      // Reset container position
      this.container.style.top = '';
      this.container.style.left = '';
      this.container.style.width = '1px';
      this.container.style.height = '1px';
      
      if (typeof w != "undefined" && typeof h != "undefined") {
            this.container.style.width = w+'px';
            if (!Browser.isIE) {
                this.placeHolder.style.height = h+"px";
                this.container.style.marginLeft = '0px';
            }
            this.container.style.height = h+"px";
            this.element.style.width = w+'px';
            this.element.style.height = h+"px";
            return ;
      }
      
      if (Browser.isIE) {
          this.container.style.position = '';
          var availableSpace = Element.getDimensions(this.parent);
          this.container.style.width = availableSpace.width+'px';
          this.container.style.height = this.options.height+'px';
          this.element.style.width = this.options.width+'px';
          this.element.style.height = this.options.height+'px';
      } else {
          // Align center horizontally
          var availableSpace = Element.getDimensions(this.placeHolder);
          var width = Math.min(this.options.width, availableSpace.width);
          this.container.style.width = width+'px';
          this.container.style.height = availableSpace.height+'px';
          this.element.style.width = width+'px';
          this.element.style.height = availableSpace.height+'px';
          this.container.style.marginLeft = Math.round((availableSpace.width-width)/2)+'px';
      }
  },
  _createCloseButton: function() {
      this.closeButton = document.createElement("input");
      this.closeButton.type = "button";
      this.closeButton.id = "flashplayer-back";
      this.closeButton.style.position = "absolute";
      this.closeButton.style.top = "2px";
      this.closeButton.style.right = "2px";
      this.closeButton.value = _("Back to my page");
      this.closeButton.style.zIndex = parseInt(this.container.style.zIndex)+2;
      // Fix for IE7 only: width required
      if (Browser.isIE && Browser.version > 6) {
          this.closeButton.style.width = (this.closeButton.value.length)+"em";
      }
      this.closeButton.onclick = function() {
          this.setFullScreenMode(false);
      }.bind(this);

      document.body.appendChild(this.closeButton);
  },
  _createLinks: function(linkUrl, linkText) {
      if (this.links) Element.remove(this.links);

      this.links = document.createElement("p");
      this.links.align = 'center';
      this.links.style.padding = '4px';

      // Hide the fullscreen link
      /*if (this.options.showFullScreenLink) {
        var linkFullScreen = document.createElement("a");
        linkFullScreen.href = "javascript:void(0)";
        linkFullScreen.onclick = this.setFullScreenMode.bindAsEventListener(this);
        linkFullScreen.innerHTML = _("Full screen");
        this.links.appendChild(linkFullScreen);
        this.links.appendChild(document.createTextNode(' | '));
      }*/

      if (linkUrl && linkText) {
          var linkOriginal = document.createElement("a");
          linkOriginal.href = linkUrl;
          linkOriginal.rel = 'external';
          linkOriginal.target = '_blank';
          linkOriginal.innerHTML = linkText.stripTags();
          this.links.appendChild(linkOriginal);
      }

      this.links.appendChild(document.createTextNode(' | '));
      var linkClose = document.createElement("a");
      linkClose.href = "javascript:void(0)";
      linkClose.onclick = this.hide.bind(this);
      linkClose.innerHTML = _("Hide");

      this.links.appendChild(linkClose);
      this.parent.appendChild(this.links);
  },
  // Fixes FF/Opera rendering z-index issue with flash
  // Used to stop others videos running too (all browsers)
  _fixAdjacentModules: function() {
      if (!this.options.fixModuleId) return;

      // Hide/restore column resizer background-image
      var columnGrippers = $('application').getElementsByClassName("gripper");
      for (var i=0;  i < columnGrippers.length; i++) {
          var gripper = columnGrippers[i];
          if (this.isFullScreen) gripper.style.visibility="hidden"; else gripper.style.visibility="";
      }
      // Hide/restore modules
      for (var i=0, module; module=App.moduleList[i]; i++) {
          if (module.dataObj.tab == App.selectedTab && module.dataObj.fixId != this.options.fixModuleId) {
              if (this.isFullScreen) module.hide(); else module.show();
          }
      }

      // Hide/restore the info box if displayed
      //if (this.isFullScreen && Element.visible('infoBox')) {
         //Element.hide('infoBox');
         //this.options.shouldDisplayInfoBox = true;
      //} else if (this.options.shouldDisplayInfoBox == true) {
         //Element.show('infoBox');
         //delete this.options.shouldDisplayInfoBox;
      //}
  }
}

