$(document).ready(function() {

	var GL_FadeSpeed = 600;
	//var environmentURL = "http://www.armadahoffler.com/qa/";
	var environmentURL = "http://www.armadahoffler.com/";

	//GENERAL - Track file download stats
	$("a").each(function() {
		var $a = $(this);
		var href = $a.attr("href");
		if (href != "" && href != null && href != undefined) {
			if (href.match(/\.(?:pdf|doc|xls|zip|rar)($|\&|\?)/)) {
				$a.click(function() {
					_gaq.push(['_trackPageview', '->DOWNLOAD->' + href]);
				});
			}
		}
	});
	
	//GENERAL - Home Button in Header
	$("h1#ahLogo").click(function() {
		window.location = environmentURL;
	});

	//GENERAL - Set nav bar height to content height
	function setNavHeight() {
		var contentHeight = $("div#content").height();
		$("nav").css("height", contentHeight);
	}

	//GENERAL - Enable "scroll to top" anchor
	$("span#returnToTop, span#currentPage").localScroll({duration:600, easing:"easeOutCirc"});

	//NAV - Main navigation highlight
	//Get main nav section links & reset...
	var currentSection 		= $("span#currentSection a").text().toUpperCase();
	var allSections			= $("ul#navigation li > a");
	allSections.removeClass("on");
	//Get subnavigation links & reset
	var currentSubSection 	= $("span#currentPage a").text().toUpperCase();
	var allSubSections		= $("ul#sectionSubNav li a");
	allSubSections.removeClass("on");
	//Highlight current respective section & subsection
	$("ul#navigation li > a[title='" + currentSection + "']").addClass("on");
	$("ul#sectionSubNav li a[title='" + currentSubSection + "']").addClass("on");

	//PORTFOLIO - PROCESS IMAGE SUBMENU MOUSE CLICKS
	$("div.imageSelectorBar a").click(function() {
		var splitTitle		= $(this).attr("title").split("|");
		var projID 			= splitTitle[0];
		var projThumbID 	= ($(this).attr("id")).substring(5);
		var projLinkID 		= "projectLink" + projID;
		var projPicID 		= "projectPic" + projID;
		var projImageSrc 	= $(this).attr("href");
		var projImageHref 	= environmentURL + "images/portfolio/" + projID + "-" + projThumbID + "-big.jpg";
		var newImageObj 	= new Image();
		var newTitle		= $(this).attr("title");

		$(this).siblings().removeClass("on");
		$(this).addClass("on");
		$("#" + projPicID).stop().fadeOut(GL_FadeSpeed, function() {
			$(newImageObj).load(function () {
				$(this).css("display", "none");
				$(this).attr("id", projPicID);
				$("a#projectLink" + projID).attr("title", newTitle);
				$("#" + projPicID).replaceWith(this);
				$(this).fadeIn(GL_FadeSpeed);
				$("#" + projLinkID).attr("href", projImageHref);
			}).attr("src", projImageSrc);
		});
		return false;
	}).filter(function(index) { return $(this).text() == 1; }).click();	//auto select all of the #1 buttons

	//PORTFOLIO - Fancybox
	$("a.imgLoader").fancybox({
		'padding'			: 1,
		'opacity'			: true,
		'scrolling'			: 'no',
		'centerOnScroll'	: 'true',
		'titleShow'			: true,
		'titlePosition'		: 'over',
		'titleFormat'		: formatTitle,
		'onComplete'		: titleFadeIn,
		'overlayShow'		: true,
		'overlayOpacity'	: .5,
		'overlayColor'		: '#000',
		'hideOnContentClick': true,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'easingIn'			: 'easeOutBack',
		'easingOut'			: 'easeInBack',
		'speedIn'			: 500,
		'speedOut'			: 500
	});

	//PORTFOLIO - Fancybox Title Generation
	function formatTitle (title) {
		var separatedTitle = title.split("|");
		return "<div class=\"captionOverlay\"><h3>" + separatedTitle[1] + "</h3><h4>" + separatedTitle[2] + "</h4></div>";
	}
	function titleFadeIn() {
		$("div.captionOverlay").fadeTo(0, .1, function() {
			Cufon.refresh();
			$(this).fadeTo(GL_FadeSpeed, 1);
		});
	}

	//CONTACT FORM - FORM PLUGIN SETTINGS AND CALLBACKS
	var contactOptions = {
        target:        	'#contactMsg',	// target element(s) to be updated with server response
        success:       	showResponse 	// post-submit callback
        //clearForm: 		true        	// clear all form fields after successful submit
	};
	$('#contactForm').ajaxForm(contactOptions);

	//CONTACT FORM - post-submit callback
	function showResponse(responseText, statusText, xhr, $form)  {
		$("#contactMsg").show(GL_FadeSpeed, function() { setNavHeight(); });
	}

	//Initial settings on document load
	$("#contactMsg").hide();
	setNavHeight();
});

