$(document).ready(function(){	
	$.easy.navigation();
	$.easy.tooltip();
	$.easy.popup();
	$.easy.external();
	$.easy.rotate();
	$.easy.forms();
	$.easy.showhide();
	$.easy.jump();
	
	// Form placeholder
	$.fn.replaceholder({
		focusColor: "#000",
		blurColor: "#000"
	});
	
	// Rollover top-nav
	var $current_topnav = $('#top-nav li.current');
	$('#top-nav li').hover(function () {
		if ($(this).children('a').attr('href') != $current_topnav.children('a').attr('href')) {
			var rel = $(this).attr('rel');
			$(this).addClass(rel + '-current');
		}
	},
	function () {
		if ($(this).children('a').attr('href') != $current_topnav.children('a').attr('href')) {
			var rel = $(this).attr('rel');
			$(this).removeClass(rel + '-current');		
		}
	
	});
	
	var $current_featurednav = $('#nav-featured li.current');
	$('#nav-featured li').hover(function () {
		if ($(this).children('a').attr('href') != $current_featurednav.children('a').attr('href')) {
			var rel = $(this).attr('rel');
			$(this).addClass(rel + '-current');
		}
	},
	function () {
		if ($(this).children('a').attr('href') != $current_featurednav.children('a').attr('href')) {
			var rel = $(this).attr('rel');
			$(this).removeClass(rel + '-current');		
		}
	
	});
	
	// Scrollpane
	if ($(".scrollable").length > 0)
		$(".scrollable").jScrollPane({scrollbarMargin: 0,reinitialiseOnImageLoad: true});
	
	$('#opt-header .opt-nav a').hover(function() {
		$(this).parent().addClass('hover');
	}, function () {
		$(this).parent().removeClass('hover');
	});
	
	$('#opt-content .opt-nav a').hover(function() {
		$(this).parent().addClass('hover');
	}, function () {
		$(this).parent().removeClass('hover');
	});
	
	// Scrollpane nav
	$('#opt-content .opt-nav a').click(function() {
		var id = $(this).attr('href');

		$('#opt-content .tab-group').hide();
		$('#opt-content '+ id).show();		
		$('#opt-content .opt-nav li.current').removeClass('current');
		$(this).parent().addClass('current');

		if ($('#opt-content '+ id +' .scrollable').length > 0)
			$('#opt-content '+ id +' .scrollable').jScrollPane({scrollbarMargin: 0,reinitialiseOnImageLoad: true});
	});
	
	// Swf Slideshow
	var csscolor = $('#top-nav li.current').css('border-bottom-color');
	csscolor = rgb2hex(csscolor);
	$('#gallery-now-slideshow').flash({
		swf: $('#gallery-now-slideshow').attr('rel'),
		width: 615,
		height: 300,
		flashvars: {
			urlxml: $('#gallery-now-slideshow').attr('param'),
			csscolor: csscolor,
			rootUrl: 'http://www.theprivatespacebcn.com/'
		}
	});

	$('#slideshow-large').flash({
		swf: $('#slideshow-large').attr('rel'),
		width: 690,
		height: 330,
		flashvars: {
			urlxml: $('#slideshow-large').attr('param'),
			csscolor: csscolor,
			rootUrl: 'http://www.theprivatespacebcn.com/'
		}
	});

	$('#slideshow-xsmall').flash({
		swf: $('#slideshow-xsmall').attr('rel'),
		width: 360,
		height: 290,
		flashvars: {
			urlxml: $('#slideshow-xsmall').attr('param'),
			csscolor: csscolor,
			rootUrl: 'http://www.theprivatespacebcn.com/'
		}
	});
	
	// truncate summary
	$('.summary').summary();
	
	// Photobook Display
	$("#photobook-display-carousel").jCarouselLite({
    btnNext: ".next",
    btnPrev: ".prev",
    visible: 6
	});
	
	$("#photobook-display-carousel li a").click(function() {
		$("#photobook-display img").attr('src', $(this).attr('href'));
		$("#photobook-display h3").text($(this).attr('title'));	
		return false;
	});
	
	// Forgot password
	$('#forgot-password-link').click(function() {
		$('#forgot-password').show();	
		return false;
	});

	$('#forgot-password').submit(function() {
		var form = $(this);
		var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

		if (form.find('input[name="front-end-authentication[username]"]').val() == "" || !form.find('input[name="front-end-authentication[username]"]').val().match(re)) {
			alert('Debes introducir una dirección de correo electrónico válida');
			return false;
		}
	});
	
	// Submit
	$('#photobook-contact-form, #register-form').submit(function() {
		var form = $(this);

		if (form.find('input[name="fields[username]"]').val() == "") {
			alert('Debes introducir una dirección de correo electrónico válida');
			return false;
		}

		if (form.find('input[name="fields[password]"]').val() == "") {
			alert('Debes introducir una contraseña');
			return false;
		}

		if (form.find('input[name="fields[password]"]').val() != form.find('input[name="fields[re-password]"]').val()) {
			alert('Las contraseñas no coinciden.');
			return false;
		}

		if (!form.find('input[name="accept"]').is(':checked')) {
			alert('Debes aceptar las condiciones de la política de provacidad.');
			return false;
		}
	});
});

function rgb2hex(rgb) {
 if (rgb != undefined) {
	 rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
	 function hex(x) {
	  return ("0" + parseInt(x).toString(16)).slice(-2);
	 }
	 return  hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
	}
	return 'cccccc';
}
