jQuery.fn.buscDef = function() { // limpia cajas de búsqueda; si no se modifica, devuelve valores originales
	var orVal = jQuery(this).val();
	if ( jQuery.trim(orVal) != '' ) {
		jQuery(this).data( 'defVal', orVal )
	} else {
		jQuery(this).data( 'defVal', 'Buscar' )
	}
	jQuery(this).blur(function() {
			if ( jQuery(this).val() == '' ) {
			jQuery(this).val( jQuery(this).data('defVal') )
			}
		}).focus(function() {
			jQuery(this).select();
			if ( jQuery(this).val() == jQuery(this).data('defVal') ) {
				jQuery(this).val('');
			}
		});
}
var ajax = {
	ajax_error_management : function(errorCode){
		if (errorCode == 404) {
			error_message = 'No se ha encontrado el contenido solicitado';
		} else if ( errorCode == 401 || errorCode == 403 ){
			error_message = 'No está autorizado a ver este contenido. Por favor inice sesión e inténtelo nuevamente';
		} else if ( errorCode == 408 ) {
			error_message = 'Se ha agotado el tiempo de espera para esta solicitud. Por favor inténtelo nuevamente';
		} else if ( errorCode ==  503 ) {
			error_message = 'Temporalmente fuera de servicio. Por favor espere un momento e inténtelo nuevamente';
		} else { // default
			error_message = 'Ha ocurrido un error. Por favor inténtelo nuevamente';
		}
		alert(error_message); // This could be prettier
	},
	get_contents : function(e){
		$e = $(e);
		if ( $e.parent().hasClass('active') ) {
			return false;
		} else if ( $e.attr('rel').match('ajax') ) {
			// Retrieve AJAX Content
			$main_nav = $('#main-nav');
			$content_wrap = $('#content-wrap');
			$activate_content = $('#' + $e.attr('rev') + '-loader');
			title = $e.text();
			$('li.active', $main_nav).removeClass('active');
			$e.parent().addClass('active');
			$('div.active-content', $content_wrap).fadeTo('slow', 0.3, function(){
				$content_wrap.addClass('loading');
				$.ajax({
					url: $e.data('ajax_target'),
					dataType: 'html',
					dataFilter : function(data, type){
							var $content = $('#content', data);
							var out = $content.removeAttr('id');
							if ( $activate_content.attr('id').match('editorial-team') ) {
								$('a', $content).each(function(){
									$(this).replaceWith('<strong>'+ $(this).text() +'</strong>');
								})
							}
							return out;
						},
					success: function(data){
							$activate_content.
								html(data).
								prepend('<h2>'+ title + '</h2>').
								addClass('loaded');
							$e.removeAttr('rel');
							new_height = ( $activate_content.outerHeight(true) > $('#sidebar').outerHeight(true) ) ? $activate_content.outerHeight(true) : $('#sidebar').outerHeight(true);
							$content_wrap.removeClass('loading').height(new_height+30);
							$('div.active-content', $content_wrap).removeClass('active-content').removeAttr('style');
							$activate_content.addClass('active-content');
						},
					error: function(XMLHttpRequest, textStatus, errorThrown){
							$activate_content.
								removeClass('loading').
								html('<p>Ha ocurrido un error. Por favor vuelva a intentarlo</p>');
						}
				});				
			});
		} else {
			$main_nav = $('#main-nav');
			$content_wrap = $('#content-wrap');
			$activate_content = $($e.attr('href')+'-loader');
			$('li.active', $main_nav).removeClass('active');
			$e.parent().addClass('active');
			ajax.animate_height( $activate_content, $content_wrap );
		}
	},
	animate_height : function($activate_content, $content_wrap ){
			new_height = ( $activate_content.outerHeight(true) > $('#sidebar').outerHeight(true) ) ? $activate_content.outerHeight(true) : $('#sidebar').outerHeight(true);
			$content_wrap.animate(
				{
					height: new_height + 30
				},
				0,
				function(){
					$('div.active-content', $content_wrap).removeClass('active-content');
					$activate_content.addClass('active-content');
				}
			);		
	}
}
jQuery(document).ready(function($){
	if ( $.browser.msie && ( $.browser.version == '6.0' || $.browser.version == '7.0' ) ) $('#search-submit').val('');
	$('#query').buscDef();
	
	/**
	 * Programatically replace ids, so we can use AJAX Bookmarks
	 */
	$content_boxes = $('#content-wrap .content-box');
	content_boxes_count = $content_boxes.length;
	for ( i = 0; i < content_boxes_count; i++ ) {
		this_id = $($content_boxes[i]).attr('id');
		$($content_boxes[i]).attr('id', this_id+'-loader');
	}
	
	/**
	 * Replace links in AJAX requests
	 */
	$ajax_links = $('a[rel=ajax]');
	ajax_links_count = $ajax_links.length;
	for ( i = 0; i < ajax_links_count; i++ ) {
		$($ajax_links[i]).
			data('ajax_target', $($ajax_links[i]).attr('href')).
			attr('href', '#' + $($ajax_links[i]).attr('rev'));
	}
	
	$('#main-nav li a').click(function(e){ ajax.get_contents(e.target) });
	
	$('.colorbox-content').each(function(){
		original_href = $(this).attr('href');
		$(this).attr('href', original_href+'-loader');
		cb_width = ( $(this).parents('#login-actions').length == 1 ) ? 300 : 600;
		cb_height = ( $(this).parents('#login-actions').length == 1 ) ? 320 : 500;
		$(this).colorbox({
			inline: true,
			width: cb_width,
			height: cb_height,
			href: $(this).attr('href')
		});
	});
	
	$('#indexes tbody tr:odd').addClass('alt');
	
	if ( location.hash ) {
		ajax.get_contents( $('#main-nav a[href='+ location.hash +']') );
	}
});
