var porclass = {
	menu_actividad_reciente : function(){
		var elements = $(".actividad_reciente h4");
		var menu = '<ul id="menuSide" class="cf">';
		for( i = 0 ; i < elements.length ; i++ ){
			var texto = elements[i].innerHTML;
			var style = ' style="float: left; width: auto; margin-right: 8px;"';
			menu += '<li'+style+'><a href="#'+elements[i].parentNode.id+'" title="'+texto+'">'+porclass.dividir_texto(texto)+'</a></li>';
		}
		menu += '<ul>';
		$(".actividad_reciente h4").remove();
		$("#boxActiRece").prepend(menu);
	},
	acti_menu_actividad_reciente : function(url){
		$(".actividad_reciente").hide();
		$("#menuSide li").removeClass("acti");
		var elements = $(".actividad_reciente");
		var lista = new Array("most-commented", "most-viewed", "ultimos-comentarios", "ultimas-entradas");
		var url_id = (url.split("#"))[1];
		if ( url.match(/#/) && jQuery.inArray(url_id, lista)!=-1 ) {
			$("#menuSide li a[href$='"+url_id+"']").parent().addClass("acti");
			$("#"+url_id).fadeIn("normal")	
		} else {
			$("#menuSide li:first-child").addClass("acti");
			$("#most-commented").show();
		}
	},
	dividir_texto : function(texto){
		var partes = texto.split(" ");
		if(partes.length==2) return partes[0]+"<br />"+partes[1];
		if(partes.length==3) return partes[0]+" "+partes[1]+"<br />"+partes[2];
	}
}

$(document).ready( function(){
	if($.id("#boxActiRece")){
		porclass.menu_actividad_reciente();
		porclass.acti_menu_actividad_reciente(location.hash);
	}

	$("#menuSide li a").bind("click", function(e){ porclass.acti_menu_actividad_reciente(e.target.href); return false; });

	$('#s, #nRs').blur(function() {
		if ( $(this).val() == '' ) {
			$(this).val('Ingresa tu búsqueda' )
		}
	});
	
	$('#s, #nRs').focus(function() {
		$(this).select();
		if ( $(this).val() == 'Ingresa tu búsqueda' ) {
			$(this).val('');
		}
	});

	$('form#commentform').submit(function() {
		$('form#commentform div.error, form#commentform div.errorInfo').remove();
		
		var formFail = false;
		var whatError = new Array();
		
		if ( jQuery.trim( $('input#author').val() ) == '' ) {
			formFail = true;
			$('input#author').css('background-color', '#FAE9E9').next('samp').replaceWith('<div class="errorInfo">Ingresa tu nombre</div>');
		}
		
		thisEmail = $('input#email').val()
		if ( jQuery.trim( thisEmail ) == '' ) {
			formFail = true;
			$('input#email').css('background-color', '#FAE9E9').next('samp').replaceWith('<div class="errorInfo">Ingresa tu email</div>');

		} else if ( thisEmail.indexOf('@') ) {
			var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
			if ( pattern.test(thisEmail) ) {
				formFail = false; } 
			else {
				formFail = true;
				$('input#email').css('background-color', '#FAE9E9').next('samp').replaceWith('<div class="errorInfo">Ingresa tu email</div>');
			}
		}			
		if ( jQuery.trim( $('textarea#comment').val() ) == '' ) {
			formFail = true;
			whatError[3] = 'noComment';
			$('textarea#comment').css({'background-color': '#FAE9E9', 'margin-bottom': '0.8em'}).after('<div id="commentTextError" class="errorInfo">Ingresa tu comentario</div>');
		}
		
		
		if(formFail) {
			$('form#commentform').prepend('<div class="error" id="formError"><strong>Lo sentimos</strong><br />Faltan datos por completar o ingresaste un dato erróneo</div>');
			return false;
		}
	});

});


$.extend({ //extension de metodo jQuery
id : function(id) {
	var id = id.split("#");
	return document.getElementById(id[1])?true:false;
}
});