//DOM Core
function emptyElement(target) {
	while (target.hasChildNodes()) {
		target.removeChild(target.firstChild);
	} //end target.hasChildNodes() WHILE
} //end emptyElement
function makeStrong(text) {
	var strong = document.createElement("strong");
	strong.appendChild(document.createTextNode(text));
	return (strong);
} //end makeStrong

//AJAX Core
function HttpClient(method,async,xmlhttp,result) {
	this.requestType = method;
	this.isAsync = async;
	this.xmlhttp = xmlhttp;
	this.requestResult = result;
} //end HttpClient
HttpClient.prototype = {
	callback:false,
	onSend:false,
	onLoad:false,
	onError:function(error) {
		alert(error.message);
	}/*onError:function(error)*/,
	init:function() {
		try {
			this.xmlhttp = new XMLHttpRequest();
		} catch (e) {
			var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP');
			var success = false;
			for (var i = 0; i < XMLHTTP_IDS.length && !success; i++) {
				try {
					this.xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
					success = true;
				} catch (e) {}
			} //end var i = 0; i < XMLHTTP_IDS.length && !success; i++ FOR
			if (!success) {
				throw new Error('Unable to create XMLHttpRequest.');
			} //end !success IF
		} //end try/catch
	} /*end init:function()*/,
	makeRequest:function(url,payload) {
		if (!this.xmlhttp) {
			this.init();
		} //end !this.xmlhttp IF
		this.xmlhttp.open(this.requestType,url,this.isAsync);
		var self = this;
		if (this.callback != false) {
			this.xmlhttp.onreadystatechange = function() {self._readyStateChangeCallback();}
		} //end this.callback != false IF
		if (this.requestType == "POST") {
			this.xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
		} //end this.requestType == "POST" IF
		this.xmlhttp.send(payload);
		if (!this.isAsync) {
			return this.xmlhttp.responseText;
		} //end !this.isAsync IF
	} /*end makeRequest:function(url,payload)*/,
	_readyStateChangeCallback:function() {
		switch(this.xmlhttp.readyState) {
			case 2:
				if (this.onSend != false) {
					this.onSend();
				} //end this.onSend != false IF
			break;
			case 4:
				if (this.onLoad != false) {
					this.onLoad();
				} //end this.onLoad != false IF
				if (this.xmlhttp.status == 200) {
					if (this.requestResult == 'XML') {
						this.callback(this.xmlhttp.responseXML);
					} else {
						this.callback(this.xmlhttp.responseText);
					} //end this.requestResult == 'XML' IF
				} else {
					this.onError(new Error('HTTP Error Making Request: ['+this.xmlhttp.status+'] '+this.xmlhttp.statusText));
				} //end this.xmlhttp.status == 200 IF
			break;
		} //end this.xmlhttp.readyState SWITCH
	} //end _readyStateChangeCallback:function()
} //end HttpClient.prototype

//Browser Core
function updateBrowser(display,object) {
	var http = new HttpClient("POST",true,false,"XML");
	http.callback = function(xmlDoc) {
		if (xmlDoc.getElementsByTagName("video").length > 0) {
			//Clear browser
			var target = document.getElementById("video_browser");
			emptyElement(target);
			
			//Place the videos on the page
			var videos = xmlDoc.getElementsByTagName("video");
			var tr = document.createElement("tr");
			for (i = 0; i < videos.length; i++) {
				//Build cell for this video
				var td = document.createElement("td");
				td.setAttribute("class","video_browser_thumbs");
				
				//Build thumbnail
				var a = document.createElement("a");
				a.setAttribute("href","?id="+videos[i].getAttribute("id"));
				var img = document.createElement("img");
				img.setAttribute("src","videos/thumbs/medium/"+videos[i].getAttribute("id")+".jpg");
				img.setAttribute("title",videos[i].getAttribute("title"));
				img.setAttribute("border","0");
				img.setAttribute("class","thumb_bottom_padding");
				a.appendChild(img);
				td.appendChild(a);
				td.appendChild(document.createElement("br"));
				
				//Build title
				var a = document.createElement("a");
				a.setAttribute("href","?id="+videos[i].getAttribute("id"));
				var strong = document.createElement("strong");
				strong.appendChild(document.createTextNode(videos[i].getAttribute("title")));
				a.appendChild(strong);
				td.appendChild(a);
				td.appendChild(document.createElement("br"));
				
				//Build rating
				var rating = videos[i].getAttribute("rating");
				for (j = 1; j <= rating; j++) {
					var img = document.createElement("img");
					img.setAttribute("src","/i/blue_star_on.jpg");
					img.setAttribute("alt","");
					img.setAttribute("style","padding-top:5px;");
					td.appendChild(img);
				} //end j = 1; j <= rating; j++ FOR
				while (j <= 5) {
					var img = document.createElement("img");
					img.setAttribute("src","/i/blue_star_off.jpg");
					img.setAttribute("alt","");
					img.setAttribute("style","padding-top:5px;");
					td.appendChild(img);
					j++;
				} //end j <= 5 WHILE
				
				//Attach cell
				tr.appendChild(td);
				
				//Start a new row after every 4 videos
				if (i % 4 == 3) {
					target.appendChild(tr);
					var tr = document.createElement("tr");
				} //end i % 4 == 3 IF
			} //end i = 0; i < videos.length; i++ FOR
			while (i % 4 < 3) {
				var td = document.createElement("td");
				td.appendChild(document.createTextNode(" "));
				tr.appendChild(td);
				i++;
			} //end i % 4 < 3 WHILE
			target.appendChild(tr);
			
			//Style hacks for crappy IE
			if (document.all) {
				var tds = target.getElementsByTagName("td");
				for (i = 0; i < tds.length; i++) {
					tds[i].style.width = "130px";
					tds[i].style.paddingRight = "45px";
					tds[i].style.paddingBottom = "20px";
					tds[i].getElementsByTagName("img")[0].style.paddingBottom = "6px";
				} //end i = 0; i < tds.length; i++ FOR
			} //end document.all IF
		} //end xmlDoc.getElementsByTagName("video").length > 0 IF
	} //end http.callback = function(xmlDoc)
	http.makeRequest("videos/browser.php","display="+display);
	
	//Set style for headers
	var links = object.parentNode.getElementsByTagName("a");
	for (i = 0; i < links.length; i++) {
		links[i].style.fontWeight = "normal";
	} //end i = 0; i < links.length; i++ FOR
	object.style.fontWeight = "bold";
} //end updateBrowser

//Rating Core
function rateVideo() {
	//Hide all previous error messages
	document.getElementById("rate_rating_error").style.display = "none";
	document.getElementById("rate_terms_error").style.display = "none";

	//Determine the rating
	var rating = (document.getElementById("rate_5").checked ? 5 : (document.getElementById("rate_4").checked ? 4 : (document.getElementById("rate_3").checked ? 3 : (document.getElementById("rate_2").checked ? 2 : (document.getElementById("rate_1").checked ? 1 : "error")))));
	
	//Validate user input
	var hasError = false;
	if (rating == "error") {
		document.getElementById("rate_rating_error").style.display = "block";
		hasError = true;
	} //end rating == "error" IF
	if (!document.getElementById("rate_terms").checked) {
		document.getElementById("rate_terms_error").style.display = "block";
		hasError = true;
	} //end !document.getElementById("rate_terms").checked IF
	
	//Send request
	if (!hasError) {
		var http = new HttpClient("POST",true,false,"XML");
		http.callback = function(xmlDoc) {
			hideLightbox();
			updateRating();
			document.getElementById("rating_confirmation").style.display = "block";
		} //end http.callback = function(xmlDoc)
		var payload = "id="+videoID+"&rating="+rating+"&name="+escape(document.getElementById("rate_name").value)+"&city="+escape(document.getElementById("rate_city").value)+"&state="+escape(document.getElementById("rate_state").value)+"&comment="+escape(document.getElementById("rate_comment").value)+(document.getElementById("rate_newsletter").checked ? "&email="+escape(document.getElementById("rate_email").value) : "");
		http.makeRequest("videos/rate.php",payload);
	} //end !hasError IF
} //end rateVideo
function updateRating() {
	var http = new HttpClient("POST",true,false,"XML");
	http.callback = function(xmlDoc) {
		var target = document.getElementById("rating");
		emptyElement(target);
		
		var rating = parseInt(xmlDoc.getElementsByTagName("rating")[0].firstChild.data);
		for (i = 1; i <= rating; i++) {
			var img = document.createElement("img");
			img.setAttribute("src","/i/blue_star_on.jpg");
			img.setAttribute("alt","Star");
			img.setAttribute("title","Click to Rate Video");
			img.setAttribute("class","pointer");
			if (document.all) {
				img.setAttribute("onclick",function(){showLightbox('rate');});
			} else {
				img.setAttribute("onclick","showLightbox('rate');");
			} //end document.all IF
			target.appendChild(img);
		} //end i = 1; i <= rating; i++ FOR
		while (i <= 5) {
			var img = document.createElement("img");
			img.setAttribute("src","/i/blue_star_off.jpg");
			img.setAttribute("alt","Star");
			img.setAttribute("title","Click to Rate Video");
			img.setAttribute("class","pointer");
			if (document.all) {
				img.setAttribute("onclick",function(){showLightbox('rate');});
			} else {
				img.setAttribute("onclick","showLightbox('rate');");
			} //end document.all IF
			target.appendChild(img);
			i++;
		} //end i <= 5 WHILE
	} //end http.callback = function(xmlDoc)
	http.makeRequest("videos/rate.php","action=fetch&id="+videoID);
} //end updateRating

//View Tracking Core
function trackView() {
	var http = new HttpClient("POST",true,false,"XML");
	http.callback = false;
	http.makeRequest("videos/track.php","id="+videoID);
} //end trackView