var oDiv = document.createElement('div');
var oUl = document.createElement('ul');
var oLi = document.createElement('li');
var oA = document.createElement('a');

var MovieSwap = {
	oMovie: null,

	Init: function(defaults) {
		if (defaults.type) {
			sType = defaults.type;
		} else {
			sType = 'sizes';
		}
		this.aSizes = defaults;
		
		if (sType == 'sizes') {
			//Intial stuff
			if (defaults.gallery) {
				this.hasGallery = defaults.gallery;
			} else {
				this.hasGallery = false;
			}

			//this.sDefaultSize = sDefault || false;
		
			for (size in this.aSizes) {
				var obj = this.aSizes[size];
				if (obj['default']) this.sDefaultSize = size;
			}

			this.oWrapper = $('quicktime');
			this.oSwapper = $('swapper');
			
			if(!isQTInstalled()) { 
				// if qt isn't installed, prompt
				this.oSwapper.innerHTML = '<a href="/go/quicktime/"><img src="http://images.apple.com/pro/images/quicktime_noqt.gif" alt="Get Quicktime free now." width="432" height="240" border="0"><\/a>';
			} else {
				// Create transport (wrapper div around buttons and slider) div
				var oTransport = oDiv.cloneNode(true);
				oTransport.appendChild(document.createTextNode(' '));
				oTransport.id = 'transport';

				// Create/attach back button
				var oBtnBack = oA.cloneNode(true);
				oBtnBack.appendChild(document.createTextNode(' '));
				oBtnBack.id = 'btnBack';
				oBtnBack.href = '#';
				oTransport.appendChild(oBtnBack);

				// Create/attach play/pause button
				var oBtnPlayPause = oA.cloneNode(true);
				oBtnPlayPause.appendChild(document.createTextNode(' '));
				oBtnPlayPause.id = 'btnPlayPause';
				oBtnPlayPause.href = '#';
				oBtnPlayPause.className = 'pause';
				oTransport.appendChild(oBtnPlayPause);

				// Create/attach forward button
				var oBtnForward = oA.cloneNode(true);
				oBtnForward.appendChild(document.createTextNode(' '));
				oBtnForward.id = 'btnForward';
				oBtnForward.href = '#';
				oTransport.appendChild(oBtnForward);

				// Create/attach timline div
				var oTimeline = oDiv.cloneNode(true);
				oTimeline.appendChild(document.createTextNode(' '));
				oTimeline.id = 'timeline';

				// Create/attach timline position div
				var oPosition = oDiv.cloneNode(true);
				oPosition.appendChild(document.createTextNode(' '));
				oPosition.id = 'position';
				oTimeline.appendChild(oPosition);
				oTransport.appendChild(oTimeline);

				// Attach transport (wrapper div around buttons and slider) div
				this.oWrapper.appendChild(oTransport);

				// Add Events to buttons
				addEventToObject($('btnBack'),'onclick',MovieSwap.Back);
				addEventToObject($('btnPlayPause'),'onclick',MovieSwap.Stop);
				addEventToObject($('btnForward'),'onclick',MovieSwap.Forward);


				// Create/attach links for swapping movie sizes
				var oLinksUl = oUl.cloneNode(true);
				oLinksUl.id = 'quicktimenav';
				for (size in this.aSizes) {
					var oLink = oA.cloneNode(true);
					oLink.appendChild(document.createTextNode(this.aSizes[size].displayname));
					oLink.id = 'quicktimenav_'+size;
					oLink.href = '#';
					oLink.target = size;
					var oLinkLi = oLi.cloneNode(true);
					oLinkLi.appendChild(oLink);
					oLinksUl.appendChild(oLinkLi);
				}
				this.oWrapper.appendChild(oLinksUl);

				// remove the background separator on the last link
				var links = $t('li', $('quicktimenav'));
				links[links.length-1].style.background = 'none';

				// Add events and active state to links
				for (var i=0; i<links.length; i++) {
					addEventToObject(links[i],'onclick',MovieSwap.Swap);
					if (links[i].firstChild.id == 'quicktimenav_'+this.sDefaultSize) { links[i].className = 'active'; }
				}

				// if qt is installed, swap out for default movie
				this.oSwapper.innerHTML = QT_GenerateOBJECTText(MovieSwap.aSizes[this.sDefaultSize].url, MovieSwap.aSizes[this.sDefaultSize].width, MovieSwap.aSizes[this.sDefaultSize].height,'','id','movie','emb#name','movie','emb#id','movieID','autoplay','true','cache','true','controller','false','type','video/quicktime');
				MovieSwap.SetMovie();
			}
		}
	},
	
	Swap: function(ev) {
		stopDefaultAction(ev);
		var size = getClickedLink(ev).target || false;
		var url = (getClickedLink(ev).href == "#") ? false : getClickedLink(ev).href;

		try{ MovieSwap.oMovie.Stop(); }catch(e){}
		removeEventFromObject($('btnPlayPause'),'onclick');
		$('btnPlayPause').className = "play";
		addEventToObject($('btnPlayPause'),'onclick',MovieSwap.Play);

		if (this.hasGallery) {
			try{ MovieSwap.oGalleryMovie.Stop(); }catch(e){}
			removeEventFromObject($('gallery_btnPlayPause'),'onclick');
			$('gallery_btnPlayPause').className = "play";
			addEventToObject($('gallery_btnPlayPause'),'onclick',MovieSwap.PlayGallery);
		}

		if (size) { // if you clicked on a size link
			MovieSwap.oMovie = null;
			
			$('transport').style.width = MovieSwap.aSizes[size].width+'px';
			$('timeline').style.width = MovieSwap.aSizes[size].width-100+'px';
			
			MovieSwap.oSwapper.innerHTML = QT_GenerateOBJECTText(MovieSwap.aSizes[size].url, MovieSwap.aSizes[size].width, MovieSwap.aSizes[size].height,'','id','movie','emb#name','movie','emb#id','movieID','autoplay','true','cache','true','controller','false','type','video/quicktime');
			MovieSwap.SetMovie();

			removeEventFromObject($('btnPlayPause'),'onclick');
			$('btnPlayPause').className = "pause";
			addEventToObject($('btnPlayPause'),'onclick',MovieSwap.Stop);

			var aNavItems = $t('li',$('quicktimenav'));
			for(var i=0; i<aNavItems.length; i++) {
				if (aNavItems[i].firstChild.id == 'quicktimenav_'+size) {
					aNavItems[i].className = 'active';
				} else {
					aNavItems[i].className = '';
				}
			}
		} else if (url) { // if you clicked on a movie link
			MovieSwap.oGalleryMovie = null;
			MovieSwap.oGallerySwapper.innerHTML = QT_GenerateOBJECTText(url, MovieSwap.oGallerySize.width, MovieSwap.oGallerySize.height,'','id','galMovie','emb#name','galMovie','emb#id','galMovieID','autoplay','true','cache','true','controller','false','type','video/quicktime');
			MovieSwap.SetGallery();

			removeEventFromObject($('gallery_btnPlayPause'),'onclick');
			$('gallery_btnPlayPause').className = "pause";
			addEventToObject($('gallery_btnPlayPause'),'onclick',MovieSwap.StopGallery);

			if ($('gallery_nav')) {
				aGalleryNavItems = $t('li', $('gallery_nav'));
				aGalleryNavLinks = $t('a', $('gallery_nav'));
				for(var i=0; i<aGalleryNavLinks.length; i++) {
					if (aGalleryNavLinks[i].href == url) {
						aGalleryNavItems[i].className = 'active';
					} else {
						aGalleryNavItems[i].className = '';
					}
				}
			}
		}
	},
	
	Play: function(ev) {
		stopDefaultAction(ev);
		try{ MovieSwap.oMovie.Play(); }catch(e){}
		removeEventFromObject($('btnPlayPause'),'onclick');
		$('btnPlayPause').className = "pause";
		addEventToObject($('btnPlayPause'),'onclick',MovieSwap.Stop);

		try{ MovieSwap.oGalleryMovie.Stop(); }catch(e){}
		removeEventFromObject($('gallery_btnPlayPause'),'onclick');
		$('gallery_btnPlayPause').className = "play";
		addEventToObject($('gallery_btnPlayPause'),'onclick',MovieSwap.PlayGallery);
		return false;
	},
	
	Stop: function(ev) {
		stopDefaultAction(ev);
		try{ MovieSwap.oMovie.Stop(); }catch(e){}
		removeEventFromObject($('btnPlayPause'),'onclick');
		$('btnPlayPause').className = "play";
		addEventToObject($('btnPlayPause'),'onclick',MovieSwap.Play);
		return false;
	},

	Back: function(ev) {
		stopDefaultAction(ev);
		try{ MovieSwap.oGalleryMovie.Stop(); }catch(e){}
		if (this.hasGallery) {
			removeEventFromObject($('gallery_btnPlayPause'),'onclick');
			$('gallery_btnPlayPause').className = "play";
			addEventToObject($('gallery_btnPlayPause'),'onclick',MovieSwap.PlayGallery);
		}

		try{ MovieSwap.oMovie.Step(-100); }catch(e){}

		try{ MovieSwap.oMovie.Play(); }catch(e){}
		removeEventFromObject($('btnPlayPause'),'onclick');
		$('btnPlayPause').className = 'pause';
		addEventToObject($('btnPlayPause'),'onclick',MovieSwap.Stop);
		return false;
	},

	Forward: function(ev) {
		stopDefaultAction(ev);
		try{ MovieSwap.oGalleryMovie.Stop(); }catch(e){}
		if (this.hasGallery) {
			removeEventFromObject($('gallery_btnPlayPause'),'onclick');
			$('gallery_btnPlayPause').className = "play";
			addEventToObject($('gallery_btnPlayPause'),'onclick',MovieSwap.PlayGallery);
		}

		try{ MovieSwap.oMovie.Step(100); }catch(e){}

		try{ MovieSwap.oMovie.Play(); }catch(e){}
		removeEventFromObject($('btnPlayPause'),'onclick');
		$('btnPlayPause').className = 'pause';
		addEventToObject($('btnPlayPause'),'onclick',MovieSwap.Stop);
		return false;
	},

	StartSlider: function(){
		try { var time_end = MovieSwap.oMovie.GetDuration(); } catch(e) { }// Total time
		try { var time_now = MovieSwap.oMovie.GetTime(); } catch(e) { } // Current time position
		if (time_end > 0 && time_now) {
			$('position').style.width = (time_now/time_end)*100 + "%";
		}
		// setup a timer
		MovieSwap.Timer = setTimeout('MovieSwap.StartSlider()', 50);
	},
	  
	SetMovie: function() {
		if (!MovieSwap.oMovie) {
		  MovieSwap.GetQTMovie();
		  this.waitingTimer = setTimeout(MovieSwap.SetMovie, 50);
		} else {
		  clearTimeout(this.waitingTimer);
		  MovieSwap.StartSlider();
		}
	},

	GetQTMovie: function(){
		// Different browsers see different objects
		try { 
			  var o = MovieSwap.oSwapper.getElementsByTagName('object')[0]; 
				o.GetPluginStatus(); 
				MovieSwap.oMovie = o;
		} catch(e) { 
			try { 
				var o = MovieSwap.oSwapper.getElementsByTagName('embed')[0] 
				o.GetPluginStatus();
				MovieSwap.oMovie = o; 
			} catch(e) {
				MovieSwap.oMovie = null;
			}
		}
		if ( (MovieSwap.oMovie != null) && (MovieSwap.oMovie.GetPluginStatus().toLowerCase() == "playable" || MovieSwap.oMovie.GetPluginStatus().toLowerCase() == "complete") ){
			return true;
		} else {
		  return false;
		}
	},

	PlayGallery: function(ev) {
		stopDefaultAction(ev);
		try{ MovieSwap.oMovie.Stop(); }catch(e){}
		removeEventFromObject($('btnPlayPause'),'onclick');
		$('btnPlayPause').className = "play";
		addEventToObject($('btnPlayPause'),'onclick',MovieSwap.Play);

		try{ MovieSwap.oGalleryMovie.Play(); }catch(e){}
		MovieSwap.StartGallerySlider();
		removeEventFromObject($('gallery_btnPlayPause'),'onclick');
		$('gallery_btnPlayPause').className = "pause";
		addEventToObject($('gallery_btnPlayPause'),'onclick',MovieSwap.StopGallery);
		return false;
	},

	StopGallery: function(ev) {
		stopDefaultAction(ev);
		try{ MovieSwap.oGalleryMovie.Stop(); }catch(e){}
		removeEventFromObject($('gallery_btnPlayPause'),'onclick');
		$('gallery_btnPlayPause').className = "play";
		addEventToObject($('gallery_btnPlayPause'),'onclick',MovieSwap.PlayGallery);
		return false;
	},

	BackGallery: function(ev) {
		stopDefaultAction(ev);
		try{ MovieSwap.oMovie.Stop(); }catch(e){}
		removeEventFromObject($('btnPlayPause'),'onclick');
		$('btnPlayPause').className = "play";
		addEventToObject($('btnPlayPause'),'onclick',MovieSwap.Play);

		try{ MovieSwap.oGalleryMovie.Step(-100); }catch(e){}
		MovieSwap.StartGallerySlider();

		try{ MovieSwap.oGalleryMovie.Play(); }catch(e){}
		removeEventFromObject($('gallery_btnPlayPause'),'onclick');
		$('gallery_btnPlayPause').className = 'pause';
		addEventToObject($('gallery_btnPlayPause'),'onclick',MovieSwap.StopGallery);
		return false;
	},

	ForwardGallery: function(ev) {
		stopDefaultAction(ev);
		try{ MovieSwap.oMovie.Stop(); }catch(e){}
		removeEventFromObject($('btnPlayPause'),'onclick');
		$('btnPlayPause').className = "play";
		addEventToObject($('btnPlayPause'),'onclick',MovieSwap.Play);

		try{ MovieSwap.oGalleryMovie.Step(100); }catch(e){}
		MovieSwap.StartGallerySlider();

		try{ MovieSwap.oGalleryMovie.Play(); }catch(e){}
		removeEventFromObject($('gallery_btnPlayPause'),'onclick');
		$('gallery_btnPlayPause').className = 'pause';
		addEventToObject($('gallery_btnPlayPause'),'onclick',MovieSwap.StopGallery);
		return false;
	},

	StartGallerySlider: function(){
		var time_end = MovieSwap.oGalleryMovie.GetDuration(); // Total time
		var time_now = MovieSwap.oGalleryMovie.GetTime(); // Current time position
		if (time_end > 0)
		  $('gallery_position').style.width = (time_now/time_end)*100 + "%";
		// setup a timer
		MovieSwap.GalleryTimer = setTimeout('MovieSwap.StartGallerySlider()', 50);
	},

	SetGallery: function() {
		if (!MovieSwap.oGalleryMovie) {
		  MovieSwap.GetQTGallery();
		  this.waitingTimer = setTimeout(MovieSwap.SetGallery, 50);
		} else {
		  clearTimeout(this.waitingTimer);
		  MovieSwap.StartGallerySlider();
		}
	},

	GetQTGallery: function(){
		// Different browsers see different objects
		try { 
			  var o = MovieSwap.oGallerySwapper.getElementsByTagName('object')[0]; 
				o.GetPluginStatus(); 
				MovieSwap.oGalleryMovie = o;
		} catch(e) { 
			try { 
				var o = MovieSwap.oGallerySwapper.getElementsByTagName('embed')[0] 
				o.GetPluginStatus();
				MovieSwap.oGalleryMovie = o; 
			} catch(e) {
				MovieSwap.oGalleryMovie = null;
			}
		}
		if ( (MovieSwap.oGalleryMovie != null) && (MovieSwap.oGalleryMovie.GetPluginStatus().toLowerCase() == "playable" || MovieSwap.oGalleryMovie.GetPluginStatus().toLowerCase() == "complete") ){
			return true;
		} else {
		  return false;
		}
	}

}
