/* Rating */

var videoId;
var naturalRating;
var ratingNormalActive = new Image();
var ratingNormalInactive = new Image();
var ratingHoverActive = new Image();
var ratingHoverInactive = new Image();
var xhr = new XHR();

ratingNormalActive.src = "/images/star1.gif";
ratingNormalInactive.src = "/images/star0.gif";
ratingHoverActive.src = "/images/starvs.gif";
ratingHoverInactive.src = "/images/starv.gif";

function hoverRating (index) {
	var elem, imgSrc;
	for (var i = 1; i <= 5; i++) {
		elem = document.getElementById("star_" + i);
		if (i <= index) {
			imgSrc = ratingHoverActive.src;
		} else {
			imgSrc = ratingHoverInactive.src;
		}
		elem.setAttribute("src", imgSrc);
	}
	document.getElementById("star_" + index).style.cursor = "pointer";
}

function unHoverRating(index) {
	var elem, imgSrc;
	for (var i = 1; i <= 5; i++) {
		elem = document.getElementById("star_" + i);
		imgSrc = naturalRating >= i 
			? ratingNormalActive.src 
			: ratingNormalInactive.src;
		elem.setAttribute("src", imgSrc);
	}
	document.getElementById("star_" + index).style.cursor = "default";
}

function sendRating(rating, that) {
	that.blur();
//	alert("rating: " + rating);
	var url = "/vote/" + videoId + "," + rating + ".ajaxt";
	xhr.open("GET", url, true);
	xhr.onreadystatechange = updateRating;
	xhr.send(null);
}

function updateRating() {
	if (xhr.readyState == 4) {
		var response = xhr.responseText;
		var res = response.split(",");
		var votes = res[1];
		naturalRating = res[2];
		var rating = res[3];
		var elem, imgSrc;
		
		for (var i = 1; i <= 5; i++) {
			elem = document.getElementById("star_" + i);
			elem.setAttribute("onmouseover", null);
			elem.setAttribute("onmouseout", null);
			elem.setAttribute("onclick", null);
			imgSrc = naturalRating >= i 
				? ratingNormalActive.src 
				: ratingNormalInactive.src;
			elem.setAttribute("src", imgSrc);
			
		}
		
		document.getElementById("rating").innerHTML = rating;
		document.getElementById("votes").innerHTML = votes;
	}
}

function XHR() {
	var xmlhttp;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest != "undefined") {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp; 
}

/* Comments */

function showCommentAdd() {
	document.getElementById("commentAddOff").style.display = "block";
	document.getElementById("commentAddOn").style.display = "none";
	document.getElementById("commentAdd").style.display = "block";
}

function hideCommentAdd() {
	document.getElementById("commentAddOn").style.display = "block";
	document.getElementById("commentAddOff").style.display = "none";
	document.getElementById("commentAdd").style.display = "none";
}

function hideCommentAddFx() {
	document.getElementById("commentAdd").style.height = "237px";
	moveDown();
}

function showCommentAddFx() {
	document.getElementById("commentAddOff").style.display = "block";
	document.getElementById("commentAddOn").style.display = "none";

	document.getElementById("commentAdd").style.display = "block";
	document.getElementById("commentAdd").style.height = "0px";
	moveUp();
}

function moveUp() {
	var height = parseInt(document.getElementById("commentAdd").style.height);
	height += 45;
	if (height > 237) {
		height = 237;
	}
	document.getElementById("commentAdd").style.height = height + "px";
	
	if (height < 237) {
		window.setTimeout("moveUp()", 0.5);
	}
}

function moveDown() {
	var height = parseInt(document.getElementById("commentAdd").style.height);
	height -= 45;
	if (height < 0) {
		height = 0;
	}
	document.getElementById("commentAdd").style.height = height + "px";
	
	if (height > 0) {
		window.setTimeout("moveDown()", 5);
	} else {
		document.getElementById("commentAdd").style.display = "none";
		document.getElementById("commentAddOn").style.display = "block";
		document.getElementById("commentAddOff").style.display = "none";
	}
}

var commentFormText = null;
var commentFormName = null;
var commentFormEmail = null;
var commentFormHp = null;
var commentFormSubmit = null;

var xhr_comment = null;

function checkFormular() {
	if (commentFormText == null) {
		commentFormText = document.getElementById("commentText");
		commentFormName = document.getElementById("commentName");
		commentFormEmail = document.getElementById("commentEmail");
		commentFormHp = document.getElementById("commentHomepage");
		commentFormSubmit = document.getElementById("commentSave");
	}

	if (commentFormText.value != ""
			&& commentFormName.value != ""
			&& commentFormEmail.value != ""
			&& isValidEmail(commentFormEmail.value)) {
		commentFormSubmit.removeAttribute("disabled");
		commentFormSubmit.className = "active";
	} else {
		commentFormSubmit.setAttribute("disabled", "disabled");
		commentFormSubmit.className = "inactive";
	}
}

function checkEmail(that) {
	if (!isValidEmail(that.value)) {
		that.style.backgroundColor = "#FFD4D2";
	} else {
		that.style.backgroundColor = "#FFF";
	}
}

function isValidEmail(email) {
    return !(email.search(/^([a-z0-9_\-\.])+@([a-z0-9\-]\.?)+(\.[a-z]{2,4})$/i) == -1);
}

function saveComment() {
	xhr_comment = new XHR();
	var url = "/commentadd.ajaxt";
	xhr_comment.open("POST", url, true);
	xhr_comment.onreadystatechange = displayComment;
	xhr_comment.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xhr_comment.send("video_id=" + videoId + 
		"&name=" + commentFormName.value + 
		"&email=" + commentFormEmail.value + 
		"&homepage=" + commentFormHp.value + 
		"&text=" + commentFormText.value);
	
	document.getElementById("commentAddOverlay").style.display = "block";
}

function displayComment() {
	if (xhr_comment.readyState == 4) {
		var response = xhr_comment.responseText;

		if (response == "0") {
			alert("error adding the comment");
		}

		// adding comment
		if (response == "1") {
			var commentContainer = document.getElementById("comments");
			var divider = document.getElementById("divider");
			
			var commentHead = document.createElement("div");
			commentHead.className = "commentHead";
			
			var userName = document.createElement("strong");
			userName.innerHTML = commentFormName.value;
			
			// with hp
			if (commentFormHp.value != "" && commentFormHp.value != "http://") {
				var link = document.createElement("a");
				link.href = commentFormHp.value;
				link.appendChild(userName);
				commentHead.appendChild(link);
			}

			// no hp
			else {
				commentHead.appendChild(userName);
			}

			// date
			var d = new Date();
			var dtString = d.getFullYear() + "-" + d.getMonth() + "-" + d.getDate() +
				" " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
			
			var date = document.createElement("small");
			date.innerHTML = dtString;
			commentHead.appendChild(date);

			// text
			var textValue = commentFormText.value;
			textValue = textValue.replace(/(\r\n)|(\n\r)|\r|\n/g, "<br/>");
			var commentText = document.createElement("p");
			commentText.className = "commentText";
			commentText.innerHTML = textValue;

			if (divider.nextSibling) {
				commentContainer.insertBefore(commentText, divider.nextSibling);
			} else {
				commentContainer.appendChild(commentText);
			}

			commentContainer.insertBefore(commentHead, commentText);
		
			document.getElementById("commentForm").reset();
			commentFormSubmit.setAttribute("disabled", "disabled");
			commentFormSubmit.className = "inactive";
		}
		document.getElementById("commentAddOverlay").style.display = "none";
		hideCommentAdd();
	}
}
