/**
 *  Adjusts the spacing of the links on the header navigation bar. Called by window.onload
 *  event in a setTimout()---a delay is required since the data is being written to the page
 *  asynchronously (AJAX).
 *  @return void
 */
function adjustHeaderNav() {
  if (!document.getElementById("ds_header_nav")) {
    return;
  }

  var hdrNav = document.getElementById("ds_header_nav");
  var hdrW = hdrNav.offsetWidth - 100;  // less 100 px padding
  var hnSpans = hdrNav.getElementsByTagName("span");
  var spanTotalW = 0;
  var numGutters, gutterSpace;

  for (var i = 0; i < hnSpans.length; i++) {
    spanTotalW += hnSpans[i].offsetWidth;
  }

  numGutters = hnSpans.length - 1;
  gutterW = Math.floor((hdrW - spanTotalW) / numGutters);

  for (var i = 0; i < hnSpans.length; i++) {
    if (numGutters--) {
      hnSpans[i].style.marginRight = Math.abs(gutterW) + "px";
    }
  }

  try {
    hdrNav.style.visibility = "visible";
  }
  catch (e) {}
}

function fnGetCookie(val) {
  var returnVal = "";
  var allcookies = document.cookie;

  if (allcookies.length > 0) {
    var pos = allcookies.indexOf(val + "=");

    if (pos != -1) {
      var start = allcookies.indexOf("=", pos) + 1;
      var end = allcookies.indexOf(";", start);
      if (end == -1) {
        end = allcookies.length;
      }

      var value = allcookies.substring(start, end);
      returnVal = value;
    }
  }
  return returnVal;
}

function fnAdjustPage() {
  if (document.getElementById("ds_frame_body") &&
      document.getElementById("ds_frame_body").scrollHeight > 90 ) {
    try {
      document.getElementById("ds_frame_mid").style.height = (document.getElementById("ds_frame_body").scrollHeight - 90) + "px";
    }
    catch (e) {}
  }

  try {
    document.getElementById("ds_content_container").style.visibility = "visible";
  }
  catch (e) {}

  try {
    document.getElementById("ds_content_container_homepage").style.visibility = "visible";
  }
  catch (e) {}
  
  setTimeout("adjustHeaderNav()", 200);
}

function fnSetWarrior(val) {
  document.location.href = "warrior_detail.php?weapon=" + val;
}

function fnHidePopup(id) {
  try {
    document.getElementById(id).style.visibility = "hidden";
  }
  catch (e) { return; }

  if (id == "ds_video_player") {
    try {
      var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);

      if (hasRightVersion) {
        fnGetFlashMovie("video_player").stopMovie();
      }
      else {
        document.getElementById("ds_video_player_flash").innerHTML = "";
      }
    } 
    catch (e) {}
  }
}

function fnHidePopups() {
  fnHidePopup("ds_video_player");
  fnHidePopup("ds_newsletter");
  fnHidePopup("ds_viewer_wrapper");
  fnHideMask();
}

/**
 *  Sets the mask size based on client type.
 *  @return void
 */
function setMaskSize()
{
  var html = document.getElementsByTagName("html")[0];
  var body = document.getElementsByTagName("body")[0];

  switch (getUserAgent()) {
    case "CHROME":
    case "SAFARI":
      document.getElementById("ds_mask").style.width = body.scrollWidth + "px";
      document.getElementById("ds_mask").style.height = body.scrollHeight + "px";
      break;

    default:
      document.getElementById("ds_mask").style.width = html.scrollWidth + "px";
      document.getElementById("ds_mask").style.height = html.scrollHeight + "px";
  }
}

function showPopup(id) {
  fnHidePopups();

  if (id == "ds_video_player") {
    try {
      var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);

      if (hasRightVersion) {
        fnGetFlashMovie("video_player").playMovie();
      }
      else {
        if (fnGetCookie("video_def") == "hd") {
          fnSetVideoDef(0);
        }
        else {
          fnSetVideoDef(1);
        }
      }
    }
    catch(e) {}
  }

  document.getElementById(id).style.visibility = "visible";
  fnShowMask();
}


function fnShowMask() {
  try {
    document.getElementById("ds_mask").style.visibility = "visible";
  }
  catch (e) {}
}

function fnHideMask() {
  try {
    document.getElementById("ds_mask").style.visibility = "hidden";
  }
  catch (e) {}
}

/**
 *  Swaps an image by changing its src. Does nothing if swap fails.
 *  @param  {String} id     ID attribute of image element to replace
 *  @param  {String} imgSrc  replacement Image object
 *  @type   void
 */
function fnSwapImg(id, imgSrc) {
  if (document.getElementById) {
    document.getElementById(id).src = imgSrc;
  }
  else if (document.all) {
    document.all[id].src = imgSrc;
  }
}

function fnSetVideoDef(val) {
  var url;
  var buttonHtml, hasRightVersion, alternateContent;

  if (val == 0) {
    url = mp4_hd;
    buttonHTML = '<span class="active">HD</span>' +
      ' <span style="display: inline-block; width: 18px;"></span> ' +
      '<span><a href="javascript:void(0);" target="_self" onclick="fnSetVideoDef(1);">SD</a></span>';
    document.cookie = "video_def=hd";
  }
  else {
    url = mp4_sd;
    buttonHTML = '<span><a href="javascript:void(0);" target="_self" onclick="fnSetVideoDef(0);">HD</a></span>' +
      ' <span style="display: inline-block; width: 18px;"></span> ' +
      '<span class="active">SD</span>';
    document.cookie = "video_def=sd";
  }

  document.getElementById("ds_video_sdhd_button").innerHTML = buttonHTML;
  hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);

  if (hasRightVersion) {
    fnGetFlashMovie("video_player").setVideoDef(url);
  }
  else {
    alternateContent = "<video id='video_player' width='864' height='480' controls preload autoplay><source src='" + url + "'  type='video/mp4'  /></video>";
    document.getElementById("ds_video_player_flash").innerHTML = alternateContent;
    document.getElementById("video_player").src = url;
    document.getElementById("video_player").play();
  }
}

function fnGetFlashMovie(movieName) {
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  return (isIE) ? window[movieName] : document[movieName];
}

function fnIsIE() {
  return navigator.appName.indexOf("Microsoft") != -1;
}

function fnTweet() {
  try {
    window.open("http://twitter.com/home?status=" + document.getElementById("TwitterText").innerHTML, "_blank");
  }
  catch (e) {}
}

function fnShowCreatureAnimation(flashMovie, alternateImage) {
  var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);

  if(hasRightVersion) {  // if we've detected an acceptable version
    // embed the flash movie
    AC_FL_RunContent(
      'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,124,0',
      'width', '460',
      'height', '345',
      'src', 'video_looper',
      'quality', 'high',
      'pluginspage', 'http://www.adobe.com/go/getflashplayer',
      'align', 'middle',
      'play', 'true',
      'loop', 'true',
      'scale', 'showall',
      'wmode', 'transparent',
      'devicefont', 'false',
      'id', 'video_looper',
      'bgcolor', '#000000',
      'name', 'video_looper',
      'menu', 'false',
      'allowFullScreen', 'false',
      'allowScriptAccess','sameDomain',
      //'movie', 'flash/video_looper',
      'movie', flashMovie,
      //'FlashVars', flashQuery,
      'salign', ''
      ); //end AC code
  } else {  // flash is too old or we can't detect the plugin
    var alternateContent = "<img src='" + alternateImage + "'/>";
    document.write(alternateContent);  // insert non-flash content
  }
}

function fnShowBackgroundAnimation() {
  var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
  if(hasRightVersion) {  // if we've detected an acceptable version
    // embed the flash movie
    AC_FL_RunContent(
      'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0',
      'width', '100%',
      'height', '354',
      'src', 'homepage_bg',
      'quality', 'high',
      'pluginspage', 'http://www.adobe.com/go/getflashplayer',
      'align', 'middle',
      'play', 'true',
      'loop', 'true',
      'scale', 'noscale',
      'wmode', 'gpu',
      'devicefont', 'false',
      'id', 'ds_homepage_bg',
      'bgcolor', '#000000',
      'name', 'ds_homepage_bg',
      'menu', 'true',
      'allowFullScreen', 'false',
      'allowScriptAccess','sameDomain',
      'movie', 'flash/homepage_bg',
      'salign', ''
      ); //end AC code
  } else {  // flash is too old or we can't detect the plugin
    var alternateContent = "<img src='images/bg_homepage.jpg'/>";
    document.write(alternateContent);  // insert non-flash content
  }
}

function fnShowVideoPlayer(flashQuery, html5videoQuery) {
  var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);

  if (hasRightVersion) {  // if we've detected an acceptable version
    // embed the flash movie
    AC_FL_RunContent(
      'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,124,0',
      'width', '864',
      'height', '480',
      'src', 'video_player',
      'quality', 'high',
      'pluginspage', 'http://www.adobe.com/go/getflashplayer',
      'align', 'middle',
      'play', 'true',
      'loop', 'true',
      'scale', 'showall',
      'wmode', 'window',
      'devicefont', 'false',
      'id', 'video_player',
      'bgcolor', '#000000',
      'name', 'video_player',
      'menu', 'false',
      'allowFullScreen', 'false',
      'allowScriptAccess','sameDomain',
      'movie', 'video_player',
      'FlashVars', flashQuery,
      'salign', ''
      ); //end AC code
  }
  else {  // flash is too old or we can't detect the plugin
    //var alternateContent = "<video id='video_player' width='864' height='480' controls preload><source src='" + html5videoQuery + "'  type='video/mp4'/></video>";
    //document.write(alternateContent);  // insert non-flash content
  }
}

/**
 *  Returns the user agent.
 *  @return Valid values: OLD_IE, IE, CHROME, SAFARI, OPERA, FIREFOX, and blank.
 */
function getUserAgent()
{
  var nua = navigator.userAgent;
  var regxIE;
  
  if (navigator.appName == "Microsoft Internet Explorer") {
    regxIE = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    regxIE.exec(nua);
  
    if (parseFloat( RegExp.$1 ) < 8) {
      return "OLD_IE";
    }
    else if (document.documentMode < 8) {
      return "OLD_IE";
    }
    else {
      return "IE";
    }
  }
  else if (nua.indexOf("Chrome") != -1) {
    return "CHROME";
  }
  else if (nua.indexOf("Safari") != -1 && nua.indexOf("Chrome") == -1) {
    return "SAFARI";
  }
  else if (nua.indexOf("Opera") != -1) {
    return "OPERA";
  }
  else if (nua.indexOf("Firefox") != -1 && nua.indexOf("Opera") == -1) {
    return "FIREFOX";
  }
  
  return "";
}

/**
 *  Tests (trimmed) string variable for content.
 *  @param  {String} s
 *  @type   boolean
 */
function isBlank(s)
{
  return (s.trim() == "");
}

/**
 *  Trims leading whitespace from a string. Method extends String object.
 *  @requires String
 *  @return   New string with spaces removed from the left.
 *  @type     String
 */
String.prototype.trimLeft = function ()
{
  return this.replace(/^\s+/, "");
};

/**
 *  Trims trailing whitespace from a string. Method extends String object.
 *  @requires String
 *  @return   New string with spaces removed from the right.
 *  @type     String
 */
String.prototype.trimRight = function ()
{
  return this.replace(/\s+$/, "");
};

/**
 *  Trims leading and trailing whitespace from a string. Method extends String
 *  object.
 *  @requires String
 *  @return   New string with leading and trailing spaces removed.
 *  @type     String
 */
String.prototype.trim = function ()
{
  return this.trimLeft().trimRight();
};

function changeGlow(id, className) {
  document.getElementById(id).className = className;
}

