/**
 * @package WordPress
 * @subpackage SmashingMagazine_Theme
 * @maintainer Robin Schulz <robin.schulz@smashing-media.com>
 * @version 1.1.0
 */

var ajaxurl = 'http://www.smashingmagazine.com/wp-admin/admin-ajax.php';

jQuery.fn.jRating = function(options){
    var opts = jQuery.extend({
		msgOut:1500
	},options);
	var ratedyet = false;
	jQuery.jRating={
		/***
		 * Replace via CSS rating buttons on mouseover/mouseout
		 * @param {object} el
		 * @param {integer} hstate
		 * @param {string} hstate
		 */
		hRate: function(el,hstate,nstate)
		{
			if(hstate) {
				jQuery(el).removeClass(nstate+'_active').addClass(nstate);
			}else{
				jQuery(el).removeClass(nstate).addClass(nstate+'_active');
			}
		},
		/***
		 * Fadein/Fadeout the commentrating-div on mouseover/mouseout
		 * @param {integer} cid
		 * @param {integer} state
		 */
		hParent: function(cid,state)
		{
			var div = jQuery('#comment-'+cid+' .commentrating');
			if(!div.hasClass('rated'))
			{
				if(!div.hasClass('active-'+cid))
				{
					div.children('.ratebtn').stop().animate({opacity:state?1:0.3},250);
				}
			}
		},
		/***
		 * Rate up/down comments based on Comment ID
		 * @param {object} el
		 * @param {integer} cid
		 * @param {integer} rate_up
		 * @param {integer} rate_down
		 */
		rate: function(el,cid,rate_up,rate_down)
		{
			if (!ratedyet) {
				ratedyet = true;
			jQuery(el).removeClass().addClass('indicator');
			var div = jQuery('#comment-'+cid+' .commentrating').addClass('active-'+cid);//.css('opacity',1);
				jQuery.post(ajaxurl, {
					action: 'process_cmtratings_json',
					cid: cid,
					rate_down: rate_down,
					rate_up: rate_up
				}, function(res){
				if(res != -1) {
					// replace the rating-buttons with static buttons to bring down unnecessary ajax-requests
					// (after rating/if You have already rated this comment)
		            if(res.status > -1) {
						div.html('<div class="rateresult'+res.style+'">'+res.result+'</div><a id="btn-'+cid+'-down" class="ratebtn'+(res.rating_state=='up'?' down_active desel':' down')+'" title="Vote down! ('+res.rating_down+' votes)"></a><a id="btn-'+cid+'-up" class="ratebtn'+(res.rating_state=='up'?' up':' up_active desel')+'" title="Vote up! ('+res.rating_up+' votes)"></a>');
						jQuery.jRating.message('Thanks for your vote',cid,true);
						return true;
						}
						else {
						var actRating = jQuery(el).parent('.commentrating').children('.rateresult').html();
						div.html('<div class="rateresult'+res.style+'">'+actRating+'</div><a id="btn-'+cid+'-down" class="ratebtn down_active" title="Vote down! ('+res.rating_down+' votes)" title="Vote down! ('+res.rating_down+' votes)"></a><a id="btn-'+cid+'-up" class="ratebtn up_active" title="Vote up! ('+res.rating_up+' votes)"></a>');
						div.addClass('checked');
					}
					// Show Textmessage from AJAX call/response
					jQuery.jRating.message(res.msg,cid);
					return false;
				}
	        });
	        return false;
			}
		},
		/***
		 * Method to show text under rating-buttons 
		 * @param {string} text
		 * @param {integer} cid
		 */
		message: function(text,cid,voted)
		{
			ratedyet = false;
			if(jQuery('#comment-'+cid+' .commentrating .ratingmsg').length === 0)
			{
				var rated = (typeof(arguments[2]) !== 'undefined')?true:false; 
				var div = jQuery('#comment-'+cid+' .commentrating');
				
				if(!div.hasClass('active-'+cid))
				{
					div.addClass('active-'+cid);
				}
				var styleclass = 'ratingmsg';
				if(!rated)
				{
					styleclass += ' error';
				}else{
					div.addClass('rated');
				}
				var msgDiv = jQuery('<div class="'+styleclass+'">'+text+'</div>');
				div.append(msgDiv);
				msgDiv.fadeIn('normal',function()
				{
					var timer = setTimeout(function()
					{
						msgDiv.fadeOut('normal',function()
						{
							if(div.hasClass('active-'+cid))
							{
								div.removeClass('active-' + cid);
							}
							msgDiv.remove();
							clearTimeout(timer);
						});
					},opts.msgOut);
				});
			}
		},
		/***
		 * Unhide downrated comment
		 * @param {object} el
		 * @param {integer} cid
		 */
		showComment: function(el,cid)
		{
			jQuery('#hcmt-'+cid).css('display','block');
			jQuery(el).remove();
		}
    };
	jQuery('.comment').hover(
		function()
		{
			if(jQuery(this).hasClass('badrated')) {
				jQuery(this).children('.gravatar').stop().animate({opacity:1.0},'normal');
				var parent = jQuery(this).children('.comment-text');
					parent.children('.date, .time, p, .edit-comment').stop().animate({color:'#343434'},'normal');
					parent.children('.comment-author').stop().animate({color:'#3151A2'},'normal');
			}
			jQuery.jRating.hParent(this.id.split('-')[1],1);
		},
		function()
		{
			if(jQuery(this).hasClass('badrated')) {
				var parent = jQuery(this).children('.comment-text');
					parent.children('.comment-author, .date, .time, p, .edit-comment').stop().animate({color:'#aaa'},'normal');
					jQuery(this).children('.gravatar').stop().animate({opacity:0.6},'normal');
			}
			jQuery.jRating.hParent(this.id.split('-')[1],0);
		}
	);
	jQuery('.comment .commentrating .ratebtn')
	.live('mouseenter mouseleave', function(e)
	{
		if(jQuery(this).parent('.rated').length === 0)
		{
			jQuery.jRating.hRate(this,(e.type === 'mouseover')?1:0,this.id.split('-')[2]);
		}
	})
	.live('click',function(e) {
		e.preventDefault();
		var set = this.id.split('-');
		if(jQuery(this).parent('.rated').length > 0 || jQuery(this).parent('.checked').length > 0) {
			jQuery.jRating.message('You have already rated this comment',set[1]);
		}else{
			jQuery.jRating.rate(this,set[1],(set[2]==='up')?1:0,(set[2]!=='up')?1:0);
		}
	});
};
jQuery(document).ready(function()
{
	/* initialize jrating */
	jQuery().jRating();
});
