/*
	(C) 2008 Frozenball <orkkiolento@gmail.com>
	
	I Love jQuery <3
	
*/
$(document).ready(function(){
				
	/* Redirect Notice */
	$('#redirect_notice, #notificationmenutable').hide();
	$('#redirect_notice, #notificationmenutable').fadeIn('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;
	}
}

$(document).ready(function() {
	$('.mid_posts_text img').each(function(i){
		if ($(this).width() > 600) {
			//alert(true);
			$(this).after($("<div id='minimizedPicture"+i+"' data-showfull='' style='background-color:#e3e2bb; color:black; font-family:Tahoma; font-size:12px; cursor:hand; padding:2px; width:596px;'>Click here to view this image in full size ("+$(this).width()+" x "+$(this).height()+").</div>"));
			$(this).css('max-width','600px');
			$(this).attr('data-picId',i);
			$('#minimizedPicture'+i).click(function(){
				$(this).hide('');
				$('img[data-picId='+i+']').css('max-width','');
				return false;
			});
		}
	});
	if ($('#addreply_textbox')) {
		topic_id = $('input[name=topic]').attr('value');
		if ($.cookie("forumPostRemember"+topic_id)) {
			if ($.cookie("forumPostRemember"+topic_id).length > 20) {
				$('#addreply_textbox').after($('<br /><small><div id="rememberPost">Draft: <span></span> <a href="#">Edit draft</a></div></small>'));
				if ($.cookie("forumPostRemember"+topic_id).length > 60) {
					$('#rememberPost span').html($.cookie("forumPostRemember"+topic_id).substr(0,60)+'...');
				} else {
					$('#rememberPost span').html($.cookie("forumPostRemember"+topic_id));
				}
				$('#rememberPost a').click(function(){
					$('#addreply_textbox').attr('value',$.cookie("forumPostRemember"+topic_id));
					$('#rememberPost').hide('slow');
					$.cookie("forumPostRemember"+topic_id, null);
					return false;
				});
			}
		}
	}
	$('#addreply_textbox').change(function(){
		topic_id = $('input[name=topic]').attr('value');
		$.cookie("forumPostRemember"+topic_id, $(this).attr('value'), { expires: 7 });
		//alert($(this).attr('value'));
	});
	$('#addreply_textbox').keydown(function(){
		topic_id = $('input[name=topic]').attr('value');
		$.cookie("forumPostRemember"+topic_id, $(this).attr('value'), { expires: 7 });
		//alert($(this).attr('value'));
	});
});
