// Global JavaScripts

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

/* Just add class="rollover" to the img tag and make sure your images are named whatever.gif and whatever-over.gif */
/* You can use any kind of images and name them what you like.  The "-over" is the part that matters */
function initrollovers() {
	if (!document.getElementById) return;
	var aPreLoad = new Array();
	var sTempsrc;
	var aImages = document.getElementsByTagName('img');
	for (var i = 0; i < aImages.length; i++) {
		if (aImages[i].className == 'rollover') {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '-over'+ftype);
			aImages[i].setAttribute('hsrc', hsrc);
			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;
			aImages[i].onmouseover = function() {
				sTempsrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('hsrc'));
			}
			aImages[i].onmouseout = function() {
				if (!sTempsrc) sTempsrc = this.getAttribute('src').replace('-over'+ftype, ftype);
				this.setAttribute('src', sTempsrc);
			}
		}
	}
} 

/* Just add class="preload" to any images in the photo galleries so that they'll be loaded in the background for quicker transitions */
function preloadImages() {
	if (!document.getElementById) return;
	var aPreLoad = new Array();
	var aImages = document.getElementsByTagName('img');
	for (var i = 0; i < aImages.length; i++) {
		if (aImages[i].className == 'preload') {
			var src = aImages[i].getAttribute('src');
			aPreLoad[i] = new Image();
			aPreLoad[i].src = src;
		}
	}
}

/* Functions for setting active nav states */
function extractSectionName(hrefString, section) {
    var arr = hrefString.split('/');
    return (arr.length < 2) ? hrefString : arr[(arr.length - section)].toLowerCase();
}

function setActiveMenu(arr, crtPage) {
    for (var i = 0; i < arr.length; i++) {
        if (extractSectionName(arr[i].href, 1) == crtPage) {
            if (arr[i].firstChild.nodeValue == null) {
                var src = arr[i].firstChild.getAttribute('src');
                var ftype = src.substring(src.lastIndexOf('.'), src.length);
                arr[i].firstChild.src = src.replace(ftype, '-on' + ftype);
                arr[i].firstChild.className = "selected";
            } else {
            arr[i].className += " selected";
            }
        }
    }
}

/* Setups up the main Nav Functions */
function prepareNav() {
	var menuClosed = 107;
	var speed = 400;
	var isAnimating = false;
	
	$("#mainNav ul li h3").addClass("closed");
	
	function closeMenu(menu) {
		$(menu).next().slideUp((speed-50));
		$(menu).animate({ paddingTop: menuClosed }, speed, function(){ $(this).removeClass("open").addClass("closed");})
			.find("a").removeClass("selected");
	}
	
	function openMenu(menu, padding) {
		//if (isAnimating==false) {
	//		isAnimating = true;
			$(menu).animate({ paddingTop: padding }, speed)
				.removeClass("closed").addClass("open")
				.find("a").addClass("selected");	
				$(menu).next().slideDown(speed, function(){ /*isAnimating=false;*/ });
		//}
	}

	$('h3#work').next().hide();
	$('li#hoverWork').hoverIntent(function() {
	    if ($(this).find("h3").hasClass("closed")) { openMenu($(this).find("h3"), 62); }
	}, function() {
	if ($(this).find("h3").hasClass("open")) { closeMenu($(this).find("h3")); }
	});

	$('h3#people').next().hide();
	$('li#hoverPeople').hoverIntent(function() {
	    if ($(this).find("h3").hasClass("closed")) { openMenu($(this).find("h3"), 62); }
	}, function() {
	if ($(this).find("h3").hasClass("open")) { closeMenu($(this).find("h3")); }
	});

	$('h3#about').next().hide();
	$("li#hoverAbout").hoverIntent(function() {
	    if ($(this).find("h3").hasClass("closed")) { openMenu($(this).find("h3"), 51); }
	}, function() {
	if ($(this).find("h3").hasClass("open")) { closeMenu($(this).find("h3")); }
	})

	$('h3#contact').next().hide();
	$("li#hoverContact").hoverIntent(function() {
	    if ($(this).find("h3").hasClass("closed")) { openMenu($(this).find("h3"), 21); }
	}, function() {
	if ($(this).find("h3").hasClass("open")) { closeMenu($(this).find("h3")); }
	})
}

function openWorkNav() {
    $("h3#work").animate({ paddingTop: 62 }, 0).removeClass("closed").next().slideToggle(0);
    $("h3#work").find("a").addClass("selected");
}

function openPeopleNav() {
    $("h3#people").animate({ paddingTop: 62 }, 0).removeClass("closed").next().slideToggle(0);
    $("h3#people").find("a").addClass("selected");
}

function openAboutNav() {
    $("h3#about").animate({ paddingTop: 51 }, 0).removeClass("closed").next().slideToggle(0);
    $("h3#about").find("a").addClass("selected");
}

function openContactNav() {
    $("h3#contact").animate({ paddingTop: 21 }, 0).removeClass("closed").next().slideToggle(0);
    $("h3#contact").find("a").addClass("selected");
}


addLoadEvent(initrollovers);
addLoadEvent(preloadImages);