function send_vote(id, type){
    var url;
    //url = '/vote/' + id + '/' + type + '/' + name;
    url = '/e/public/vote/vote.php?id=' + id +'&name=game'+ '&type=' + type;
    
    
    $("#vote_send_load").html('<img src="/images/loading.gif" alt="loading" />');
    
    $.ajax({
        type: "GET",
        url: url,
        
        success: function(data){
        
            $('#vote_send_load').html('');
             if (data.status == 1) {		 	
			 		$('#vote').html('Thank you for voting');
			 		
			 		var nb_like = parseInt($('#game_nb_like').html().replace(',', ''));
			 		var nb_dislike = parseInt($('#game_nb_dislike').html().replace(',', ''));
			 		
			 		var bar_like = 0;
			 		var bar_dislike = 0;
			 		
			 		if (type == 'like') {
			 			nb_like++;
			 			$('#game_nb_like').html(nb_like);
			 		}
			 		else 
			 			if (type == 'dislike') {
			 				nb_dislike++;
			 				$('#game_nb_dislike').html(nb_dislike);
			 			}
			 		result = Math.round(nb_like / (nb_like + nb_dislike) * 100,2);
					$('#vote_result').html(result+"%");
					
			 		bar_like = Math.round((nb_like * 350) / (nb_like + nb_dislike + 1));
			 		bar_dislike = Math.round((nb_dislike * 350) / (nb_like + nb_dislike + 1));
			 		
			 		$('#game_bar_like').css('width', bar_like + "px");
			 		$('#game_bar_dislike').css('width', bar_dislike + "px");
			 	
			 }else{
			 	if (data.message == 'USER_ALREADY_VOTED') {
					
			 		$('#vote').html('You have already voted.');
			 	}else if(data.message == 'NOCOOKIE') {
			 		$('#vote').html('Error!Your browser needs to support cookie...');
			 	}
			 }
        }
    });
    /*
     document.getElementById('vote_send_load').innerHTML = '<img src="/images/loading.gif" alt="loading" />';
     
     var xhr = get_http_object();
     xhr.open('GET', '/vote/'+id+'/'+type+'/'+name, true);
     xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
     xhr.send('');
     xhr.onreadystatechange = function() {
     if (xhr.readyState == 4 && xhr.status == 200) {
     var fav = document.getElementById('favorites');
     document.getElementById('vote').innerHTML = 'Thank you for voting.';
     document.getElementById('vote_send_load').innerHTML = '';
     }
     }
     */
}

function update_vote(id){
    var url;
    url = '/e/public/vote/vote_result.php?id=' + id + '&name=game&nocache=' + new Date().getTime();
    $.ajax({
        url: url,
        type: 'get',
        dataType: 'json',
        data: {
            
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
           // alert(textStatus + " error " + errorThrown);
        },
        success: function(data){
            //alert(data.nb_like);
            if (data.status == 1) {
            	nb_like = parseInt(data.nb_like);
				nb_dislike= parseInt(data.nb_dislike);
				
                $('#game_nb_like').html(nb_like);
                $('#game_nb_dislike').html(nb_dislike);
				result = Math.round(nb_like / (nb_like + nb_dislike) * 100,2);
				$('#vote_result').html(result+"%");
                
                bar_like = Math.round((nb_like * 350) / (nb_like + nb_dislike + 1));
                bar_dislike = Math.round((nb_dislike * 350) / (nb_like + nb_dislike + 1));

                $('#game_bar_like').css('width', bar_like + "px");
                $('#game_bar_dislike').css('width', bar_dislike + "px");
            }
            else {
               // alert(data.message);
            }
        }
    })
}

function add_fav(id){
    var url = '/favorites/add/' + id;
    $('#favorites_send_load').html('<img src="/images/loading.gif" alt="loading" />');
    
    $.ajax({
        type: "GET",
        url: url,
        
        success: function(msg){
        
            if (msg.indexOf('FAV_ADD_OK') >= 0) {
                $('#favorites').html('<a href="#null" onclick="del_fav(' + id + '); return false"><div class="fav_active"></div>Remove to your<br />favorites</a>');
            }
            else 
                if (msg.indexOf('FAV_ADD_LIMIT') >= 0) {
                    $('#favorites').html('You cannot have<br />more than 200 favorites.');
                }
            
            $('#favorites_send_load').html('');
        }
    });
    
    /*
    
    
     document.getElementById('favorites_send_load').innerHTML = '<img src="/images/loading.gif" alt="loading" />';
    
    
     var xhr = get_http_object();
    
    
     xhr.open('GET', '/favorites/add/'+id, true);
    
    
     xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    
    
     xhr.send('');
    
    
     xhr.onreadystatechange = function() {
    
    
     if ((xhr.readyState == 4) && (xhr.status == 200)) {
    
    
     if (xhr.responseText.indexOf('FAV_ADD_OK') >= 0)
    
    
     {
    
    
     document.getElementById('favorites_send_load').innerHTML = '';
    
    
     var fav = document.getElementById('favorites');
    
    
     if (fav != null) {
    
    
     fav.innerHTML = '<a href="#null" onclick="del_fav('+id+'); return false"><div class="fav_active"></div>Remove to your<br />favorites</a>';
    
    
     }
    
    
     }
    
    
     else if (xhr.responseText.indexOf('FAV_ADD_LIMIT') >= 0)
    
    
     {
    
    
     document.getElementById('favorites_send_load').innerHTML = '';
    
    
     var fav = document.getElementById('favorites');
    
    
     if (fav != null) {
    
    
     fav.innerHTML = 'You cannot have<br />more than 200 favorites.';
    
    
     }
    
    
     }
    
    
     }
    
    
     }
    
    
     */
    
    
}


function del_fav(id){

    var url = '/favorites/del/' + id;
    
    $('#favorites_send_load').html('<img src="/images/loading.gif" alt="loading" />');
    
    $.ajax({
        type: "GET",
        url: url,
        
        success: function(msg){
            $('#favorites').html('<a href="#null" onclick="add_fav(' + id + '); return false"><div class="fav_inactive"></div>Add to your<br />favorites</a>');
            $('#favorites_send_load').html('');
        }
    });
    
    
    /*
    
    
     document.getElementById('favorites_send_load').innerHTML = '<img src="/images/loading.gif" alt="loading" />';
    
    
     
    
    
     var xhr = get_http_object();
    
    
     xhr.open('GET', '/favorites/del/'+id, true);
    
    
     xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    
    
     xhr.send('');
    
    
     xhr.onreadystatechange = function() {
    
    
     if (xhr.readyState == 4 && xhr.status == 200) {
    
    
     var fav = document.getElementById('favorites');
    
    
     fav.innerHTML = '<a href="#null" onclick="add_fav('+id+'); return false"><div class="fav_inactive"></div>Add to your<br />favorites</a>';
    
    
     document.getElementById('favorites_send_load').innerHTML = '';
    
    
     }
    
    
     }
    
    
     */
    
    
}


function send_comment(item_type, id, item_name){

    var comment = $('#form_content').val();
    var url = '/comments/add/' + id + '/' + item_name;
    var data = 'name=' + escape(name) + '&content=' + escape(comment);
    
    if (($('#fgcolor').length != 0) &&
    ($('#fncolor').length != 0) &&
    ($('#bdcolor').length != 0) &&
    ($('#bdstyle').length != 0)) {
        data += '&fgcolor=' + escape($('#fgcolor').val());
        data += '&fncolor=' + escape($('#fncolor').val());
        data += '&bdcolor=' + escape($('#bdcolor').val());
        data += '&bdstyle=' + escape($('#bdstyle').val());
    }
    
    $.ajax({
        type: "POST",
        url: url,
        data: data,
        success: function(msg){
        
            if (msg.indexOf('USER_ALREADY_TAKEN') >= 0) {
                $('#comment_sent').html('<strong>Sorry, this username is already taken. You can register your own username <a style="color:red;" href="/register">here</a>.</strong>');
            }
            else 
                if (msg.indexOf('USER_NOT_LOGGED') >= 0) {
                    $('#comment_sent').html('<strong>Sorry, you need to register to write comments. You can register <a style="color:red;" href="/register">here</a>.</strong>');
                }
                else 
                    if (msg.indexOf('COMMENT_FILTERED') >= 0) {
                        $('#comment_sent').html('<strong>Sorry, your comment has been filtered. It must not contain html code nor forbidden words and it must be less than 1500 characters.</strong>');
                        $('#form_content').val(comment);
                    }
                    else 
                        if (msg.indexOf('COMMENT_DENIED') >= 0) {
                            $('#comment_sent').html('<strong>Sorry, you are not allowed to write comments.</strong>');
                        }
                        else 
                            if (msg.indexOf('COMMENT_COUNT_EXCEEDED') >= 0) {
                                $('#comment_sent').html('<strong>Sorry, you cannot comment anymore for today.</strong>');
                            }
                            else {
                                $('#comment_sent').html('<strong>Comment sent</strong>');
                                get_comment(item_type, id, 1);
                            }
            
            $('#comment_send_load').html('');
        }
    });
}


function get_comment(item_type, id, page){

    var url = '/comments/get/' + item_type + '/' + id + '/' + page;
    
    $.ajax({
        type: "GET",
        url: url,
        
        success: function(msg){
            $('#comments_list').html(msg);
        }
    });
}

function showGame(str){
	$('div.adForGame').hide();
	$('div#flash').fadeIn();
}

