///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
var timeout    	= 500;
var close_timer = 0;
var current_nav = 0;
	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
$(document).ready(function(){		
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Nav Menu
	
	
	function nav_open() {
		nav_cancel_timer();
		current_nav = this.id;
		
		//$(".nav_dropdown:not(#"+current_nav+"_dropdown").fadeOut(100);
		// Must use below for safari? alas...
		if(current_nav != 'nav_skate'){
			$("#nav_skate_dropdown").fadeOut(100);
		}
		if(current_nav != 'nav_film') {
			$("#nav_film_dropdown").fadeOut(100);
		}
		if(current_nav != 'nav_culture') {
			$("#nav_culture_dropdown").fadeOut(100);
		}
		if(current_nav != 'nav_travel') {
			$("#nav_travel_dropdown").fadeOut(100);
		}
		
		$("#"+current_nav+"_dropdown").fadeIn(100);
	}
	
	function nav_close() {
		if(current_nav){
			$("#"+current_nav+"_dropdown").fadeOut(100);
		}
	}

	function nav_timer() {
		close_timer = window.setTimeout(nav_close, timeout);
	}
	
	function nav_cancel_timer() {
		if(close_timer) {
			window.clearTimeout(close_timer);
			close_timer = null;
		}
	}

	$('.nav_header').bind('mouseover', nav_open)
	$('.nav_header').bind('mouseout',  nav_timer)
	document.onclick = nav_close;
	
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Pane Actions
	
	
	$('.homepage_cyle_delay').cycle({
		easing:  'backinout',
		delay: 2000
	});
	
	$('.homepage_cyle').cycle({
		easing:  'backinout'
	});
	
	$("#travel_hit").hover(
		function () {
			$('#travel_overlay').fadeIn(300);
		}, 
		function () {
			$('#travel_overlay').fadeOut(300);
		}
	);

	$(".gallery_item").click(function(event){
		
		id = $(this).attr('id').substr(3);
		table = $('#table').html();
		if(table == 'compadres'){
			title = $(this).children('img').attr('title');
		} else {
			title = $('#title_'+id).html();
		}
		
		$.ajax({
			url: "get_slideshow.php?id="+id+"&table="+table+"&caption="+title,
			type: "POST",
			success: function(msg){
				
				$('#pane_image').html(msg);
				if(table == 'compadres'){
					$('#pane_text').html(title);
				} else {
					$('#pane_text').html(title);
				}
				$('#pane_image').cycle({
					next:   '#pane_next',
					prev:   '#pane_prev',
					after: on_cycle,
					timeout:  0,
					easing:  'backinout'
				});
				
				$("#pane_container").fadeIn(500);
				
			}
		});	
		
	});
	
	function on_cycle() {
		//table = $('#table').html();
		//if(table == 'compadres'){
			$('#pane_text').html(this.title);
		//}
	}
	
	$("#pane_next").hover(
		function () {
			$('#pane_next img').fadeIn(300);
		}, 
		function () {
			$('#pane_next img').fadeOut(300);
		}
	);
	
	$("#pane_prev").hover(
		function () {
			$('#pane_prev img').fadeIn(300);
		}, 
		function () {
			$('#pane_prev img').fadeOut(300);
		}
	);
	
	$("#pane_close").click(function(event){
		$("#pane_container").animate({
			opacity: 0.0
	  	}, 500, function() {
    		$(this).css('display', 'none');
			$(this).css('opacity', 1.0);
  		});
		// WTF? fadeOut doesn't work
	});
	
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	
	$("#nav_skate").hover(
		function () {
			$('#dropdown_skate').fadeIn(100);
		}, 
		function () {
			$('#dropdown_skate').fadeOut(100);
		}
	);
	
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
});
