
/**
 * jQuery Ajax Rater Plugin modified
 */ 
(function($){
	jQuery.fn.rater = function(url, options)
	{
		if(url == null) return;
		var settings = {
			url       : url, // post changes to 
			maxvalue  : 5,   // max number of stars
			curvalue  : 0,    // number of selected stars
			image: 'star',    // set star image
			setup: 'vote',		// set if read only
			article_id  : 0    		// article id
		};
	
		if(options) {
			$.extend(settings, options);
		};
		
		$.extend(settings, {cancel: (settings.maxvalue > 1) ? true : false});
		
		var container = $(this);
		$.extend(container, { averageRating: settings.curvalue, url: settings.url });

		if(!settings.style || settings.style == null || settings.style == 'basic') {
			var raterwidth = settings.maxvalue * 25;
			var ratingparent = '<ul class="star-'+settings.image+'" style="display: block; width:'+raterwidth+'px">';
		}
		if(settings.style == 'small') {
			var raterwidth = settings.maxvalue * 10;
			var ratingparent = '<ul class="star-'+settings.image+' small-star" style="width:'+raterwidth+'px">';
		}
		if(settings.style == 'inline') {
			var raterwidth = settings.maxvalue * 10;
			var ratingparent = '<span class="inline-rating"><ul class="star-'+settings.image+' small-star" style="width:'+raterwidth+'px">';
		}
		container.append(ratingparent);
	
		// create rater
		var starWidth, starIndex, listitems = '';
		var curvalueWidth = Math.floor(100 / settings.maxvalue * settings.curvalue);
		for(var i = 0; i <= settings.maxvalue ; i++) {
			if (i == 0) {
				listitems+='<li class="current-rating" style="width:'+curvalueWidth+'%;">'+settings.curvalue+'/'+settings.maxvalue+'</li>';
			} else {
				starWidth = Math.floor(100 / settings.maxvalue * i);
				starIndex = (settings.maxvalue - i) + 2;
				listitems+='<li class="star"><a href="#'+i+'" title="'+i+'/'+settings.maxvalue +'" style="width:'+starWidth+'%;z-index:'+starIndex+'">'+i+'</a></li>';
			}
		}
		container.find('.star-'+settings.image+'').append(listitems); // i am using find here, because the span wrapped in the small style would break children()

		if(settings.maxvalue > 1) { // add a container for the ajax result
			container.append('<div class="star-rating-result" style="display: none"></div>');
		}
		var stars = $(container).find('.star-'+settings.image+'').children('.star');
		stars.click(function() {
			
			$.setupFunction = function(name) {
				$('img#cd_ajaxvote_loading').slideDown(1000);
				
				ajax_timeout = setTimeout(function() {
							$.ajax({
								type: 'POST',
								url: container.url,
								data: 'plgCdAjaxvoteAction=' + name + '',
								success: function(msg) {
									$('div#cd_ajaxvote_status').html(msg).show(1000);
									$('img#cd_ajaxvote_loading').hide(1000);
									t = setTimeout( function () {
										$('div#cd_ajaxvote_status').hide(1000);
									}, 2000);
								}
							});
						}, 2000);
			}
			
				switch(settings.setup) {
					case 'login_first':
						$.setupFunction('login_first');
						break;
					case 'intro_only':
						$.setupFunction('intro_only');										
						break;
					
					case 'vote':
					
						settings.curvalue = stars.index(this) + 1;
						raterValue = $(this).children('a')[0].href.split('#')[1];
						
						$('img#cd_ajaxvote_loading').slideDown(1000);
						ajax_timeout = setTimeout(function() {
							$.ajax({
								type: 'GET',
								url: container.url,
								async: false,
								data: 'type=rating&rating=' + raterValue + '&beitrag=' + settings.article_id,
								success: function(msg) {
									$('div#cd_ajaxvote_status' ).html(msg).show(1000);
									$('img#cd_ajaxvote_loading').hide(1000);
									t = setTimeout( function () {
										$('div#cd_ajaxvote_status').hide(1000);
									}, 2000);
					
									if ($('div#cd_ajaxvote_status_message').hasClass('cd_ajaxvote_status_success')) {
									} else {	
										// nothing
									}
								}
							});
						}, 2000);
						break;
					default:
						return false;
						break;
				}
			return false;
		});
		return this; // strict warning: anonymous function does not always return a value. fix?
	}
})(jQuery);