$(document).ready(function(){
	$('.menuitem img').animate({width: 151}, 0);
	$('.menuitem').mouseover(function(){
			gridimage = $(this).find('img');
			gridimage.stop().animate({width:165}, 150);
		}).mouseout(function(){
			//get current button
			$currNavButton=gridimage.attr('src');
			//strip full nav button path and split on "." to get nav button file name
			$currNavButtonFilename= $currNavButton.match(/(.*)[\/\\]([^\/\\]+)\.\w+$/);
			//get array element
			$buttonfilename=$currNavButtonFilename[2];
			//replace "nav-" to get stripped version of filename to be able to compare to url path.  
			//add "/" to beginning and end because the url path we are comparing to will have for example "/works/"
			$buttonfilename="/"+$buttonfilename.replace('nav-','')+"/";
			$path=window.location.href;
			//getfilename
			$filename=getFileName(window.location.href.substr(window.location.href.lastIndexOf("/")));
			$deepPageMatch=$filename.indexOf($buttonfilename) //added "nav" because imagenames are "nav-studio" or "nav-works"
			$rootDirectoryPageMatch=$currNavButton.indexOf("nav-"+$filename) //added "nav" because imagenames are "nav-studio" or "nav-works"
			if($rootDirectoryPageMatch==-1 && $deepPageMatch==-1){
				gridimage.stop().animate({width: 151}, 150);
			}
	});
	//lastly fire the mainNavButtonBlip function to manipulate image on load of DOM, so that a mouseover event isn't required
	//to animate image
	mainNavButtonBlip();
}); 

function getFileName($fname){
	$filename=$fname;
	//get pos of "." in filename
	$dotPos=$filename.indexOf('.');
	$filename=$filename.slice(1,$dotPos);
	//alert($filename);
	switch($filename)	{
	case 'gutz':
		$filename="works";
		break;
	case '':
		$filename="index";
		break;
	default:
		//code to be executed if n is different from case 1 and 2
	}
	return $filename;
}

function mainNavButtonBlip(){
	//this function fires on the load of a page so that we don't need a mouseover event to determine what button to "raise"
	$path=window.location.href;
	//getfilename
	$filename=getFileName(window.location.href.substr(window.location.href.lastIndexOf("/")));
	//loop over each nav button
	$(".menuitem img").each(function(i){
		//get current button
		$currNavButton=this.src;
		//strip full nav button path and split on "." to get nav button file name
		$currNavButtonFilename= $currNavButton.match(/(.*)[\/\\]([^\/\\]+)\.\w+$/);
		//get array element
		$buttonfilename=$currNavButtonFilename[2];
		//replace "nav-" to get stripped version of filename to be able to compare to url path.  
		//add "/" to beginning and end because the url path we are comparing to will have for example "/works/"
		$buttonfilename="/"+$buttonfilename.replace('nav-','')+"/";
		//alert("buttonfilename: "+$buttonfilename+" \ncurrURL: "+$currURL);
		//see if there is a match when comparing button filename (with added slashes for deeper pages) to a match in the page url
		$deepPageMatch=$path.indexOf($buttonfilename) //added "nav" because imagenames are "nav-studio" or "nav-works"
		//see if we are in site home directory and have a button match with script/filename
		$rootDirectoryPageMatch=this.src.indexOf("nav-"+$filename) //added "nav" because imagenames are "nav-studio" or "nav-works"
		//alert("root match: "+$rootDirectoryPageMatch);
		if($rootDirectoryPageMatch!=-1){
			$(this).animate({width:165}, 150);
		}		
		if($deepPageMatch!=-1){
			$(this).animate({width:165}, 150);
		}	
	});				
}
