// JavaScript Document
/*************************************************
	This is hacked version of star rating created by <a href="http://php.scripts.psu.edu/rja171/widgets/rating.php">Ritesh Agrawal</a>
	It thansform a set of radio type input elements to star rating type and remain the radio element name and value,
	so could be integrated with your form. It acts as a normal radio button.
	modified by : Logan Cai (cailongqun[at]yahoo.com.cn)
	website:www.phpletter.com
	


************************************************/
/*
*	convert a set of radio buttons to star rating type of question
*/

jQuery.fn.rating = function(settings) {
	settings = jQuery.extend({
		cancel:'Cancel Rating',
		currentValue:''
	}, settings);			
	var prevElem = null;
	var valueElem = null;
	var container = jQuery(this);
	var CancelElem = null;
	var event = {
		fill: function(el){ // fill to the current mouse position.
			var stars = jQuery(valueElem).siblings('.star');
			var index = stars.index(el) + 1;
			jQuery(valueElem).siblings('.star').children('a').css('width', '100%').end()
			jQuery(valueElem).siblings('.star').slice(0,index).addClass('star_on').end();
			$('#userscore').html(index + '/10');
		},
		drain: function() { // drain all the stars.
			var stars = jQuery(valueElem).siblings('.star');
			stars
				.filter('.star_on').removeClass('star_on').end()
				.filter('.star_hover').removeClass('star_hover').end();
		},
		reset: function(){ // Reset the stars to the default index.
			var stars = jQuery(valueElem).siblings('.star');
			jQuery(valueElem).siblings('.star').slice(0,settings.currentValue).addClass('star_on').end();
		}
	};
	
	return this.each(function(i){
		if(i == 0){//prepend cancel option at the begining
			valueElem = jQuery('<input type="hidden" name="' + this.name + '" value="" >');
			jQuery(this).before(valueElem);
			var CancelElem = jQuery('<span></span>');
			prevElem = CancelElem;
			jQuery(this).before(prevElem);
			jQuery(CancelElem)
				.mouseover(function(){
					event.drain();
					jQuery(this).addClass('star_on');
				})
				.mouseout(function(){
					event.reset();
					jQuery(this).removeClass('star_on');
				});
			// click events.
			jQuery(CancelElem).click(function(){
				settings.currentValue = jQuery(this).children('a').attr('title');
				$(valueElem).val(settings.currentValue);
				event.drain();
				return false;
			});
		}
		//insert rating option right after preview element
		if(settings.currentValue != ''){
			if(i >= settings.currentValue){
				preElemTemp  = jQuery('<div class="star"><a href="javascript:void(0);" title="' + this.value + '">' + this.value + '</a></div>');
			}else{
				preElemTemp  = jQuery('<div class="star star_on"><a href="javascript:void(0);" title="' + this.value + '">' + this.value + '</a></div>');
			}
		}else{
				preElemTemp  = jQuery('<div class="star"><a href="javascript:void(0);" title="' + this.value + '">' + this.value + '</a></div>');
			}
		jQuery(prevElem).after(preElemTemp);
		if($('#video_voted').val() == 0){
			jQuery(preElemTemp)
				.mouseover(function(){
					event.drain();
					event.fill(this);
				})
				.mouseout(function(){
					event.drain();
					event.reset();
				});			
			jQuery(preElemTemp).click(function(){
				//alert(jQuery(this).children('a').attr('title'));
				settings.currentValue = jQuery(this).children('a').attr('title');
				//alert(jQuery(this).children('a').attr('title'));
				jQuery(valueElem).val(settings.currentValue);
				event.drain();
				//event.reset();
				event.fill(this);
				var myurl,myparams;
				try{
					if($('#video_id')){
						myurl = '/dotvrating.php';
						myparams = 'score=' + settings.currentValue + '&video_id=' + $('#video_id').val();
					}
				}catch(e){}
				jQuery.getJSON(myurl, myparams, function(json) {
					$('#medscore').attr("src", "/images/midGscore_" + json.score + ".gif");
					alert(json.alert);
					return false;
					//document.location = document.location;
				});
			});
		}
		prevElem = preElemTemp;
		preElemTemp = null;
		//remove this checkbox
		if(i + 1 == this.length){
			event.reset();
		}
		//console.log(i + ' : ' + $(this).siblings('.star').length);
		$(this).remove();
		//
		
	});
}
