$(document).ready(function() {
	$('.user_rating').each(function(index) {
		var action= $(this).attr('action');
		var user_rating = $(this).html();
		$(this).html('');
		if(user_rating <= 0){
			$(this).ratings('user',5,user_rating).bind('ratingchanged', function(event, data) {
				$.post($(this).attr('action'), {rating: data.rating }, 
					function(response){
						//$("#status").html(response);
					});
				//$(this).parent().parent().find('.user_rating_container').hide('fast');
				//$(this).parent().parent().find('.average_rating_container').fadeIn('fast');
				$('.jquery-ratings-star').each(function(index){
					$(this).unbind('mouseenter');
					$(this).unbind('mouseleave');	
				}
				);
			  });
		}else{
			$(this).ratings('average',5,user_rating);
		}
	  });
	$('.average_rating').each(function(index) {
		var average = $(this).html();
		$(this).html('');
		$(this).ratings('average',5,average);
	  });
	$('.rate_me_button').click(function(){
		if(logged_in){
			$(this).parent().parent().find('.average_rating_container').hide('fast');
			$(this).parent().parent().find('.user_rating_container').fadeIn('fast');
		}else{
			$.post(baseUrl+"accounts/user/"+Math.random(1002),
					function(response){
						if(response.status == '0')
						{
							$("#pop_up_form").html(response.content);
							$( "#pop_up_form" ).dialog({
										autoOpen: true,
										height: 550,
										width: 380,
										modal: true,
							});
						}
			},"json");
		}
	});
});
	jQuery.fn.ratings = function(type,stars, initialRating) {

		  //Save  the jQuery object for later use.
		  var elements = this;

		  //Go through each object in the selector and create a ratings control.
		  return this.each(function() {
		  
		    //Make sure intialRating is set.
		    if(!initialRating)
		      initialRating = 0;
		      
		    //Save the current element for later use.
		    var containerElement = this;
		    
		    //grab the jQuery object for the current container div
		    var container = jQuery(this);
		    
		    //Create an array of stars so they can be referenced again.
		    var starsCollection = Array();
		    
		    //Save the initial rating.
		    containerElement.rating = initialRating;
		    
		    //Set the container div's overflow to auto.  This ensure it will grow to
		    //hold all of its children.
		    container.css('overflow', 'auto');
		    
		    //create each star
		    for(var starIdx = 0; starIdx < stars; starIdx++) {
		      
		      //Create a div to hold the star.
		      var starElement = document.createElement('div');
		      
		      //Get a jQuery object for this star.
		      var star = jQuery(starElement);
		      
		      //Store the rating that represents this star.
		      starElement.rating = starIdx + 1;
		      
		      //Add the style.
		      star.addClass('jquery-ratings-star');
		      
		      //Add the full css class if the star is beneath the initial rating.
		      if(starIdx < initialRating) {
		        star.addClass('jquery-ratings-full');
		      }
		      
		      //add the star to the container
		      container.append(star);
		      starsCollection.push(star);
		      if(type == 'average'){
		    	continue;  
		      }
		      //hook up the click event
		      star.click(function() {
		        //set the containers rating
		        containerElement.rating = this.rating;
		        
		        //When clicked, fire the 'ratingchanged' event handler.  
		        //Pass the rating through as the data argument.
		        elements.triggerHandler("ratingchanged", {rating: this.rating});
		      });
		      
		      star.mouseenter(function() {
		        //Highlight selected stars.
		        for(var index = 0; index < this.rating; index++) {
		          starsCollection[index].addClass('jquery-ratings-full');
		        }
		        //Unhighlight unselected stars.
		        for(var index = this.rating; index < stars; index++) {
		          starsCollection[index].removeClass('jquery-ratings-full');
		        }
		      });
		      
		      container.mouseleave(function() {
		        //Highlight selected stars.
		        for(var index = 0; index < containerElement.rating; index++) {
		          starsCollection[index].addClass('jquery-ratings-full');
		        }
		        //Unhighlight unselected stars.
		        for(var index = containerElement.rating; index < stars ; index++) {
		          starsCollection[index].removeClass('jquery-ratings-full');
		        }
		      });
		    }
		  });
		};
