

var videoviewer = {
	enableTitle: true, //Should "title" attribute of link be used as description?
	enableAnimation: true, //Enable fading animation?
	definefooter: '<div class="footerbar" onClick="videoviewer.closeit()">X</div>',
	defineLoading: '<img src="images/loading.gif" />Loading...',
	scrollbarwidth: 16,
	opacitystring: 'filter:progid:DXImageTransform.Microsoft.alpha(opacity=10); -moz-opacity: 0.1; opacity: 0.1',
	targetlinks:[], //Array to hold links with rel="video"
	
	createthumbBox:function() {
		///document.write('<div id="video_box" onClick="videoviewer.closeit()"><div id="video"></div>'+this.definefooter+'</div>')
		
		document.write('<div id="video_box"><div id="video"></div>'+this.definefooter+'</div>')
		document.write('<div id="video_loading">'+this.defineLoading+'</div>')
		this.thumbBox = document.getElementById("video_box")
		this.thumbImage = document.getElementById("video")
		this.thumbLoading = document.getElementById("video_loading")
		this.standardbody = (document.compatMode == "CSS1Compat") ? document.documentElement : document.body
	},
	
	centerDiv:function(divobj) {
		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
		var objwidth = divobj.offsetWidth
		var objheight = divobj.offsetHeight
		var topposition = (docheight > objheight) ? scroll_top+docheight/2-objheight/2+"px" : scroll_top+10+"px"
		divobj.style.left = docwidth/2-objwidth/2+"px"
		divobj.style.top = Math.floor(parseInt(topposition))+"px"
		divobj.style.visibility = "visible"
	},
	
	showthumbBox:function() {
		videoviewer.thumbLoading.style.visibility = "hidden"
		this.centerDiv(this.thumbBox)
		if (this.enableAnimation) {
			this.currentopacity = 0.1
			this.opacitytimer = setInterval("videoviewer.opacityanimation()", 20)
		}
	},
	
	loadimage:function(link) {
		if (this.thumbBox.style.visibility == "visible")
			this.closeit()
		
		var videoHTML = '';
		if (link.getAttribute("type") == "youtube") {
			videoHTML += '<object width="480" height="385">';
			videoHTML += '<param name="movie" value="http://www.youtube.com/v/'+link.getAttribute("href")+'"></param>';
			videoHTML += '<param name="wmode" value="transparent"></param>';
			videoHTML += '<embed src="http://www.youtube.com/v/'+link.getAttribute("href")+'" type="application/x-shockwave-flash" wmode="transparent" width="480" height="385"></embed>';
			videoHTML += '</object>';
		} else if (link.getAttribute("type") == "vbox7") {
			videoHTML += '<object width="450" height="403">';
			videoHTML += '<param name="movie" value="http://i47.vbox7.com/player/ext.swf?vid='+link.getAttribute("href")+'"></param>';
			videoHTML += '<param name="quality" value="high"></param>';
			videoHTML += '<embed src="http://i48.vbox7.com/player/ext.swf?vid='+link.getAttribute("href")+'" quality="high" type="application/x-shockwave-flash" width="450" height="403"></embed>';
			videoHTML += '</object>';
		}


	
		///alert(videoHTML);
		////var videoHTML = '<img src="'+link.getAttribute("href")+'" style="'+this.opacitystring+'" />'
		///if (this.enableTitle && link.getAttribute("title"))
		///	videoHTML += '<br />'+link.getAttribute("title")
		
		
		this.centerDiv(this.thumbLoading)
		this.thumbImage.innerHTML = videoHTML
		
		
		///this.featureImage = this.thumbImage.getElementsByTagName("embed")[0]
		///if (this.featureImage.complete)
			videoviewer.showthumbBox()
		//else {
		///	this.featureImage.onload = function() {
		///		videoviewer.showthumbBox()
		///	}
		///}
		///if (document.all && !window.createPopup)
		///	this.featureImage.src = link.getAttribute("href")
		///this.featureImage.onerror = function() {
		///	videoviewer.thumbLoading.style.visibility = "hidden"
		//}
	},
		
	setimgopacity:function(value) {
		var targetobject = this.featureImage
		if (targetobject.filters && targetobject.filters[0]) {
			if (typeof targetobject.filters[0].opacity == "number")
				targetobject.filters[0].opacity = value * 100
			else
				targetobject.style.filter = "alpha(opacity=" + value * 100 + ")"
		}
		else if (typeof targetobject.style.MozOpacity != "undefined")
			targetobject.style.MozOpacity = value
		else if (typeof targetobject.style.opacity != "undefined")
			targetobject.style.opacity = value
		else
			this.stopanimation()
	},
		
	opacityanimation:function() {
		this.setimgopacity(this.currentopacity)
		this.currentopacity += 0.1
		if (this.currentopacity > 1)
			this.stopanimation()
	},
	
	stopanimation:function() {
		if (typeof this.opacitytimer != "undefined")
			clearInterval(this.opacitytimer)
	},
		
	closeit:function() {
		this.stopanimation()
		this.thumbBox.style.visibility = "hidden"
		this.thumbImage.innerHTML = ""
		this.thumbBox.style.left = "-2000px"
		this.thumbBox.style.top = "-2000px"
	},
		
	cleanup:function() {
		this.thumbLoading = null
		if (this.featureImage) this.featureImage.onload = null
		this.featureImage = null
		this.thumbImage = null
		for (var i=0; i<this.targetlinks.length; i++)
			this.targetlinks[i].onclick = null
		this.thumbBox = null
	},
		
	dotask:function(target, functionref, tasktype) {
		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() {
		if (!this.enableAnimation)
			this.opacitystring = ""
		var pagelinks = document.getElementsByTagName("a")
		for (var i=0; i<pagelinks.length; i++) {
			if (pagelinks[i].getAttribute("rel") && pagelinks[i].getAttribute("rel") == "video") {
				pagelinks[i].onclick = function() {
					videoviewer.stopanimation()
					videoviewer.loadimage(this)
					return false
				}
				this.targetlinks[this.targetlinks.length] = pagelinks[i]
			}
		}
		this.dotask(window, function() {
			if (videoviewer.thumbBox.style.visibility == "visible")
				videoviewer.centerDiv(videoviewer.thumbBox)}, "resize")
		}
	}
		
	videoviewer.createthumbBox()
	videoviewer.dotask(window, function() { videoviewer.init() }, "load")
	videoviewer.dotask(window, function() { videoviewer.cleanup() }, "unload")
	
	
