//	clip.js for ludd
//	copyright mr.snow house of laudanum 2006-2007


/*	so if a clip is playing
	and another is selected
	that hasn't loaded yet
	then the loading clip
	autoplays
*/

			var plugin;
			var debugging = 0;
//			var controller = 'true';
			var controller = 'false';
			var loop = true;
			
//	timers
			var update = 150	//	delay before polling the current load/play state
			var t
//			var c				//	clip title stopwatch
			var f 				//	firefox bug delay

			var prog_width = 382
			
			var auto = 0		//	auto play next clip ?
			var clips

			scrubOK = 0
			playing = 0
			wasPlaying = 0
			var active

			scriptable = 1
			
			min_safari_version = 200
//			min_safari_version = 500


			function detect() {
				if ( browser.isSafari && parseInt(browser.versionMajor) < min_safari_version ) {
					scriptable = 0
					noscr_init()
			//		alert(scriptable)
					debug("incompatible safari version " + browser.versionMajor)
				}
			//	alert(active)
			}
			
			
			function noscr_init() {
		//	hide the pause button
		//	hide the play progress bars
		
				document.getElementById('pausebutton').style.display = 'none'
				document.getElementById('progress').style.display = 'none'
			}
			
			function Clip(src, title, director, img, duration, info) {

				this.movie = src + '.mp4'// + this.filetype

				this.id = img

				this.title_on = new Image()
				this.title_on.src = 'nav/' + img + '_on.gif'
				this.title_off = new Image()
				this.title_off.src = 'nav/' + img + '_off.gif'
				
				this.poster = new Image()
				this.poster.src = img + '_poster.jpg'

				if ( info != 0 ) {
					this.info = new Image()
					this.info.src = img + '_info.gif'
				}

				this.title = title
				this.director = director
				this.duration = duration
				
			//	this.init()
			}			
			
			
			function makeButton() {
				node = document.getElementById('title')
			}


//			Clip.prototype.filetype = 'mp4'
//			Clip.prototype.init = makeButton
	
	
			function swapTitleImage(id, on) {
				if ( ! on && id == active )
					return

				if ( ! clips )
					return
					
				obj = clips[id]
				
//				clearTimeout(c)
				
				if ( on )
					swapImage(obj.id, obj.title_on.src)
				else
					swapImage(obj.id, obj.title_off.src)
				
//				c = setTimeout("swapTitleImage(" + active + ", 1)", 1000)
			}
					
			
			function play() {
				showClip()
				debug('play')

				if ( !scriptable ) {
					noscr_play()
					return
				}
				
				try {
					plugin.Play()
				} catch (e) {
					debug(e)
				}
				
//	firefox bug
				clearInterval(f)
				if (( browser.isFirefox && ! plugin.GetRate()) || ( browser.isIE && ! playing ) ) {
					debug('force play')
					f = setInterval("play()", 200)
				} else {
//					debug('force show')
//					f = setInterval("showClip()", 200)
				}
				

				playing = 1
			}
			
			
			function pause() {
				if ( plugin )
					try {
						plugin.Stop()
					} catch (e) {
						debug(e)
					}
				playing = 0
			}
			
			
			function closeClip () {
				hideClipAndPoster()
			}
			
			
			function showClip () {
				document.getElementById('clipholder').style.visibility = 'visible'
				document.getElementById('poster').style.visibility = 'hidden'
				document.getElementById('player').style.visibility = 'visible'
				
				try {
					if ( plugin.GetControllerVisible())
						plugin.SetControllerVisible(0)
				} catch (e) {
					debug(e)
				}
					
			}
			
			
			function showPoster () {
				pause()

//	firefox won't get info on a plugin that isn't visible
				if ( browser.isFirefox || browser.isIEOff ) {
					showClip()
//					document.getElementById('clipholder').style.width = '1px'
//					document.getElementById('clipholder').style.height = '1px'
					
				//	return
				}
				
				document.getElementById('clipholder').style.visibility = 'hidden'
				document.getElementById('poster').style.visibility = 'visible'
				document.getElementById('player').style.visibility = 'visible'

//	if we've written a quicktime in by hand ( old browsers ) its time to kill it
				if ( ! scriptable )
					emptyNode('clipholder')
				

			}
			
			
			function show(id) {
				if ( id == active )
					return

				playing = 0
				pause()				
				setClip(id)
				showPoster()
			}
			
			
			function setClip(id) {
				active = id
				obj = clips[id]

				if ( scriptable ) {
					setMovie(obj.movie)
				}

				_setPlayHead(1)

				setPoster(obj.poster.src)
				if ( obj.info )
					setInfo(obj.info.src)
			
				for ( i = 0; i < clips.length; i++ )
					if ( i == id )				
						swapTitleImage(i,1)
					else
						swapTitleImage(i,0)

				if ( scriptable ) {
					_setPlayHead(1)
					pause()				
					_setPlayHead(0)
					pause()				
				}

//				plugin.SetTime(0)
	//			pause()
			}
			
			
			function noscr_play() {
				id = active

//				alert("noscr_play("+id+") " + clips[id].title)
				
//				clean the clipholder, ready for a new clip
				emptyNode('clipholder')
				
//				node.appendChild(buildEmbed(id))
//				node.appendChild(buildObject(id))
				nodom_writeEmbed(id)
			}

			
			function buildObject(id) {

/*			

					<OBJECT 
							classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"
							codebase="http://www.apple.com/qtactivex/qtplugin.cab"
							width="352" height="288" 
							controller="false"
							autostart="false"
							autoplay="false"
							id="the_screen"> 

							<PARAM name="src" value="montage.mp4">  
							<PARAM name="autoplay" value="false">  
							<PARAM name="autostart" value="false">  
							<PARAM name="controller" value="false">  
							<PARAM name="scale" value="tofit">  
							<PARAM name="bgcolor" value="#cacaca">  
*/	

				obj = document.createElement('object')
				
				if ( browser.isIE ) {
					obj.setAttribute('classid', "clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B")
					obj.setAttribute('codebase', "http://www.apple.com/qtactivex/qtplugin.cab")

					param = document.createElement('param')
					param.setAttribute('name', 'src')
					param.setAttribute('value', clips[id].movie)
					obj.appendChild(param)

				}
				
				
				if ( ! browser.isIE ) {					
					obj.setAttribute('data', clips[id].movie)
					obj.setAttribute('type', "video/quicktime")
				}
				
				obj.setAttribute('width', 352)
				obj.setAttribute('height', 288)
				obj.setAttribute('controller', controller)
				obj.setAttribute('autostart', "true")
				obj.setAttribute('autoplay', "true")
				obj.setAttribute('kioskmode', "true")
				obj.setAttribute('id', 'the_screen')
				
				param = document.createElement('param')
				param.setAttribute('name', 'scale')
				param.setAttribute('value', 'tofit')
				obj.appendChild(param)

				param = document.createElement('param')
				param.setAttribute('name', 'controller')
				param.setAttribute('value', controller)
				obj.appendChild(param)

				param = document.createElement('param')
				param.setAttribute('name', 'autoplay')
				param.setAttribute('value', 'true')
				obj.appendChild(param)

				param = document.createElement('param')
				param.setAttribute('name', 'kioskmode')
				param.setAttribute('value', 'true')
				obj.appendChild(param)

				return obj
				//	node.appendChild(obj)

			}


			function buildEmbed(id) {

alert("embed " + id)
/*
	<EMBED WIDTH=352 HEIGHT=288 
								SRC="hi61.mp4"
								TYPE="video/quicktime"
								PLUGINSPAGE="http://www.apple.com/quicktime/download"
								EnableJavaScript="true"
								controller="false"
								autostart="false"
								autoplay="false"
								loop="false"
								scale="tofit"
								NAME="the_screen"
								bgcolor="#cacaca"
							/>
*/

				obj = document.createElement('embed')
alert("finished createElement embed " + clips[id].movie)

				obj.setAttribute('src', clips[id].movie)
//				obj.src = clips[id].movie
alert("finished src embed")
				obj.setAttribute('type', "video/quicktime")
				obj.setAttribute('width', 352)
				obj.setAttribute('height', 288)

alert("finished height embed")

				obj.setAttribute('controller', controller)
				obj.setAttribute('autostart', "true")
				obj.setAttribute('autoplay', "true")
				obj.setAttribute('name', 'the_screen')
				obj.setAttribute('loop', 'false')
				obj.setAttribute('scale', 'tofit')
				obj.setAttribute('EnableJavaScript', 'true')
				obj.setAttribute('PLUGINSPAGE', 'http://www.apple.com/quicktime/download')

alert("finished building embed")

				return obj
				//	node.appendChild(obj)

			}



			function nodom_writeEmbed(id) {

				content = ""
/*
	<EMBED WIDTH=352 HEIGHT=288 
								SRC="hi61.mp4"
								TYPE="video/quicktime"
								PLUGINSPAGE="http://www.apple.com/quicktime/download"
								EnableJavaScript="true"
								controller="false"
								autostart="false"
								autoplay="false"
								loop="false"
								scale="tofit"
								NAME="the_screen"
								bgcolor="#cacaca"
							/>
*/
				content += "<embed "
				content += " src='" + clips[id].movie +"'"
				content += " type='video/quicktime'"
				content += " width='352'"
				content += " height='288'"
				content += " bgcolor='#cacaca'"
				content += " controller='" + controller + "'"
				content += " autostart='true'"
				content += " autoplay='true'"
				content += " name='the_screen'"
				content += " loop='false'"
				content += " scale='tofit'"
				content += " EnableJavaScript='true'"
				content += " kioskmode='true'"
				content += " volume='100'"
				content += " PLUGINSPAGE='http://www.apple.com/quicktime/download'"
				content += " />"

				document.getElementById("clipholder").innerHTML = content

				return null
			}

			
			function setPoster(src) {
				document.getElementById('posterimage').src = src
			}
			
			
			function setMovie(src) {
				_setPlayHead(0)

				try {
					plugin.SetURL(src)
				} catch (e) {
					debug(e)
					return
				}

				_setPlayHead(0)
	
				if ( browser.isFirefox ) {
					debug("firefox");
					plugin.Play();
					plugin.Rewind();
					plugin.Stop();
//					alert(plugin.GetURL())
				}
			}
			
			
			function setInfo(src) {
				document.getElementById('info').src = src
			}

			
			function hideClipAndPoster() {
				pause()
				
				document.getElementById('clipholder').style.visibility = 'hidden'
				document.getElementById('poster').style.visibility = 'hidden'
				document.getElementById('player').style.visibility = 'hidden'
				if ( ! scriptable )
					emptyNode('clipholder')
			}
			
			
			function _getStatus(obj) {
				if ( ! obj || !obj.GetPluginStatus())
					return null
				else
					return obj.GetPluginStatus()
			}
			
			
			function _getLoadProgress(obj) {
			//	alert(obj.GetMovieSize)
				progress = obj.GetMaxBytesLoaded()
				size = obj.GetMovieSize()
				return parseInt(progress/size*100)
			}


			function _getPlayProgress() {
				if ( ! plugin )
					return

				try {
					duration = plugin.GetDuration()
					loaded = plugin.GetMaxTimeLoaded()
					time = plugin.GetTime()
				} catch (e) {
					debug('can\'t get duration')
					return
				}

//	alert(plugin)
/*
				if ( plugin.GetDuration() )
					duration = plugin.GetDuration()
				loaded = plugin.GetMaxTimeLoaded()
				time = plugin.GetTime()
*/

//				played = parseInt(time/duration*1000)/10 + "%"
//				ready = parseInt(loaded/duration*1000)/10 + "%"

				played = parseInt(time/duration*prog_width)
				ready = parseInt(loaded/duration*prog_width)

				if ( browser.isFirefox ) {
					if ( ! isNaN(played) ) {
						document.getElementById('play').style.width = played + "px"
					}
					if ( ! isNaN(ready) ) {
						document.getElementById('load').style.width = ready + "px"
					} else {
						debug("time : " + time)
					}
				} else {
					document.getElementById('play').style.width = played + "px"
					document.getElementById('load').style.width = ready + "px"
				}
				
				if ( loaded/duration == 1 && ! playing && auto) {
					debug("auto")
					play()
				}
				
				if ( time/duration == 1 && playing ) {
				
					debug("about to play() " + time)
//	we're at the end, rewind and play
					_setPlayHead(0)
					if ( auto ) {
//	auto play the next clip
						active++
						if ( active >= clips.length )
							active = 0
						setClip(active)
					}
					if ( loop )
						play()
					else
						playing = 0
				}
//				window.status = played + " / " + ready
			}
			
			
			function _setPlayHead(position) {
				if ( ! plugin )
					return

				try {
					duration = plugin.GetDuration()
					time = parseInt(duration * position)
//					window.status = ( "setPlayHead " + position + " : " + time)
					plugin.SetTime(time)			
				} catch (e) {
					debug(e)
					return
				}
			}
			
			
			function monitor() {
				clearTimeout(t)
				if ( browser.isSafari && ! playing )
					pause()
				_getPlayProgress()
				t = setTimeout("monitor()", update)
			}
			
			
			function watch(on) {
				if ( on ) {
					document.onmousedown = trigger
					document.onmousemove = scrub
					document.onmouseup = kill
				}
				else {
					document.onmousedown = null
					document.onmouseup = null
					kill()
				}
			}
			
			
		//	called by mouse down inside the progress bar	
			function trigger(e) {
			//	left = document.getElementById('progress').style.left
			//	left = 132
			//	position = (event.pageX - left)/prog_width
			//	_setPlayHead(position)		

				if ( playing )
					wasPlaying = 1
				else
					wasPlaying = 0

				scrubOK = 1
				
			}


			function scrub(e) {
				if ( ! scrubOK )
					return
				pause()
				left = 132
				position = (event.pageX - left)/prog_width
				_setPlayHead(position)		
			}				


			function kill(e) {
//			alert('killing')
				scrub(e)
				document.onmousemove = null
				scrubOK = 0
				if ( wasPlaying ) {
					debug("killed + play")
					play()
				}	
			}
			

			function emptyNode(id) {
				node = document.getElementById(id)

				while ( node.hasChildNodes() ) {
					node.removeChild(node.firstChild)
//					alert(node.hasChildNodes())
				}
			
			}
			
			
			var log = ""
			cr = "\n"
			
			function pad(num) {
				if ( num < 10 )
					num = "0" + num
				return num
			}
			
			
			
			function debug (msg) {
//				if ( ! browser.isIE )
//					return
				if ( ! debugging )
					return
					
				if ( clips )
					msg += " [ active : " + active + "]"
//				msg += "\nplaying :\t" + playing
	//			msg += "\nwasPlay :\t" + wasPlaying
		//		msg += "\nscrubOK :\t" + scrubOK
				
//				alert(msg)
//				window.status = msg
				
				date = new Date()
				now = pad(date.getHours()) + ":" + pad(date.getMinutes()) + ":" + pad(date.getSeconds())
				log = now + " " + msg + cr + log
			}
			

