var BROWSER_SAFARI = 1;
var BROWSER_FIREFOX = 2;
var BROWSER_INTERNET_EXPLORER = 3;
var BROWSER_OTHER = 4;

function AXMediaDetected() {

  // if we've already figured out that AXMedia is present, rely on that data:
  //if ('true' == getCookie(AXMedia_INSTALLED_COOKIE_NAME)) return true;

  // If we are on the Mac, assume that AXMedia is installed.
  //if (-1 != navigator.userAgent.indexOf("Macintosh")) return true;

  if (BROWSER_INTERNET_EXPLORER == detectedBrowser()) {
    return AXMediaActiveXComponentInstalled();
  }
  
  // last chance:
  return AXMediaMozillaPluginDetected();
}

function detectedBrowser() {
  if (-1 != navigator.userAgent.indexOf("AppleWebKit")) return BROWSER_SAFARI;
  if (-1 != navigator.userAgent.indexOf("Firefox")) return BROWSER_FIREFOX;
  if (-1 != navigator.userAgent.indexOf("MSIE ")) return BROWSER_INTERNET_EXPLORER;
  else return BROWSER_OTHER;
}

/**
 * We interpret the presence of the AXMedia ActiveX Component to mean that AXMedia itself has been installed.
 * @return true if the AXMedia ActiveX Component was successfully loaded.
 */
function AXMediaActiveXComponentInstalled() {
var returnVal = false;
  var detectObj = document.getElementById('axmedia-activex');
   if ((detectObj != null) && (typeof(detectObj) != "undefined")) {
    if (typeof(detectObj.runAXMedia) != "undefined") {
      returnVal = true;
      }
}
return returnVal;
}

/**
 * We interpret the presence of the AXMedia Firefox plugin to mean that AXMedia itself has been installed.
 * @return true if the AXMedia Firefox plugin was successfully loaded.
 */
function AXMediaMozillaPluginDetected() {
  var result = false;
  if (navigator.plugins && navigator.plugins.length > 0) {
    for (var i=0; i < navigator.plugins.length; i++ ) {
      var plugin = navigator.plugins[i];
      var pluginName = plugin.name;
      if (pluginName.indexOf("AXMedia Plugin") > -1) { result = true }
    }
  }
  info("FF plugin detected: " + result);
  return result;
}

/**
 * This is the main entry point from WebObjects code.  See MHBrowserRedirect.java
 *
 * @param url the url to open if AXMedia is installed
 * @param downloadUrl the url to go to to download AXMedia
 * @param overridePanelId the id to unhide if the browser is firefox/opera.
 * @param noClose if true, don't close the browser window after opening AXMedia.
 */
function p2pClickAxmedia(redirectUrl, downloadUrl) {
  
  if (AXMediaDetected()) {
    if (detectedBrowser() == BROWSER_INTERNET_EXPLORER) 
    var detectObj = document.getElementById('axmedia-activex');
    else var detectObj = document.getElementById('axmedia-plugin');
    var result = 0;
    if(detectObj == null) 
    {
		alert('Error: AXMedia Plugin not initialized');
		result = 1;
    }
    else
	 result = detectObj.runAXMedia(downloadUrl);
    if(result==1) window.location.replace(redirectUrl);
    else if(result==3) alert('Error:'+result+' starting AXMedia, please contact our staff');
    else if(result==4) alert('Error:'+result+' downloading the object, please contaact our staff');
  }
  else {
    var b = detectedBrowser();
    if (BROWSER_INTERNET_EXPLORER == b || BROWSER_FIREFOX == b || BROWSER_SAFARI == b) {
          window.location.replace(redirectUrl);
    }
     }
  return true;
}

/**
 * Open the given url (using AXMedia) and make a best effort to close or go back in the current window.
 */
function replaceCurrentPageWithUrl(url) {

  window.location.href = url;

  info("Window History Length: " + window.history.length);
  if (window.history.length < 2) {
    setTimeout('window.close()', 100);
  } else {
    setTimeout('window.history.back()', 100);
  }
  return true;
}

function setCookie(cookieName,cookieValue,ttlMillis) {
  var expire = new Date();
  expire.setTime(expire.getTime() + ttlMillis);
  var cookie = cookieName + "=" + escape(cookieValue) + "; expires=" + expire.toGMTString();
  info("setCookie(): " + cookie);
  document.cookie = cookie;
}

function getCookie(cookieName) {
  if (null == document.cookie || null == cookieName) return null;
  var cookies = document.cookie.split(';');
  var result = null;
  for (var i=0; i < cookies.length; i++) {
    var c = cookies[i];
    var keyValue = c.split('=');
    if (-1 < keyValue[0].indexOf(cookieName)) {
      result = unescape(keyValue[1]);
      break;
    }
  }
  info("getCookie(" + cookieName + "): " + result);
  return result;
}

function dbg(str) {
//  return alert(str);
}

function info(str) {
//  return alert(str);
}
