/*
 *
 *	jQuery Timer plugin v0.1
 *		Matt Schmidt [http://www.mattptr.net]
 *
 *	Licensed under the BSD License:
 *		http://mattptr.net/license/license.txt
 *
 */

 jQuery.timer = function (interval, callback)
 {
 /**
  *
  * timer() provides a cleaner way to handle intervals
  *
  *	@usage
  * $.timer(interval, callback);
  *
  *
  * @example
  * $.timer(1000, function (timer) {
  * 	alert("hello");
  * 	timer.stop();
  * });
  * @desc Show an alert box after 1 second and stop
  *
  * @example
  * var second = false;
  *	$.timer(1000, function (timer) {
  *		if (!second) {
  *			alert('First time!');
  *			second = true;
  *			timer.reset(3000);
  *		}
  *		else {
  *			alert('Second time');
  *			timer.stop();
  *		}
  *	});
  * @desc Show an alert box after 1 second and show another after 3 seconds
  *
  *
  */

	var interval = interval || 100;

	if (!callback)
		return false;

	_timer = function (interval, callback) {
		this.stop = function () {
			clearInterval(self.id);
		};

		this.internalCallback = function () {
			callback(self);
		};

		this.reset = function (val) {
			if (self.id)
				clearInterval(self.id);

			var val = val || 100;
			this.id = setInterval(this.internalCallback, val);
		};

		this.interval = interval;
		this.id = setInterval(this.internalCallback, this.interval);

		var self = this;
	};

	return new _timer(interval, callback);
 };
;function XMLEnvironment() {

  if (!window.ActiveXObject) {
    XPathResult.prototype.nextNode = function () {
      return (this.iterateNext());
      };

    Node.prototype.loadXML = function (StringValue) {
      var parser = new DOMParser();
      var tempDoc = parser.parseFromString(StringValue, "text/xml");
      
      if (tempDoc == null) {
        return (false);
	}

      if (this.hasChildNodes()) {
        this.replaceChild(tempDoc.firstChild.cloneNode(true), this.firstChild);
        } else {
        this.appendChild(tempDoc.firstChild.cloneNode(true));
        }
       return (true);
      };

    Node.prototype.__defineGetter__ ("xml", function () {
      var markup;
      var xmlSerializer = new XMLSerializer();
      markup = xmlSerializer.serializeToString(this);
      return (markup);
      });

    Node.prototype.selectSingleNode = function (XPath) {
      var ret;
      if (this.ownerDocument == null) {
        ret = this.evaluate(XPath, this, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
        } else {
        ret = this.ownerDocument.evaluate(XPath, this, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
        }
      return (ret.singleNodeValue);
      };

    Node.prototype.selectNodes = function (XPath) {
      var nodeSet;
      if (this.ownerDocument == null) {
        nodeSet = this.evaluate(XPath, this, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
        } else {
        nodeSet = this.ownerDocument.evaluate(XPath, this, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
        }

      return (nodeSet);
      };

    Node.prototype.transformNodeToObject = function (Stylesheet, Output) {
      var processor = new XSLTProcessor();
      processor.importStylesheet(Stylesheet);
      var fragment = processor.transformToFragment(this, Output);
      Output.appendChild(fragment);
      };

    Node.prototype.transformNode = function (Stylesheet) {
      var dummyDoc = XMLEnvironment.createDocument();
      var processor = new XSLTProcessor();
      processor.importStylesheet(Stylesheet);
      var fragment = processor.transformToFragment(this, dummyDoc);
      return (fragment.xml);
      };

    HTMLElement.prototype.oldSetAttribute = HTMLElement.prototype.setAttribute;

    HTMLElement.prototype.setAttribute = function (Name, Value) {
      var realName = Name;

      if (Name == "className") {
        realName = "class";
        }
      this.oldSetAttribute(realName, Value);
      };

    HTMLElement.prototype.__defineGetter__ ("innerText", function () {
      return (this.textContent);
      });

    HTMLElement.prototype.__defineSetter__ ("innerText", function (NewValue) {
      this.textContent = NewValue;
      });

    Node.prototype.__defineGetter__ ("text", function () {
      return (this.textContent);
      });

    Node.prototype.__defineSetter__ ("text", function (NewValue) {
      this.textContent = NewValue;
      });

    XPathResult.prototype.nextNode = function() {
      return (this.iterateNext());
      };

    XMLEnvironment.createDocument = function() {
      var ret = document.implementation.createDocument("", "", null);
      
      if (!ret.load) {
        ret.load = function(url) {
	  var xmlHttpX = XMLEnvironment.createXmlHttp();
	  xmlHttpX.open("GET", url, false);
          try {
            xmlHttpX.send(null);
           } catch (e) {
           }
	   
          if ((xmlHttpX.status != 0)  &&   /* success for local requests*/
              (xmlHttpX.status != 200) ) {
	    return (false);
	    }
	    
         return (ret.loadXML(xmlHttpX.responseText));	    
	  };
	  
        }
	
      return (ret);
      };

    XMLEnvironment.createXmlHttp = function () {
      var xmlHttp = new XMLHttpRequest();
      xmlHttp.overrideMimeType("text/xml");
      xmlHttp.oldOpen = xmlHttp.open;

      xmlHttp.open = function (Method, Url, Async) {
      
        if ((Url.toLowerCase().substr(0,7) == "http://") ||
             (Url.toLowerCase().substr(0,8) == "https://")){
          try {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
            } catch (e) {
            }
          }
        this.oldOpen(Method, Url, Async);
        };

      return (xmlHttp);
      };

    } else {

    XMLEnvironment.createDocument = function() {
      var newDoc = new ActiveXObject("MSXML.DOMDocument");
      newDoc.async = true;
      newDoc.preserveWhiteSpace = true;

      return (newDoc);
      };

    XMLEnvironment.createXmlHttp = function () {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      return (xmlHttp);
      };
    };

  XMLEnvironment.Copy = function (Target, Source, XPath) {
    try {
      var nodeList = Source.selectNodes(XPath);
      var node;
      while (node = nodeList.nextNode() != null) {
        Target.appendChild(node.cloneNode(true));
        }
      } catch (e) {}
    };

  XMLEnvironment.Set = function (TargetNode, Source, XPath) {
    var value = "";
    var sourceNode = Source.selectSingleNode(XPath);
    if (sourceNode) {
      if (sourceNode.firstChild) {
        value = sourceNode.firstChild.nodeValue;
        }
      }
    var valueNode = TargetNode.ownerDocument.createTextNode(value);
    oldNode = TargetNode.firstChild;
    if (oldNode) {
      TargetNode.replaceChild(valueNode, TargetNode.firstChild);
      } else {
      TargetNode.appendChild(valueNode);
      }

    };

  };

XMLEnvironment();
/*******************************************************************************

  GenPlugin() constructor

*******************************************************************************/
function GenPlugin(pluginname) {
  this.genPlugin = this.initialize(pluginname);
  };


/*******************************************************************************

  GenPlugin.initialize()

*******************************************************************************/
GenPlugin.prototype.initialize = function(pluginName) {

  if (window.ActiveXObject) {
    /* IE way */
    try {
      plugin = new ActiveXObject(pluginName);
      } catch(e) {
      return(null);
      }

    } else {
    /* Mozilla way */
    
    var mimeType = this.findMimeHandler("x-vnd-rsj-prodown");
    
    if (mimeType == null) {
      navigator.plugins.refresh(false);     
    
      mimeType = this.findMimeHandler("x-vnd-rsj-prodown");
      }
    
    if (mimeType == null) {
      return(null);
      }
    
     try {    
      /* instantiate the plugin */
      plugin = document.getElementById(pluginName);
    
      if (plugin != null) {
        return (plugin);
        }
     } catch(e) {
     }
    
    plugin = document.createElement("object");
    plugin.setAttribute("type", mimeType);
    plugin.setAttribute("progid", pluginName);
    plugin.setAttribute("style", "width: 0px; height: 0px;");
    document.body.appendChild(plugin);
    }

  return(plugin);
  };

// ------------------------------------------------------------------------------

GenPlugin.prototype.findMimeHandler = function (name) {
  var ret = null;
  
  var reg = new RegExp(name, "i");
  
  for (var i = 0; i < navigator.plugins.length; i++) {
    for (var j = 0; j < navigator.plugins[i].length; j++) {
      if (navigator.plugins[i][j].type.search(reg) != -1) {
        return ("application/" + name);
        }
      }
    }
  
  return (null);
  };
 /*******************************************************************************

  GenPlugin.execute()

*******************************************************************************/
GenPlugin.prototype.execute = function (xmlInput) {
  if (!this.genPlugin) {
    return("");
    }
  
  if (!xmlInput || xmlInput == "") {
    return("");
    }
  try {
    var ret = this.genPlugin.invoke(xmlInput);
    return(ret);
    } catch (e) {
    return("");
    }
  };

// -------------------------------------------------------------------------------------------

GenPlugin.prototype.unload = function() {
  if (this.genPlugin) {
  
    if (!window.ActiveXObject) {
//      document.body.removeChild(this.genPlugin);
      };
    
//    this.genPlugin = null;
    }
  };
/*******************************************************************************

  ProgressiveDownloadPlayer() constructor

*******************************************************************************/
function ProgressiveDownloadPlayer () {  
  this.request = XMLEnvironment.createDocument();
  this.prodownPlayer = null;
  this.pluginVersion = "not initialized";

  this.image = "";
  this.imageSize = "";
  this.parameter = "";
  this.transformation=null;
  this.url = "";
  this.hintUpload = "";
  this.description = "";
  this.uploadUrl = "";
  this.fileName = "";
  this.autoLaunch = "";
  this.autoStop = false;
  this.detailView = false;
  this.throttle = false;
  this.detailViewLinkText = "Detail View";
  };

ProgressiveDownloadPlayer.prototype.xslt = "";
ProgressiveDownloadPlayer.prototype.version = "0.00.0000";

/*******************************************************************************

  displayStatus()

*******************************************************************************/
ProgressiveDownloadPlayer.prototype.displayStatus = function(event) {
  // XSLT status to HTML
  var xsltout;

  if (!event) {
    return (event);
    }

  var statusDiv = $('#prodownstatus');
  if (!statusDiv) {
    return(event);
    }

  if (this.transformation == null) {
    this.loadTransformation();
    }
//alert(event);
  var eventDoc = XMLEnvironment.createDocument();
  eventDoc.async = false;
  eventDoc.loadXML(event);
  xsltout = eventDoc.transformNode(this.transformation);

  // Rewrite status div(s)
  $(statusDiv).html(xsltout);

  var detailed = $(".prodownStatusDetailed");

  if (this.detailView) {
    detailed.each(function() { $(this).show() });
    } else {
    detailed.each(function() { $(this).hide() });
    }

  $('#detailViewLink').text(this.detailViewLinkText);

  var statusAttribute = eventDoc.selectSingleNode("/Status/@status");

  if (statusAttribute != null) {
    var curStatus = statusAttribute.text;

    if (curStatus == "STATUS_LOADING" || curStatus == "STATUS_READY") {
      $('#prodownspinner').show() ;
      } else {
      $('#prodownspinner').hide() ;
      }

    if (curStatus == "STATUS_READY") {
      $('#prodownready').show() ;
      } else {
      $('#prodownready').hide() ;
      }
    }

  return (event);
  };

/*******************************************************************************

  setVerb()

*******************************************************************************/
ProgressiveDownloadPlayer.prototype.setVerb = function (verb) {
  this.request.loadXML("<" + verb + "/>");
  this.setAttribute("jsVersion", this.version);
  };

/*******************************************************************************

  setAttribute()

*******************************************************************************/
ProgressiveDownloadPlayer.prototype.setAttribute = function (attr, value) {
  this.request.firstChild.setAttribute(attr, value);
  };

/*******************************************************************************

  execute()

*******************************************************************************/
ProgressiveDownloadPlayer.prototype.execute = function () {

  this.passParameters();

  var request = this.request.xml;
  var response = this.transport.execute(request);

  if (response == undefined) {
    //alert(request);
    }

  return (response);
  };

/*******************************************************************************

  eventReceived()

*******************************************************************************/
ProgressiveDownloadPlayer.prototype.eventReceived = function (event) {
  this.displayStatus(event);
  };


/*******************************************************************************

  initialize()

*******************************************************************************/
ProgressiveDownloadPlayer.prototype.initialize = function() {
  var curPlayer = this;

  // Connect to service process
  this.transport = new GenPlugin("prodown.GenRSJ");

  this.loadTransformation();

  this.transport.eventReceived = function (event) {
    curPlayer.eventReceived(event);
    };


  //this.setVerb("QueryStatus");
  //this.displayStatus(this.execute());
  };

/*******************************************************************************

  loadAndRun()

*******************************************************************************/
ProgressiveDownloadPlayer.prototype.loadAndRun = function() {
  this.setVerb("LoadImage");
  this.displayStatus(this.execute());
  };

/*******************************************************************************

  load()

*******************************************************************************/
ProgressiveDownloadPlayer.prototype.load = function() {
  this.setVerb("Load");

  this.displayStatus(this.execute());
  };

/*******************************************************************************

  launch()

*******************************************************************************/
ProgressiveDownloadPlayer.prototype.launch = function() {
  this.setVerb("Launch");

  this.displayStatus(this.execute());
  };

/*******************************************************************************

  stop()

*******************************************************************************/
ProgressiveDownloadPlayer.prototype.stop = function() {
  this.setVerb("Stop");
  this.displayStatus(this.execute());
  };

/*******************************************************************************

  uploadImage()

*******************************************************************************/
ProgressiveDownloadPlayer.prototype.uploadImage = function() {
  this.setVerb("UploadImage");

  this.displayStatus(this.execute());
  };

/*******************************************************************************

  mountImage()

*******************************************************************************/
ProgressiveDownloadPlayer.prototype.mountImage = function() {
  this.setVerb("MountImage");

  this.displayStatus(this.execute());
  };


/*******************************************************************************

  listImages()

*******************************************************************************/
ProgressiveDownloadPlayer.prototype.listImages = function(formatted) {
  var retxmlDoc;

  this.setVerb("ListImages");

  var retxml = this.execute();

  if (!formatted) {
    return(retxml);
    }

  var retxmlDoc = XMLEnvironment.createDocument();
  retxmlDoc.async = false;
  retxmlDoc.loadXML(retxml);

  var xsltout = retxmlDoc.transformNode(this.transformation);

  return(xsltout);
  };

/*******************************************************************************

  debug()

******************************************************************************/
ProgressiveDownloadPlayer.prototype.debug = function() {
  this.setVerb("Debug");
  this.displayStatus(this.execute());
  };

/*******************************************************************************

  queryStatus()

*******************************************************************************/
ProgressiveDownloadPlayer.prototype.queryStatus = function() {
  this.setVerb("QueryStatus");
  return(this.displayStatus(this.execute()));
  };

/*******************************************************************************

  deleteImage()

*******************************************************************************/
ProgressiveDownloadPlayer.prototype.deleteImage = function() {
  this.setVerb("DeleteImage");
  this.displayStatus(this.execute());
  };

/*******************************************************************************

  createImage()

*******************************************************************************/
ProgressiveDownloadPlayer.prototype.createImage = function() {
  this.setVerb("CreateImage");
  this.displayStatus(this.execute());
  }

/*******************************************************************************

  loadTransformation()

*******************************************************************************/
ProgressiveDownloadPlayer.prototype.loadTransformation = function() {

  var doc = XMLEnvironment.createDocument();
  doc.async = false;
  if (doc.load(this.xslt)) {
    this.transformation = doc;
    }

  return(doc);
  }


/*******************************************************************************

  getVersion()

*******************************************************************************/
 ProgressiveDownloadPlayer.prototype.getVersion = function() {

	this.setVerb("QueryStatus");

	var response = this.execute();

    if (response == null) {
      return ("-1");
    }

    var responseDoc = XMLEnvironment.createDocument();
    responseDoc.async = false;
    responseDoc.loadXML(response);
    var version = responseDoc.selectSingleNode("/Status/@controlVersion");

	return (version.text);
  };

/*******************************************************************************

  getStatus()

*******************************************************************************/
 ProgressiveDownloadPlayer.prototype.getStatus = function() {

	this.setVerb("QueryStatus");

	var response = this.execute();

    if (response == null) {
      return ("-1");
    }

    var responseDoc = XMLEnvironment.createDocument();
    responseDoc.async = false;
    responseDoc.loadXML(response);
    var status = ( responseDoc.selectSingleNode("/Status/@status") != null ) ? responseDoc.selectSingleNode("/Status/@status").text : '';

	return (status);
  };

/******************************************************************************

  passParameters

******************************************************************************/
ProgressiveDownloadPlayer.prototype.passParameters = function() {

  for (var key in this) {
    if ((typeof(this[key]) == 'string') ||
        (typeof(this[key]) == 'number') ||
        (typeof(this[key]) == 'boolean')) {
      this.setAttribute(key, this[key]);
      }
    }
}

/*******************************************************************************

  ProgressiveDownloadFrame() constructor

*******************************************************************************/
function ProgressiveDownloadFrame(xsltName)
{
  this.transformation=null;
  this.statusFunction = "";
  this.autoLaunch = 0; /* 5 Minutes */
  this.initialized = false;
  this.xslt = xsltName;
  thisFrame = this;
  
  this.Initialize();
  
  return(this);
}


/*******************************************************************************

  Initialize()

*******************************************************************************/
ProgressiveDownloadFrame.prototype.Initialize = function() {

  if (this.initialized) {
    return;
    }

  this.player = new ProgressiveDownloadPlayer();

  this.player.xslt = this.xslt;

  this.player.initialize();

  this.statusTimer = $.timer(1000, function(timer) {
    thisFrame.player.queryStatus();
    timer.reset(3000);
    });
  
  this.initialized = true;
  
  return;
}

/*******************************************************************************

  Launch()

*******************************************************************************/
ProgressiveDownloadFrame.prototype.Launch = function(autoLaunch)
{
  
  return(this.player.launch());
}

/*******************************************************************************

  Load()

*******************************************************************************/
ProgressiveDownloadFrame.prototype.Load = function()
{
  
  
  return(this.player.load());
}


/*******************************************************************************

  Delete()

*******************************************************************************/
ProgressiveDownloadFrame.prototype.Delete = function()
{
  
  
  return(this.player.deleteImage());
}

/*******************************************************************************

  Stop()

*******************************************************************************/
ProgressiveDownloadFrame.prototype.Stop = function()
{
  
  
  this.player.stop();
  
  if (false) {
  this.statusTimer.stop();

  this.statusTimer = null;

  
  this.player = null;
  this.initialized = false;  

  var statusDiv = $('#prodownstatus');
  $(statusDiv).html("<div />");
  }
}

/*******************************************************************************

  ListImages()

*******************************************************************************/
ProgressiveDownloadFrame.prototype.ListImages = function(formatted)
{
  
  return(this.player.listImages(formatted));
}


/*******************************************************************************

  Debug()

*******************************************************************************/
ProgressiveDownloadFrame.prototype.Debug = function()
{
  return(this.player.debug());
}

/*******************************************************************************

  CreateImage()

*******************************************************************************/
ProgressiveDownloadFrame.prototype.CreateImage = function()
{
  return(this.player.createImage());
}

/*******************************************************************************

  CreateImage()

*******************************************************************************/
ProgressiveDownloadFrame.prototype.MountImage = function()
{
  return(this.player.mountImage());
}

/*******************************************************************************

  upload()

*******************************************************************************/
ProgressiveDownloadFrame.prototype.UploadImage = function()
{
  return(this.player.uploadImage());
}

/*******************************************************************************

  QueryStatus()

*******************************************************************************/
ProgressiveDownloadFrame.prototype.QueryStatus = function()
{
  
  alert(this.player.queryStatus());
};

ProgressiveDownloadFrame.prototype.getVersion = function()
{
  return(this.player.getVersion());  
};

ProgressiveDownloadFrame.prototype.getStatus = function()
{
  return(this.player.getStatus());  
};

function DRM () {
  this.drm = null;
  this.request = XMLEnvironment.createDocument();
  this.request.async = false; 
  };

// ---------------------------------------------------------------

DRM.prototype.version = "0.00.0000";

// ---------------------------------------------------------------
  
DRM.prototype.initialize = function () {
  this.drm = new GenPlugin("prodown.GenRSJ");

  if (this.drm.genPlugin == null) {
     return (false);
     }
  return (true);     
  };	
  
// --------------------------------------------------------------

DRM.prototype.execute = function(req) {

  if (!this.drm) {
      if (!this.initialize()) {
        return (null);
	}
      }

  var req = this.request.xml;
  this.rsp = this.drm.execute(req);
  
  this.unload();
  
  return (this.rsp);
  }; 

// --------------------------------------------------------------

DRM.prototype.setVerb = function (verb) {
  this.request.loadXML("<" + verb + "/>");
  this.setAttribute("jsVersion", this.version);
  };

// --------------------------------------------------------------

DRM.prototype.setAttribute = function (attr, value) {
   this.request.firstChild.setAttribute(attr, value);
   };

// --------------------------------------------------------------
  
DRM.prototype.getHardwareId = function() {

  if (!this.hardwareId) {
    this.setVerb("QueryHardwareId");
  
    var response = this.execute();
    
    if (response == null) {
      return (null);
      }
	
    var responseDoc = XMLEnvironment.createDocument();
    responseDoc.async = false;
    responseDoc.loadXML(response);

    this.hardwareId = ( responseDoc.selectSingleNode("/Response/@hardwareId") != null ) ? responseDoc.selectSingleNode("/Response/@hardwareId").text : '';
    }
    
  return (this.hardwareId);
  };	
  
// ------------------------------------------------------------------------

DRM.prototype.acquireLicense = function (url) {

  this.setVerb("AcquireLicense");
  this.setAttribute("url", url);

  var response = this.execute();

  return (response != null);
  };
  
// ------------------------------------------------------------------------
/*
DRM.prototype.renewLicense = function() {
    alert("1");
  this.setVerb("RenewLicense");
  var response = this.execute();  
    
  return (response != null);
  };
  */
// ------------------------------------------------------------------------

DRM.prototype.queryCatalog = function() {
  this.setVerb("QueryCatalog");
    
  var response = this.execute();    
    
  return (response);
  };

// ------------------------------------------------------------------------

DRM.prototype.deleteCatalog = function(id) {

  this.request.loadXML("<ModifyCatalog><Delete id='" + id + "'/></ModifyCatalog>");
  this.setAttribute("jsVersion", this.version);
  var response= this.execute();

  return (response != null);
  };

// ------------------------------------------------------------------------

DRM.prototype.unload = function() {
   if (this.drm != null) {
     this.drm.unload();
     this.drm = null;
     }
   };

// ------------------------------------------------------------------------

 DRM.prototype.hasLicense = function(id) {
  this.setVerb("QueryCatalog");
    
  var response = this.execute();    
    
    if (response == null) {
      return (false);
    }
	
    var responseDoc = XMLEnvironment.createDocument();
    responseDoc.async = false;
    responseDoc.loadXML(response);	
    var license = ( responseDoc.selectSingleNode("/Response/Catalog/Game[@id='" + id + "']/@license") != null ) ? responseDoc.selectSingleNode("/Response/Catalog/Game[@id='" + id + "']/@license").text : '';
	
	return (license != null);
  };
  
// ------------------------------------------------------------------------

DRM.prototype.renewLicense = function(id) {

	this.setVerb("RenewLicense");
	
	if (id != null) {
		this.setAttribute("id", id);
	}
	
  var response = this.execute();  
  
  return (response != null);
  };
  
// ------------------------------------------------------------------------

DRM.prototype.testRequirements = function(url, xslt) {
  this.setVerb("TestRequirements");
  this.setAttribute("href", url);
  var response = this.execute();
  
  if (xslt != null) {
      
    var xsltDoc = XMLEnvironment.createDocument();
    xsltDoc.async = false;
    if (!xsltDoc.load(xslt)) {
      return (false);
      }
    
    var responseDoc = XMLEnvironment.createDocument();
    responseDoc.async = false;
    responseDoc.loadXML(response);
    
    response = responseDoc.transformNode(xsltDoc);
    }
    
  return (response);
  };
  
// ------------------------------------------------------------
  
function  Loader () {
  this.drm = new DRM();	
  this.prodownPlayer = new ProgressiveDownloadPlayer();
  var loader = this;
  
  $(".run").click(function() {
    loader.run();
    });
  
  $(".load").click(function() {
    loader.load();
    });

  };	
  
// --------------------------------------------------------------

Loader.prototype.getParm = function (url) {
  var hrefComp = url.split("?");
  
  if (hrefComp.length > 1) {
  
    hrefComp = hrefComp[1].split("&");
  
    for (var i = 0; i < hrefComp.length; i++) {
      var setParm = hrefComp[i].split("=");
      
      if (setParm.length > 1) {
        this[setParm[0]] = decodeURIComponent(setParm[1]);
	}
     }     
   }
    this.prodownPlayer.xslt = this.playerXslt;
  };
  
// --------------------------------------------------------------

Loader.prototype.run = function() {
  if (this.installPlugin()) {
     if (this.testVersion()) {
       if (this.checkRequirements()) {	     
         if(this.load()) {
	   }
	 }
      }	 
    }
  	     
  };	
  
// --------------------------------------------------------------
  
Loader.prototype.installPlugin = function() {
  
  if (this.drm.initialize()) {
    $("#installPlugin").hide();	  
    return (true);
    }	  
  $("#installPlugin").show();
  return (false);    
  };
  
// -------------------------------------------------------------

Loader.prototype.testVersion = function() {

  this.prodownPlayer.initialize();
  
  if (!this.minVersion) {
    return (true);
    }    
  
  if (this.prodownPlayer.getVersion()>= this.minVersion) {
    $("#updatePlugin").hide();    
    return (true);
    }	  
  $("#updatePlugin").show();    
  return (false);
  };    
    
// --------------------------------------------------------------

Loader.prototype.checkRequirements = function() {

  if (!this.requirements) {
     return (true);
    }    
  
	
  var rsp = this.drm.testRequirements(this.requirements, null);
  var rspDoc = XMLEnvironment.createDocument();
  rspDoc.async = false;
  rspDoc.loadXML(rsp);
  
  var okNode = rspDoc.selectSingleNode("/Response/Requirements/@ok");
	
	
  if ((okNode != null) &&
      (okNode.text == "true")) {
    $("#correctRequirements").hide();	  
    return (true);
    }	  
  
  var rsp = this.drm.testRequirements(this.requirements, this.reqTransform);
    
  $("#requirements").html(rsp);    
    
  $("#correctRequirements").show();    	
	
  return (false);
  };	
    	  
// -----------------------------------------------------------------

Loader.prototype.load = function () {
	
  $("#loading").show();
	
  var rsp = this.drm.acquireLicense(this.acquireUrl);
  
  if (!rsp) {
    return (false);
    }
	
  // Copy all parameters 
  var rspDoc = XMLEnvironment.createDocument();
  rspDoc.async = false;
  rspDoc.loadXML(this.drm.rsp);
  
  this.prodownFrame = new ProgressiveDownloadFrame(this.playerXslt);

  ProdownFrame = this.prodownFrame;

  var thisLoader = this;

  $("#prodownready").click(function() {
    thisLoader.prodownFrame.player.useLoadParms='true'; 
    thisLoader.prodownFrame.Launch();
    });

  $("#prodownstop").click(function() {
    thisLoader.prodownFrame.Stop();; 

    });
  	
  var attributes = rspDoc.selectNodes("/Response/ModifyCatalog/Game/@*");	
  while ((attrNode = attributes.nextNode()) != null) {
    this.prodownFrame.player[attrNode.name] = attrNode.value;
    }	  
  
  this.prodownFrame.player.autoLaunch = 0;
  this.prodownFrame.player.autoStop = "true";
  this.prodownFrame.Load();
		
  return (false);
  };	
	
// -----------------------------------------------------------------------------------------------

Loader.prototype.testInstalled = function() {
  return (drm.initialize());	
  };	
  
/*******************************************************************************

  toggle_detail_view()

*******************************************************************************/
function toggle_detail_view()
{
  if (!ProdownFrame || !ProdownFrame.player) {
    return;
    }
  
  if (ProdownFrame.player.detailView) {
    ProdownFrame.player.detailView = false;
    ProdownFrame.player.detailViewLinkText = 'Detail View';
    } else {
    ProdownFrame.player.detailView = true;
    ProdownFrame.player.detailViewLinkText = 'Standard View';
    }
  
  ProdownFrame.player.queryStatus();  
}

;var ProdownFrame = null;
var clearTimer = null;

/*******************************************************************************

  will be executed on load ()

*******************************************************************************/
$(function () {

  InitProdown(ProdownFrameXslt);
  });

/*******************************************************************************

  Init Function()

*******************************************************************************/
function InitProdown(xsltName)
{
  ProdownFrame = new ProgressiveDownloadFrame(xsltName);
  };


/*******************************************************************************

  toggle_detail_view()

*******************************************************************************/
function toggle_detail_view()
{
  if (!ProdownFrame || !ProdownFrame.player) {
    return;
    }
  
  if (ProdownFrame.player.detailView) {
    ProdownFrame.player.detailView = false;
    ProdownFrame.player.detailViewLinkText = 'Detail View';
    } else {
    ProdownFrame.player.detailView = true;
    ProdownFrame.player.detailViewLinkText = 'Standard View';
    }
  
  ProdownFrame.player.queryStatus();  
}
;// Copyright 2007 Google, Inc.
// This sample code is under the Apache2 license, see
// http://www.apache.org/licenses/LICENSE-2.0 for license details.
/**
 * @fileoverview Wrapper for Time Tracking
 */

/**
 * @class Time Tracking component.
 *     This class encapsulates all logic for time tracking on a particular
 *     page. Time tracking could be for any object within a page or the page
 *     itself.
 *
 * @param {Array.<Number>} arg1 Optional array that represents the bucket
 * @constructor
 */
var TimeTracker = function(opt_bucket,category,action,label) {
  if (opt_bucket) {
    this.bucket_ = opt_bucket.sort(this.sortNumber);
  } else {
    this.bucket_ = TimeTracker.DEFAULT_BUCKET;
  }
  this.category_ = category;
  this.action_ = action;
  this.label_ = label;
};

TimeTracker.prototype.startTime_;
TimeTracker.prototype.stopTime_;
TimeTracker.prototype.bucket_;
TimeTracker.DEFAULT_BUCKET = [100, 500, 1500, 2500, 5000];
TimeTracker.prototype.category_;
TimeTracker.prototype.action_;
TimeTracker.prototype.label_;

/**
 * Calculates time difference between start and stop
 * @return {Number} The time difference between start and stop
 */
TimeTracker.prototype._getTimeDiff = function() {
  return (this.stopTime_ - this.startTime_);
};

/**
 * Helper function to sort an Array of numbers
 * @param {Number} arg1 The first number
 * @param {Number} arg2 The second number
 * @return {Number} The difference used to sort
 */
TimeTracker.prototype.sortNumber = function(a, b) {
  return (a - b);
}

/**
 * Records the start time
 * @param {Number} arg1 Optional start time specified by user
 */
TimeTracker.prototype._recordStartTime = function(opt_time) {
  if (opt_time != undefined) {
    this.startTime_ = opt_time;
  } else {
    this.startTime_ = (new Date()).getTime();
  }
};

/**
 * Records the stop time
 * @param {Number} arg1 Optional stop time specified by user
 */
TimeTracker.prototype._recordEndTime = function(opt_time) {
  if (opt_time != undefined) {
    this.stopTime_ = opt_time;
  } else {
    this.stopTime_ = (new Date()).getTime();
  }
};

/**
 * Tracks the event. Calculates time and sends information to
 * the event tracker passed
 * @param {Object} arg1 GA tracker created by user
 * @param {String} arg2 Optional event object name
 * @param {String} arg3 Optional event label
 */
TimeTracker.prototype._track = function() {
  _gaq.push(['_trackEvent', this.category_, this.action_, this.label_, this._getTimeDiff()]);
  //console.log(this.category_ + ' ' + this.action_ + ' ' + this.label_ + ' ' + bucketString + ' ' + this._getTimeDiff());
};

/**
 * Sets the bucket for histogram generation in GA
 * @param {Array.<Number>} The bucket array
 */
TimeTracker.prototype._setHistogramBuckets = function(buckets_array) {
  this.bucket_ = buckets_array.sort(this.sortNumber);
};
;