// JavaScript Document

/*

*/
var ajaxRatings = {
	rating : 0,
	ratings : 0,
	ratingBox : 'page_rating_bottom',
	ratingResult : 'page_rating_bottom_result',
	links : [],
	id : 0, 
	type : "article",
	urlToPhp : '/assets/scripts/ratings.php?rating=',
	urlToStar : '/assets/misc/star',
	xmlHttp : null,
	delim : '[__DELIM__]',
	active : false,

	init : function(id, rating, type) {
		this.links = document.getElementById(this.ratingBox).getElementsByTagName("a");
		this.rating = rating;
		this.type = type;
		this.xmlHttp = this.createXmlHttp();
		
		if (this.xmlHttp) {
			var ref = this;
			
			this.active = true;
			this.id = id;
			for (i = 0; i < 10; i++) {
				this.links[i].score = (i + 1);
				
				this.links[i].onmouseover = function() {
					ajaxRatings.showHoverRating(this.score);	
				}
				this.links[i].onmouseout = function() {
					ajaxRatings.hideHoverRating();
				}
				this.links[i].onclick = this.links[i].mousedown = function() {
					ajaxRatings.submitRating(this.score);
					this.blur();
					return false;
				}
				this.links[i].setAttribute("href", "#");
			}
		}
	},
	
	/* thanks Robert Nyman */
	createXmlHttp : function (){
		this.xmlHttp = null;
		if(typeof XMLHttpRequest != "undefined"){
			this.xmlHttp = new XMLHttpRequest();
		}
		else if(typeof window.ActiveXObject != "undefined"){
			try {
				this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");
			}
			catch(e){
				try {
					this.xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
				}
				catch(e){
					try {
						this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch(e){
						this.xmlHttp = null;
					}
				}
			}
		}
		return this.xmlHttp;
	},

	showHoverRating : function (score) {
		for	(i = 0; i < 10; i++) {
			var img = this.links[i].getElementsByTagName("img")[0];	
			var side = "left";
			if ((i+1)%2 == 0) {
				side = "right";
			}
			var type="off";
			if ((i+1) <= score) {
				type = "selected";
			}
			if ((i+1) <= score) {
				type = "on";	
			}
		
			img.src = this.urlToStar + "_" + side + "_" + type + ".png";
		}
	},
	
	hideHoverRating : function() {
		for	(i = 0; i < 10; i++) {
			var img = this.links[i].getElementsByTagName("img")[0];	
		
			var side = "left";
			if ((i+1)%2 == 0) {
				side = "right";
			}
			var type="off";
			if ((i+1) <= this.rating) {
				type = "selected";
			}
		
			img.src = this.urlToStar + "_" + side + "_" + type + ".png";
		}
	},
	
	submitRating : function(rating) {
		if (this.active) {
			var url = this.urlToPhp + rating + "&" + this.type + "=" + this.id;
		
			this.xmlHttp.onreadystatechange = function (){};
			this.xmlHttp.abort();
			this.xmlHttp.open("GET", url, true);
			this.xmlHttp.onreadystatechange = this.getResponse;
			this.xmlHttp.send(null);
			
			alert("submit rating!");
		}
	},
	
	getResponse : function(message) {
		if(ajaxRatings.xmlHttp.readyState == 4 && ajaxRatings.xmlHttp.responseText.length > 0 && ajaxRatings.xmlHttp.responseText != 0){
			ajaxRatings.showResults();
		}
	},
	
	showResults : function() {
		var result = this.xmlHttp.responseText;
		
		var rating = 0;
		var message = "";

		if (result.indexOf(this.delim) >= 0) {
			//split
			results = result.split(this.delim);
			message = results[0];
			this.rating = results[1];
			this.ratings = results[2];
			
			this.clearRating();
		} else {
			message = results[0];	
		}
		
		this.showMessage(message);
	},
	
	showMessage : function(message) {
		var el = document.getElementById(this.ratingResult).innerHTML = message;
	},
	
	clearRating : function() {
		this.active = false;
		
		for (i = 0; i < 10; i++) {
			this.links[i].onmouseover = function() { return false; };
			this.links[i].onmouseout = function() { return false; };
			this.links[i].onclick = this.links[i].mousedown = function() { this.blur(); return false; };
			
			var img = this.links[i].getElementsByTagName("img")[0];	
		
			var side = "left";
			if ((i+1)%2 == 0) {
				side = "right";
			}
			var type="off";
			if ((i+1) <= this.rating) {
				type = "selected";
			}
			
			this.links[i].setAttribute("title", "");
		
			img.src = this.urlToStar + "_" + side + "_" + type + ".png";
		}
		var rating_lang = "ratings";
		if (this.ratings == 1) {
			rating_lang = "rating";
		}
		var val = 'Page rating: ' + (this.rating / 2 ) + '\/5, ' + this.ratings + ' ' + rating_lang;
		document.getElementById(this.ratingBox).setAttribute("title", val);
	},
	
	closeSession : function (){
		delete ajaxRatings;
		ajaxRatings = null;
	},
	
	addEvent : function (elm, evt, func){
		if(elm){
			if(elm.addEventListener){
				elm.addEventListener(evt, func, false);
			}
			else if(window.attachEvent){
				elm.attachEvent(("on" + evt), func)
			}
		}
	}
}

ajaxRatings.addEvent(window, "unload", function(){ajaxRatings.closeSession();});