/*
 ### jQuery Star Rating Plugin v2.61 - 2009-01-23 ###
 * Home: http://www.fyneworks.com/jquery/star-rating/
 * Code: http://code.google.com/p/jquery-star-rating-plugin/
 *
	* Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 ###
*//*
	Based on http://www.phpletter.com/Demo/Jquery-Star-Rating-Plugin/
 Original comments:
	This is hacked version of star rating created by <a href="http://php.scripts.psu.edu/rja171/widgets/rating.php">Ritesh Agrawal</a>
	It transforms 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)
*/

/*# AVOID COLLISIONS #*/
;if(window.jQuery) (function($){
/*# AVOID COLLISIONS #*/
	
	// IE6 Background Image Fix
	if ($.browser.msie) try { document.execCommand("BackgroundImageCache", false, true)} catch(e) { }
	// Thanks to http://www.visualjquery.com/rating/rating_redux.html
	
	// default settings
	$.rating = {
		cancel: 'Cancel Rating',   // advisory title for the 'cancel' link
		cancelValue: '',           // value to submit when user click the 'cancel' link
		split: 0,                  // split the star into how many parts?
		
		// Width of star image in case the plugin can't work it out. This can happen if
		// the jQuery.dimensions plugin is not available OR the image is hidden at installation
		starWidth: 16,
		
		//NB.: These don't need to be defined (can be undefined/null) so let's save some code!
		//half:     false,         // just a shortcut to settings.split = 2
		//required: false,         // disables the 'cancel' button so user can only select one of the specified values
		//readOnly: false,         // disable rating plugin interaction/ values cannot be changed
		//focus:    function(){},  // executed when stars are focused
		//blur:     function(){},  // executed when stars are focused
		//callback: function(){},  // executed when a star is clicked
		
		// required properties:
		groups: {},// allows multiple star ratings on one page
		event: {// plugin event handlers
			fill: function(n, el, settings, state){ // fill to the current mouse position.
				//if(window.console) console.log(['fill', $(el), $(el).prevAll('.star_group_'+n), arguments]);
				this.drain(n);
				$(el).prevAll('.star_group_'+n).andSelf().addClass('star_'+(state || 'hover'));
				// focus handler, as requested by focusdigital.co.uk
				var lnk = $(el).children('a'); val = lnk.text();
				if(settings.focus) settings.focus.apply($.rating.groups[n].valueElem[0], [val, lnk[0]]);
			},
			drain: function(n, el, settings) { // drain all the stars.
				//if(window.console) console.log(['drain', $(el), $(el).prevAll('.star_group_'+n), arguments]);
				$.rating.groups[n].valueElem.siblings('.star_group_'+n).removeClass('star_on').removeClass('star_hover');
			},
			reset: function(n, el, settings){ // Reset the stars to the default index.
				if(!$($.rating.groups[n].current).is('.cancel'))
					$($.rating.groups[n].current).prevAll('.star_group_'+n).andSelf().addClass('star_on');
				// blur handler, as requested by focusdigital.co.uk
				var lnk = $(el).children('a'); val = lnk.text();
				if(settings.blur) settings.blur.apply($.rating.groups[n].valueElem[0], [val, lnk[0]]);
			},
			click: function(n, el, settings){ // Selected a star or cancelled
				$.rating.groups[n].current = el;
				var lnk = $(el).children('a'); val = lnk.text();
				// Set value
				$.rating.groups[n].valueElem.val(val);
				// Update display
				$.rating.event.drain(n, el, settings);
				$.rating.event.reset(n, el, settings);
				// click callback, as requested here: http://plugins.jquery.com/node/1655
				if(settings.callback) settings.callback.apply($.rating.groups[n].valueElem[0], [val, lnk[0]]);
			}      
		}// plugin events
	};
	
	$.fn.rating = function(instanceSettings){		
		if(this.length==0) return this; // quick fail
		
		instanceSettings = $.extend(
			{}/* new object */,
			$.rating/* global settings */,
			instanceSettings || {} /* just-in-time settings */
		);
		
		// loop through each matched element
		this.each(function(i) {
			var thisParent = $(this).parent();			
			////if(window.console) console.log([this.name, instanceSettings.half, instanceSettings.split], '#');
			
			// Generate internal control ID
			// - ignore square brackets in element names
			var n = (this.name || 'unnamed-rating').replace(/\[|\]+/g, "_");
   
			// Grouping
			if(!$.rating.groups[n]) $.rating.groups[n] = {count: 0};
			i = $.rating.groups[n].count; $.rating.groups[n].count++;
			
			// Accept readOnly setting from 'disabled' property
			$.rating.groups[n].readOnly = $.rating.groups[n].readOnly || instanceSettings.readOnly || $(this).attr('disabled');
			
			// Things to do with the first element...
			if(i == 0){
				// Create value element (disabled if readOnly)
				$.rating.groups[n].valueElem = $('<input type="hidden" name="' + n + '" value=""' + (instanceSettings.readOnly ? ' disabled="disabled"' : '') + '/>');
				// Insert value element into form
				//$(this).before($.rating.groups[n].valueElem);
				thisParent.append($.rating.groups[n].valueElem);
				
				if($.rating.groups[n].readOnly || instanceSettings.required){
					// DO NOT display 'cancel' button
				}
				else{
					// Display 'cancel' button
					thisParent.append(
						$('<div class="cancel"><a title="' + instanceSettings.cancel + '">' + instanceSettings.cancelValue + '</a></div>')
						.mouseover(function(){ $.rating.event.drain(n, this, instanceSettings); $(this).addClass('star_on'); })
						.mouseout(function(){ $.rating.event.reset(n, this, instanceSettings); $(this).removeClass('star_on'); })
						.click(function(){ $.rating.event.click(n, this, instanceSettings); })
					);
				}
			}; // if (i == 0) (first element)
			
			// insert rating option right after preview element
			eStar = $('<div class="star"><a title="' + (this.title || this.value) + '">' + this.value + '</a></div>');
			thisParent.append(eStar);
			
			// Prepare division instanceSettings
			if(typeof instanceSettings.split=='number' && instanceSettings.split>0){
				var stw = instanceSettings.starWidth;
				var spi = (i % instanceSettings.split), spw = Math.floor(stw/instanceSettings.split);
				 
				eStar
				// restrict star's width and hide overflow (already in CSS)
				.width(spw)
				// move the star left by using a negative margin
				// this is work-around to IE's stupid box model (position:relative doesn't work)
				.find('a').css({ 'margin-left':'-'+ (spi*spw) +'px' })
			};
			
			// Remember group name so controls within the same container don't get mixed up
			eStar.addClass('star_group_'+n);
			
			// readOnly?
			if($.rating.groups[n].readOnly)//{ //save a byte!
				// Mark star as readOnly so user can customize display
				eStar.addClass('star_readonly');
			//}  //save a byte!
			else//{ //save a byte!
				eStar
				// Enable hover css effects
				.addClass('star_live')
				// Attach mouse events
				.mouseover(function(){ $.rating.event.drain(n, this, instanceSettings); $.rating.event.fill(n, this, instanceSettings, 'hover'); })
				.mouseout(function(){ $.rating.event.drain(n, this, instanceSettings); $.rating.event.reset(n, this, instanceSettings); })
				.click(function(){ $.rating.event.click(n, this, instanceSettings); });
			//}; //save a byte!
			
			////if(window.console) console.log(['###', n, this.checked, $.rating.groups[n].initial]);
			if(this.checked) $.rating.groups[n].current = eStar;
			
			//remove this checkbox
			//$(this).remove();
			
			// reset display if last element
			if(i + 1 == this.length) $.rating.event.reset(n, this, instanceSettings);
		
		}); // each element
			
		// initialize groups...
		for(n in $.rating.groups)//{ not needed, save a byte!
			(function(c, v, n){ if(!c) return;
				$.rating.event.fill(n, c, instanceSettings || {}, 'on');
				$(v).val($(c).children('a').text());
			})
			($.rating.groups[n].current, $.rating.groups[n].valueElem, n);
		//}; not needed, save a byte!
		
		return this; // don't break the chain...
	};
	
	/*
		### Default implementation ###
		The plugin will attach itself to file inputs
		with the class 'multi' when the page loads
	*/
	$(function(){ $('input[type=radio].star').rating(); });	
/*# AVOID COLLISIONS #*/
})(jQuery);
/*# AVOID COLLISIONS #*/


/*
 * Init rating..
 */
$(function() {
	$('.item-star').rating({
		split: 2,
		width: 16,
		callback: function(value, link){
			var parentNode = $(this).parent();
			var itemId = parentNode.find('[name=item_id]').get(0).value
			var rateContainer = $('.rateId-'+itemId);
			
			if(typeof(recBar) != 'undefined') {
				recBar.getInfo(itemId, value);
			}
									
			$.getJSON("/vote/set/item_id/" + itemId + "/value/" + value+ "/doCount/1",
			   	function(data){
					if(data.item_id){
						if(data.average_rating > 0) {
							$('.rating-meta-' + itemId).css('display', 'block');
						}
						else {						
							$('.rating-meta-' + itemId).css('display', 'none');
						}
						
						var avg =	Math.round(data.average_rating*5)/100;
											
						$('.rating-meta-' + itemId).find('.average_rating').html(avg);
						$('.rating-meta-' + itemId).find('.votes_count').html(data.votes_count);
						
						var loveOverlay = $('.item_' + itemId).find('.overlay-love');
						var hateOverlay = $('.item_' + itemId).find('.overlay-hate');
						
						loveOverlay.hide();
						hateOverlay.hide();
						
						if(value == 100) {
							loveOverlay.fadeIn('fast');
						}
						if(value == 10) {
							hateOverlay.fadeIn('fast');
						}
						
						if(data.notLogged){
							if(typeof(checkLazySingin) == 'function') {
								checkLazySingin()
							}
						}
			    	}
			   	}
			);
		} 
	});
	
	$("#lazySingInLink").fancybox({
		'zoomSpeedIn': 0, 
		'zoomSpeedOut': 0, 
		'frameWidth' : 350,
		'frameHeight' : 180,
		'overlayShow': true,
		'overlayOpacity' : .4
		
	});
});

/*
 * Init dynamic rating..
 */
function randint()
{
	return Math.floor(Math.random()*1000000000)
}
function buildRatingWidget(itemId,value)
{
	rand=randint()
	ratingContainer=$('<div class="rating_container_fix">')
	ratingContainer.append($('<input type="hidden" name="item_id" value="'+itemId+'"/>'))
	for(rating_val=10;rating_val<=100;rating_val=rating_val+10)
	{
		input=$('<input name="value_'+itemId+'_'+rand+'" type="radio" class="item-star " value="'+rating_val+'"/>')
		if (rating_val<=20)title="I hate it"
		else if (rating_val<=40)title="I don't like it"
		else if (rating_val<=60)title="It's ok"
		else if (rating_val<=80)title="I like it"
		else if (rating_val<=100)title="I love it"
		
		input.attr('title',title)
		if(rating_val==value)
			input.attr('checked',true)
		ratingContainer.append(input)
	}
	
	
	ratingContainer2=$('<div class="rating_container rateId-'+itemId+'-'+rand+'"></div>');
	ratingContainer2.append(ratingContainer)
	ratingWidget=$('<div id="ratingWidget-'+itemId+'"></div>')
	ratingWidget.append(ratingContainer2)
	return ratingWidget
}