
/* ------------- Tools ---------------*/

function checkAll() {
	// Test on second ( gt(0 ) checkbox
	if ( $( "input:checkbox.check:gt(0)" ).attr('checked') == true ) {
		$("input:checkbox.checker").attr('checked',false);
		$("input:checkbox.check").attr('checked',false);
	} else {
		$("input:checkbox.checker").attr('checked',true);
		$("input:checkbox.check").attr('checked',true);
	}
}

function minusCounter() {
	var current_counter = parseInt($('.message_count').html());
	current_counter = Math.max( 0, current_counter - 1 );
	$('.message_count').html(current_counter);
}

function plusCounter() {
	var current_counter = parseInt($('.message_count').html());
	current_counter++;
	$('.message_count').html(current_counter);
}

function makeVisible(id) {
	$("#" + id).css("visibility","visible");
}

function makeHidden(id) {
	$("#" + id).css("visibility","hidden");
}

function popup(file, windowname, features) {
	mywindow = window.open(file, windowname, features);
	mywindow.focus();
	return false;
}

function linkTo(optVal){
	if ( optVal == "" ) {
		return false;
	}
	window.open(optVal);
}


function showToolTip(field) {
	$(".tooltip").hide();
	$('#' + field).fadeIn(100);
	setTimeout("hideToolTip('" + field + "')",4000);
}

function hideToolTip(field) {
	$('#' + field).fadeOut(400);
}

function hideAllToolTips() {
	$(".tooltip").fadeOut(400);
}

function checkBox(id) {
	if ($('#' + id).attr('checked') == true) {
		$('#' + id).val('');
	} else {
		$('#' + id).val('yes');
	}
}

function submitPostPager( targetUrl ) {
	$('#pagerPost').attr( 'action' ) = targetUrl;
	$('#pagerPost').submit();
}

/* ------------- AJAX functions ---------------*/
function ajax_changeLanguage(token) {
	var url 		= unescape(window.location.pathname);
	var check = window.location.hostname.split('.');
	if (check.length > 2 && window.location.hostname != 'dev.playnik.it') {
		var sub = window.location.hostname;
	} else {
		var sub = 'de.' + window.location.hostname;
	}

	$.ajax({
		type: 		'POST',
		url:		'http://' + sub,
	  	data:		{ajax:true,action:'change_language',token:token,ajax_change_language:true},
		cache: 		false,
	  	success: 	function(html){
			window.location.replace(url);
	  	}
	});
}

function ajax_addReport(support_case,support_item,support_parent,node_id) {
	$('#report_' + node_id).html(lang_request);
	$.ajax({
		type: 		'POST',
		url:		'/ajax',
	  	data:		{ajax:true,action:'new_report',support_case:support_case,support_item:support_item,support_parent:support_parent},
		cache: 		false,
	  	success: 	function(html){
			$('#report_' + node_id).html(lang_takecare);
	  	},
	  	error:		function( req, status, error ) {
  			$('#report_' + node_id).html( req.responseText );
	  	}
	});
}

function ajax_addPoke(user_id,node_id) {
	$('#' + node_id).html(lang_request);

	$.ajax({
		type: 		'POST',
		url:		'/ajax',
		dataType:	'html',
	  	data:		{ajax:true,action:'new_poke',user_id:user_id,ajax_add_notification:true},
		cache: 		false,
	  	success: 	function( html ) {
			if (parseInt(html) == 1) {
				$('#' + node_id).html(lang_poke);
  			} else {
  				$('#' + node_id).html(html);
  			}
	  	},
	  	error:		function( req, status, error ) {
  			$('#' + node_id).html( req.responseText );
	  	}
	});
}


function ajax_updateUserStatus(node_id) {
	var user_status = $('#user_status').val();

	// disable button during ajax
	$('#submit_status').attr('disabled','disabled');
	$.ajax({
		type: 		'POST',
		url:		'/ajax',
	  	data:		{ajax:true,action:'update_status',user_status:user_status,ajax_update_status:true},
		cache: 		false,
	  	success: 	function(html){
	  		// Insert html in node, hide form, display updated node
			$('.status').html(html);
			$('#fieldset_user_status').hide();
  			$('#' + node_id).show();
	  		$('#submit_status').removeAttr('disabled');
	  	},
	  	error:		function( req, status, error ) {
	  		// TODO: Show req.responseText somewhere...
	  	}
	});
}

function ajax_addComment( class_id, ref_id, action ) {
	var comment_text = $('#comment_text').val();

	// disable button during progress
	$('#submit_comment').attr('disabled','disabled');
	$.ajax({
		type: 		'POST',
		url:		'/ajax',
	  	data:		{ajax:true,action:action,comment_text:comment_text,ajax_new_comment:true,ref_id:ref_id},
		cache: 		false,
	  	success: 	function(html){
			// insert html before first element of class;
			$("." + class_id + ":first").before(html)

			$('#comment_text').val('');
			$('#noentry_comments').slideUp("slow");
			$('.placeholder').remove();

			$('#submit_comment').removeAttr('disabled');
  		},
  		error:		function( req ) {
  			$('.ajax_error').html( req.responseText );
			$('#submit_comment').removeAttr('disabled');
	  	}
	});
}

function ajax_deleteComment(node_id,comment_id) {
	$('#delete_' + node_id).html(lang_request);
	$.ajax({
		type: 		'POST',
		url:		'/ajax',
	  	data:		{ajax:true,action:'delete_guestbook',comment_id:comment_id,ajax_delete_comment:true},
		cache: 		false,
	  	success: 	function(html){
			$('#comment_' + comment_id).slideUp("slow");

		  	// display noentry if last element removed)
		  	if ($('.entries').length == 0) {
		  		$('#noentry_comments').slideDown("slow");
		  		$('.comments_more').hide();
		  	}
	  	},
	  	error:		function( req ) {
	  		$('#delete_' + node_id).html( req.responseText );
	  	}
	});
}

function ajax_deletenotification(node_id) {
	$.ajax({
		type: 		'POST',
		url:		'/ajax',
	  	data:		{ajax:true,action:'delete_notification',notification_id:node_id,ajax_delete_notification:true},
		cache: 		false,
	  	success: 	function(html){
			$('#notification_' + node_id).slideUp("slow");

		  	// display noentry if last element removed)
		  	if ($('.notificationentries').length == 0) {
		  		$('#noentry_notifications').slideDown("slow");
		  		$('.note_more').slideUp();
		  	}
	  	},
	  	error:		function( req ) {
	  		// TODO: Show req.responseText somewhere...
	  	}
	});
}

function ajax_deleteFeed(node_id) {
	$.ajax({
		type: 		'POST',
		url:		'/ajax',
	  	data:		{ajax:true,action:'delete_feed',feed_id:node_id,ajax_delete_feed:true},
		cache: 		false,
	  	success: 	function(html){
			$('#feed_' + node_id).slideUp("slow");

			if ( $('.feedentries').length == 0) {
		  		$('#noentry_feeds').slideDown();
		  		$('.feed_more').slideUp();
		  	}
	  	},
	  	error:		function( req ) {
	  		// TODO: Show req.responseText somewhere...
	  	}
	});
}

function ajax_handleFriend(action,node_id,user_id,user_url) {
	$('#' + node_id).html(lang_request);
	$.ajax({
		type: 		'POST',
		url:		'/ajax',
	  	data:		{ajax:true,action:action,user_id:user_id,user_url:user_url,ajax_check_friendrequest:true},
		cache: 		false,
	  	success: 	function(html){
			if (html == 'request') {
				$('#' + node_id).html(lang_friendreq);
  			} else if (html == 'already') {
  				$('#' + node_id).html(lang_friendalready);
  			} else if (html == 'pending') {
  				$('#' + node_id).html(lang_friendpending);
  			} else if (html == 'established') {
  				$('#' + node_id).html(lang_friendest);
  			} else if (html == 'deny') {
  				$('#' + node_id).html(lang_frienddeny);
  			} else {
  				$('#' + node_id).html( html );
  			}
	  	},
	  	error:		function( req ) {
	  		$('#' + node_id).html( req.responseText );
	  	}
	});
}


function ajax_deleteFriend(friend_id,friend_url) {
	$.ajax({
		type: 		'POST',
		url:		'/ajax',
	  	data:		{ajax:true,action:'delete_friend',friend_id:friend_id,friend_url:friend_url,ajax_delete_friend:true},
		cache: 		false,
	  	success: 	function(html){
	  		$('#' + friend_id).remove();
	  	},
	  	error:		function( req ) {
	  		// TODO: Show req.responseText somewhere...
	  	}
	});
}

function ajax_markAsRead() {
	ajax_handleMessages( 'messages_read');
}

function ajax_markAsUnread() {
	ajax_handleMessages( 'messages_unread' );
}

function ajax_deleteMessages() {
	ajax_handleMessages( 'messages_delete' );
}

function ajax_deleteMessage( id, link ) {
	ajax_handleMessages( 'messages_delete', id );
	if ( typeof link != "undefined" ) {
		window.location = link;
	}
}

function ajax_trashMessages() {
	ajax_handleMessages( 'messages_trash' );
}

function ajax_trashMessage( id, link ) {
	ajax_handleMessages( 'messages_trash', id );
	if ( typeof link != "undefined" ) {
		window.location = link;
	}
}

function ajax_untrashMessages() {
	ajax_handleMessages( 'messages_untrash' );
}

function ajax_untrashMessage( id, link ) {
	ajax_handleMessages( 'messages_untrash', id );
	if ( typeof link != "undefined" ) {
		window.location = link;
	}
}

function ajax_handleMessages( action, id ) {
	var messages 	= new Array();

	if ( typeof id == "undefined" ) {
		var checker 	= $('.check:checked');
		for( i=0; i < checker.length; i++ ) {
			messages.push( checker[i].getAttribute("id") );
		}
	} else {
		messages.push( id );
	}

	for ( i = 0; i < messages.length; ++i ) {
		if ( messages[i] == null ) continue;
		messages[i] = messages[i].substr( messages[i].lastIndexOf( '_') + 1 );
	}

	var messagesStr = messages.join('|');
	$.ajax({
		type: 		'POST',
		url:		'/ajax',
	  	data:		{ajax:true,action:action,messages:messagesStr},
		cache: 		false,
	  	success: 	function(html) {
	  		for ( i = 0; i < messages.length; ++i ) {
	  			var id = messages[i];
				switch ( action ) {
					case 'messages_delete':
					case 'messages_trash':
						if ( $("#check_" + id).attr("name") == 'unread') {
							minusCounter();
						}
						// No break here, slideUp/remove is also required for trash/delete
					case 'messages_untrash':
						// Slide element up, once completed remove it, and check if last
						// element was removed. If so, hide msg panel and show noEntry panel
						$("#message_" + id).slideUp( 'slow', function() {
							$("#message_" + id).remove();
						});
						break;
					case 'messages_read':
			  			if ( $("#check_" + id ).attr("name") == 'unread') {
			  				$("#sub_" + id).css('font-weight', 'normal');
			  				minusCounter();
			  			}
						$("#check_" + id).attr({name:"read"});
						break;
					case 'messages_unread':
			  			if ( $("#check_" + id ).attr("name") == 'read' ) {
			  				$("#sub_" + id).css('font-weight', 'bold');
		  					plusCounter();
			  			}
			  			$("#check_" + id).attr({name:"unread"});
						break;
				}
	  		}
	  		switch ( action ) {
				case 'messages_delete':
				case 'messages_trash':
				case 'messages_untrash':
//			  		window.setTimeout( 650, function() {
//					  	if ($('.msg').length == 0) {
//					  		$('#noentry_messages').show();
//					  		$('.msg_panel').hide();
//					  	}
//			  		});
			  		// No break here, default stuff should be executed too
			  	default:
			  		// Unset checkboxes
					$("input:checkbox.checker").attr('checked',false);
					$("input:checkbox.check").attr('checked',false);
	  		}
	  	},
	  	error:		function( req ) {
	  		// TODO: Show req.responseText somewhere...
	  	}
	});
}

function ajax_updateDisplayStatus(article_id,dir) {
	$('#' + dir + '_' + article_id).html(lang_request);
	$.ajax({
		type: 		'POST',
		url:		'/ajax',
	  	data:		{ajax:true,action:dir,article_id:article_id},
		cache: 		false,
	  	success: 	function(html){
			if (dir == 'gopublic') {
				$('#' + dir + '_' + article_id).html( lang_gopublic );
			} else if (dir == 'gooffline') {
				$('#' + dir + '_' + article_id).html( lang_gooffline );
			}
	  	},
	  	error:		function( req ) {
	  		$('#' + dir + '_' + article_id).html( req.responseText );
	  	}
	});

}

function ajax_updateUserSettings(name) {
	$('#but_' + name).html( lang_request );
	$('#submit_form_' + name).attr('disabled','disabled');

	var checked = $("input." + name + ":checked").val();

	$.ajax({
		type: 		'POST',
		url:		'/ajax',
	  	data:		{ajax:true,action:name,set_value:checked},
		cache: 		false,
	  	success: 	function( html ) {
			$('#but_' + name).html( lang_saved );
			$('#submit_form_' + name).removeAttr('disabled');
	  	},
	  	error:		function( req ) {
			$('#but_' + name).html( req.responseText );
			$('#submit_form_' + name).removeAttr('disabled');
	  	}
	});
}

function ajax_blockUser(node_id,user_id) {
	$('#block_' + node_id).html(lang_request);
	$.ajax({
		type: 		'POST',
		url:		'/ajax',
	  	data:		{ajax:true,action:'new_block',user_id:user_id,ajax_add_block:true},
		cache: 		false,
	  	success: 	function(html) {
			$('#block_' + node_id).html(lang_blocked);
	  	},
	  	error:		function( req ) {
	  		$('#block_' + node_id).html( req.responseText );
	  	}
	});
}

function ajax_freeBlocked(blocked_id,blocked_url) {
	$('#' + blocked_id).remove();
	$.ajax({
		type: 		'POST',
		url:		'/ajax',
	  	data:		{ajax:true,action:'new_free',blocked_id:blocked_id,ajax_free_blocked:true},
		cache: 		false,
	  	success: 	function(html){
	  		if (html == '1') {
			  	var count = $('.blocked_n_sort');
			  	count = count.length;
			  	if (count == 0) {
			  		$('#noentry_blocked').show();
			  	}
		  	}
	  	},
	  	error:		function( req ) {
	  		// TODO: Show req.responseText somewhere...
	  	}
	});

}

function ajax_changeReceiverAvatar( node_id ) {
	var user_id = $( node_id ).val();

	if (user_id == '') {
		$('.compose').hide();
	} else {
		$.ajax({
			type: 		'POST',
			url:		'/ajax',
		  	data:		{ajax:true,action:'compose',user_id:user_id},
			cache: 		false,
		  	success: 	function(html){;
				$('.compose').html('');
				$('.compose').html(html);
				$('.compose').show();
		  	},
		  	error:		function( req ) {
		  		// TODO: Show req.responseText somewhere...
		  	}
		});
	}
}

function ajax_removeGame(game_id) {
	$.ajax({
		type: 		'POST',
		url:		'/ajax',
	  	data:		{ajax:true,action:'remove_game',game_id:game_id},
		cache: 		false,
	  	success: 	function(html){
	  		$("#game_" + game_id).remove();
		  	var count = $('.summary');
		  	count = count.length;

		  	// display noentry (if necessary)
		  	if (count == 0) {
		  		$('#noentry_playlist').show();
		  	}
	  	},
	  	error:		function( req ) {
	  		// TODO: Show req.responseText somewhere...
	  	}
	});
}

function ajax_addCongrat( ach_id, game_url ) {
	$('#ach_congrat').html(lang_request);
	$.ajax({
		type: 		'POST',
		url:		'/ajax',
	  	data:		{ajax:true,action:'add_congrat',ach_id:ach_id,game_url:game_url},
		cache: 		false,
	  	success: 	function(html){
			$('#ach_congrat').html(lang_congrat);
	  	},
	  	error:		function( req ) {
	  		$('#ach_congrat').html( req.responseText );
	  	}

	});

}
