// ajaxNav
// this function is used to set up the main navigation, and any in-line navigations as appropriate
$.fn.ajaxNav = function() {
	
	// defaults
	var myNav;
	var label = "";
	
	// loop through all ajax navs
	for (var i=0; i<this.length; i++) {
	
		// flag for this element
		myNav = this[i];

		// bind this element's click action
		$(myNav).bind("click",function(e) { 

			// don't actuate the previous binding
			e.preventDefault();
			
			// set parts
			parts = $(this).attr("href").split("#")[1];

			// set the full url
			location.href = siteURL + "/p/#" + parts;
		
			// fade out current content, then set the new page content
			$("#interior-content").fadeOut(300,setPage);
		
		});
	
	}
	
}

// fixNav
// this function looks at the current label and sets the navigation appropriately
function fixNav() {
	
	$("ul#nav a").removeClass("selected");
	$("ul#nav li").removeClass("selected");
	$("#nav-" + label).addClass("selected");
	$("#nav-" + label).parents("li").parents("li").addClass("selected");
	$("#nav-" + label).parents("li").children("a.top").addClass("selected");
	
}

// loadContent
// this function loads the content of this page based on the URL and its label/subLabel pair
function loadContent() {
	
	var doc = document.location.toString();
	if (doc.split("#").length > 1) parts = doc.split("#")[1];
	setPage();
	
}

// setPage
// this function loads the right content for the page based on the label/subLabel variables
// and scrolls the background imagery to the correct location based on the label
function setPage() {
	
	// are we NOT already navigation?
	if (!navigating) {

		// set navigating flag
		navigating = true;

		// parse label and subLabel
		pullParts();
		
		// check for portfolio
		if (label != "portfolio") {
			$("#portfolio-holder").fadeOut();
			$("#portfolio-nav").fadeOut();
		}

		// find the correct picture
		curPic = arrayPos(label,pageImages);
	
		// set the margin value to display the correct picture
		var newMargin = ((curPic + 1) * 1000) - 525;

		// animate the pictures to display the correct picture
		$("#interior-content-images").animate({ marginLeft:-(newMargin) },1000,function() {

			// load new content based on the label/subLabel
			$.get("a/" + parts + "/",function(data) {

				// set new content
				$("#interior-content").html(data);

				// fade in new content
				$("#interior-content").fadeIn(function() {
				
					// check for team image refresh
					teamImageRefresh();

				});
			
				// fix navigation
				fixNav();
			
				// re-run page change
				onPageChange();
			
			});
		
		});
		
	}
	
}

// onPageChange
// this function is run after setPage retrieves new content
function onPageChange() {
	
	// check for new label/subLabel
	pullParts();
	
	// tracking
	try { var pageTracker = _gat._getTracker("UA-2257259-1"); pageTracker._trackPageview(); } catch(err) {};
	
	// reset pagination
	$("div.page").pagination();

	// portfolio
	if (label == "portfolio") $(".portfolio").portfolioSetup();

	// lightbox module
	$("[rel=lightbox]").fancybox({overlayOpacity:0.6});
	
	// color bullets
	$(".about-us-awards #interior ul").makeColorBullets('gold','white');
	
	// incidental ajax navigation
	$("#interior a.ajax-nav").ajaxNav();
	
	// main team navigation
	// $("#team-nav a").teamNav();
	$("a.team-nav").teamNav();
	$("#team-nav-right").teamNavNext();
	$("#team-nav-left").teamNavPrevious();
		
	// flag for finishing animationg/navigation
	navigating = false;

}

// pagination
// this function resets all pagination on the page, determines if pagination is necessary, and then displays pagination controls
$.fn.pagination = function() {

	// reset pagination dots
	$("#pagination-pages").html("");

	// reset curPage
	curPage = 0;
	
	// loop through all page divs
	for (var i=0; i<this.length; i++) {

		// show first page
		if (i == 0) $(this[i]).fadeIn(0);
	
		// add dots
		if (i == 0) $("#pagination-pages").append('<a href="#" class="page-dot selected" title="' + i + '"></a>');
		else $("#pagination-pages").append('<a href="#" class="page-dot" title="' + i + '"></a>');
		
	}

	// get total pages
	totalPages = i;
	
	// reset pagination controls
	$("#pagination a").paginationControls();

	// do we need pagination controls? if so, fade in.
	if (totalPages > 1) $("#pagination").fadeIn();

}

// paginationControls
// this function sets the bindings for controls for pagination
$.fn.paginationControls = function() {

	// loop through pagination controls
	for (var i=0; i<this.length; i++) {

		// unbind, then bind a click event
		$(this[i]).unbind();
		$(this[i]).bind("click",function(e){
		
			// prevent default click action
			e.preventDefault();
		
			// is this a dot?
			if ($(this).hasClass("page-dot")) {
		
				// set curPage based on this dot's numeral
				curPage = $(this).attr("title");
			
			// is this the next button?
			} else if ($(this).attr("title") == "next") {
			
				// set curPage, if at end, to beginning
				if (curPage == totalPages - 1) {
					curPage = 0;
				// set curPage to next element
				} else {
					curPage++;
				}
			
			// this is the previous button
			} else {
			
				// set curPage, if at beginning, to end
				if (curPage == 0) {
					curPage = totalPages - 1;
				// set curPage to previous element
				} else {
					curPage--;
				}
			
			}
		
			// de-select all dots on click
			$(".page-dot").removeClass("selected");
			
			// fade out all pages
			$("div.page").fadeOut();
			
			// fade in only current page
			$("div.page:eq(" + curPage + ")").fadeIn();
			
			// set only current dot
			$(".page-dot:eq(" + curPage + ")").addClass("selected");

		});
		
	}
	
	
}
