QuickTime = {

	version: 0.1,
	
	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 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;
				
					 links[j].onclick = function(){ return QuickTime.playMovie(this); }
				}
			}
			
			if (found)
			{
				//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+"' type='video/quicktime' pluginspage='http://www.apple.com/quicktime/download/' src='"+link+"' 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;

		/** This bypasses the "Click to Activate" message **/
		var movieObj = element.lastChild;

		// 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);

		
		/** This comes from the Apple QuickTime scripts **/
		movieObj = element.lastChild;
		
		if(!movieObj.GetControllerVisible()) setTimeout( function() { movieObj.SetControllerVisible(true); }, 100);
	},


	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);