jQuery.fn.nospam = function(settings) {
	settings = jQuery.extend({
		replaceText: false, 	// optional, accepts true or false
		filterLevel: 'normal' 	// optional, accepts 'low' or 'normal'
	}, settings);

	return this.each(function(){
		e = null;
		if(settings.filterLevel == 'low') { // Can be a switch() if more levels added
			if($(this).is('a[rel]')) {
				e = $(this).attr('rel').replace('//', '@').replace(/\//g, '.');
			} else {
				e = $(this).text().replace('//', '@').replace(/\//g, '.');
			}
		} else { // 'normal'
			if($(this).is('a[rel]')) {
				e = $(this).attr('rel').split('').reverse().join('').replace('//', '@').replace(/\//g, '.');
			} else {
				e = $(this).text().split('').reverse().join('').replace('//', '@').replace(/\//g, '.');
			}
		}
		if(e) {
			if($(this).is('a[rel]')) {
				$(this).attr('href', 'mailto:' + e);
				if(settings.replaceText) {
					$(this).text(e);
				}
			} else {
				$(this).text(e);
			}
		}
	});
};

jQuery(document).ready(function($) {
	$('.email').nospam({
		replaceText: true
	});

	var item_width = 272;
	$('div.portfolio-box').each(function() {
		var current_item = 1;
		var list = $(this).find('ul.portfolio-holder');
		var items = list.find('li');
		if (!items.length) return;
		
		$(this).find('a.next').click(function() {
			var items = list.find('li');
			if (current_item <= items.length - 3) {
				current_item++;
			} else {
				var old_item = list.find('li:first');
				list.css({
					marginLeft: - (current_item - 2) * item_width
				});
				old_item.clone().appendTo(list);
				old_item.remove();
			}
			list.stop().animate({marginLeft: - (current_item - 1) * item_width});
			return false;
		});
		
		$(this).find('a.prev').click(function() {
			var items = list.find('li');
			if (current_item > 1 ) {
				current_item--
			} else {
				var old_item = list.find('li:last');
				list.css({
					marginLeft: - (current_item) * item_width
				});
				old_item.clone().prependTo(list);
				old_item.remove();
			}
			list.stop().animate({marginLeft: - (current_item - 1) * item_width});
			return false;
		});
		
		list.css({
			marginLeft: 0,
			width: items.length * item_width
		});
	});
	
	/*$('ul.portfolio-holder a, ul.portfolio-page a, a.map').lightBox({
		imageLoading: 'assets/img/lightbox-ico-loading.gif',
		imageBtnClose: 'assets/img/lightbox-btn-close.gif',
		imageBtnPrev: 'assets/img/lightbox-btn-prev.gif',
		imageBtnNext: 'assets/img/lightbox-btn-next.gif',
		txtImage: 'Imagen',
		txtOf: 'de'
	});*/

	$('ul.portfolio-holder a, ul.portfolio-page a, a.map').fancybox();

	$('.contact-form input[type=text], .contact-form textarea').focus(function() {
		if (this.value == this.defaultValue) this.value = '';
	}).blur(function() {
		if (this.value == '') this.value = this.defaultValue;
	});
	
	$('#contact-form').submit(function() {
		var incomplete = false;
		var email_filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
		$('#contact-messages').hide();
		
		$('#form-name, #form-email, #form-comments').removeClass('error').each(function() {
			if ($(this).val() && $(this).val() != this.defaultValue) return;
			$(this).addClass('error');
			incomplete = true;
		});
		
		if (incomplete) {
			$('#contact-messages').text('Por favor, complete todos los campos requeridos.').slideDown();
			return false;
		}
		
		if (!email_filter.test($('#form-email').val())) {
			$('#contact-messages').text('Por favor, ingrese una dirección de e-mail válida.').slideDown();
			return false;
		}
		
		$('<div id="layer-bg"></div>').appendTo('body').css({
			height: $(document).height(),
			width: $(document).width(),
			opacity: 0.75
		}).fadeIn();
		
		$('<div id="contact-sending"></div>').appendTo('body').text('Enviando contacto...').css({
			top: ($(window).height() - $('#contact-sending').height()) / 2,
			left: ($(window).width() - $('#contact-sending').width()) / 2,
			cursor: 'normal'
		}).fadeIn().unbind('click');
		
		$.ajax({
			url: 'contacto.php',
			type: 'post',
			dataType: 'json',
			data: {
				name: $('#form-name').val(),
				company: $('#form-company').val(),
				email: $('#form-email').val(),
				comments: $('#form-comments').val(),
				method: 'ajax',
				action: 'contact'
			},
			success: function(resp) {
				if (resp.success) {
					$('#contact-sending').html('Su contacto se envió con éxito. Le responderemos a la brevedad.').click(function() {
						location.href = 'index.php';
					}).css({cursor: 'pointer'});
				} else {
					$('#contact-sending').html('<p>Ocurrió un error. Por favor, inténtelo nuevamente.</p><p><a href="#">Aceptar</a></p>').click(function() {
						$('#contact-sending, #layer-bg').remove();
					}).css({cursor: 'pointer'});
				}
			},
			error: function() {
				$('#contact-sending').html('<p>Ocurrió un error. Por favor, inténtelo nuevamente.</p><p><a href="#">Aceptar</a></p>').click(function() {
					$('#contact-sending, #layer-bg').remove();
				}).css({cursor: 'pointer'});
			}
		});
		
		return false;
	});
});