/**
 * Object for collecting ratings data on an item.
 * 
 * @author Kirk R. Gordon
 * @copyright 2008 World Wrestling Entertainment. All Rights Reserved.
 */
WWERatings = function() {
	
	var me = this;
	
	// ** holds the cookie pertaining to items rated
	var ratingsData = 'undefined';
	
	// ** name of the cookie to set on initilization
	var defaultCookieName = '.videoRated';
	
	// ** URL to the ratings recording app...
	var url = 'http://gen.proxy.wwe.com/rating/index.php?Str1=';
	
	var currentObjectId;
	
	var currentObjectRatings;
		
	// ** initilize our cookie
	me.initilize = function(name) {
		if (typeof(name) == 'undefined') {
			 name = defaultCookieName; 
		}
		
		ratingsData = new WWE.Cookie(document, name, null, '/', host);
		if (!ratingsData.load()) { 
			ratingsData.store();
		}
		
		$('ratings').childElements().each(function(n, c) {
			c++;
			$(n).observe('mouseover', function() {WWERatings.respondToMover(n)}.bind(n)); 
			$(n).observe('mouseout', function() {WWERatings.respondToMout(n)}.bind(n));
			$(n).observe('click', function() { WWERatings.vote(c)}.bind(c));
			/* $(n).innerHTML = '<a ' + 'id="star' + c + '" href="javascript:void();">'; */
		});		
	}
	
	me.setCurrentObjectId = function(id) {
		currentObjectId = id;
	}
	
	me.setCurrenObjectRatings = function(rating) {
		currentObjectRating = rating;
	}
	
	// ** set cookie to track the video a user voted on...
	me.set = function(value) {
		var tempObjectIds = ratingsData.objectIds;
		var delimiter = '';
		if (typeof(value) != 'undefined') {
			if (typeof(tempObjectIds) != 'undefined') {
				delimiter = '-';
			}
			ratingsData.objectIds = tempObjectIds + delimiter + value;
			ratingsData.store(); 
		}
	}
	
	// ** return an array of object ids a user voted on...
	me.get = function() {
		var ids = new Array();
		if (typeof(ratingsData.objectIds) != 'undefined') {
			ids = ratingsData.objectIds.split('-');
		}
		return ids;
	}
	
	// ** returns true if the the object id exists... false otherwise...
	me.check = function(index) {
		var ids = WWERatings.get();
		var idx;
		for (var idx in ids) {
			if (ids[idx] == index) {
				return true;
			}
		}
		return false;
	}
	
	/* watch the stars */
	var starsMax;
	var holder;
	var preSet;
	var rated;
	
	me.respondToClick = function(theID) {

	}
	
	// ** highlight stars
	me.respondToMover = function(ele) {
		var id = ele.id.replace("r", ''); 
		if (id > 0) {
			for (var i=1; i <= id; i++) {
				$('r'+i).addClassName('on');
			}
		} 
	}
	
	// ** dim stars
	me.respondToMout = function() {
		for (var i=5; i > 0 ; i--) {
			$('r'+i).removeClassName('on');
		}
	}
	
	// ** cast a user's vote
	me.vote = function(vote) {
		var objectId = currentObjectId;
		if (!WWERatings.check(objectId) && typeof(objectId) != 'undefined') {
			var voteUrl = url + objectId + '&Str2=' + vote;
			new Image().src = voteUrl;
			WWERatings.set(objectId);
		}
	}
	
	// ** records a view
	me.view = function() {
		var objectId = currentObjectId;
		if (typeof(objectId) != 'undefined') {
			var viewUrl = url + objectId;
			new Image().src = viewUrl;
		}
	}
}

var WWERatings = new WWERatings();
