$(document).ready(function() {
	// Fancybox
	$("a.fancybox").fancybox();
	
	// Centrarse en los campos importantes al cargar
	$('input.focus').focus();
	
	// Texto de los campos del formulario de contacto (el que reemplaza los labels)
	$('form.sinlabel input[type=text], form.sinlabel input[type=password], form.sinlabel textarea').each(function() {
		if ($(this).val() == '') {
			$(this).val($(this).attr('data-value'));
			$(this).css('color', '#AAA');
		}
		else {
			if ($(this).val() != $(this).attr('data-value')) {
				$(this).css('color', '#555');
			}
		}
	});
	
	// Cambio de color de los campos del formulario de contacto
	$('form.sinlabel input[type=text], form.sinlabel input[type=password], form.sinlabel textarea').focus(function() {
		$(this).css('background-color', '#DDD');
		if ($(this).val() == $(this).attr('data-value')) {
			$(this).val('');
			$(this).css('color', '#555');
		}
	}).blur(function() {
		$(this).css('background-color', '#FFF');
		if ($(this).val() == '') {
			$(this).val($(this).attr('data-value'));
			$(this).css('color', '#AAA');
		}
	});
	
	// Borrar valores por defecto del formulario antes de enviar para activar el validador
	$('form.sinlabel').submit(function() {
		$(this).find('input[type=text], input[type=password], textarea').each(function() {
			if ($(this).val() == $(this).attr('data-value'))
				$(this).val('');
		});
	});
	
	// Cambia el label de color al marcar o desmarcar un checkbox
	function labelChecked(checkbox) {
		if ($(checkbox).attr('checked'))
			$(checkbox).siblings('label').addClass('checked');
		else
			$(checkbox).siblings('label').removeClass('checked');
	}
	
	$('input[type=checkbox]').each(function() {
		labelChecked($(this));
	});
	
	$('input[type=checkbox]').change(function() {
		labelChecked($(this));
	});
	
	// Cambia los mensajes de alerta de CakePHP por pop-ups
	/*$("#flashMessage, #authMessage").attr('title', 'Silent Designs');	
	$("#flashMessage, #authMessage").dialog();*/
	
	// Esconde los mensajes de alerta despues de algunos segundos
	$('.success').delay(6000).hide(600);
	
	// Funcion para copiar las direcciones de los articulos
	$('.dialog').dialog({autoOpen: false, width: 420});
	$("a.open").click(function() {
		$('#' + $(this).attr('data-id')).dialog('open');
		return false;
	});
	
	// Transforma los textarea a editores WYSIWYG
	$(".html textarea").elrte({height:"250px"});	
	
	// Modifca el estilo de los input para subir archivos
	$("form.ingresar input[type=file]").filestyle({ 
		'image': "/img/iconos/explorar.jpg",
		'imageheight': 32,
		'imagewidth': 50,
		'width': 582
	});
	
	// Scroll a links con #nombre
	$.localScroll();
	
	// Check ALL the checkboxes
	$('input[type=checkbox].checkall').change(function() {
		$(this).parents('form').find('input[type=checkbox]').attr('checked', $(this).is(':checked'));
	});
	
	// Confirmar antes de realizar la accion
	$("input.confirmar").click(function() {
		return confirm("¿Estas seguro?");
	});
	
	// Menu flotante de los servicios (ADVERTENCIA: Dejar linea de codigo siempre al ultimo!)
	$('#serviciosMenu').stickyfloat({duration: 800, delay: 100, easing: 'swing'});
});

