/*
 * jQuery vote - http://www.onlinetvcast.com/
 *
 * Copyright (c) 2010 Valery Nayda
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 */
 
 (function($) {
 	$.vote = function(options) {
        var defaults = {
        	classes_action: {
		            voted:          'voted',                       
		            plus:           'plus',
		            minus:          'minus',
		            positive:       'positive',
		            negative:       'negative',
		            quest:          'quest'
		    },
		    classes_element: {
		            voting:         'voting',
		            count:          'count',                       
		            total:          'total',                       
		            plus:           'plus',
		            minus:          'minus'
		    },
		    typeVote:  {
		            topic_comment: {
		                    url: '/include/ajax/voteComment.php',
		                    targetName: 'idComment'
		            }
		    }
        };       
        var options = $.extend(defaults, options);
                                
        this.init = function(){
        	thisObj = this;
        	$('.' + options.classes_action.plus).live('click', function(){
        		var id = $(this).attr('id').split('_');
        		thisObj.vote(id[1], this, 1, 'topic_comment');
        		return false;
        	});
        	
        	$('.' + options.classes_action.minus).live('click', function(){
        		var id = $(this).attr('id').split('_');
        		thisObj.vote(id[1], this, -1, 'topic_comment');
        		return false;
        	});
		},
		
		this.vote = function(idTarget, objVote, value, type) { 
            if (! options.typeVote[type]) {
                    return false;
            }
           
            thisObj = this;
            this.idTarget = idTarget;
            this.objVote = $(objVote);
            this.value = value;
            this.type = type;
            
            var targetName = options.typeVote[type].targetName;
            $.ajax({
				url : options.typeVote[type].url,
				type : "POST",
				dataType : 'json',
				timeout : 30000,
				error : function() {
            		thisObj.ajaxError();
				},
				success : function(json) {
					thisObj.onVote(json, thisObj);
				},
				// Parametrs
				data : { value : value, id : idTarget }
			});        
    	},
    	
    	this.onVote = function(json, thisObj) {            
        	if (! json) {
        		showGrowl('<h1 class="mGrowl">Please try again later</h1>', 'error');
        	}  
        	if (json.error) {
        		showGrowl('<h1 class="mGrowl">' + json.error + '</h1>', 'error');
        	} else {
                showGrowl('<h1 class="mGrowl">' + json.sMsgTitle + '</h1>', 'ok');
               
                var divVoting = thisObj.objVote.parent('.' + options.classes_element.voting);                
                divVoting.addClass(options.classes_action.voted);
               
                if (this.value > 0) {
                	divVoting.addClass(options.classes_action.plus);
                }
                if(this.value < 0) {
                	divVoting.addClass(options.classes_action.minus);
                }
                var divCount = divVoting.find('.' + options.classes_element.count);
                if (divCount && divCount[0]) {
                	divCount.text(json.iCountVote);
                }
                
                var divTotal = divVoting.find('.' + options.classes_element.total);              
                json.iRating = parseFloat(json.iRating);  
                divVoting.removeClass(options.classes_action.negative);    
                divVoting.removeClass(options.classes_action.positive); 
                
                if (json.iRating > 0) {                        
                	divVoting.addClass(options.classes_action.positive);
                	divTotal.text('+' + json.iRating);
                }
                if (json.iRating < 0) {                        
                	divVoting.addClass(options.classes_action.negative);
                	divTotal.text(json.iRating);
                }
                if (json.iRating == 0) {
                	divTotal.text('0');
                }
                /*if (thisObj.type == 'user' && $('user_skill_' + thisObj.idTarget)) {
                	$('user_skill_' + thisObj.idTarget).text(json.iSkill);
                }*/
        	}      
        },
    	
    	this.ajaxError = function(message) {
			if ($(message).size() == 0) {
				message = 'Please try again later';
			}
			showGrowl('<h1 class="mGrowl">' + message + '</h1>', 'error');
		},
		
		this.init();
        var self = this;
		return this;
    };
 })(jQuery);
