$(document).ready(function() {


	/* SITE NAVIGATION
	----------------------------------------- */
	
	// 1. Get the items we are working on
	var page_class = $('body').attr('class');
	var $current_selected = (page_class != '')? $('body > header > nav.primary > ul.public > li.' + page_class) : $('body > header > nav.primary > ul.public > li.none');
	var $parent_menus = $('body > header > nav.primary > ul.public > li').not($current_selected).children('ul').siblings('a');
	
	// 2. Add the selected class to the current selection, and bind the mouseover and mouseout methods
	$current_selected.children('ul').parent().addClass('selected');
	$parent_menus.bind({'mouseenter': display_submenu});
	$parent_menus.parent().bind({'mouseleave': hide_submenu});
	
	// 3. Display menu method
	function display_submenu(e) {
	
		// Get the data we need
		var $target = $(e.target);
		var $parent_elem = $target.parent();
		
		// Change the class of the current selected item to 'un_selected'
		$current_selected.children('ul').parent().removeClass('selected').addClass('un_selected');
		
		// Set the selected class on this items parent
		$parent_elem.addClass('selected');
		
	}
	
	// 4. Hide menu method
	function hide_submenu(e) {
	
		// Change the class of the current selected to 'selected' again
		$current_selected.children('ul').parent().removeClass('un_selected').addClass('selected');
		
		// Remove the selected class from all items except the selected on 
		$parent_menus.parent().removeClass('selected');
	
	}
	
	
	/* SLIDESHOW
	----------------------------------------- */
	
	// Apply the scrollable plugin to the slideshow
	$('#slideshow').each(function(){
	
		// Get the items we will be working on
		var $scroll_area = $(this).find('.scroll_area');
		var $scroll_next = $(this).find('.next');
		var $scroll_prev = $(this).find('.previous');
		
		// Remove the default methods from links
		$scroll_next.click(function(){return false});
		$scroll_prev.click(function(){return false});
		
		// Apply the scrollable plugin
		$scroll_area.scrollable({ 
			horizontal: true,
			next: $scroll_next,
			prev: $scroll_prev,
			circular: true
		}).autoscroll({ interval: 10000 });
	
	});
	
	
	/* SCROLLABLE CONTENT
	----------------------------------------- */
	
	// Apply the scrollable plugin to all scrollable elements
	$('.scrollable').each(function(){
		
		// Get the items we will be working on
		var $scroll_area = $(this).find('.scroll_area');
		var $scroll_next = $(this).find('.next');
		var $scroll_prev = $(this).find('.previous');
		
		// Remove the default methods from links
		$scroll_next.click(function(){return false});
		$scroll_prev.click(function(){return false});
		
		// Apply the scrollable plugin
		$scroll_area.scrollable({ 
			vertical: true,
			next: $scroll_next,
			prev: $scroll_prev,
			mousewheel: true
		});
		
		
	});
	
	
	/* IMAGE GALLERIES
	----------------------------------------- */
	
	// Apply the Lightbox plugin to all image galleries
	$('.gallery ul a').fancybox();
	
	
	/* FOOTER
	----------------------------------------- */
	
	// 1. Get the footer content to show/hide
	var $footer_data = $('body > footer .collapse');
	var $wrapper = $footer_data.parent();
	var wrapper_height = $wrapper.height();
	$footer_data.hide();
	$footer_data.after('<a href="#" class="switch open">Mas Contenido</a>');
	$wrapper.height(0);
	
	// 2. Get the inserted switch link
	var $switch_link = $('body > footer a.switch');
	
	// 3. Set the click method for the switch link
	$switch_link.click(function(){
		
		// Check the current state
		if ($(this).hasClass('open')) {
			
			$wrapper.animate({height:wrapper_height}, {duration:250, complete:function() {
				$footer_data.fadeIn('slow');
			}});
			$(this).removeClass('open').addClass('close');
			$(this).text('Cerrar');
			
		} else {
			
			$footer_data.fadeOut('fast', function(){
				$wrapper.animate({height:0}, {duration:250});
				
			});
			$(this).removeClass('close').addClass('open');
			$(this).text('Mas Contenido');
			
		}
		
		return false;
		
	});
	
	

});
