var isStaging = window.location.search.indexOf("testing=1") > -1;
$(function() {
	//--- Initialize Navigation Accordian
	Accordian();
	
	syncNav();
	
	// That darn thing needs to stay at the bottom
	// and we are using scirpt to keep it there.
	$("#footer").css("top", Math.max(document.body.scrollHeight,  document.body.clientHeight) + "px");
	
	// Enable History Links
	$("a.history").address(function() {
		var href = $(this).attr('href').replace(/^#/, '').split('/');
		return buildHashPair(href[1], href[2]);
	});

	Tabs();	
	Accolades();
	LightBox();
});

function Accordian() {
	// Define how links are selected.
	function navFilter() {
		
		return window.location.pathname.indexOf('/-/') !== -1 && this.href.toLowerCase().indexOf(window.location.pathname) !== -1;
	}
	
	// Build the accordion.
	$('#navigation')
		.accordion({header: 'dt', autoHeight: false, navigation: true, collapsible: true, active: false, navigationFilter: navFilter });
	
	$('#navigation dt a')
		.click(function(ev) {
			// Don't stop the prettyPhoto links.
			if($(this).hasClass("prettyPhoto")) {
				ev.stopPropagation();
				ev.preventDefault();
				return false;
			}
			ev.stopPropagation();
			ev.preventDefault();
			if(this.href) {
				window.location.href = this.href;
			}
			return false;
		});
}

function Tabs(config) {
	// Only continue if there were tabs.
	if($(".tabs").length === 0) { return; }
	config = jQuery.extend({
					tabHeaders: $('.tab-headers > LI'),
					tabBodies: $('.tab-content > LI')
				}, config || {});
	
	$.address.change($onChange);
	
	config.tabHeaders.hover(hoverOver, hoverOut);
	
	function showTab(tabId) {
		// Get Tab Content
		var content = $("#" + tabId);
		var head = $(config.tabHeaders[content.selfIndex()]);
		
		currentBody().removeClass('selected');
		currentHead().removeClass('selected');
		
		content.addClass('selected');
		head.addClass('selected');
	}
	
	function currentBody() {
		return config.tabBodies.filter('.selected');
	}
	
	function currentHead() {
		return config.tabHeaders.filter('.selected');
	}
	
	function $onChange(ev) {
		var tabKey = valueFromHashPairs('gallery', ev.value);
		if(!tabKey) { return; }
		showTab('tab-' + tabKey);
	}	
}

function Accolades() {
	var el = $("#accolades");
	$("#accolades li.first").addClass("current");
	
	var delay = 6000;
	
	setTimeout(next, delay);
	
	function next() {
		var curr = el.find('li.current');
		
		//--- Fade Out
		curr.fadeOut('slow', function() {			
			//--- Move the Current Class
			curr.removeClass("current");
			
			var nextEl = curr.nextOrFirst();
				nextEl.addClass("current");
				
			//--- Fade in new current
			nextEl.fadeIn('slow', function() {
				setTimeout(next, delay);
			});
		});		
	};
}


function LightBox() {
	// If URL has thing in it.
	//if(!isStaging) { return; }
	
	$("a[class^='prettyPhoto']").prettyPhoto({
			animation_speed: 'fast', /* fast/slow/normal */
			slideshow: 5000, /* false OR interval time in ms */
			autoplay_slideshow: false, /* true/false */
			opacity: 0.80, /* Value between 0 and 1 */
			show_title: true, /* true/false */
			allow_resize: true, /* Resize the photos bigger than viewport. true/false */
			default_width: 500,
			default_height: 344,
			counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
			theme: 'pp_default', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
			horizontal_padding: 20, /* The padding on each side of the picture */
			hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
			wmode: 'opaque', /* Set the flash wmode attribute */
			autoplay: true, /* Automatically start videos: True/False */
			modal: false, /* If set to true, only the close button will close the window */
			deeplinking: false, /* Allow prettyPhoto to update the url to enable deeplinking. */
			overlay_gallery: true, /* If set to true, a gallery will overlay the fullscreen image on mouse over */
			keyboard_shortcuts: true, /* Set to false if you open forms inside prettyPhoto */
			changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
			callback: function(){}, /* Called when prettyPhoto is closed */
			ie6_fallback: true,
			social_tools: '<div class="pp_social"><div class="twitter"><a href="http://twitter.com/share" class="twitter-share-button" data-count="none">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div class="facebook"><iframe src="http://www.facebook.com/plugins/like.php?locale=en_US&href='+location.href+'&amp;layout=button_count&amp;show_faces=true&amp;width=500&amp;action=like&amp;font&amp;colorscheme=light&amp;height=23" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:500px; height:23px;" allowTransparency="true"></iframe></div></div>' /* html or false to disable */
		
		}).click(function(e) {
		e.preventDefault();
		e.stopPropagation();
		return false;
	});;	
}

jQuery.fn.extend({
	nextOrFirst : function () {
		if(this.next().length == 0) {
			return this.parent().children().eq(0);
		}
		return this.next();
	},
	selfIndex: function() {
		return this.parent().children().index(this);
	}
});
		
function hoverOver() { $(this).addClass("hover"); }
function hoverOut() { $(this).removeClass("hover"); } 

function syncNav() {
	var loc = window.location.pathname;
	$("#navigation dt[slug]").each(function(index) {		
		if(loc.indexOf("/" + $(this).attr('slug') + "/") != -1) {
			
			$('#navigation').accordion('activate', index);
		}
	});
}

function valueFromHashPairs(key, hashStr) {
	var path = hashStr || $.address.path();
	if(path.indexOf(['/', key, '/'].join('')) === -1) { return; }
	var keyVals = path.split('/');
	for(var c=0;c<keyVals.length;c++) {
		if(keyVals[c] === key) {
			return unescape(keyVals[c+1]);
		}
	}
	return "";
}

function buildHashPair(key, val) {
	var path = $.address.path();
	var finalHashPair;
	val = escape(val);
	
	if(path.indexOf(['/', key, '/'].join('')) === -1) { 
		finalHashPair = jQuery.merge(path.split('/'), [key, val]);
	} else {
		var keyVals = path.split('/');
		for(var c=0;c<keyVals.length;c++) {
			if(keyVals[c] === key) {
				if(keyVals[c+1] === val) {
					return; 
				}
				
				keyVals[c+1] = val;
			}
		}
		finalHashPair = keyVals.slice(1);
	}
	
	return finalHashPair.join('/');
}
