

function loadForum() {

	var forum_div = $('<div></div>').attr('id','forum');
	$('#site').after(forum_div);

	$('#site').css("min-height", 275);
	$('#site').height(275);
	$.get('forum.php', function(html) { $('#forum').append(html); forum(); });
}

function unloadForum() {

	$('#site').css("min-height", 580);
	$('#site').height('100%');
	$('#forum').empty().remove();
}

function forum() {

	// Miscelaneous functions
	String.prototype.trim = function() { return this.replace(/^\s*|\s*$/g,''); }
	String.prototype.rmvlb = function() { return this.replace(/(\n\r|\n|\r)/gm,''); }
	String.prototype.striptags = function() { return this.replace(/(<([^>]+)>)/ig,''); }

	var filter = /^[a-zA-Z0-9][\w.-]*@[a-zA-Z0-9][\w\-\.]+\.[a-zA-Z0-9]{2,6}$/;
	var videofilter_cod = /^(?:<[^>]+>)*<.*src="(?:http:\/\/)?(?:[\w_-]{2,3}\.)?youtube\.com\/v\/([\w_-]{11})"?[^>]*>\s?<\/embed>(?:<\/[^>]+>)*\s*$/;
	var videofilter_uri = /^(?:http:\/\/)?(?:[\w_-]{2,3}\.)?youtube\.com\/watch\?v=([\w_-]{11}).*$/;


	$('.pagination a').click(function() {

		var lnk = $(this).attr("title");
		$.get("forum.php", { page:lnk }, function(html) { $('#forum').html(html); forum(); });
		return false;
	});

	// Layout & Effects
	$(".ein").click(function() { $('.help').slideToggle(); });

	// Form inputs
	value = new Array();

	$("input.text, textarea").each(function(i) { value[$(this).attr('name')] = $(this).val(); });
	$("input.text, textarea").focus(function() { if ($(this).val() == value[$(this).attr('name')]) { $(this).val(''); } });
	$("input.text, textarea").blur(function() { if ($(this).val()=='') { $(this).val(value[$(this).attr('name')]); } });

	// Form validation
	function check_message() {

		var nickname = $('#commentform #nickname').val();
		var email = $('#commentform #email').val();
		var video = $('#commentform #video').val();
		var comment = $('#commentform #comment').val();

		nickname = (nickname != '') ? nickname.trim() : '';
		email = (email != '') ? email.trim().toLowerCase() : '';
		video = (video != '') ? video.trim().rmvlb() : '';
		comment = (comment != '') ? comment.trim() : '';


		var errors = new Array();

		if (nickname == 'tu nombre') {

			errors.splice(0, 0, 'Tienes que introducir tu nombre');
			$('#nickname').css('border-color', '#1696fa');

		} else if (nickname.length < 3) {

			errors.splice(0, 0, 'Ese email no parece correcto');
			$('#nickname').css('border-color', '#1696fa');
		}

		if (email != 'tu email' && !filter.test(email)) {

			errors.splice(errors.length, 0, 'Ese email no parece correcto');
			$('#email').css('border-color', '#1696fa');
		}

		if (video != 'codigo del video' && (!videofilter_cod.test(video) && !videofilter_uri.test(video))) {

			errors.splice(errors.length, 0, 'El c&oacute;digo del v&iacute;deo no es correcto');
			$('#video').css('border-color', '#1696fa');
		}

		if (comment == 'tu comentario') {

			errors.splice(errors.length, 0, 'Tienes que introducir un comentario');
			$('#comment').css('border-color', '#1696fa');

		} else if (comment.length < 10) {

			errors.splice(errors.length, 0, 'El comentario es demasiado corto');
			$('#comment').css('border-color', '#1696fa');
		}

		if (errors.length < 1) {

			$('#submit').val('enviando...').attr('disabled', 'disabled');

			$.post('forum.php', {

				nickname: $("#nickname").val(),
				email: $("#email").val(),
				video: $("#video").val(),
				comment: $("#comment").val()

			}, function(html) {

				$('#submit').val('comentario enviado! :)').attr('disabled', 'disabled');
				$('#forum').html(html);
				forum();
			});

		} else {

			for (i=1, error_text = errors[0]; i<errors.length; i++) { error_text += ' / ' + errors[i]; }

			$('#msg span').html(error_text).fadeIn();
		}
	}

	$('#commentform').submit(function() { check_message(); return false; });
}

$(document).ready(function() {

	$('#site').flashembed({src:flash_dir+'home.swf', id:'site_flash', width:767, height:'100%', bgcolor:171717, menu:false, scale:'noscale', allowfullscreen:false, version:[9,0], expressInstall:flash_dir+'expressinstall.swf'});
});