QuickTime = {

	version: 0.4,
	
	upgrade: undefined,
	
	getElementsByClassName: function(className, tag, elm)
	{
		//http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
		
		var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
		var tag = tag || "*";
		var elm = elm || document;
		var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
		var returnElements = [];
		var current;
		var length = elements.length;
		for(var i=0; i<length; i++)
		{
			current = elements[i];
			
			if(testClass.test(current.className))
			{
				returnElements.push(current);
			}
		}
		return returnElements;
	},

	init: function()
	{		
		var installed = QuickTime.isQTInstalled();
		
		if (!installed && !QuickTime.upgrade) return;		
		
		var regexp = new RegExp("\\.mov$");
		
		var divs = QuickTime.getElementsByClassName("quicktime","div");
		
		for (var i = 0 ; i < divs.length ; ++i)
		{
			var found = false;
			
			var links = divs[i].getElementsByTagName("a");
			
			for (var j = 0 ; j < links.length ; ++j)
			{
				if (regexp.test(links[j].href))
				{
					found = true;
				
					if (installed)
					{
						links[j].onclick = function(){ return QuickTime.playMovie(this); }
					}
					else
					{
						links[j].href = QuickTime.upgrade;
					}
				}
			}
			
			if (found && installed)
			{
				//add space for the control
				divs[i].style.height = divs[i].clientHeight + 16 + "px";
			}
		}
	},

	playMovie: function(link)
	{
		var div = link.parentNode;
		
		var width = div.clientWidth;
		var height = div.clientHeight;

		//TODO: do this properly with element creation code
		var embed = "<embed width='"+width+"' height='"+height+"' src='"+link+"' type='video/quicktime' pluginspage='http://www.apple.com/quicktime/download/' controller='true' autoplay='true' scale='aspect'></embed>";
		var obj = "<object width='"+width+"' height='"+height+"' classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B' codebase='http://www.apple.com/qtactivex/qtplugin.cab'><param name='src' value='"+link+"' /><param name='controller' value='true' /><param name='autoplay' value='true' /><param name='scale' value='aspect' />"+embed+"</object>";

		div.innerHTML = obj;

		QuickTime.fixIE(div);
		
		return false;
	},
		
	fixIE: function(element)
	{
		if(navigator.appName != "Microsoft Internet Explorer") return;

		var movieObj = element.lastChild;
		
		/** This bypasses the "Click to Activate" message **/
		
		/**
		// I originally added this code to work around the "Click to Activate" dialog introduced
		// because of the Eolas patent. It looks, however, as if this is now casuing IE & and above
		// to embed two copies of the movie one on top of the other which is only obvious when
		// you pause the visible movie only for the sound to continue to play.
		
		// I've commented out the code so that this no longer happens. I think looking into the
		// Eolas problem in more detail this code isn't needed at all as long as this script is
		// kept in a JS file external to the HTML page.
			
			// find param tags within object
			var params = movieObj.getElementsByTagName('param');
			var inner = '';

			// if there are params, but param tags ca not be found within innerHTML
			if (params.length && !/<param/i.test(movieObj.innerHTML))
			{
				// add all param tags to 'inner' string
				for (var x=0;x < params.length;x++)
				{
					inner += params.item(x).outerHTML;
				}
			}

			// put 'inner' string with param tags in the middle of the outerHTML
			movieObj.outerHTML = movieObj.outerHTML.replace('>', '>' + inner);
			movieObj = element.lastChild;
		**/
		
		/** This comes from the Apple QuickTime scripts **/		
		
		if(!movieObj.GetControllerVisible()) setTimeout( function() { movieObj.SetControllerVisible(true); }, 100);
	},
	
	isQTInstalled: function()
	{
		var qtInstalled = false;
		qtObj = false;
		if (navigator.plugins && navigator.plugins.length)
		{
			for (var i=0; i < navigator.plugins.length; i++ )
			{
				var plugin = navigator.plugins[i];
				if (plugin.name.indexOf("QuickTime") > -1) 
				{
					qtInstalled = true;
				}
			}
		}
		else
		{
			execScript('on error resume next: qtObj = IsObject(CreateObject("QuickTimeCheckObject.QuickTimeCheck.1"))','VBScript');
			qtInstalled = qtObj;
		}
		
		return qtInstalled;
	},

	addEvent: function(obj,event,func,capture)
	{
		if (window.addEventListener)
		{
			obj.addEventListener(event,func,capture);
			return true;
		}
		else
		{
			return obj.attachEvent("on" + event,func);
		}
	}
};

QuickTime.addEvent(window,"load",QuickTime.init);