var currentMenu = null; // Track the currently open menu
var content = null;
var contentWidth = 0;

$(document).ready(function() {

	if($("#content")) {
		// Position and show spinner
		var spinner = $("#spinner");
		var spinnerLeft = (($(window).width()-125)/2)+125;
		spinner.css("left", spinnerLeft + "px");
		spinner.css("display","block");
	}

	$(".sub-nav").each(function() {
		if($(this).css('display') != 'none') currentMenu = $(this).parent().children("a").get(0);
	});

	$(".expandable").click(function(e) {
		if(this == currentMenu) { // If user clicks the currently open menu, close it
			closeMenu($(currentMenu));
			currentMenu = null;
		} else {
			openMenu($(this));
			if(currentMenu)
				closeMenu($(currentMenu)); // Close the current menu
			currentMenu = this; // Now this is the current menu
		}
		e.preventDefault();
	});

	// About - Partners
	$('#chandler').click(function(e) {
		var chandler = $(this);
		if(chandler.hasClass('on')){
			chandler.removeClass('on');
			$('#chandler-desc').hide();
		} else {
			chandler.addClass('on');
			$('#ousey').removeClass('on');
			$('#ousey-desc').hide();
			$('#chandler-desc').show();
			e.preventDefault();
		}
	});
	$('#ousey').click(function(e) {
		var ousey= $(this);
		if(ousey.hasClass('on')){
			ousey.removeClass('on');
			$('#ousey-desc').hide();
		} else {
			ousey.addClass('on');
			$('#chandler').removeClass('on');
			$('#chandler-desc').hide();
			$('#ousey-desc').show();
			e.preventDefault();
		}
	});

});

// Need to wait for all images to load before sliding in...
$(window).load(function() {slideInPhotos();});

function slideInPhotos() {
	content = $("#content"); // The div that contains the photos
	var contentInside = $("#content-inside"); // An inner div that we will animate
	//content.css('padding-right',$(window).width() + "px"); // Set the width to have a viewport-sized right padding
	// Remove spinner
	$("#spinner").css("display","none");

	// Make sure content is as large as the viewport, minus 275
	var minWidth = $(window).width() - 275;
	if(content.width() < minWidth) content.css('width',minWidth + "px");

	content.css('overflow','hidden'); // So we can move the inner div without the scrollbar moving
	contentInside.css('position','relative');
	contentInside.css('right','-750px');
	contentInside.css('opacity','0');
	contentInside.css('visibility','visible');
	
	contentInside.animate({
		opacity: 1.0,
		right: '+=750'
	}, 700, "easeOutBack", function() {
		setTimeout("setupBackNext()",1); // Need a little delay here to avoid bug
	});
}

function openMenu(menu) {
	var url = menu.attr("href");
	menu.parent().children("ul").first().slideDown("slow", function() { window.location = url; });
}

function closeMenu(menu) {menu.parent().children("ul").first().slideUp("slow");}

/*function setupSlider() {
	contentWidth = $("#content-inside img:last").offset().left - 325;
	$('#slider-wrapper').css('display','block');
	$("#slider").slider({
		slide: function(event, ui) {
			var to = (Math.round(contentWidth * ui.value/100)*-1) + 275;
			content.css('left',''+to+'px');;
		}
	});
}*/

function setupBackNext() {
	var images = [];
	var imgs = $("#content-inside img").each(function(){images.push($(this).offset().left-325);});

	var currentImage = 0;
	var content = $('#content');

	$("#next").show().click(function()
	{	
		if (currentImage < images.length-1)
		{
			currentImage++;
			content.animate({left: 275 - images[currentImage]}, 250);
		}
	});


	$("#back").show().click(function()
	{
		if (currentImage > 0)
		{
			currentImage--;
			content.animate({left: 275 - images[currentImage]}, 250);
		}
	});

}
