var LEDjs = {

  init: function()
  {
    // $('.dropDown').hide();
    $('li.hMenuItem#gameMenuLink').hover(
      function()
      {
        $('ul#gameMenu').toggle();
      }
    );

    if ( $.browser.msie ) {
      if(parseInt($.browser.version, 10) < 8)
      {
        $('ul.horizontalMenu li.hMenuItem a[class!="highlight"]').parent('li[id!="gameMenuLink"]').hover(
          function(){
            $(this).addClass('ieMenuHover');
          },
          function(){
            $(this).removeClass('ieMenuHover');
        });
        $('ul.horizontalMenu li.hMenuItem a.highlight').addClass('ieMenuHover');
      }
    }

  },

  hideGame: function(containerId, hideId)
  {
    $('#'+containerId).addClass('hideGame');
    $('#'+hideId).show();
  },
  showGame: function(containerId, hideId)
  {
    $('#'+hideId).hide();
    $('#'+containerId).removeClass('hideGame');
  },

  //returns true if the user is running Firefox or IE — the two browsers we know support the shockwave plugin well
  hasFirefoxOrIE : function() {
    return navigator.userAgent && ((navigator.userAgent.indexOf('Firefox') >= 0) || (navigator.userAgent.indexOf('MSIE') >= 0));
  },

  //Determines whether the browser has the Shockwave Director plugin installed
  //Original code from http://kb2.adobe.com/cps/157/tn_15722.html
  shockwaveFound : function(tMajorVersion, tMinorVersion) {
    if(!tMajorVersion) tMajorVersion = 11;
    if(!tMinorVersion) tMinorVersion = 0;
    var tShockwaveFound = 0;
    if (navigator.mimeTypes && navigator.mimeTypes["application/x-director"] && navigator.mimeTypes["application/x-director"].enabledPlugin) {
      if (navigator.plugins && navigator.plugins["Shockwave for Director"] && (tVersionIndex = navigator.plugins["Shockwave for Director"].description.indexOf(".")) != - 1) {
        var tMajorVersionString = navigator.plugins["Shockwave for Director"].description.substring(tVersionIndex-2, tVersionIndex);
        var tMinorVersionString = navigator.plugins["Shockwave for Director"].description.substring(tVersionIndex+1, tVersionIndex+2);
        if (parseInt(tMajorVersionString) >= tMajorVersion) {
          if (tMinorVersion > 0) {
            if (parseInt(tMinorVersionString) >= tMinorVersion) {
              tShockwaveFound = 1;
            }
          } else {
            tShockwaveFound = 1;
          }
        }
      }
    } else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 && (navigator.userAgent.indexOf("Windows 95")>=0 || navigator.userAgent.indexOf("Windows 98")>=0 || navigator.userAgent.indexOf("Windows NT")>=0 )) {
      // With what was here before, this function would have to be called before the page was done loading.
      // Don't want to do that. So we'll just let IE figure it out.
      tShockwaveFound = 1;
    }
    if ( tShockwaveFound ) {
      return true;
    } else if (!(navigator.appName && navigator.appName.indexOf("Netscape")>=0 && navigator.appVersion.indexOf("2.")>=0)){
      return false;
    }
  },

  // In place of the object that should be there (same dimensions),
  // show a message prompting the user to install Shockwave
  showSwMessage : function(containerId, width, height)
  {
    var messageText = 'You need to upgrade your Adobe Shockwave Player to play this game.<br><a href="http://get.adobe.com/shockwave/">Download it from Adobe.</a>';
    if(!containerId) {
      document.write(messageText);
      return;
    }
    if(!width)  width  = 640;
    if(!height) height = 480;

    var container = document.getElementById(containerId);

    var content  = '<div id="swMessage" style="width:'+ width +'px;height:'+ height +'px;">';
    content += '<div style="width:100%;background:#eee;font-size:1em;text-align:center;margin:0 auto;padding:0;margin-top:'+(height/3)+'px">';
    content += messageText;
    content += '</div>';
    content += '</div>';

    container.innerHTML = content;
  },

  // Embed a Director file into a page
  embedShockwave : function(src, width, height, version, containerId, objectId, hideId)
  {
    if(!src)         src         = '/images/image_not_found.jpg';
    if(!width)       width       = '640';
    if(!height)      height      = '480';
    if(!version)     version     = '11,0,0,0';
    if(!containerId) containerId = 'gameEmbedGame';
    if(!hideId)      hideId      = 'gameEmbedImage';
    if(!objectId)    objectId    = 'LEDEmbeddedGame';

    // might want to read the values below (currently 11,0) from the version string
    if(!this.shockwaveFound(11,0)) {
      this.showSwMessage(containerId, width, height);
      return false;
    }

    var loc    = window.location;
    var origin = loc.protocol + "//" + loc.hostname + (loc.port == "" ? "" : ":") + loc.port;

    // kill cache
    src += "?1";

    var objectString  = '';
    objectString += '<object type="application/x-director" classid="clsid:166B1BCA-3F9C-11CF-8075-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#';
    objectString += 'version=' + version + '" ';
    objectString += 'ID="' + objectId + '" ';
    objectString += 'width="' + width +'" height="'+ height +'" ';
    objectString += '>';
    objectString += '<param name=src value="'+ src +'">';
    objectString += '<param name=swRemote value="swSaveEnabled=\'false\' swVolume=\'true\' swRestart=\'false\' swPausePlay=\'false\' swFastForward=\'false\' swContextMenu=\'true\'">';
    objectString += '<param name=swStretchStyle value=none>';
    objectString += '<param name=bgColor value="#DDDDDD">';
    objectString += '<param name=logo value=FALSE>';
    objectString += '<param name=PlayerVersion value="' + version + '"> ';
    objectString += '<embed src="'+ src +'" ';
    objectString += 'width="'+ width +'" height="'+ height +'" ';
    objectString += 'bgColor="#DDDDDD" logo=FALSE ';
    objectString += 'swRemote="swSaveEnabled=\'false\' swVolume=\'true\' swRestart=\'false\' swPausePlay=\'false\' swFastForward=\'false\' swContextMenu=\'true\'" swStretchStyle=none type="application/x-director" pluginspage="http://get.adobe.com/shockwave/" ';
    objectString += 'PlayerVersion="' + version + '" ';
    objectString += 'currentDomain="' + origin + '" ';
    objectString += document.location.search.slice(1).replace(/&/g, ' '); // give all page parameters to shockwave
    objectString += '>';
    objectString += '</embed>';
    objectString += '</object>';

    $('#'+hideId).hide();
    document.getElementById(containerId).innerHTML = objectString;
    var th = this;

    $('#gameMenuLink').hover(function(){th.hideGame(containerId, hideId)}, function(){th.showGame(containerId, hideId)});
  }
};

//for shorthand: L.embedShockwave() is the same as LEDjs.embedShockwave()
var L = LEDjs;

