// when the DOM is ready...
$(document).ready(function () {

	var $panels = $('#slider .scrollContainer > div');
	var $container = $('#slider .scrollContainer');

	// if false, we'll float all the panels left and fix the width 
	// of the container
	var horizontal = true;

	// float the panels left if we're going horizontal
	if (horizontal) {
	  $panels.css({
		'float' : 'left',
		'position' : 'relative' // IE fix to ensure overflow is hidden
	  });
	  
	  // calculate a new width for the container (so it holds all panels)
	  $container.css('width', $panels[0].offsetWidth * $panels.length);
	}

	// collect the scroll object, at the same time apply the hidden overflow
	// to remove the default scrollbars that will appear
	var $scroll = $('#slider .scroll').css('overflow', 'hidden');

	// apply our left + right buttons
	/*$scroll
	  .before('<img class="scrollButtons left" src="images/scroll_left.png" />')
	  .after('<img class="scrollButtons right" src="images/scroll_right.png" />');*/

	// handle nav selection
	function selectNav() {
	  $(this)
		.parents('ul:first')
		  .find('a')
			.removeClass('selected')
		  .end()
		.end()
		.addClass('selected');
	}

	$('.navigation').find('a').click(selectNav);

	// go find the navigation link that has this target and select the nav
	function trigger(data) { 
	  var el = $('ul.navigation').find('a[href$="' + data.id + '"]').get(0);
	  selectNav.call(el);
	}

	if (window.location.hash) {
	  trigger( { id : window.location.hash.substr(1) });
	} else {
	  $('ul.navigation a:first').click();
	}

	// offset is used to move to *exactly* the right place, since I'm using
	// padding on my example, I need to subtract the amount of padding to
	// the offset.  Try removing this to get a good idea of the effect
	var offset = parseInt((horizontal ? 
	  $container.css('paddingTop') : 
	  $container.css('paddingLeft')) 
	  || 0) * -1;


	var scrollOptions = {
	  target: $scroll, // the element that has the overflow
	  
	  // can be a selector which will be relative to the target
	  items: $panels,
	  
	  navigation: '.navigation a',
	  
	  // selectors are NOT relative to document, i.e. make sure they're unique
	//  prev: 'img.left', 
	//  next: 'img.right',
	  
	  // allow the scroll effect to run both directions
	  axis: 'xy',
	  
	  onAfter: trigger, // our final callback
	  
	  offset: offset,
	  
	  // duration of the sliding effect
	  duration: 500,
	  
	  // easing - can be used with the easing plugin: 
	  // http://gsgd.co.uk/sandbox/jquery/easing/
	  easing: 'swing'
	};

	// apply serialScroll to the slider - we chose this plugin because it 
	// supports// the indexed next and previous scroll along with hooking 
	// in to our navigation.
	$('#slider').serialScroll(scrollOptions);

	// now apply localScroll to hook any other arbitrary links to trigger 
	// the effect
	$.localScroll(scrollOptions);

	// finally, if the URL has a hash, move the slider in to position, 
	// setting the duration to 1 because I don't want it to scroll in the
	// very first page load.  We don't always need this, but it ensures
	// the positioning is absolutely spot on when the pages loads.
	scrollOptions.duration = 1;
	$.localScroll.hash(scrollOptions);

	var mensajes = {
	  nombre: 'Ingresa tu nombre',
	  email: { required: 'Ingresa tu direcci&oacute;n e-mail', email: 'Ingresa un email v&aacute;lido'},
	  mensaje: { required: 'Ingresa tu comentario o sugerencia'}
	}

	// Reglas que vamos a aplicar
	var reglas = {
	  nombre: 'required',
	  email: { required: true, email: true },
	  mensaje: { required: true }
	}

	var v = jQuery("#form").validate({
		  rules: reglas,
		  messages: mensajes,
		  debug: false,
		  errorElement: 'em',
		  errorContainer: $( '#result' ),
		  submitHandler: function(form) { 
			  $('#result_form').show();
			  $.ajax({
				type : 'post',
				url  : 'post.php',
				data : 'nombre='+$('#nombre').val()+'&email='+$('#email').val()+'&mensaje='+$('#message').val(),
				target: "#result",
				success : function (text) {									
					if (text)
					{
						$('#form').remove();
						$('#result_form').html('<p class="alert">Gracias por tus comentarios, en breve me pondre en contacto.</p>');
					}
					else
					{
						$('#result_form').html('<p class="error">Ha ocurrido un error en el servidor, por favor inténtalo de nuevo</p>');
					}
				}
			  });
		  }
	});

	$(".main_image .desc").show(); //Show Banner
	$(".main_image .block").animate({ opacity: 0.85 }, 1 );

	$(".image_thumb ul li:first").addClass('active'); //Add the active class (highlights the very first list item by default)
	$(".image_thumb ul li").click(function(){
		//Set Variables
		var imgAlt = $(this).find('img').attr("alt"); //Get Alt Tag of Image
		var imgTitle = $(this).find('a').attr("href"); //Get Main Image URL
		var imgDesc = $(this).find('.block').html();  //Get HTML of the "block" container
		var imgDescHeight = $(".main_image").find('.block').height(); //Find the height of the "block"

		if ($(this).is(".active")) {  //If the list item is active/selected, then...
			return false; // Don't click through - Prevents repetitive animations on active/selected list-item
		} else { //If not active then...
			//Animate the Description
			$(".main_image .block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 250 , function() { //Pull the block down (negative bottom margin of its own height)
					$(".main_image .block").html(imgDesc).animate({ opacity: 0.85,	marginBottom: "0" }, 250 ); //swap the html of the block, then pull the block container back up and set opacity
					$(".main_image img").attr({ src: imgTitle , alt: imgAlt}); //Switch the main image (URL + alt tag)
				});
			}
			//Show active list-item
			$(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all list-items
			$(this).addClass('active');  //Add class of 'active' on the selected list
			return false; 

		}) .hover(function(){ //Hover effects on list-item 
			$(this).addClass('hover'); //Add class "hover" on hover 
			}, function() {
			$(this).removeClass('hover'); //Remove class "hover" on hover out
		});

		$("a.collapse").click(function(){
			$(".main_image .block").slideToggle(); //Toggle the description (slide up and down)
			$("a.collapse").toggleClass("show"); //Toggle the class name of "show" (the hide/show tab)
		});
});