jQuery(function($) {
	
	if ( $('.flecha-nav-izquierda').length ) {
		$('.flecha-nav-izquierda').attr('href', $('a', $('#navegacion li.activo').prev() ).attr('href') );
		$('.flecha-nav-derecha').attr('href', $('a', $('#navegacion li.activo').next() ).attr('href') );
	}
	
	if ( $('body#productos').length ) {
		$('#contenido div.marca:nth-child(3n)').css('margin-right','0px');
		$('#contenido img.galeria-interior').each(function(){
			if ( $('#'+$(this).attr('rel')).data('imagenes') == undefined ) {
				$('#'+$(this).attr('rel')).data('imagenes',[]);
			}
			$('#'+$(this).attr('rel')).data('imagenes').push({
				'href': 	$(this).data('ruta'),
				'title':	$(this).attr('title')
			});
		});
		$('#contenido a.galeria-producto').click(function(){
			$.fancybox( $(this).data('imagenes'), {
				'padding'			: 0,
				'type'              : 'image',
				'titlePosition'		: 'inside',
				'titleFormat'		: function(title, currentArray, currentIndex, currentOpts){
					return '<span class="titulo">' + title + '</span><span class="indice">'+ ( currentIndex + 1 ) + '/' + currentArray.length +'</span>';
				},
				'onComplete'		: function() {
					Cufon.replace('.fancybox-title-inside');
				}
			});
			return false;
		});
	}
	
	// Formularios
	$('form.rpc').live('submit', function() {
		$(this).ajaxSubmit({
			dataType:	'json',
			beforeSubmit:	function(formData, form, options) {
				this.form = form;
				$('input[type="submit"]', this.form).attr('disabled', 'disabled');
				form.addClass('cargando');
			},
			success:	function( respuesta ) {
				$('input[type="submit"]', this.form).removeAttr('disabled');
				this.form.removeClass('cargando');
				if ( respuesta.exito != 1 ) {
					if ( respuesta.mensaje ) alert( respuesta.mensaje );
					return false;
				}
				this.form.append('<p>Tu mensaje ha sido enviado</p>');
				setTimeout("$('#formulario-contacto p:last').fadeOut();", 5000 );
				Cufon.replace('#formulario-contacto p:last');
				$('input[type="text"], textarea', this.form).val('');
			},
			// There be dragons...
			error:	function( jqXHR, textStatus, errorThrown ) {
				$('input[type="submit"]', this.form).removeAttr('disabled');
				this.form.removeClass('cargando');
				console.log( arguments );
				alert('Error inesperado en el formato de respuesta del servidor.');
			}
		});
		return false;
	});
});
Cufon.replace('#navegacion li a, #contenido.zag, button, .link-exterior');
