jQuery.noConflict();
jQuery(document).ready(function() {
	/* Oculta botones noscript */
	jQuery('input.load').hide().end();
	
	/* Imprimir */
	jQuery(".script").show();
	if(jQuery('#imprimir').length) {
		jQuery('#imprimir').show();
		jQuery("#imprimir").css('cursor','pointer');
		jQuery('#imprimir').bind(($.browser.msie ? "click" : "change"),function() {
			window.print();
			return false;
		});
	}

	/* Enlaces externos */
	jQuery('.external').click(function () {
		window.open(this.href);
		return false;
   	});
	
	/* Idiomas */
	if(jQuery('#idiomas ul li').length>1) {
		jQuery('#idiomas').tabs();
	} else {
		jQuery('#idiomas ul').hide();
	}
	jQuery('form').submit(function() {
		jQuery('#anchor').val(window.location.hash);
	});
	
	/* Confirmar */
	jQuery('.confirm').click(function() {
		if(confirm('Se va a proceder a eliminar el objeto seleccionado.\n¿Está seguro/a?')) {
			return true;
		}
		return false;
	});
	
	/* Expandir/Contraer */
	jQuery(".show_more").each(function() {
		if(!jQuery(this).hasClass('down')) {
			jQuery(this).next(".more").slideUp("fast");
		}
		jQuery(this).click(function () {
			var bgimage = jQuery(this).css('backgroundImage');
			if(jQuery(this).next(".more").is(":visible")) {
				jQuery(this).removeClass('down');
				jQuery(this).next(".more").slideUp("fast");
			}
			else {
				jQuery(this).addClass('down');
				jQuery(this).next(".more").slideDown("fast");
			}
		});
	}).css('cursor', 'pointer');
	
	/* Menu */
	jQuery("ul.topnav li a").click(function() {
		//Following events are applied to the subnav itself (moving subnav up and down)
		jQuery(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click
		jQuery(this).addClass("subNavOver");
		jQuery(this).parent().hover(function() {
		}, function(){	
			jQuery(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
			jQuery(this).removeClass("subNavOver");			
		});
		if(jQuery(this).next('ul').length) {
			return false;
		}
		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() { 
			jQuery(this).addClass("subNavOver");
		}, function(){	//On Hover Out
			jQuery(this).removeClass("subNavOver");
	});
	
	/* Login */
	if(jQuery("#login form").length) {
		jQuery("#login form").submit(function() {
			xajax_login(jQuery(this).children('div').children("input[name='usuario']").val(), jQuery(this).children('div').children("input[name='password']").val());
			return false;
		});
	}
	
	/* Panel */
	if(jQuery('#panel').length) {
		jQuery('#acciones h4').css('cursor','pointer');
		currentIndex = (jQuery("#acciones h4.selected").prevAll('h4').length);
		if(currentIndex < 0) currentIndex = 0;
		jQuery('#acciones').accordion({ 
			autoheight: false,
			header: "h4",
			active: currentIndex
		});
	}
	
	/* Categorías */
	if(jQuery('#categorias').length) {
		jQuery('#categorias h3').css('cursor','pointer');
		currentIndex = jQuery("#categorias h3.selected").index() == 2 ? 1 : 0;
		jQuery('#categorias').accordion({ 
			autoheight: false,
			header: "h3",
			active: currentIndex
		});
	}
	if(jQuery('#subcategorias').length) {
		jQuery('#subcategorias h4').css('cursor', 'pointer');
		currentIndex = jQuery("#subcategorias h4.selected").index() == 2 ? 1 : 0;
		jQuery('#subcategorias').accordion({
			autoheight: false,
			header: "h4",
			active: currentIndex
		});
	}
	
	/* Captcha */
	if(jQuery("#captcha").length) {
		jQuery("#captcha").captcha();
	}
	
	/* Mensajes */
	jQuery('#mensaje .cerrar').show();
	jQuery('#mensaje .cerrar').click(function(){
		jQuery(this).parent().fadeOut('slow', function() {
			jQuery(this).remove();
		});
	});
	
	/* jQuery UI Dialog */
	jQuery("#dialog").dialog({
		bgiframe: true,
		height: 'auto',
		width: 420,
		modal: true,
		autoOpen: false,
		draggable: false,
		resizable: false,
		show: 'bounce',
		hide: 'blind'
	});

	/* jQuery mbTooltip */
	jQuery("[title]").mbTooltip();
	
	if(jQuery("table.tablesorter, table.tabla").length) {
		jQuery('td input:text').each(function() {
			this.style.textAlign='center';
			this.style.background='none';
			this.style.border='none';
			//this.style.color='#000';
			//this.className='test';
			this.onclick=this.onfocus=function() {
				this.style.background='#fff';
				this.style.border='1px solid #292929';
				//this.style.color='#000';
			};
			this.onblur=function() {
				this.style.background='none';
				this.style.border='none';
				//this.style.color='#fff';
				var id = this.id.split('_');
				xajax_guardar(id[0], id[1], id[2], this.value);
			};
		});
		
		jQuery('td input:checkbox').each(function() {
			this.onclick=this.onfocus=function() {
				var id = this.id.split('_');
				xajax_guardar(id[0], id[1], id[2], this.checked);
			};
		});
		if(jQuery("table.tablesorter").length) {
			/**
			 * Busca la cabecera 'orden' y si la encuentra la ordena de forma ascendente.
			 */
			var sortList = [];
			for(var c=0;c<jQuery("table.tablesorter th").length;c++) {
				if(jQuery("table.tablesorter th")[c].className == "orden") {
					sortList = [[c,0]];
					break;
				}
			}
			
			jQuery("table.tablesorter").tablesorter({sortList:sortList, widgets: ['zebra']});
			jQuery("table.tablesorter thead tr th.acciones").css({cursor:'default'}).unbind();
		}
	}
	
	/* Soporte */
	jQuery('#soporte').click(function() {
		xajax_peticion(jQuery(location).attr('href'));
		return false;
	});
});
