//
// iTunes Client Detection code
//
// This javascript library is tied intimately to MHBrowserRedirect.
// Note also that the iTunes U team links to this file on phobos, so check w/ them before changing it.
//
// copied from http://ax.phobos.apple.com.edgesuite.net/detection/itmsCheck.js
// (found in itunes redirect page http://itunes.apple.com/artist/digamma/id373959962 )
//
//
var BROWSER_SAFARI = 1;
var BROWSER_FIREFOX = 2;
var BROWSER_INTERNET_EXPLORER = 3;
var BROWSER_CHROME = 4;
var BROWSER_OTHER = 5;

var ITUNES_INSTALLED_COOKIE_NAME="iTunesPresent";

function iTunesDetected() {

  // if we've already figured out that iTunes is present, rely on that data:
  //if ('true' == getCookie(ITUNES_INSTALLED_COOKIE_NAME)) return true;

  // If we are on the Mac, assume that iTunes is installed.
  if (-1 != navigator.userAgent.indexOf("Macintosh")) return true;

  if (BROWSER_INTERNET_EXPLORER == detectedBrowser()) {
    return iTunesActiveXComponentInstalled();
  }
  
  // last chance:
  return iTunesMozillaPluginDetected();
}

function detectedBrowser() {
  if (-1 != navigator.userAgent.indexOf("Chrome")) return BROWSER_CHROME;
  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 iTunes ActiveX Component to mean that iTunes itself has been installed.
 * @return true if the iTunes ActiveX Component was successfully loaded.
 */
function iTunesActiveXComponentInstalled() {
  var detectObj = document.getElementById('iTunesDetectorIE');
  var returnVal = false; // If we can't load the ActiveX control, assume we do not have ITMS

  if ((detectObj != null) && (typeof(detectObj) != "undefined")) {
    if (typeof(detectObj.IsITMSHandlerAvailable) != "undefined") {
      returnVal = detectObj.IsITMSHandlerAvailable;
      //dbg(typeof(detectObj.IsITMSHandlerAvailable));
    }

    if ((returnVal == null) || (typeof (returnVal) == "undefined")) returnVal = false;
  }
  //dbg("ActiveX Control result: " + returnVal);
  return returnVal;
}

/**
 * We interpret the presence of the iTunes Firefox plugin to mean that iTunes itself has been installed.
 * @return true if the iTunes Firefox plugin was successfully loaded.
 */
function iTunesMozillaPluginDetected() {
  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("iTunes Application Detector") > -1) { result = true }
    }
  }
  //info("FF plugin detected: " + result);
  return result;
}

function url_change()
{
    if (iTunesDetected())
    {
    }
}

