/**
 * @version $Id: game.js
 */

$(document).ready(function(){

	// Sharing a game with the complete link feature
	$("#game-share").focus(function() {
		$('#game-share').select();
	});
	    
    NS_Like();
	NS_Favorite();
    NS_Comment();
    
});

var NS_Comment = function(){

    var _self = this.NS_Comment,
    
    _variables = {
        container: $('#user-comment'),
        user_id: $('#user_id').val(),
        game_id: $('#game_id').val(),
        login: $('#login').val(),
        comment_id: 0,
        default_text: 'Leave a comment about this game',
        default_path: $('#default_path').val()
    };
    
    
    _self.init = function(){
    
       _self.prepareComment()
    
    };
    
    _self.prepareComment = function(){
    
        if (!_variables.user_id)
        {
        
            $('#user-comment-login, #user-comment').hover(function(){
			
            
                $('#user-comment-login').show();
                
            }, function(){
                
                $('#user-comment-login').hide();
                
            });
            
            $('#submit-comment').click(function(){
                _self.feedbackMessage('You must Sign In to add a comment.');
            });
            
        }
        else
        {
        
            _self.toggleDefaultText();
            
            $('#submit-comment').click(function(){
            
                if (_variables.container.val() != _variables.default_text && _variables.container.val() != '')
                {
                    _self.sendComment();
                }
                else
                {
                    _self.feedbackMessage('Enter a comment before sending.');
                }
            });
        }
    }
    
    _self.sendComment = function(){
    
        _variables.container.val()

        $.ajax({
			type: "POST",
			url: _variables.default_path+"/ajax/comment.php",
			data: "action=add&game_id="+_variables.game_id+"&comment="+_variables.container.val(),
			success: function(data){
				
                if (!isNaN(data))
                {
                
                    structure = '<div class="user-comment-box" id="comment-'+data+'"><a href="'+_variables.default_path+'/gamercard/view/'+_variables.user_id+'/'+_variables.login+'.html">'+_variables.login+'</a><span class="comment-date">(now)</span><p>'+_variables.container.val()+'</p></div>';
                    
                    $("#ajax-comment").append(structure);
                    
                    _variables.container.val('');
                }
            }
		});       
    }
    
    _self.deleteComment = function(){
    
        $.ajax({
			type: "GET",
			url: _variables.default_path+"/ajax/comment.php",
			data: "action=delete&game_id="+_variables.game_id+"&comment_id="+_variables.comment_id,
			success: function(data){
		 	
				$("#comment-" + _variables.comment_id).fadeOut("slow");		 
			}
		});
    }
    
    
    _self.toggleDefaultText = function(){
    
        _variables.container.focus(function(){
    
            if (_variables.container.val() == _variables.default_text)
            {
                _variables.container.val('').unbind('focus'); 
            }
        })
    }
    
    
    _self.feedbackMessage = function(message) {
    
        $('#submit-comment').unbind('click');
    
        $('#feedback-message').html(message).stop(true, true).fadeIn(100).delay(5000).fadeOut(500, function(){
            _self.prepareComment();
        });
        
    }

    _self.init();

};


var NS_Favorite = function(){

    var _self = this.NS_Favorite,
    
    _variables = {
        container: $('#favorite-button'),
        user_id: $('#user_id').val(),
        game_id: $('#game_id').val(),
        default_path: $('#default_path').val()
    };
    
    
    _self.init = function(){
    
        _self.prepareFavorite();
    };
    
    
    _self.prepareFavorite = function(){
        $('#favorite-button').click(function(){
            _self.toggleFavorite();
        });
    }

    
    _self.toggleFavorite = function(){
            
    
        if (_variables.user_id)
        {
    
            _variables.container.toggleClass('active');
            
        
            if (_variables.container.hasClass('active'))
            {
                _self.manageFavorite('add');
                _self.feedbackMessage('The game was added to your favorites.');
            }
            else
            {
                _self.manageFavorite('remove');
                _self.feedbackMessage('The game has been removed from your favorites.');
            }   
        }
        else
        {
            _self.feedbackMessage('You must Sign In to add this game to your Favorites!');
        }
        
    };
    
    _self.manageFavorite = function(action){
            
        $.ajax({
            type: "GET",
            url: _variables.default_path+"/ajax/favorite.php",
            data: "action="+action+"&game_id="+_variables.game_id,
            success: function(data){}
        });
    }
    
    
    _self.feedbackMessage = function(message) {
    
        $('#favorite-button').unbind('click');
    
        $('#feedback-message').html(message).stop(true, true).fadeIn(100).delay(5000).fadeOut(500, function(){
            _self.prepareFavorite();
        });
        
    }
    
    _self.init();
    
}


var NS_Like = function(){

    var _self = this.NS_Like,
    
    _variables = {
        likes: 0,
        dislikes: 0,
        percent_like: 0,
        percent_dislike: 0,
        width_like: 0,
        width_dislike: 0,
        width_max: 70,
        votes: 0,
        game_id: $('#game_id').val(),
        default_path: $('#default_path').val()
    };
    
    
    _self.init = function() {
        
        _variables.likes = parseInt($("#like-button").text());
        _variables.dislikes = parseInt($("#dislike-button").text());
        _variables.votes = _variables.likes + _variables.dislikes;
        
        _self.setVoteLength();
        _self.prepareVote();        
        
    };
    
    
    _self.prepareVote = function(){
    
        $('#like-button').click(function(){
        console.log('vote');
            _self.vote('like');
        });
        
        $('#dislike-button').click(function(){
            _self.vote('dislike');
        });

    }
    
    
    _self.setVoteLength = function() {
    
        if (_variables.likes > 0) {
            _variables.percent_like = Math.round((_variables.likes / _variables.votes) * 100);
        }
        if (_variables.dislikes > 0) {
            _variables.percent_dislike = Math.round((_variables.dislikes / _variables.votes) * 100);
        }
        
        
        _variables.width_like = Math.round((_variables.percent_like * _variables.width_max) / 100);
        _variables.width_dislike = Math.round((_variables.percent_dislike * _variables.width_max) / 100);
        
        
        if (_variables.width_like > 0) {
            $('#like-bar').css({'width': _variables.width_like+'px'})
        }
        if (_variables.width_dislike > 0) {
            $('#dislike-bar').css({'width': _variables.width_dislike+'px'})
        }
        
        
        $('#like-percent-value').text(_variables.percent_like+'%');
        $('#dislike-percent-value').text(_variables.percent_dislike+'%');
    
    }
    
    
    _self.vote = function(answer) {
    
        if (!$.cookie('game_'+_variables.game_id+'_vote'))
		{
			$.ajax({
				type: "GET",
				url: _variables.default_path+"/ajax/like.php",
				data: "game_id="+_variables.game_id+"&answer="+answer,
				success: function(data){
                
                    if (data == 'success')
                    {
                    
                        if (answer == 'like')
                        {
                            _variables.likes+=1;
                            $("#like-votes").text(_variables.likes);
                        }
                        else
                        {
                            _variables.dislikes+=1;
                            $("#dislike-votes").text(_variables.dislikes);
                        }
                        _variables.votes++;
                        
                        
                        _self.setVoteLength();
                        _self.feedbackMessage("Thanks for voting. Also try leaving a comment!");
                        _self.setCookie(answer);
                    
                    }
				}
			});
			
		} else {
            _self.feedbackMessage("You already voted!");
		}

    }
    
    
    _self.feedbackMessage = function(message) {
        
        $('#like-button').unbind('click');
        $('#dislike-button').unbind('click');

        $('#feedback-message').html(message).stop(true, true).fadeIn(100).delay(5000).fadeOut(500, function(){
            _self.prepareVote();
        });
        
    }
    
    
    _self.setCookie = function(answer) {
        $.cookie("game_"+_variables.game_id+"_vote", answer, { expires: 1 });
    }
    
    _self.init();
    
};

