/*
	(C) 2008 Frozenball <orkkiolento@gmail.com>
	
	I Love jQuery <3
	
*/
$(document).ready(function(){
				
	/* Redirect Notice */
	$('#redirect_notice').hide();
	$('#redirect_notice').show('slow');
	$('.table_notification td').hide().fadeIn('slow');
	
	/* Topic Listing */
	 $(".topic_listing_icon").click(function(){
		$(this).html('<input type=\'checkbox\' name=\'topic[]\' value=\''+$(this).attr('data-topic')+'\' checked=\'checked\' />');
		$('#topics_order').fadeOut('fast', function() { $('#topics_action').show(); })
	 });
	 
	/* Password Match */
	f = function(){
		p1 = $(this)
		p2 = $('#'+$(this).attr('data-password-match'))
		
		if (p1.attr('value') != p2.attr('value')) {
			$(this).css('color','red');
		} else {
			$(this).css('color','green');
		}
	}
	$(".js_password_match").keyup(f).change(f);
	 
	/* Data Validation */
	f = function(){
		reg = new RegExp($(this).attr('data-valid'));
		if ($(this).attr('value') == '' || reg.test($(this).attr('value'))) {
			$(this).css('color','green');	
		} else {
			$(this).css('color','red');		
		}
	}
	$(':input[data-valid]').keyup(f).change(f);
	
    /* Ajax */
	$('.js_delete_post').click(function(event){
		event.preventDefault();
		post_id = $(this).attr('data-post-id');
		security = $(this).attr('data-security');
		ajax_delete("forum_action.php?action2=delete&security_code="+escape(security)+"&selected_posts="+escape(post_id),'#post'+post_id+', #post'+post_id+'_2');
	})
 
	/* Auto Complete */
	$(".js_users").autocomplete("misc.php", { extraParams:{'act':'autocomplete','type':'users'} });
	$(".js_ip").autocomplete("misc.php", { extraParams:{'act':'autocomplete','type':'ip'} });
	$(".js_email").autocomplete("misc.php", { extraParams:{'act':'autocomplete','type':'email'} });
	$(".js_topic, .js_topics").autocomplete("misc.php", { extraParams:{'act':'autocomplete','type':'topics'} });

});

// This should be better coded, no onclick="" should be required.
function open_tools_menu(id) {
	$('#'+id).toggle('slow');
}

// Simple AJAX to be added for deleting comments etc.
function ajax_delete(theurl,ids) {
	
	if (theurl == false || theurl == '') {
		$(ids).fadeOut("slow");
		return false;
	} else {
		// Loading Bar
		if ($('#loadingbar').length <= 0) {
			$('body').append("<div id='loadingbar' style='position:fixed; top:10px; left:10px; width:200px; height:20px; opacity:0.5;'><div style='background-color:green; height:20px; width:0px;'></div></div>");
		} else {
			$('#loadingbar').show();
			$('#loadingbar div').css('width','0px');
		}
		$('#loadingbar div').animate({ 
			width: "200px"
		  }, 5000 );
		
		// Ajax
		$.ajax({
			type: "GET",
			url: theurl,
			error: function(a,b,c) { document.location = theurl; },
			success: function(msg){
				$(ids).fadeOut("slow");
				$('#loadingbar').fadeOut('slow');
			}
		});
			 
		// To avoid normal behaviour
		return false;
	}
}
