// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
jQuery.ajaxSetup({
	'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript");}
});

$.tools.tabs.addEffect("GH-ajax", function(i, done) {
	var pane = this.getPanes().eq(0);
	$.get(this.getTabs().eq(i).attr("href"), function (data) {
		pane.html(data);
		done();
	});
});

$(document).ready(function () {
	// Set Peak and Footer to initial states
	$('#peak').css({height:0});
	$('#footer').hide();
	
	$('#radio').currentRadio();
	
	// Setup Email Subscriber Form
	$('#new_subscriber').submit(function () {
		var isValid = $('#new_subscriber').valid();
		if(isValid) {
			$.post($(this).attr('action'), $(this).serialize(), null, 'script');
		}
		return false;
	}).find('.submit img')
	.click(function () {
		$('#new_subscriber').submit();
		
	}).parent().find('.button').css({display:'none'});
	
	
	$('#new_subscriber').validate({
		showErrors: function(errorMap, errorList) {}
	});
	
	var initialValue = $('#subscriber_email').val();
	$('#subscriber_email').focus(function() {
		if(jQuery.trim(this.value) == initialValue) {
			this.value = '';
		}
	}).blur(function() {
		if(jQuery.trim(this.value) == '') {
			this.value = initialValue;
		}
		this.value = jQuery.trim(this.value);
		toggleButton();
	}).keyup(toggleButton).change(toggleButton);
	
	function toggleButton() {
		var isValid = $('#new_subscriber').valid();
		if(isValid) {
			$('#new_subscriber .submit img').fadeIn('slow');
		} else {
			$('#new_subscriber .submit img').fadeOut('slow');
		}
	}
	
	
	
	
	
	
	
	
	// Apply Mask
	$('#mask').addClass('js');
	
	// Main Tabs
	$("ul.tab-menu").tabs("div.panes > div", {effect:'GH-ajax', 
		onClick: function (event, tabIndex) {			
			if (this.getCurrentTab().hasClass('photos')) {
				$('#content').addClass('photos');
			} else {
				$('#content').removeClass('photos');
			}
		}
	}).history();
	var tabs = $('ul.tab-menu').tabs();



	$peak = $('#peak');
	$peakPanels = $peak.find('.panels > div').addClass('hidden');
	$footer = $('#footer');
	
	menuHeight = $('#peak div.nav').outerHeight();
	
	menuIsHidden = false;
	menuShouldRevealThreshold = 229;
	menuShouldHideThreshold = 300;
	
	footerIsHidden = false;
	
	peakIsHidden = true;
	
	$('.peak-link').click(function () {
		
		$target = $peakPanels.filter(this.hash);
		if (!$target.hasClass('hidden')) {
			// Panel is visible, do nothing
			console.log('do nothing');
			return false;
		}
		
		if (peakIsHidden) {
			$peakPanels.addClass('hidden');
			$target.removeClass('hidden');
			
			$peakPanels.hide();
			$target.show();
			
			peakIsHidden = false;
			$peak.stop().animate({height: $peak.find('.content').outerHeight()});
		} else {
			$peak.stop().animate({height:menuHeight}, function () {
				peakIsHidden = false;
				$peakPanels.addClass('hidden');
				$target.removeClass('hidden');
				
				$peakPanels.hide();
				$target.show();
				
				$peak.animate({height: $peak.find('.content').outerHeight()});
			});
		}
		return false;
	});
	
	$('#content').click(function() {
		if (!peakIsHidden) {
			$peak.stop().animate({height:0},function () {
				$peakPanels.addClass('hidden');
				
				$peakPanels.hide();
				
				peakIsHidden = true;
				menuIsHidden = true;
			});
			
		}
	});
	
	// Activate Menu/Footer Areas
	$('#mask').mousemove(function(e)
	{
		if (!peakIsHidden) {
			return;
		}
		
		if (tabs.getIndex() == 0) {
			menuShouldRevealThreshold = $(document).height() - ($(document).height() / 1.618);
			footerShouldRevealThreshold = $(document).height() - 200;
		} else {
			footerShouldRevealThreshold = $(document).height() - 30;
			menuShouldRevealThreshold = 30;
		}
		
		// Menu 
		if (e.pageY < menuShouldRevealThreshold && menuIsHidden) {
			$peak.stop().animate({height:menuHeight + 'px'});
			menuIsHidden = false;
		} else if (e.pageY >= menuShouldHideThreshold && !menuIsHidden) {
			$peak.stop().animate({height:0});
			menuIsHidden = true;
		}

		// Footer
		footerShouldHideThreshold = $(document).height() - 250;
		
		if (e.pageY > footerShouldRevealThreshold && footerIsHidden) {
			$footer.slideDown('slow');
			footerIsHidden = false;
		} else if (e.pageY <= footerShouldHideThreshold && !footerIsHidden) {
			$footer.slideUp('slow');
			footerIsHidden = true;
		}
	});
	
	// Event poster swap
	var origSrc;
	var $poster = $('#events .poster img');
	$('#events .title a').hover(function () {
		$hoverImg = $(this).parent().parent().find('.image img');
		origSrc = $poster.attr('src');
		$poster.attr('src', $hoverImg.attr('src'));
	}, function () {
		$poster.attr('src', origSrc);
	});
	
	// $('#events a').overlay({ 
	//     target: '#poster-overlay', 
	//     expose: '#000'
	// }).gallery({ 
	// 	preload: true,
	//     speed: 800,
	// 	template: '<a href="${title}" target="_blank">Website</a>'
	// });
});
