jQuery.fn.rating = 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
    };
    
	
    if(options) {
       jQuery.extend(settings, options);
    };
   jQuery.extend(settings, {cancel: (settings.maxvalue > 1) ? true : false});
   
   
   var container = jQuery(this);
	
	jQuery.extend(container, {
            averageRating: settings.curvalue,
            url: settings.url,
            username: settings.username
        });
	for(var i= 0; i <= settings.maxvalue ; i++){
		var size = settings.maxvalue;
		 if (i == 0) {
				if(settings.cancel == true){
		             var div = '<div class="cancel"><a href="#0" title="Cancel Rating"></a></div>';
					 container.append(div);
				}
	        } 
			else {
	             var div = '<div class="star"><a href="#'+i+'" title="Give it '+i+'/'+size+'"></a></div>';
				 container.append(div);

	        }
 
		

	}
	
	var stars = jQuery(container).children('.star');
    var cancel = jQuery(container).children('.cancel');
	
    stars
	        .mouseover(function(){
                event.drain();
                event.fill(this);
            })
            .mouseout(function(){
                event.drain();
                event.reset();
            })
            .focus(function(){
                event.drain();
                event.fill(this)
            })
            .blur(function(){
                event.drain();
                event.reset();
            });

    stars.click(function(){
	    if(settings.username){
		if(settings.cancel == true){
			if(settings.curvalue){
			//alert('You have already rated this video');
			//$('#alert').jAlert('You have already rated this video', "fatal");
            //setTimeout(function(){
  			//	$(".flash").fadeOut("slow", function () {
  			//		$(".flash").remove();
      		//	});
			//}, 2000); 
			//exit;	
			$('html, body').animate({scrollTop:0}, 'slow'); //auto scroll to top
			var $messageDiv = $('.flash_error'); // get the reference of the div
			$messageDiv.show().html('You have already rated this video'); // show and set the message
			setTimeout(function(){ $messageDiv.fadeOut("slow", function () {
				  });
			}, 3000);  
			return;
			}
			
			 
            settings.curvalue = stars.index(this) + 1;
            jQuery.post(container.url, {
                "rating": jQuery(this).children('a')[0].href.split('#')[1] 
            });
           
		}
		else if(settings.maxvalue == 1){
			settings.curvalue = (settings.curvalue == 0) ? 1 : 0;
			$(this).toggleClass('on');
			jQuery.post(container.url, {
                "rating": jQuery(this).children('a')[0].href.split('#')[1] 
            });
           
			return false;
		}
		//alert('Thank you for your rating');
		//$('#alert').jAlert('Thank you for your rating', "success");	
		$('html, body').animate({scrollTop:0}, 'slow'); //auto scroll to top
		var $messageDiv = $('.flash_success'); // get the reference of the div
		$messageDiv.show().html('Thank you for your rating'); // show and set the message
		setTimeout(function(){ $messageDiv.fadeOut("slow", function () {
			  });
		}, 3000);
		return true;
	}
	else
	{
	  //alert('Please login in to rate this video');
	  //$('#alert').jAlert('Please login to rate this video', "warning");
	    $('html, body').animate({scrollTop:0}, 'slow'); //auto scroll to top
		var $messageDiv = $('.flash_warning'); // get the reference of the div
		$messageDiv.show().html('Please login to rate this video'); // show and set the message
		setTimeout(function(){ 
			$messageDiv.fadeOut("slow", function() {				
			});
		}, 3000);

	}
			
    });

        // cancel button events
	if(cancel){
        cancel
            .mouseover(function(){
                event.drain();
                jQuery(this).addClass('on')
            })
            .mouseout(function(){
                event.reset();
                jQuery(this).removeClass('on')
            })
            .focus(function(){
                event.drain();
                jQuery(this).addClass('on')
            })
            .blur(function(){
                event.reset();
                jQuery(this).removeClass('on')
            });
        
        // click events.
        cancel.click(function(){
            event.drain();
			settings.curvalue = 0;
            jQuery.post(container.url, {
                "rating": jQuery(this).children('a')[0].href.split('#')[1] 
            });
            return false;
        });
	}
	
	if(!jQuery.fn.lt){
   jQuery.fn.lt=function(n){
       return this.slice(0,n);
    }

 }

        
	var event = {
		fill: function(el){ // fill to the current mouse position.
			var index = stars.index(el) + 1;
			stars
				.children('a').css('width', '100%').end()
				.lt(index).addClass('hover').end();
		},
		drain: function() { // drain all the stars.
			stars
				.filter('.on').removeClass('on').end()
				.filter('.hover').removeClass('hover').end();
		},
		reset: function(){ // Reset the stars to the default index.
			stars.lt(settings.curvalue).addClass('on').end();
		}
		
	}        
	event.reset();
	
	return(this);	

}
