// JavaScript Document

// VotingStar object constructor
function VotingStar(controller) {
	this.imageElem = null;
}

// activate element, visibility
VotingStar.prototype.activate = function(state) {
	this.imageElem.style.display = (state) ? "inline" : "none";
}

// build star DOM element
VotingStar.prototype.build = function() {
	var starElem = document.createElement("img");
	
	starElem.src = "/image/rating/star_hollow.png";
	starElem.style.display = "none";
	starElem.onmouseover = function () {this.parentNode.voteController.fill(this, true);};
	starElem.onmouseout = function () {this.parentNode.voteController.fill(this, false);};
	starElem.onclick = function () {this.parentNode.voteController.vote(this); };
	
	this.imageElem = starElem;
	return starElem;
}

// toggle star image, filled/hollow
VotingStar.prototype.toggle = function(filled) {
	this.imageElem.src = (filled) ? "/image/rating/star_filled.png" : "/image/rating/star_hollow.png";	
}


// VotingStarController object constructor
function VotingStarController(amount, nCalendarId) {
        this.nCalendarId = -1;
	this.stars = null;	
	this.init(amount, nCalendarId);
}

// initialise controller
VotingStarController.prototype.init = function(amount, nCalendarId) {
	this.stars = new Array(amount);
        this.nCalendarId = nCalendarId;
	for (var i=0; i<amount; i++) {
		this.stars[i] = new VotingStar(this);
	}
}

// build voting element
VotingStarController.prototype.build = function() {
	var divElem = document.createElement("div");
	divElem.voteController = this;
	divElem.style.height = "15px"; 	
	divElem.style.width = (this.stars.length * 16) + "px";	
	divElem.onmouseover = function () {this.voteController.activate(true);};
	divElem.onmouseout = function () {this.voteController.activate(false);};
	
	for(var i=0; i<this.stars.length; i++) {
		divElem.appendChild(this.stars[i].build());
	}

	return divElem;
}

// (de)activate voting functionality
VotingStarController.prototype.activate = function(state) {
	for(var i=0; i<this.stars.length; i++) {
		this.stars[i].activate(state);
	}	
}

// toggle stars, filled/hollow
VotingStarController.prototype.fill = function(srcElem, fill) {
	var go = true;
	for (var  i=0; i<this.stars.length && go; i++) {
		if (srcElem == this.stars[i].imageElem) {
			go = false;			
		}
		this.stars[i].toggle(fill);
	}
}

// perform vote action
VotingStarController.prototype.vote = function(srcElem) {
	var go = true;
	for (var  i=0; i<this.stars.length && go; i++) {
		if (srcElem == this.stars[i].imageElem) {
			location.href = '/calendar/votedirect/cid/' + this.nCalendarId + '/rating/' + (i+1);
		}
	}
}

$(document).ready(function(){
	$('.content-rating').each(function(){
                var $this = $(this);
                var nCalendarId = $this.attr('class').split('calendarid')[1].split(' ')[0];
		var sc = new VotingStarController(5, nCalendarId);
		$this.text('').append(sc.build());
	});
});		  
