/*

File: qtp_poster.js

Version: <2.0>

Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
Computer, Inc. ("Apple") in consideration of your agreement to the
following terms, and your use, installation, modification or
redistribution of this Apple software constitutes acceptance of these
terms.  If you do not agree with these terms, please do not use,
install, modify or redistribute this Apple software.

In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software. 
Neither the name, trademarks, service marks or logos of Apple Computer,
Inc. may be used to endorse or promote products derived from the Apple
Software without specific prior written permission from Apple.  Except
as expressly stated in this notice, no other rights or licenses, express
or implied, are granted by Apple herein, including but not limited to
any patent rights that may be infringed by your derivative works or by
other works in which the Apple Software may be incorporated.

The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.

IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

Copyright © 2006-2007 Apple Computer, Inc., All Rights Reserved

*/ 

if (typeof(QTP) == "undefined") { 
	/** QTP is a namespace for QuickTime Player javascript objects. */
	QTP = {}; 
}

/**
 * Include the necessary prerequisites for this script: 
 *   QTP.Movie, QTP.Controller, and QTP.UI:
 */
{
	var head = document.getElementsByTagName('head')[0];
	var scripts = $A(document.getElementsByTagName('script'));
	var baseURL;
	scripts.each(function(script) {
		var src = script.getAttribute('src');
		if(typeof(src) == 'undefined')
			return;
			
		var match = src.match(/(.*)qtp_poster.js/);
		if(!match)
			return;
			
		baseURL = src.match(/(.*)qtp_poster.js/)[1];
		if(typeof(baseURL) != 'undefined')
			throw($break);
	});
	
	var include = function(url) {
		var script = document.createElement('script');
		script.setAttribute('language', 'javascript');
		script.setAttribute('type', 'text/javascript');
		script.setAttribute('src', url);
		head.appendChild(script);
	};
	if(typeof(QTP.Movie) == "undefined")
		include(baseURL + "qtp_movie.js");
	if(typeof(QTP.Button) == "undefined")
		include(baseURL + "qtp_ui.js");
	if(typeof(QTP.MovieController) == "undefined")
		include(baseURL + "qtp_controller.js");
}

/**
 * Creates an object which listens for a mouse click, intercepts it, and creates a DOM element
 * in response.  Creates a QTP.Movie using the <code>url</code> parameter, and a QTP.MovieController
 * object for that movie.
 * @param {String} url Initializes the object's url member
 * @class Poster
 * @constructor
 */
QTP.Poster = function(url) 
{ 
	this.url = url;
};

/**
 * URL to use when activating the popup DOM element.
 * @type String
 */
QTP.Poster.prototype.url = null;
/**
 * Caches the movie created when the popup is activated.
 * @type QTP.Movie
 */
QTP.Poster.prototype.movie = null;
/**
 * Caches the "click to play" DOM element.
 * @type Element
 */
QTP.Poster.playButton = null;
/**
 * Caches the poster image
 * @type Element
 */
QTP.Poster.img = null;
/**
 * Caches the DOM movie wrapper element
 * @type Element
 */
QTP.Poster.element = null;
/**
 * Caches the movie controller.
 * @type QTP.Controller
 */
QTP.Poster.controller = null;
/**
 * Determines whether to allow the popup to fade into existence.  Set this
 * to <code>false</code> to disable the fade behavior.
 * @type boolean
 */
QTP.Poster.prototype.fadeIn = true;
/**
 * Sets the default clickText of the popup DOM element.  Set this to another String
 * to change the text displayed in the poster element.
 * @type String
 */
QTP.Poster.prototype.clickText = "Click to Play";
/**
 * Sets the default className of the movieController DOM element.  Set this to another String
 * to change the defailt controller className.
 * @type String
 */
QTP.Poster.prototype.controllerType = 'QTP_Poster';

/**
 * Sets the default className for the poster DOM element.  Set this to another String to
 * change the default poster className.
 * @type String
 */
QTP.Poster.prototype.className = 'QTP';

/**
 * Sets the default attributes to be passed to QTP.Movie when the movie is instantiated.
 * Change or add attributes to control the movie created by the poster object.
 */
QTP.Poster.prototype.attributes = {
		controller: 'false',
		autoplay: 'true', 
		bgcolor: 'black', 
		scale: 'aspect',
		postdomevents: 'true'
	};

/**
 * Caches whether the movie element is currently popped up.
 * @type boolean
 */
 QTP.Poster.prototype.swapped = false;

/**
 * Walks through the DOM heirarchy, looking for <code>&lt;a&gt;</code> tags with a <code>rel</code>
 * attribute of <code>'qtpopup'</code>.  For each it finds, the function creates a new Poster
 * object and attaches the object to the link as its <code>popup</code> member.  The Poster
 * object listens for the link's <code>click</code> event.
 * @example 
 * &lt;script type="javascript" src="qtp_library.js"&gt;&lt;/script&gt;
 * &lt;script type="javascript" src="qtp_ui.js"&gt;&lt;/script&gt;
 * &lt;script type="javascript" src="qtp_controller.js"&gt;&lt;/script&gt;
 * &lt;script type="javascript" src="qtp_popup.js"&gt;&lt;/script&gt;
 * ...
 * &lt;a href="myMovie.mov" rel="qtpopup"&gt;
 */
QTP.Poster.instantiatePosters = function()
{
	if (navigator.platform.indexOf('iPhone') > -1) 
		return;

	var links = $A(document.getElementsByTagName('a'));
	var posters = links.findAll(function(link){ return link.rel && link.rel == 'qtposter'; });
	
	posters.each(function(link){
		var movieUrl = link.href;
		var img = link.getElementsByTagName('img')[0];
		
		if (navigator.platform.indexOf('iPhone') > -1) 
		{
			var movie = new QTP.Movie({src:movieUrl});
			link.parentNode.replaceChild(movie.element, link);
			return;
		}
		
		var poster = new QTP.Poster(movieUrl);		
		var element = document.createElement('div');
		
		
		if(Element.hasAttribute(img, 'width'))
			poster.attributes.width = parseInt(img.getAttribute('width'));
		if(Element.hasAttribute(img, 'height'))
			poster.attributes.height = parseInt(img.getAttribute('height'));

		if(!Element.hasAttribute(element, 'className'))
			element.className = poster.className;
		poster.element = element;
		element.poster = poster;
		element.style.position = 'relative';

		var playContainer = element.appendChild(document.createElement('div'));
		playContainer.className = 'playContainer';
		
		poster.play = playContainer.appendChild(document.createElement('span'));
		poster.play.innerHTML = poster.clickText;
		poster.play.className = 'playButton';
		QTP.Poster.FixOperaOpacity(poster.play);

		element.appendChild(img);
		poster.img = img;
			
		if(link.getAttribute('jscontroller')=='false')
		{
			poster.attributes.controller = 'true';
		}
		else
		{
			poster.controller = new QTP.MovieController(poster.controllerType);
			poster.controller.setMovie(poster);
			element.appendChild(poster.controller.element);
		}

		element.onclick = poster.posterListener.bindAsEventListener(poster);
		link.parentNode.replaceChild(element, link);
	});
};

/**
 * Static utility function which fixes misalignment in the default movie controller.
 * @param {Element} movie element to fix
 */
QTP.Poster.FixIEController = function(element)
{
	if(navigator.appName != "Microsoft Internet Explorer")
		return;

	if(!element.GetControllerVisible());
		setTimeout( function() { element.SetControllerVisible(true); }, 100);
}

/**
 * Static utility function to fix the opacity problems present in Opera.
 * @param {Element} element to fix
 */
QTP.Poster.FixOperaOpacity = function(element)
{
	if(navigator.appName != "Opera")
		return;

	element.style.opacity = "1.0";
}


/**
 * Event listener which calls <code>poster()</code> once, and prevents the poster action from
 * being called again.
 * @param {Event} event The instigating event
 */
QTP.Poster.prototype.posterListener = function(event)
{
	if(this.swapped)
		return;
	this.swapped = true;
	
	Event.stop(event);

	this.play.style.display = 'none';
	this.img.style.opacity = 1.0;

	var intervalId = setInterval(function() {
		var opacity = parseFloat(this.img.style.opacity);
		opacity = Math.max(0.0, opacity - 0.2);
		this.img.style.opacity = opacity;
		this.img.style.filter = 'alpha(opacity='+(opacity*100)+')';
	}.bind(this), 25);
	
	setTimeout(function() {
		clearInterval(intervalId); 
		
		var attributes = this.attributes;
		if(Element.hasAttribute(this.element, 'width'))
			attributes.width = this.element.getAttribute('width');
		if(Element.hasAttribute(this.element, 'height'))
			attributes.height = this.element.getAttribute('height');
			
		attributes.src = this.url;
		var movie = new QTP.Movie(attributes);
		this.movie = movie;
		
		if(this.controller)
			this.controller.setMovie(movie);
		
		this.element.replaceChild(movie.element, this.img);
		
		if(attributes.controller != 'false')
			QTP.Poster.FixIEController(movie.element);
	}.bind(this), 250);
	
	return true;
};

Event.observe(window, 'load', QTP.Poster.instantiatePosters);


/*

File: qtp_movie.js

Version: <2.0>

Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
Computer, Inc. ("Apple") in consideration of your agreement to the
following terms, and your use, installation, modification or
redistribution of this Apple software constitutes acceptance of these
terms.  If you do not agree with these terms, please do not use,
install, modify or redistribute this Apple software.

In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software. 
Neither the name, trademarks, service marks or logos of Apple Computer,
Inc. may be used to endorse or promote products derived from the Apple
Software without specific prior written permission from Apple.  Except
as expressly stated in this notice, no other rights or licenses, express
or implied, are granted by Apple herein, including but not limited to
any patent rights that may be infringed by your derivative works or by
other works in which the Apple Software may be incorporated.

The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.

IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

Copyright © 2006-2007 Apple Computer, Inc., All Rights Reserved

*/ 

if (typeof(QTP) == "undefined") { 
	/** QTP is a namespace for QuickTime Player javascript objects. */
	QTP = {}; 
}

/**
 * @fileoverview This file contains functions to generate a JavaScript-based QuickTime movie 
 * object, compatable with both Desktop and iPhone platforms. 
 *
 * @requires Prototype.Browser (Prototype.js or Mini_prototype.js)
 * @requires Event.observe (Prototype.js or Mini_prototype.js)
 */

/** 
 * Movie is an class which abstracts the creation of embed/object/video tags.
 * The correct tag will be created depending on the user's browser.
 * @param {Object} hash a key-value dictionary of attributes
 * @constructor
 * @example
 * var movie = new Movie({src: 'myMovie.mov', autoplay: 'false', scale: 'aspect'});
 * document.body.appendChild(movie.element);
 */
QTP.Movie = function(hash)
{
	
	// Create a <video> tag for builds of Safari which support it:
	if( QTP.Movie.useVideoTag && QTP.Movie.isVideoTagSupported() )
	{
		this.element = document.createElement( 'video' );
		this.addParameters(hash);
	}
	else
	{
		this.element = document.createElement( 'object' );
	
		if( Prototype.Browser.IE )
		{
			this.element.setAttribute('codebase', 'http://www.apple.com/qtactivex/qtplugin.cab#version=7,1,6,0');
		}
		else
		{
			this.element.setAttribute('type', 'video/quicktime');
		}
		
		this.addParameters(hash);
		
		if( Prototype.Browser.IE )
		{
			this.element.setAttribute('classid', 'clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B');
		}
	}
};

/**
 * Determines whether to use the &lt;video&gt; tag where supported.  Defaults to true.
 * @type boolean
 */
QTP.Movie.useVideoTag = true;
	
/**
 * The DOM element created by Movie, either an embed, object, or video tag
 * depending on the user's browser.
 * @type DOMElement
 */
QTP.Movie.prototype.element = null;

/**
 *
 */
QTP.Movie.isVideoTagSupported = function()
{
	try {
		if (typeof(window.HTMLVideoElement) == 'undefined')
			return false;
			
		var video = document.createElement('video');
		return (video.constructor == window.HTMLVideoElement)
	} catch(err) {
		return false;
	}	
}
	
/**
 * Adds the specified attribute key and value to the DOM element. Keys of the type 
 * "obj#xxx" and "emb#xxx" are supported and will be added only if the DOM element 
 * type is appropriate.  The parameter will be of the form of a tag attribute, or a child
 * param object, depending on the DOM element type and the parameter key.
 * @param {String} key The attribute name
 * @param {String} value The attribute value
 */
QTP.Movie.prototype.addParameter = function(key, value)
{
	if( this.element.tagName.toLowerCase() == 'embed' )
	{
		if(key.toLowerCase().indexOf('obj#') == 0)
			return; // Do not add obj-specific tags to embeds
		this.element.setAttribute(key, value);
	}
	else if( this.element.tagName.toLowerCase() == 'object' )
	{
		if(key.toLowerCase().indexOf('emb#') == 0)
			return; // Do not add emb-specific tags to objects
		if(key.toLowerCase() == 'width' || key.toLowerCase() == 'height' || key.toLowerCase() == 'id' || key.toLowerCase() == 'data')
			this.element.setAttribute(key, value);
		else if(key.toLowerCase() == 'src' && Prototype.Browser.Gecko)
		{
			this.element.setAttribute('data', value);
		}
		else
		{
			var param = document.createElement('param');
			param.setAttribute('name', key);
			param.setAttribute('value', value);
			this.element.appendChild(param);
		}
	}
	else if( this.element.tagName.toLowerCase() == 'video' )
	{
		if(key.toLowerCase().indexOf('emb#') == 0 || key.toLowerCase().indexOf('obj#') == 0)
			return; // Do not add emb- or obj-specific tags to objects
			
		// convert from embed-style keys to video tag keys
		switch(key.toLowerCase()) {
			case 'controller':
				key = 'controls';
			case 'autoplay':
				if(value.toLowerCase() == 'false')
					return; // for the <video> tag, the presence of a boolean attribute == 'true'
				break;
		}

		this.element.setAttribute(key, value);					
	}
};

/**
 * Removes the specified attribute from the DOM element, whether the attribute is
 * a tag attribute, or a child param tag.
 * @param {String} key The attribute name
 */
QTP.Movie.prototype.removeParameter = function(key)
{
	if( this.element.hasAttribute(key) )
		this.element.removeAttribute(key);
	else
	{
		for(i in childNodes)
		{
			if(childNodes[i].getAttribute('name') == key)
			{
				this.element.removeChild(childNodes[i]);
			}
		}
	}
};

/** 
 * Replaces an existing parameter value with a new one.  The new value is set
 * whether or not the previous value exists.
 * @param {String} key The attribute name
 * @param {String} value The attribute value
 */
QTP.Movie.prototype.replaceParameter = function(key, value)
{
	this.removeParameter(key);
	this.addParameter(key, value);
};

/**
 * Adds the specified attribute key and value to the DOM element.
 * @param {Object} pair A key-value pair
 * @config {String} key The attribute name
 * @config {String} value The attribute value
 */
QTP.Movie.prototype.addParameterPair = function(pair)
{
	return this.addParameter(pair.key, pair.value);
};
	
/**
 * Adds the specified attributes to the DOM element.
 * @param {Array} pair An array of key-value pairs
 * @config {String} key The attribute name
 * @config {String} value The attribute value
 */
QTP.Movie.prototype.addParameters = function(hash)
{
	for(var key in hash )
	{
		this.addParameter(key, hash[key]);
	}
};

/**
 * Determines whether the DOM element has enabled DOM media events.
 * @returns {boolean} true, if elements have been enabled.
 */
QTP.Movie.prototype.areEventsEnabled = function()
{
	switch(this.element.tagName.toLowerCase())
	{
	case 'video':
	case 'embed':
		return this.element.getAttribute('postdomevents').toLowerCase() == 'true';
	case 'object':
		var params = this.element.getElementsByTagName('param');
		for(var index in params)
			if(params[index].name == 'postdomevents' && params[index].value == 'true')
				return true;
	}
	return false;
}

/**
 * Determines whether the DOM element supports HTML5-style media events.
 * @returns {boolean} true, if elements are supported. false, if elements are not supported.
 * undefined, if the plugin is not sufficiently loaded to make a determination.
 */
QTP.Movie.prototype.areEventsSupported = function()
{
	if(typeof(this._eventsSupported) != 'undefined')
		return this._eventsSupported;
		
	if(QTP.isMobileSafari() && this.areEventsEnabled())
	{
		this._eventsSupported = true;
		return this._eventsSupported;
	}
	
	$A(navigator.plugins).each(function(plugin) {
		if(plugin.name.indexOf('QuickTime Plug-In ') != 0)
			return;
		var version = plugin.name.slice(18).split('.');
		if(version.length < 3)
			version[2] = 0;
		if(!(version[0] < 7 || version[1] < 2 || version[2] < 1))
			this._evenstSupported = true;
	}.bind(this));
	
	return this._eventsSupported;
};

/**
 * Detects if the browser is an iPhone/iPod Touch
 * @returns bool <code>true</code> if the browser user agent is Mobile Safari, <code>false</code> otherwise.
 */
QTP.isMobileSafari = function()
{
	var ua = navigator.userAgent;
	return RegExp(" AppleWebKit/").test(ua) && RegExp(" Mobile/").test(ua)
};



/*

File: qtp_ui.js

Version: <2.0>

Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
Computer, Inc. ("Apple") in consideration of your agreement to the
following terms, and your use, installation, modification or
redistribution of this Apple software constitutes acceptance of these
terms.  If you do not agree with these terms, please do not use,
install, modify or redistribute this Apple software.

In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software. 
Neither the name, trademarks, service marks or logos of Apple Computer,
Inc. may be used to endorse or promote products derived from the Apple
Software without specific prior written permission from Apple.  Except
as expressly stated in this notice, no other rights or licenses, express
or implied, are granted by Apple herein, including but not limited to
any patent rights that may be infringed by your derivative works or by
other works in which the Apple Software may be incorporated.

The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.

IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

Copyright © 2006-2007 Apple Computer, Inc., All Rights Reserved

*/ 

if (typeof(QTP) == "undefined") { QTP = {}; }

/**
 * @fileoverview
 * This file contains functions to generate simple, stylable, DOM-based buttons and sliders.
 */

/** 
 * Button is an class which creates a DOM button element, and provides listener methods
 * for the element's actions.
 * @param {String} name the className of the DOM element
 * @constructor
 */
QTP.Button = function(name)
{
	this.name = name;
		
	this.element = document.createElement('button');
	this.element.className = this.name;
			
	Event.observe(this.element, "click", this.onClick.bindAsEventListener(this));
	Event.observe(this.element, "mousedown", this.onDown.bindAsEventListener(this));
	Event.observe(this.element, "mouseup", this.onUp.bindAsEventListener(this));
	Event.observe(this.element, "mouseover", this.onOver.bindAsEventListener(this));
	Event.observe(this.element, "mouseout", this.onOut.bindAsEventListener(this));
};
	

/**
 * Cache of the button's className
 * @type String
 */
QTP.Button.prototype.name = '';
/**
 * Cache of the button's enabled state
 * @type String
 */
QTP.Button.prototype.enabled = true;
/**
 * Button's mouseup handler.  Set this to a function, and it will be called when 
 * the button receives a mouseup event.
 * @type Function
 */
QTP.Button.prototype.upAction = null;
/**
 * Button's mousedown handler.  Set this to a function, and it will be called when 
 * the button receives a mousedown event.
 * @type Function
 */
QTP.Button.prototype.downAction = null;
/**
 * Button's click handler.  Set this to a function, and it will be called when 
 * the button receives a click event.
 * @type Function
 */
QTP.Button.prototype.clickAction = null;
/**
 * Button's mouseover handler.  Set this to a function, and it will be called when 
 * the button receives a mouseover event.
 * @type Function
 */
QTP.Button.prototype.overAction = null;
/**
 * Button's mouseout handler.  Set this to a function, and it will be called when 
 * the button receives a mouseout event.
 * @type Function
 */
QTP.Button.prototype.outAction = null;
/**
 * DOM button element
 * @type DOMElement
 * @memberOf Button
 */
QTP.Button.prototype.element = null;
	
/**
 * Internal message handler.
 * @private
 */
QTP.Button.prototype.onClick = function(event)
{
	if((this.clickAction != null) && this.enabled)
		return this.clickAction.call(Event.element(event), event);
	return true;
};

/**
 * Internal message handler.
 * @private
 */
QTP.Button.prototype.onDown = function(event)
{
	if(this.downAction && this.enabled)
		return this.downAction.call(Event.element(event), event);
	return true;
};

/**
 * Internal message handler.
 * @private
 */
QTP.Button.prototype.onUp = function(event)
{
	if(this.upAction && this.enabled)
		return this.upAction.call(Event.element(event), event);
	return true;
};

/**
 * Internal message handler.
 * @private
 */
QTP.Button.prototype.onOver = function(event)
{
	if(this.overAction && this.enabled)
		return this.overAction.call(Event.element(event), event);
	return true;
};

/**
 * Internal message handler.
 */
QTP.Button.prototype.onOut = function(event)
{
	if(this.outAction && this.enabled)
		return this.outAction.call(Event.element(event), event);
	return true;
};

/**
 * Enables or disables the button. This will set the DOM element's <code>disabled</code>
 * property to <code>true</code>.
 */
QTP.Button.prototype.setEnabled = function(enabled)
{
	if(enabled === this.enabled)
		return;
	this.element.disabled = !enabled;
	this.enabled = enabled;
};

/** 
 * Sets the DOM button element's className.
 */
QTP.Button.prototype.setName = function(name)
{
	if(name === this.name)
		return;
	this.element.className = this.element.className.replace(this.name, name);
	this.name = name;
};

/** 
 * Slider is an class which creates a DOM-based slider element, and provides listener methods
 * for the element's actions.
 * @param {String} name the className of the DOM element
 * @constructor
 */
QTP.Slider = function(name)
{
	this.name = name;
	this.knob = new QTP.Button('Knob');

	this.left = document.createElement('div');
	this.left.className = 'Left';
	
	this.right = document.createElement('div');
	this.right.className = 'Right';
	
	this.element = document.createElement('div');
	this.element.className = this.name;
	
	this.element.appendChild(this.left);
	this.element.appendChild(this.knob.element);
	this.element.appendChild(this.right);
	
	Event.observe(this.element, "click", this.onClick.bindAsEventListener(this));
	Event.observe(this.element, "mousedown", this.onDown.bindAsEventListener(this));
	Event.observe(this.element, "mouseup", this.onUp.bindAsEventListener(this));
	Event.observe(this.element, "mouseover", this.onOver.bindAsEventListener(this));
	Event.observe(this.element, "mouseout", this.onOut.bindAsEventListener(this));
	
	this.knob.downAction = this.beginDrag.bindAsEventListener(this);
	this.knob.upAction = this.endDrag.bindAsEventListener(this);
};
	
/**
 * Cache of the slider's className
 * @type String
 */
QTP.Slider.prototype.name = '';
/**
 * The DOM button element which represents the slider as a whole.
 * @type DOMElement
 */
QTP.Slider.prototype.element = null;
/**
 * The DOM button element which represents the slider knob.  The element
 * has an initial class name of 'Knob'.
 * @type DOMElement
 */
QTP.Slider.prototype.knob = null;
/**
 * The DOM element which represents the track to the left of the knob.  The element
 * has an initial class name of 'Left'.
 * @type DOMElement
 */
QTP.Slider.prototype.left = null;
/**
 * The DOM element which represents the track to the right of the knob.  The element
 * has an initial class name of 'Right'.
 * @type DOMElement
 */
QTP.Slider.prototype.right = null;
/**
 * Cache of the enabled state of the slider.
 * @type boolean
 */
QTP.Slider.prototype.enabled = true;
/**
 * Slider's mouseup handler.  Set this to a function, and it will be called when 
 * the slider receives a mouseup event.
 * @type Function
 */
QTP.Slider.prototype.upAction = null;
/**
 * Slider's mousedown handler.  Set this to a function, and it will be called when 
 * the slider receives a mousedown event.
 * @type Function
 */
QTP.Slider.prototype.downAction = null;
/**
 * Slider's click handler.  Set this to a function, and it will be called when 
 * the slider receives a click event.
 * @type Function
 */
QTP.Slider.prototype.clickAction = null;
/**
 * Slider's mouseover handler.  Set this to a function, and it will be called when 
 * the slider receives a mouseover event.
 * @type Function
 */
QTP.Slider.prototype.overAction = null;
/**
 * Slider's mouseout handler.  Set this to a function, and it will be called when 
 * the slider receives a mouseout event.
 * @type Function
 */
QTP.Slider.prototype.outAction = null;
/**
 * Slider's changeAction handler.  Set this to a function, and it will be called when 
 * the slider's position changes.
 * @type Function
 */
QTP.Slider.prototype.changeAction = null;
/**
 * Slider's beginDrag handler.  Set this to a function, and it will be called when 
 * the user begins to drag the slider's knob.
 * @type Function
 */
QTP.Slider.prototype.beginDragAction = null;
/**
 * Slider's endDrag handler.  Set this to a function, and it will be called when 
 * the user stops dragging the slider's knob.
 * @type Function
 */
QTP.Slider.prototype.endDragAction = null;
/**
 * Cache of the position value of the slider.
 * @type float
 */
QTP.Slider.prototype.position = 0.0;
/**
 * Cache of the mouse position when the slider began dragging
 * @type Array
 */
QTP.Slider.prototype.dragStart = null;
/**
 * Cache of the knob position when the slider began dragging
 * @type Array
 */
QTP.Slider.prototype.knobStart = null;
/**
 * Sets whether the slider goes from top-to-bottom or from left-to-right.
 * @type boolean
 */
QTP.Slider.prototype.horizontal = true;
	
/**
 * Internal message handler.
 */
QTP.Slider.prototype.beginDrag = function(event)
{
	// Install document-level event listeners
	this.continueDragListener = this.continueDrag.bindAsEventListener(this);
	this.endDragListener = this.endDrag.bindAsEventListener(this);
	Event.observe(document, "mousemove", this.continueDragListener );
	Event.observe(document, "mouseup", this.endDragListener );
	
	this.dragStart = new Array(event.clientX, event.clientY);
	this.knobStart = [this.knob.element.offsetLeft, this.knob.element.offsetTop];
	if((this.beginDragAction != null) && this.enabled)
		return this.beginDragAction.call(Event.element(event), event);
	
	return true;
};

/**
 * Internal message handler.
 * @private
 */
QTP.Slider.prototype.continueDrag = function(event)
{
	var delta = [event.clientX - this.dragStart[0], event.clientY - this.dragStart[1]];
	var availSize = [this.element.clientWidth - this.knob.element.clientWidth, 
		this.element.clientHeight - this.knob.element.clientHeight];
	var newPos = [this.knobStart[0] + delta[0], this.knobStart[1] + delta[1]];

	newPos[0] = Math.max(0, Math.min(availSize[0], newPos[0]));
	newPos[1] = Math.max(0, Math.min(availSize[1], newPos[1]));
		
	if(newPos[0] != this.knob.element.offsetLeft || newPos[1] != this.knob.element.offsetTop)
	{
		this.setPosition(this.horizontal ? newPos[0] / availSize[0] : newPos[1] / availSize[1]);
		this.onChange(event);
	}		
};

/**
 * Internal message handler.
 * @private
 */
QTP.Slider.prototype.endDrag = function(event)
{			
	Event.stopObserving(document, "mousemove",this.continueDragListener);
	Event.stopObserving(document, "mouseup", this.endDragListener, true);		

	if((this.endDragAction != null) && this.enabled)
		return this.endDragAction.call(Event.element(event), event);
	return true;
};

/**
 * Internal message handler.
 * @private
 */
QTP.Slider.prototype.onClick = function(event)
{
	var parPos = Element.cumulativeOffset(this.element);
	var newPos = [event.clientX - parPos[0] - this.element.clientLeft - this.knob.element.clientWidth/2, event.clientY - parPos[1] - this.element.clientTop - this.knob.element.clientHeight/2];

	var availSize = [this.element.clientWidth - this.knob.element.clientWidth, 
		this.element.clientHeight - this.knob.element.clientHeight];
		
	newPos[0] = Math.max(0, Math.min(availSize[0], newPos[0]));
	newPos[1] = Math.max(0, Math.min(availSize[1], newPos[1]));

	if(newPos[0] != this.knob.element.offsetLeft || newPos[1] != this.knob.element.offsetTop)
	{
		this.setPosition(this.horizontal ? newPos[0] / availSize[0] : newPos[1] / availSize[1]);
		this.onChange(event);
	}		

	if((this.clickAction != null) && this.enabled)
		return this.clickAction.call(Event.element(event), event);
	return true;
};

/**
 * Internal message handler.
 * @private
 */
QTP.Slider.prototype.onDown = function(event)
{
	if(this.downAction && this.enabled)
		return this.downAction.call(Event.element(event), event);
	return true;
};

/**
 * Internal message handler.
 * @private
 */
QTP.Slider.prototype.onUp = function(event)
{
	if(this.upAction && this.enabled)
		return this.upAction.call(Event.element(event), event);
	return true;
};

/**
 * Internal message handler.
 * @private
 */
QTP.Slider.prototype.onOver = function(event)
{
	if(this.overAction && this.enabled)
		return this.overAction.call(Event.element(event), event);
	return true;
};

/**
 * Internal message handler.
 * @private
 */
QTP.Slider.prototype.onOut = function(event)
{
	if(this.outAction && this.enabled)
		return this.outAction.call(Event.element(event), event);
	return true;
};
	
/**
 * Internal message handler.
 * @private
 */
QTP.Slider.prototype.onChange = function(event)
{
	if(this.changeAction && this.enabled)
		return this.changeAction.call(Event.element(event), this.position);
	return true;
};

/**
 * Enables or disables the slider.  This will set the DOM element's <code>disabled</code>
 * property to <code>true</code>.
 */
QTP.Slider.prototype.setEnabled = function(enabled)
{
	if(enabled === this.enabled)
		return;
	this.element.disabled = !enabled;
	this.enabled = enabled;
};

/** 
 * Sets the DOM element's className.
 */
QTP.Slider.prototype.setName = function(name)
{
	if(name === this.name)
		return;
	this.element.className = this.element.className.replace(this.name, name);
	this.name = name;
};

/**
 * Sets the slider position.
 * @param {float} position the position, from [0.0 .. 1.0].
 */
QTP.Slider.prototype.setPosition = function(position)
{
	if(isNaN(position))
		position = 0;
		
	position = Math.max(0, Math.min(1, position));

	if(this.position === position)
		return;
		
	this.position = position;
		
	var availSize = [this.element.clientWidth - this.knob.element.clientWidth, 
		this.element.clientHeight - this.knob.element.clientHeight];
	var knobPos = [position * availSize[0], position * availSize[1]];
	var rightPos = [position * availSize[0], position * availSize[1]];
	var leftPos = [(1-position) * availSize[0], (1-position) * availSize[1]];
	
	if(this.horizontal)
	{
		if(knobPos[0] != this.knob.element.offsetLeft)
			this.knob.element.style.left = knobPos[0] + 'px';
		if(rightPos[0] != this.right.offsetLeft)
			this.right.style.left = rightPos[0] + 'px';	
		if(leftPos[0] != this.left.offsetRight)
			this.left.style.right = leftPos[0] + 'px';
	}
	else
	{
		if(knobPos[1] != this.knob.element.offsetTop)
			this.knob.element.style.top = knobPos[1] + 'px';
		if(rightPos[1] != this.right.offsetTop)
			this.right.style.top = rightPos[1] + 'px';
		if(leftPos[1] != this.right.offsetBottom)
			this.left.style.bottom = leftPos[1] + 'px';
	}
	
};


/*

File: qtp_controller.js

Version: <2.0>

Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
Computer, Inc. ("Apple") in consideration of your agreement to the
following terms, and your use, installation, modification or
redistribution of this Apple software constitutes acceptance of these
terms.  If you do not agree with these terms, please do not use,
install, modify or redistribute this Apple software.

In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software. 
Neither the name, trademarks, service marks or logos of Apple Computer,
Inc. may be used to endorse or promote products derived from the Apple
Software without specific prior written permission from Apple.  Except
as expressly stated in this notice, no other rights or licenses, express
or implied, are granted by Apple herein, including but not limited to
any patent rights that may be infringed by your derivative works or by
other works in which the Apple Software may be incorporated.

The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.

IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

Copyright © 2006-2007 Apple Computer, Inc., All Rights Reserved

*/ 

if (typeof(QTP) == "undefined") { 
	/** QTP is a namespace for QuickTime Player javascript objects. */
	QTP = {}; 
}

/**
 * An embed-style API adapter for video tags.
 * @class QTP.VideoEmbedAdapter
 */
QTP.VideoEmbedAdapter =
{
	// QuickTime Embed interface
	Play: function() { this.play(); },
	Stop: function() { this.pause(); },
	GetTime: function() { return this.currentTime; },
	SetTime: function(t) { this.currentTime = t; },
	GetMute: function() { return this.muted; },
	SetMute: function(m) { this.muted = m; },
	GetTimeScale: function() { return 1.0; },
	GetRate: function() { return this.paused || this.ended ? 0.0 : this.playbackRate ; },
	SetRate: function(r) { this.playbackRate = r; },
	GetEndTime: function() { return this.duration; },
	GetStartTime: function() { return 0.0; },
	GetMaxTimeLoaded: function() { return this.buffered.length > 0 ? this.buffered.end(this.buffered.length-1) : 0.0; },
	GetVolume: function() { return this.volume * 256; },
	SetVolume: function(v) { this.volume = v / 256; },
	GetControllerVisible: function() { return this.controls; },
	SetControllerVisible: function(v) { this.controls = v; },
	SetURL: function(u) { this.src = u; this.load(); },
	GetURL: function(u) { return this.src; }
};

/**
 * An embed-style API adapter for QTP.Poster objects.
 * @class QTP.PosterAdapter
 */
QTP.PosterAdapter =
{
	// QuickTime Embed interface
	Play: function() { this.movie ? this.movie.element.Play() : this.click(); },
	Stop: function() { this.movie ? this.movie.element.Stop() : this.click(); },
	GetTime: function() { return this.movie ? this.movie.element.GetTime() : 0.0; },
	SetTime: function(t) { if(this.movie) this.movie.element.SetTime(t); },
	GetMute: function() { return this.movie ? this.movie.element.GetMute() : true; },
	SetMute: function(m) { if(this.movie) this.movie.element.SetMute(m); },
	GetTimeScale: function() { return this.movie ? this.movie.element.GetTimeScale() : 1.0; },
	GetRate: function() { return this.movie ? this.movie.element.GetRate() : 0.0; },
	SetRate: function(r) { if(this.movie) this.movie.element.SetRate(r); },
	GetEndTime: function() { return this.movie ? this.movie.element.GetEndTime() : 1.0; },
	GetStartTime: function() { return this.movie ? this.movie.element.GetStartTime() : 0.0; },
	GetMaxTimeLoaded: function() { return this.movie ? this.movie.element.GetMaxTimeLoaded() : 1.0; },
	GetVolume: function() { return this.movie ? this.movie.element.GetVolume() : 0.0; },
	SetVolume: function(v) { if(this.movie) this.movie.element.SetVolume(v); }
};

/**
 * A DOM-based controller for media elements.
 * Works with QTP.Movie objects, video tags, and object/embed tags.
 * @class QTP.MovieController
 * @param {String} name The className to give the top DOM element.
 * @constructor
 */
QTP.MovieController = function(name) 
{
	this.name = name;
	this.play = new QTP.Button('Play');
	this.rewind = new QTP.Button('Rewind');
	this.forward = new QTP.Button('Forward');
	this.beginning = new QTP.Button('Beginning');
	this.end = new QTP.Button('End');
	this.timeline = new QTP.Slider('Timeline');
	this.playHead = this.timeline.knob;
	this.loaded = document.createElement('div');
	this.loaded.className = 'Loaded';
	this.volume = new QTP.Slider('Volume');
	this.mute = new QTP.Button('Unmute');
	this.time = document.createElement('span');
	this.time.className = 'Time';
	this.element = document.createElement('div');
	this.element.className = name;
	this.buttonHolder = document.createElement('div');
	
	this.timeline.element.insertBefore(this.loaded, this.timeline.element.firstChild);
	
	this.buttonHolder.className = 'Buttons';
	this.buttonHolder.appendChild(this.beginning.element);
	this.buttonHolder.appendChild(this.rewind.element);
	this.buttonHolder.appendChild(this.play.element);
	this.buttonHolder.appendChild(this.forward.element);
	this.buttonHolder.appendChild(this.end.element);
	this.element.appendChild(this.buttonHolder);
	this.element.appendChild(this.time);
	this.element.appendChild(this.timeline.element);
	this.element.appendChild(this.mute.element);
	this.element.appendChild(this.volume.element);
	
	this.play.clickAction = this.playButtonClicked.bindAsEventListener(this);
	this.forward.downAction = this.forwardButtonDown.bindAsEventListener(this);
	this.forward.upAction = this.forwardButtonUp.bindAsEventListener(this);
	this.forward.outAction = this.forwardButtonUp.bindAsEventListener(this);
	this.rewind.downAction = this.rewindButtonDown.bindAsEventListener(this);
	this.rewind.upAction = this.rewindButtonUp.bindAsEventListener(this);
	this.rewind.outAction = this.rewindButtonUp.bindAsEventListener(this);
	this.end.clickAction = this.endButtonClicked.bindAsEventListener(this);
	this.beginning.clickAction = this.beginningButtonClicked.bindAsEventListener(this);
	this.timeline.changeAction = this.timelineChanged.bindAsEventListener(this);
	this.timeline.beginDragAction = this.timelineDragStarted.bindAsEventListener(this);
	this.timeline.endDragAction = this.timelineDragEnded.bindAsEventListener(this);
	this.volume.changeAction = this.volumeChanged.bindAsEventListener(this);
	this.mute.clickAction = this.muteClicked.bindAsEventListener(this);

	setInterval(this.update.bind(this), this.pausedUpdateDelay);
};

/**
 * Cache of the DOM element's className
 * @type String
 */
QTP.MovieController.prototype.name = null;
/**
 * Cache of the class's movie object
 * @type QTP.Movie, video, or object/embed tag
 */
QTP.MovieController.prototype.movie = null;
/**
 * The top DOM element
 * @type DOMElement
 */
QTP.MovieController.prototype.element = null;
/**
 * The play button object.
 * @type QTP.Button
 */
QTP.MovieController.prototype.play = null;
/**
 * The rewind button object.
 * @type QTP.Button
 */
QTP.MovieController.prototype.rewind = null;
/**
 * The forward button object.
 * @type QTP.Button
 */
QTP.MovieController.prototype.forward = null;
/**
 * The beginning button object.
 * @type QTP.Button
 */
QTP.MovieController.prototype.beginning = null;
/**
 * The end button object.
 * @type QTP.Button
 */
QTP.MovieController.prototype.end = null;
/**
 * The timeline slider object.
 * @type QTP.Slider
 */
QTP.MovieController.prototype.timeline = null;
/**
 * The timeline slider knob DOM element.
 * @type DOMElement
 */
QTP.MovieController.prototype.playHead = null;
/**
 * The "maximum time loaded" indicator DOM element
 * @type DOMElement
 */
QTP.MovieController.prototype.loaded = null;
/** 
 * The volume slider.
 * @type QTP.Slider
 */
QTP.MovieController.prototype.volume = null;
/** 
 * The "current time" DOM element.
 * @type DOMElement
 */
QTP.MovieController.prototype.time = null;
/**
 * Cache of the previous rate value.
 * @type float
 */
QTP.MovieController.prototype.oldRate = 0.0;
/**
 * Number of milliseconds between updates while playing.
 * @type int
 */
QTP.MovieController.prototype.playingUpdateDelay = 50; //milliseconds
/**
 * Number of milliseconds between updates while paused.
 * @type int
 */
QTP.MovieController.prototype.pausedUpdateDelay = 250; //milliseconds
/**
 * Cache of movie properties.
 * @type Object
 */
QTP.MovieController.prototype.cache = {
	rate: 0.0,
	time: 0.0,
	scale: 1.0,
	start: 0.0,
	end: 1.0,
	max: 1.0,
	volume: 0.0,
	muted: false
};
/**
 * Array of cue points, each in the form of: <code>{time:<i>{int}</i>, listener:<i>{Function}</i>}</code>
 * @type Array
 */
QTP.MovieController.prototype.cuePoints = [];
/**
 * Array of cue ranges, each in the form of: 
 * <code>{start:<i>{int}</i>, end:<i>{int}</i>, enter:<i>{Function}</i>, exit:<i>{Function}</i}</code>
 * @type Array
 */
QTP.MovieController.prototype.cueRanges = [];
/**
 * Array of cue ranges, each in the form of: 
 * <code>{start:<i>{int}</i>, end:<i>{int}</i>, enter:<i>{Function}</i>, exit:<i>{Function}</i}</code>
 * @type Array
 */
QTP.MovieController.prototype.inRanges = [];

/**
 * Sets the top DOM element's className.
 * @param {String} name The new className value.
 */
QTP.MovieController.prototype.setName = function(name)
{
	if(name === this.name)
		return;
	this.element.className = this.element.className.replace(this.name, name);
	this.name = name;
	this.update();
};

/**
 * Sets the current movie object.  Depending on the type of movie object passed in, the object
 * may be extended with a QTP.VideoEmbedAdapter, or otherwise converted.
 * @param {QTP.Movie, video, or object/embed tag} movie The new movie object
 */
QTP.MovieController.prototype.setMovie = function(movie)
{
	this.movie = movie;
	if(movie instanceof QTP.Movie)
	{
		this.setMovie(movie.element);
		return;
	}
	else if(movie instanceof QTP.Poster)
	{
		Object.extend(movie, QTP.PosterAdapter);
	}
	else if(movie.tagName.toLowerCase() == 'video')
	{
		Object.extend(movie, QTP.VideoEmbedAdapter);
	}
	else
	{
	}
	this.update();
};

/**
 * Updates the current cached movie properties and relevent UI elements.
 */
QTP.MovieController.prototype.update = function()
{
	this.updateCache();
	this.updatePlayHead();
	this.updateLoaded();
	this.updatePlay();
	this.updateVolume();
	this.updateTime();
};

/**
 * Updates the current cached movie properties, then executes the cue points and ranges.
 */
QTP.MovieController.prototype.updateCache = function()
{
	try {
		this.cache.rate = this.movie.GetRate();
		this.cache.muted = this.movie.GetMute();
		this.cache.time = this.movie.GetTime();
		this.cache.start = this.movie.GetStartTime();
		this.cache.scale = this.movie.GetTimeScale();
		this.cache.end = this.movie.GetEndTime();
		this.cache.max = this.movie.GetMaxTimeLoaded();
		this.cache.volume = this.movie.GetVolume();
	}
	catch(err)
	{
	}
	
	// Run the cue points:
	while(this.cuePoints.length > 0 && this.cuePoints[0].time < this.cache.time)
	{
		try {
			this.cuePoints.shift().listener();
		}
		catch(err) {}
	} 
			
	// Run the cue ranges:
	for(var i = this.cueRanges.length - 1; i >= 0; --i)
	{
		if(this.cueRanges[i].start < this.cache.time && this.cueRanges[i].end > this.cache.time)
		{
			var range = this.cueRanges.splice(i, 1)[0];
			this.inRanges.push(range);
			try { range.enter(); } catch (e) {}
		}
	}

	// Run the in ranges:
	for(var i = this.inRanges.length - 1; i >= 0; --i)
	{
		if(this.inRanges[i].start > this.cache.time || this.inRanges[i].end < this.cache.time)
		{
			var range = this.inRanges.splice(i, 1)[0];
			this.cueRanges.push(range);
			try { range.exit(); } catch (e) {}
		}
	}
};

/** 
 * Periodic update timer.  Calls QTP.MovieController.prototype.update().
 */
QTP.MovieController.prototype.movieTimer = function()
{
	setTimeout(this.movieTimer.bind(this), this.pausedUpdateDelay);

	this.update();		
};

/**
 * Updates the play head to reflect the current cached time.
 */
QTP.MovieController.prototype.updatePlayHead = function()
{
	if(!this.playHead || !this.movie)
		return;
		
	var position = (this.cache.time - this.cache.start)/(this.cache.end - this.cache.start);
	if(isNaN(position))
		position = 0;
		
	this.timeline.setPosition(position);
};

/**
 * Updates the loaded bar to reflect the current max playable time.
 */
QTP.MovieController.prototype.updateLoaded = function()
{
	if(!this.loaded || !this.movie)
		return;
					
	var percent = (this.cache.max - this.cache.start) / (this.cache.end - this.cache.start);
	if (isNaN(percent))
		percent = 0.0;
		
	var right = (1 - percent) * this.timeline.element.clientWidth + 'px';
		
	if (right !== this.loaded.style.right)
	{
		this.loaded.style.right = right;
	}
};

/**
 * Updates the play button to reflect the current play state.
 */
QTP.MovieController.prototype.updatePlay = function()
{
	if(!this.play || !this.movie)
		return;

	if(this.cache.rate == 0.0 && this.play.element.className !== 'Play')
		this.play.setName('Play');
	else if(this.cache.rate != 0.0 && this.play.element.className !== 'Pause')
		this.play.setName('Pause');
};

/**
 * Updates the volume slider to reflect the curretn movie volume.
 */
QTP.MovieController.prototype.updateVolume = function()
{
	if(!this.movie)
		return;
		
	var volume = this.cache.muted ? 0 : this.cache.volume;

	if(this.volume)	
		this.volume.setPosition( volume / 256 );

	if(!this.mute)
		return;

	if(this.cache.muted && this.mute.element.className !== 'Mute')
		this.mute.setName('Mute');
	else if(!this.cache.muted && this.mute.element.className !== 'Unmute')
		this.mute.setName('Unmute');
};

/** 
 * Updates the time element to reflect the current movie time.
 */
QTP.MovieController.prototype.updateTime = function()
{
	if(!this.time || !this.movie)
		return;
		
	var time = this.movie ? this.cache.time / this.cache.scale : 0;

	var seconds = Math.floor( time % 60	);
	var minutes = Math.floor( (time / 60) % 60 );
	var hours   = Math.floor( time / (60*60) );

	if(seconds < 10) seconds = '0' + seconds;
	if(minutes < 10) minutes = '0' + minutes;
	if(hours < 10) hours = '0' + hours;

	var text = hours + ":" + minutes + ":" + seconds;
	if(this.time.textContent != undefined)
		this.time.textContent = text
	else
		this.time.innerText = text;
};
	
/**
 * Handler for the play button's click event.  Starts or stops the movie depending on the cached
 * movie rate.
 */
QTP.MovieController.prototype.playButtonClicked = function(event)
{
	if(!this.movie)
		return true;
	if(this.cache.rate>0)
	{
		this.play.setName('Play');
		this.movie.Stop();
	}
	else
	{
		this.play.setName('Pause');
		this.movie.Play();
	}
	return false;
};

/**
 * Handler for the forward button's mousedown event.  Sets the movie playback rate to 2x.
 */
QTP.MovieController.prototype.forwardButtonDown = function(event)
{
	if(!this.movie)
		return true;
	
	this.forward.down = true;
		
	this.oldRate = this.cache.rate;
	this.movie.SetRate(2.0);
	return false;
};

/**
 * Handler for the forward button's mouseup event.  Sets the movie playback rate back to its previous 
 * value.
 */
QTP.MovieController.prototype.forwardButtonUp = function(event)
{
	if(!this.movie || this.cache.rate <= 1.0)
		return true;
	
	if(this.forward.down)
	{
		this.movie.SetRate(this.oldRate);
		this.forward.down = false;
	}
	return false;
};

/**
 * Handler for the rewind button's mousedown event.  Sets the movie playback rate to -2x.
 */
QTP.MovieController.prototype.rewindButtonDown = function(event)
{
	if(!this.movie)
		return true;
		
	this.rewind.down = true;			
	this.oldRate = this.cache.rate;
	this.movie.SetRate(-2.0);
	return false;
};

/**
 * Handler for the rewind button's mouseup event.  Sets the movie playback rate back to its previous 
 * value.
 */
QTP.MovieController.prototype.rewindButtonUp = function(event)
{
	if(!this.movie || this.cache.rate >= 1.0)
		return true;

	if(this.rewind.down)
	{			
		this.movie.SetRate(this.oldRate);
		this.rewind.down = false;
	}
	return false;
};

/**
 * Handler for the end button's click event.  Sets the current movie time to the end of the movie.
 */
QTP.MovieController.prototype.endButtonClicked = function(event)
{
	if(!this.movie)
		return true;
			
	this.movie.SetTime(this.cache.end);	

};

/**
 * Handler for the beginning button's click event.  Sets the current movie time to the beginning of 
 * the movie.
 */
QTP.MovieController.prototype.beginningButtonClicked = function(event)
{
	if(!this.movie)
		return true;
	this.movie.SetTime(this.cache.start);
};

/**
 * Handler for the mute button's click event.  Toggles the movie's mute property.
 */
QTP.MovieController.prototype.muteClicked = function(event)
{
	if(!this.movie)
		return true;
	if(this.cache.muted)
	{
		this.movie.SetMute(false);
		this.mute.setName('Unmute');
	}
	else
	{
		this.movie.SetMute(true);
		this.mute.setName('Mute');
	}
};

/**
 * Handler for the volume slider's changed event.  Sets the movie volume to reflect the slider's 
 * position.
 */
QTP.MovieController.prototype.volumeChanged = function(position)
{
	if(!this.movie)
		return true;
		
	if(isNaN(position))
		position = 0.0;
		
	this.movie.SetVolume(position * 256);
};

/**
 * Handler for the timeline slider's changed event.  Sets the current movie time to reflect the 
 * slider's position.
 */
QTP.MovieController.prototype.timelineChanged = function(position)
{
	if(!this.movie)
		return true;
	
	var newTime = this.cache.start + position * (this.cache.end - this.cache.start);
	if(isNaN(newTime))
		newTime = this.cache.start;
		
	this.movie.SetTime(newTime);
	return true;
};

/**
 * Handler for the timeline slider's dragStarted event. Pauses the movie if playing.
 */
QTP.MovieController.prototype.timelineDragStarted = function(event)
{
	if(!this.movie)
		return true;
	this.paused = this.cache.rate == 0;
	this.movie.Stop();
};

/**
 * Handler for the timeline slider's dragEnded event. Restarts the movie if it was playing when
 * dragging started.
 */
QTP.MovieController.prototype.timelineDragEnded = function(event)
{
	if(!this.movie)
		return true;
	if(!this.paused)
		this.movie.Play();
};

/** 
 * Adds a cue point.  The listener function will be called once the movie's time is greater or 
 * equal than the specified time value.  The cue point is removed once it is called.
 * @param {int} timeVal Time which, when reached, the listener will be called.
 * @param {Function} listenerVal Listener function called when the timeVal is reached.
 */
QTP.MovieController.prototype.addCuePoint = function(timeVal, listenerVal)
{
	this.cuePoints.push({time:timeVal,listener:listenerVal});
	this.cuePoints.sort(function(a,b){return a.time - b.time;});
};

/** 
 * Adds a cue range.  The <code>enter</code> listener function will be called once the movie's time 
 * is inside the range specified by the <code>start</code> and <code>end</code> time values.  
 * The cue range is <i>not</i> removed once it is called. 
 * The <code>exit</code> listener function will be called once the movie's time 
 * is outside the range specified by the <code>start</code> and <code>end</code> time values.  
 * @param {int} startVal Time which, when reached, the listener will be called.
 * @param {int} endVal Time which, when reached, the listener will be called.
 * @param {Function} enterVal Listener function called when the timeVal is reached.
 * @param {Function} exitVal Listener function called when the timeVal is reached.
 */
QTP.MovieController.prototype.addCueRange = function(startVal, endVal, enterListener, exitListener)
{
	this.cueRanges.push({start:startVal,end:endVal,enter:enterListener,exit:exitListener});
	this.cueRanges.sort(function(a,b){return a.start - b.start;});
};
// End Class "QTP.MovieController"
