/* = NAV HELPER
-------------------------------------------------
	drop-down menu helper for ie6/win
-------------------------------------------------*/
function navHelper() {
	if(!document.getElementById) return false;
	if(!document.getElementsByTagName) return false;
	
	if (document.all&&document.getElementById) {
		var nav = document.getElementById("main-nav");
		var navItems = nav.getElementsByTagName("LI");
		
		for (i=0; navItems.length; i++) {
			navItems[i].onmouseover = function() {
				this.className += " over";
			};
			navItems[i].onmouseout = function() {
				this.className = this.className.replace("over", "");
			};
		}
	}
	
	return true;
}


/* = NAV HIGHTLIGHT
-------------------------------------------------
	PUTS THE CLASS "CURR" ON THE CURRENT NAVIGATION
	ITEM THAT MATCHES THE URL
-------------------------------------------------*/

function navHighlight(elementId, urlIndexArray) {
	if(!document.getElementById) return false;
	if(!document.getElementsByTagName) return false;
	if(!document.getElementById(elementId)) return false;
	if(!urlIndexArray) return false;
	
	var url = location.href.replace(location.protocol+"//"+location.hostname+"/","");
	if(url[url.length - 1] == "/")
		url += "index.php";
		
	url = url.split("/");
	
	
	// The url section that we check against
	var checkUrl = "";
	
	for ( var i = 0; i < urlIndexArray.length; i++)
	{
		if(url[urlIndexArray[i]] && i == 0)
		{
			checkUrl += url[urlIndexArray[i]];
		}
		else if(url[urlIndexArray[i]])
		{
			checkUrl += "/" + url[urlIndexArray[i]];
		}
	}
	
	var nav = document.getElementById(elementId);
	var navItems = nav.getElementsByTagName("LI");
	for ( i = 0; i < navItems.length; i++)
	{
		var linkUrl = "";
		
		var navItemLink = navItems[i].getElementsByTagName("A")[0];
		var navItemLinkUrl = navItemLink.getAttribute("href");
		
		// IE puts the hostname infront of every url by replacing this all browsers
		// are using the same information to compare page location and link href.
		// If non-IE the replace string finds no match and quits.
		navItemLinkUrl = navItemLinkUrl.replace(location.protocol+"//"+location.hostname,"");

		if(navItemLinkUrl[navItemLinkUrl.length -1] == "/")
			navItemLinkUrl += "index.php";

		navItemLinkUrl = navItemLinkUrl.split("/");
		navItemLinkUrl.shift(-1);
		
		for (var j = 0; j < urlIndexArray.length; j++)
		{
			if(navItemLinkUrl[urlIndexArray[j]] && j == 0)
			{
				linkUrl += navItemLinkUrl[urlIndexArray[j]];
			}
			else if(navItemLinkUrl[urlIndexArray[j]])
			{
				linkUrl += "/" + navItemLinkUrl[urlIndexArray[j]];
			}
		}
		
		// Checking to see if they match
		//alert(checkUrl + " = " + linkUrl);
		var isSpecialCase = checkSpecialCases(checkUrl, linkUrl);
		if(checkUrl == linkUrl || isSpecialCase)
		{
			if(navItems[i].className)
			{
				navItems[i].className += " curr";
			}
			else {
				navItems[i].className = "curr";
			}
		}
		
	}
	
	return 0;
}

function checkSpecialCases(checkUrl,linkUrl) {
	var specialCaseUrls = new Array();
	
	specialCaseUrls["adopt/animal_gallery.php"] = "adopt/animal_gallery_landing.php";
	specialCaseUrls["adopt/animal_gallery.php?category=dog"] = "adopt/animal_gallery_landing.php";
	specialCaseUrls["adopt/animal_gallery.php?category=cat"] = "adopt/animal_gallery_landing.php";
	specialCaseUrls["adopt/animal_gallery.php?category=other"] = "adopt/animal_gallery_landing.php";
	specialCaseUrls["adopt/animal_gallery.php?category=dog&color=purple"] = "adopt/animal_gallery_landing.php";
	specialCaseUrls["adopt/animal_gallery.php?category=dog&color=orange"] = "adopt/animal_gallery_landing.php";
	specialCaseUrls["adopt/animal_gallery.php?category=dog&color=green"] = "adopt/animal_gallery_landing.php";
	specialCaseUrls["adopt/your_card.php"] = "adopt/meet_match.php";

	
	for(caseUrl in specialCaseUrls) {
		if(caseUrl != checkUrl) continue;
		
		if(specialCaseUrls[caseUrl] == linkUrl) {
			return true;
		}
	}
	
	return false;
}

function callNavHighlight() {
	navHighlight("main-nav", Array("0"));
	navHighlight("section-nav-links", Array("0","1"));
}

addLoadEvent(callNavHighlight);
addLoadEvent(navHelper);

/* = ADD ON LOAD
-------------------------------------------------
	This adds functions to onload without
	stepping on each other.
-------------------------------------------------*/

function addLoadEvent(func) {
	var oldOnLoad = window.onload;
	if (typeof window.onload != 'function') 
	{
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldOnLoad();
			func();
		};
	}
}

function addDropshadow () {
	var dropshadow = $('<img src="/images/photo_shadow.png" alt="" style="position: absolute; top: 0; left: 0; z-index: 1000; border: 0 none;" />');
	
	// Loop through each image with the shadow class
	$('img.shadow').each(function(index) {
		
		// This waits for the image to load before doing its thing
		$(this).load(function() {
			// Get Height and Width
			var height = $(this).height();
			var width = $(this).width();

			// Create new div with same height and width and relative positioning
			var div = $('<div style="position: relative; height: ' + height + 'px; width: ' + width + 'px; overflow: hidden; border: 1px solid #fff;">');

			// Add absolute positioning
			var image = $(this).clone().css({'position': 'absolute', 'top': '0', 'left': '0', 'z-index': '100'});

			// Put image and dropshadow inside
			div.append(image).append(dropshadow.clone());

			$(this).replaceWith(div);
		});
	});
}

function sponsorRotate () {
	$('#sponsors .main ul').innerFade({
		speed: 'normal',
		timeout: 5000
	});
}

if (typeof jQuery != 'undefined'){	
	$(document).ready(function() {
		addDropshadow();
		sponsorRotate();
	});
};