// -------------------------------------------------------------------
// Image Thumbnail Viewer Script- By Dynamic Drive, available at: http://www.dynamicdrive.com
// Last updated: Jan 22nd, 2007
// -------------------------------------------------------------------

var youtubeviewer={
	definefooter: '<div class="footerbar">Zapri</div>', //Define HTML for footer interface
	
	scrollbarwidth: 16,
	targetlinks:[], //Array to hold links with rel="thumbnail"
	
	createYoutubeBox:function(){
		//write out HTML for Image Thumbnail Viewer plus loading div
		document.write('<div id="youtubeBox" onClick="youtubeviewer.closeit()"><div id="thumbYoutube"></div>'+this.definefooter+'</div>')
		this.youtubeBox=document.getElementById("youtubeBox")
		this.thumbYoutube=document.getElementById("thumbYoutube") //Reference div that holds the shown image
		this.standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body //create reference to common "body" across doctypes
	},
	
	
	centerDiv:function(divobj){ //Centers a div element on the page
		var ie=document.all && !window.opera
		var dom=document.getElementById
		var scroll_top=(ie)? this.standardbody.scrollTop : window.pageYOffset
		var scroll_left=(ie)? this.standardbody.scrollLeft : window.pageXOffset
		var docwidth=(ie)? this.standardbody.clientWidth : window.innerWidth-this.scrollbarwidth
		var docheight=(ie)? this.standardbody.clientHeight: window.innerHeight
		var docheightcomplete=(this.standardbody.offsetHeight>this.standardbody.scrollHeight)? this.standardbody.offsetHeight : this.standardbody.scrollHeight //Full scroll height of document
		var objwidth=divobj.offsetWidth //width of div element
		var objheight=divobj.offsetHeight //height of div element
		var topposition=(docheight>objheight)? scroll_top+docheight/2-objheight/2+"px" : scroll_top+10+"px" //Vertical position of div element: Either centered, or if element height larger than viewpoint height, 10px from top of viewpoint
		divobj.style.left=docwidth/2-objwidth/2+"px" //Center div element horizontally
		divobj.style.top=Math.floor(parseInt(topposition))+"px"
		divobj.style.visibility="visible"
	},
	
	showyoutubeBox:function(){ //Show youtubeBox div
		this.centerDiv(this.youtubeBox)
	},
	
	
	loadYoutube:function(link){ //Load image function that gets attached to each link on the page with rel="thumbnail"
		if (this.youtubeBox.style.visibility=="visible") //if thumbox is visible on the page already
			this.closeit() //Hide it first (not doing so causes triggers some positioning bug in Firefox
		
		//var imageHTML='<img src="'+link.getAttribute("href")+'" />' //Construct HTML for shown image
		//var imageHTML='<img src="'+link.getAttribute("href")+'"  />'
		var imageHTML='<object width="640" height="480"><param name="movie" value="'+link.getAttribute("href")+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'+link.getAttribute("href")+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="480"></embed></object>'
		
		if (this.enableTitle && link.getAttribute("title")) //Use title attr of the link as description?
			imageHTML+='<br />'+link.getAttribute("title")
			
		this.thumbYoutube.innerHTML=imageHTML //Populate thumbYoutube div with shown image's HTML (while still hidden)
		
		youtubeviewer.showyoutubeBox() //Display "youtubeBox" div to the world!
	},
	
	closeit:function(){ //Close "youtubeBox" div function
		this.youtubeBox.style.visibility="hidden"
		this.thumbYoutube.innerHTML=""
		this.youtubeBox.style.left="-2000px"
		this.youtubeBox.style.top="-2000px"
	},
	
	cleanup:function(){ //Clean up routine on page unload

		if (this.featureYoutube) this.featureYoutube.onload=null
			this.featureYoutube=null
		this.thumbYoutube=null
		for (var i=0; i<this.targetlinks.length; i++)
			this.targetlinks[i].onclick=null
		this.youtubeBox=null
	},
	
	dotask:function(target, functionref, tasktype){ //assign a function to execute to an event handler (ie: onunload)
		var tasktype=(window.addEventListener)? tasktype : "on"+tasktype
		if (target.addEventListener)
			target.addEventListener(tasktype, functionref, false)
		else if (target.attachEvent)
			target.attachEvent(tasktype, functionref)
	},
	
	init:function(){ //Initialize thumbnail viewer script by scanning page and attaching appropriate function to links with rel="thumbnail"
		var pagelinks=document.getElementsByTagName("a")
		for (var i=0; i<pagelinks.length; i++){ //BEGIN FOR LOOP
			if (pagelinks[i].getAttribute("rel") && pagelinks[i].getAttribute("rel")=="youtubeLink"){ //Begin if statement
				pagelinks[i].onclick=function(){
					youtubeviewer.loadYoutube(this) //Load image
					return false
				}
				this.targetlinks[this.targetlinks.length]=pagelinks[i] //store reference to target link
			} //end if statement
		} //END FOR LOOP
		//Reposition "youtubeBox" div when page is resized
		this.dotask(window, function(){if (youtubeviewer.youtubeBox.style.visibility=="visible") youtubeviewer.centerDiv(youtubeviewer.youtubeBox)}, "resize")
	
	} //END init() function

}

youtubeviewer.createYoutubeBox() //Output HTML for the image thumbnail viewer
youtubeviewer.dotask(window, function(){youtubeviewer.init()}, "load") //Initialize script on page load
youtubeviewer.dotask(window, function(){youtubeviewer.cleanup()}, "unload")