	/* TIMEINC namespace initializer */
	if (typeof TIMEINC != 'object' || !TIMEINC) { window.TI = window.TIMEINC = {}; }
	if (typeof TIMEINC.COMMUNITY != 'object' || !TIMEINC.COMMUNITY) { window.TIC = TIMEINC.COMMUNITY = {}; }
	if (typeof TIMEINC.COMMUNITY.RATINGS != 'object' || !TIMEINC.COMMUNITY.RATINGS) { /* class definition */
		window.TICR = TIMEINC.COMMUNITY.RATINGS = function (ob) {
			
			this.obj = ob;
			this.rating = 0;
			this.vote_total = 0;
			this.vote_count = 0;
			this.source = $(this.obj).attr('action');
			this.voteCountElement = $(this.obj).find('span.num');
			this.phraseElement = $('<span class="phrase"></span>');
			$(this.obj).append(this.phraseElement);
			this.temp = '';
			this.thanks = 'Thank you for voting';
		};
	}

	TIMEINC.COMMUNITY.RATINGS.prototype.parseTwo = function(i)
	{
		return parseInt(i * 100) / 100;
	}


	TIMEINC.COMMUNITY.RATINGS.prototype.parseRating = function(r) 
	{
		return  parseInt(r * 2) / 2;
	}

	TIMEINC.COMMUNITY.RATINGS.prototype.initForm = function() 
	{ 
		this.jsonLoad();
		var put = $('<input type="hidden" name="_method" value="put" />');
		put.appendTo($(this.obj).children('label:last'));
		$(this.obj).attr({
			"method": "post",
			"onsubmit": "this.submit();"
		});
		$(this.obj).children('span').children('div.rating-cancel').remove();
	}

	TIMEINC.COMMUNITY.RATINGS.prototype.jsonLoad = function() {
		var ticr = this;
		var escapedLocation = encodeURIComponent(location.href);
		var jsonpUrl = this.escapeSpace(ticr.source) + '&ratable_item[url]=' + escapedLocation + '&ratable_item[resource_id]=' + this.extractNumber(ticr.source) + '&callback=?';
		$.ajax({
			type: "GET",
			url: jsonpUrl,
			dataType: "jsonp",
			success: function(data){
				ticr.rating = data.ratable_item.rating;
				ticr.vote_count = data.ratable_item.vote_count;
				ticr.vote_total = data.ratable_item.vote_total; 
			},
			complete: function(msg) {
				ticr.setRating();
			 	ticr.setSelect();
			}
		});	   
	}
	
	TIMEINC.COMMUNITY.RATINGS.prototype.setSelect = function()
	{
		var ticr = this;
		var divs = $(this.obj).children('span').children('div');
		var tempsrc = '';
		var response = '';
		
		$(divs).click(function() {
			$(divs).unbind('click');
			$(divs).unbind('mouseover');
			$(divs).unbind('mouseout');
			
			$(ticr.obj).submit();
                        						
			var idx = $(this).parent().find('> *').index(this);
			var newValue =  $(ticr.obj).children('input:eq('+idx+')').attr('value');
			$(ticr.phraseElement).html(ticr.thanks); 

			ticr.vote_total = ticr.vote_total +  parseFloat(newValue);
			ticr.vote_count = ticr.vote_count + 1;
			ticr.rating =  ticr.vote_total / ticr.vote_count;
			
			var t = setTimeout(function() {
				ticr.setRating(t);						
			}, 1000);			
		});

		$(divs).hover(
			function () {
				var idx = $(this).parent().find('> *').index(this);
				ticr.temp = $(ticr.phraseElement).html();
				$(ticr.phraseElement).html($(ticr.obj).children('label:eq('+idx+')').text());
			}, 
			function () {
				$(ticr.phraseElement).html(ticr.temp);
			}
		);
	}
	
	TIMEINC.COMMUNITY.RATINGS.prototype.setRating = function(t) 
	{	
		$(this.voteCountElement).html(this.vote_count);
		if (parseInt(this.vote_count) > 0) {
			$(this.phraseElement).html($(this.obj).children('input[value=' + this.parseRating(this.rating) + ']').next().text());
		} else {
			$(this.phraseElement).html('Be the First to Rate It');
		}
		$(this.obj).children('input').rating('select', '' + this.parseRating(this.rating));
		clearTimeout(t);
 	}
	
	TIMEINC.COMMUNITY.RATINGS.prototype.disable = function() 
	{
		$(this.obj).children('input').rating('disable')
	}

	TIMEINC.COMMUNITY.RATINGS.prototype.enable = function()
	{ 
		$(this.obj).children('input').rating('enable');
	}

	TIMEINC.COMMUNITY.RATINGS.prototype.escapeSpace = function(str) {
		str = str.replace(' ', '%20');
		return str;
	}

	TIMEINC.COMMUNITY.RATINGS.prototype.extractNumber = function(str) 
	{
		var b = this.source.indexOf('ratable_items');
		var e = this.source.indexOf('?ratable_item');
		if (b != -1 && e != -1) { 
			return this.source.substring(b+14,e);
		} else {
			return "-1";	
		}
	}

	$(document).ready( function() {
		var myForms = new Array();
		TIMEINC.IFRAMEPOST.init();
		$('form.ratings_container').each(function (i) {
			myForms[i] = new TICR(this);	 
			myForms[i].initForm();
			TIMEINC.IFRAMEPOST.setForm(this);
		});		
	});