// various Javascript utilities. jQuery dependent, for the most part 10-10

$(function(){
	// Arrays of the link #ids and hidden nav panels, passed to functions below.
	var linkids =	['work',	'company',		'learn',		'contact'];
	var hiddens = 	['work-nav','company-nav',	'learn-nav',	'contact-nav'];

	// Global variable that keeps track of **which** link ID is open. Same for services.
	$is_open = null;
	// Dito for services page.
	$svc_open = null;
	// modX adds class "current" to current sections, which messes with us. When pressent,
	// Store the id of the current tab here, remove it when we open, add it back when we close.
	$current = null;
	// The bottom section content may vary in height, flex a little, but is controlled by overflow:none.
	set_underlays();
	$('#nav-overlay').height($(document).height());
	
	// Click any main nav link to open panels. This sets the classes for open and closed links
	// and attaches animation functions to them.
	$('.site-header ul li a').click(function(event){
		close_all(linkids,hiddens);
		var $this_id=this.id;
		var $any_open = false;

		$('.site-header ul li a').each(function (i) {
			var $panel	= $('#'+hiddens[i]);
			if ($(this).hasClass('current')) {
				$current = this.id;
				$(this).removeClass('current');
			}
			if (this.id == $this_id) { 
				$(this).addClass('active'); 
				$panel.css('display','block');
				$panel.css('z-index',605);
				//$panel.css({opacity: 0.0}).animate({opacity: 1.0}, 500);
				$is_open = $this_id;
				$any_open=true;
			}
			else { $panel.css('z-index',1); $(this).addClass('inactive'); }
		});
		// Create the lightbox-ish overlays.
		create_overlays(linkids,hiddens);
		if ($any_open==false) { $is_open=null; }
		// Swap to the white version of the logo.
		$('#logo a').addClass('logo-lightbox');
		event.preventDefault();
	});
	
	// Services page
	// Save this for later when we have content.
	//if ($('.services-icon').length) {
	//	$('.services-icon a').click(function(event){
	//		var $svc_open=this.id;
	//		close_services();
	//		toggle_services($svc_open,$(this).attr('rel'));
	//		// Different than for the headers and does not conflict.
	//		$('#nav-overlay').click(function() {
	//			close_services();
	//			$(this).fadeOut(500);
	//		});
	//		event.preventDefault();
	//	});
	//}

	// So far, just for newsletter page, hence the check
	if ($('#nl-img a').length) {
		$("a[rel^='prettyPhoto']").prettyPhoto();
	}
	// Attempt to close any open panels on escape key. Only Evan will be using it. :-)
	$(document).keyup(function(e) { 
		if (e.which == 27) { $('#nav-overlay').click(); }
	});
});

// Like it says, remove classes, fade out overlays.
function close_all (links,panels) {
	if ($is_open != null) {
		for (i=0;i<links.length;i++) {
			var $link = $('#'+links[i]);
			var $panel = $('#'+panels[i]);
			if (($current != null) && (links[i]==$current)) {
				$link.addClass('current');
				$current = null;
			}
			close_this($link,$panel);
		}
	}
	// Swap to the white version of the logo.
	$('#logo a').removeClass('logo-lightbox');
}

// Close a given panel with a fadeout, remove open active/inactive classes.
// Called recursively by the previous.
function close_this($id,$panel) {
	if ($id.hasClass('active')) { $id.removeClass('active'); }
	if ($id.hasClass('inactive')) { $id.removeClass('inactive'); }
	//if ($panel.css('opacity') > 0) { $panel.css({opacity: 1.0}).animate({opacity: 0.0}, 500); }
	if ($panel.css('display') == 'block') { $panel.css('display','none'); }
}

// Adjust the lighbox-ish overlay to full document height (not viewport) and assign a click to it
// to close open panels. This is a good workaround to the body or document click, which bubbles
// up through any links or form controls in the panels.
function create_overlays (linkids,hiddens) {
	$('#nav-overlay').fadeIn(500, function () {
            $('#full-width-nav-underlay').fadeIn(500);
	});
	$('#nav-overlay').click(function(e) {
		close_all(linkids,hiddens);
		$('#full-width-nav-underlay').css('display','none');
		$(this).fadeOut(500);
		//$(this).fadeOut(500, function () {
        	// $('#full-width-nav-underlay').fadeOut(500);
		//});
		$is_open=null;
    });
}

// The bottom man page content may flex in height, this gives is a little flexibility to grow.
// If JSS is disabled, overflow:none will keep it from exploding. Of course, if JS is disabled, the footer won't work.
function set_underlays() {
	if ($('#bottom-content').length) {
		var ht = parseInt($('#bottom-content').css('height'))+12;
		if (ht > 215) { ht = 215; } // oveflow will nix surplus
		$('#bottom-underlay').css('height',ht+'px');
	}
	if ($('#footer-underlay').length) {
		var footertop = parseInt($(document).height())-parseInt($('#footer-underlay').css('height'));
		$('#footer-underlay').css('top',footertop+'px');
		$('#footer-underlay').css('display','block');
	}
}


// Close all service navs.
function close_services() {
	$('.services-icon a').each(function (i) {
		var divid = $(this).attr('rel');
		$(this).removeClass('si-active');
		$('#'+divid).css('display','none');
	});
}

// Toggle the services classes. Actually the extra states for si.active are not needed
// with the use of the nav overlay, but left in in case it changes.
function toggle_services(linkid,divid) {
	if ($('#'+divid).length) {
		var disp = ($('#'+divid).css('display')=='none')?'block':'none';
		$('#'+divid).css('display',disp);
		if (disp=='none') { 
			$('#'+linkid).removeClass('si-active'); 
			$svc_open=null; 
		}
		else { 
			$('#'+linkid).addClass('si-active'); 
			$svc_open = linkid; 
			$('#nav-overlay').fadeIn(500);
		}
	}
}


