/**
 * @library
 * SOE WebPrez - modal class.
 * @author Benjamin Neil
 * @projectDescription
 * the modal of ultimate functionality
 */
//this is just commented out if you don't have common
//if not just add the flashInjection normally
//common.includeScripts(['flashInjection']); 
var modal = {

   // Globals
   backgroundDIV    : "overlayblackout",
   frontDIV    : "popInner",
   viewerDIV   : "movieViewerContainer",
   flashdiv : "videoContentFrame",
   currentLang : "en",
   ourbody : null, 
   pbackground : null,
   movieWidth : 0, 
   movieHeight  : 0, 
   windowMinimumWidth : 0,
   
   
   launch : function(title, movie, width, height, min_width, flashID)
   {
      // Insert the movie movie (currently those values are hard coded in this file, need to change that for later on)
      modal.GetElement('videoContentFrame').innerHTML = flashInjection.writeFlashMovie(movie,706,420);
      
      modal.GetElement(modal.viewerDIV).style.visibility = "visible";
      
      //Add FX when there completed
      //Effect.toggle(modal.frontDIV,'blind')
      
      if(modal.GetElement(modal.viewerDIV).style.display == 'none')
         modal.GetElement(modal.viewerDIV).style.display = 'block'
      
      //Add FX when there completed
      //Effect.toggle(modal.viewerDIV,'appear')

      // Get our values for repetitive use.
      modal.movieWidth = width;
      modal.movieHeight = height;
      modal.windowMinimumWidth = min_width;
      
      //Check if the background has already been instansiated
      if(!(modal.GetElement(modal.backgroundDIV)))
      {
         // Set up our background and append it to the body.
         modal.ourbody = document.getElementsByTagName("body").item(0);
         modal.pbackground = document.createElement("div");
         
         if(!(modal.IsFirefoxMac()))
         {
            modal.pbackground.setAttribute("id", modal.backgroundDIV);
         }
         else
         {
            // Mac Firefox can't handle opacity stuff, so let's do a workaround.
            modal.backgroundDIV = "overlayblackout_ffmac";
            modal.pbackground.setAttribute("id", modal.backgroundDIV);
            modal.pbackground.style.backgroundImage = "url(/images/blackbox.png)";
            modal.pbackground.style.backgroundRepeat = "repeat";
         }
   
         modal.pbackground.style.display = 'none';
         modal.ourbody.appendChild(modal.pbackground);
      }else{
         modal.pbackground.style.display = 'block';
      }
         
         // Position stuff before we start displaying things
         modal.fixMoviePos();
   
         // Add an event handler for when the user resizes/scrolls the page
         modal.SetEventHandler("resize", this.fixMoviePos);
         modal.SetEventHandler("scroll", this.fixMoviePos);
   
         modal.GetElement(modal.backgroundDIV).style.display = "block";
         
      
         var browserName=navigator.appName; 
          if (browserName=="Microsoft Internet Explorer")
          {
            modal.GetElement(modal.backgroundDIV).style.filter = "alpha(opacity=50)";
          }
   
   
         if(!(modal.IsFirefoxMac()))
         {
            modal.GetPopup().style.display = "block";
         }
         else
         {
            // Firefox/Mac can't handle fading in Flash (not an issue if the fx haven't been fixed)
            modal.GetPopup().style.display = "block";
         }
      
   },

   fixMoviePos : function()
   {
      var modal_element = modal.GetPopup();
      modal_element.style.width = (Math.max(modal.movieWidth, modal.windowMinimumWidth) + 20) + "px";

      // This needs to be set, for centering purposes.
      modal.GetElement(modal.viewerDIV).style.width = modal.movieWidth + "px";

      var background_element = modal.GetElement(modal.backgroundDIV);
      background_element.style.top = 0 + "px";
      background_element.style.left = 0 + "px";
      background_element.style.width = modal.ScrollDimensionsInfo()[0] + "px";
      background_element.style.height = modal.ScrollDimensionsInfo()[1] + "px";
      background_element.style.zIndex = 20;

      var popup_height = modal_element.clientHeight;
      var popup_width = modal_element.clientWidth;


      var element_hidden = (modal_element.style.display == "none");

      modal_element.style.visibility = "hidden";
      modal_element.style.display = "block";
      popup_height = modal_element.clientHeight;
      popup_width = modal_element.clientWidth;

      if(element_hidden)
      {
         modal_element.style.display = "none";
      }

      modal_element.style.visibility = "visible";

      // Now calculate our location based on scrolling and window size
      var top = modal.Round((modal.InnerDimensionsInfo()[1] - popup_height) / 4);

      if(top < 0)
      {
         top = 0;
      }
      top += modal.EndOfScrollPos()[1];

      var left = modal.Round((modal.InnerDimensionsInfo()[0] - popup_width) / 2);
      if(left < 0)
      {
         left = 0;
      }
      left += modal.EndOfScrollPos()[0];

      modal_element.style.top = top + "px";
      modal_element.style.left = left + "px";
      modal_element.zIndex = 99;
   },


   Cancel : function()
   {
      modal.GetElement('videoContentFrame').innerHTML = "&nbsp;";

      // remove event listener
      modal.RemoveEventHandler("resize", this.fixMoviePos);
      modal.RemoveEventHandler("scroll", this.fixMoviePos);

      // We have to scrap the flash from inside of the div.
      modal.GetElement(modal.viewerDIV).style.display = "none";
      modal.GetElement(modal.viewerDIV).style.visibility = "hidden";

      // x the movie
      modal.GetElement(modal.backgroundDIV).style.display = "none";
      modal.GetElement(modal.backgroundDIV).style.src = "";
      modal.GetPopup().style.display = "none";
      var bname = navigator.appName;
      if (bname == "Microsoft Internet Explorer")
      {window.location.reload()}
   },

   GetElement : function(id)
   {
      return(document.getElementById(id));
   },

   GetPopup : function()
   {
      return(modal.GetElement(modal.frontDIV));
   },
   
   //Round
   Round : function(val) {
      var rnum = val;
      var rlength = 2; // The number of decimal places to round to
      if (rnum > 8191 && rnum < 10485) {
         rnum = rnum-5000;
         var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
         newnumber = newnumber+5000;
      } else {
         var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
      }
      return newnumber;
   },
   
   //not used, but a good function to have
        //I was using it to fix an IE buffer error where the flash content wouldn't leave
        //after it was parsed in, however the flashInjector fixed the issue altogether :-)
   emptyDiv : function (divToClear)
   {
      var i;
      while (i=divToClear.childNodes[0])
      {
         if (i.nodeType == 1 || i.nodeType == 3)
         {
            divToClear.removeChild(i);
         }
      }
   },
   
   // Finds the browsers inner dimensions
   // might be a duplicate that belongs in browser.js
   InnerDimensionsInfo : function()
   {
      var dimensions = new Array(2);

      if(self.innerWidth)
      {
         dimensions[0] = self.innerWidth;
         dimensions[1] = self.innerHeight;
      } else
      if(document.documentElement && document.documentElement.clientWidth)
      {
         dimensions[0] = document.documentElement.clientWidth;
         dimensions[1] = document.documentElement.clientHeight;
      } else
      if(document.body)
      {
         dimensions[0] = document.body.clientWidth;
         dimensions[1] = document.body.clientHeight;
      }

      return(dimensions);
   },

   ScrollDimensionsInfo : function()
   {
      var dimensions = new Array(2);

      if(window.innerHeight && window.scrollMaxY)
      {
         dimensions[0] = document.body.scrollWidth;
         dimensions[1] = window.innerHeight + window.scrollMaxY;
      } else
      if(document.body.scrollHeight > document.body.offsetHeight)
      {
         dimensions[0] = document.body.scrollWidth;
         dimensions[1] = document.body.scrollHeight;
      } else
      {
         dimensions[0] = document.body.offsetWidth;
         dimensions[1] = document.body.offsetHeight;
      }

      return(dimensions);
   },

   EndOfScrollPos : function()
   {
      var dimensions = new Array(2);

      if(self.pageYOffset)
      {
         dimensions[0] = self.pageXOffset;
         dimensions[1] = self.pageYOffset;
      }
      else if((document.documentElement) && (document.documentElement.scrollTop))
      {
         dimensions[0] = document.documentElement.scrollLeft;
         dimensions[1] = document.documentElement.scrollTop;
      }
      else if(document.body)
      {
         dimensions[0] = document.body.scrollLeft;
         dimensions[1] = document.body.scrollTop;
      }

      return(dimensions);
   },


   SetEventHandler : function(eventname, handler)
   {
      if(window.addEventListener)
      {
         window.addEventListener(eventname, handler, false);
      }
      else if(window.attachEvent)
      {
         window.attachEvent("on" + eventname, handler);
      }
   },

   RemoveEventHandler : function(eventname, handler)
   {
      if(window.removeEventListener)
      {
         window.removeEventListener(eventname, handler, false);
      }
      else if(window.detachEvent)
      {
         window.detachEvent("on" + eventname, handler);
      }
   },

   // Some css issue with FF for mac
   IsFirefoxMac : function()
   {
      var userAgent = navigator.userAgent.toLowerCase();
        return((userAgent.indexOf('mac') != -1) && (userAgent.indexOf('firefox') != -1));
   }
};