// JavaScript Document


$(document).ready(function(){


	$(document).mousemove(function(ev){
		coord = mouseCoords(ev);
		$('#tooltip').css({top:coord.y-10, left:coord.x+12});
	if(coord.y < 80 || coord.y > 550){
		reset_thumbs();
	}

	winDims=getWidth();
	var left=(winDims/2)-490;//find left and right edge of main page content
	var right=(winDims/2)+475;
	if(coord.x < left || coord.x > right){
		reset_thumbs();
	}

});
	

original_thumbs=new Array();
hover_thumbs=new Array();
original_thumbs_string=new Array();
original_thumbs=$('.gal_thumb a').get();

for (p=0; p < original_thumbs.length; p++){
original_thumbs_string[p]=$(original_thumbs[p]).find('img').attr('src');
	if(original_thumbs_string[p].search(/_hov/i)==-1){//if original image is not a hover image already
		if(original_thumbs_string[p].search(/.gif/i)!=-1){//find out if it is a jpg or a gif
			hover_thumbs[p]=original_thumbs_string[p].replace(/.gif/i, "_hov.gif")	//append '_hov' to end of filename before extension
		}else{
			hover_thumbs[p]=original_thumbs_string[p].replace(/.jpg/i, "_hov.jpg")
		}
	}//end if
}//end for loop


function reset_thumbs(){
	if (typeof hover_dept=="undefined" || hover_dept==""){
	}else{
		for (q=0; q < original_thumbs.length; q++){
		$(original_thumbs[q]).find('img').attr({src:original_thumbs_string[q]})
		}
	$('#'+secondary_nav_id+"_current").attr({id:secondary_nav_id });
	hover_dept="";
	}
};


$('#secondary_nav a').mouseover(function(){

	reset_thumbs();//reset thumbnails if moving from one secondary nav to the next
	
	secondary_nav_id=$(this).parent().attr('id');//get the id of the secondary nav and apply the current state
	$(this).parent().attr({id:secondary_nav_id + "_current"});
	hover_dept=$(this).text().toLowerCase();	//get the text of the hovered secondary nav
	hover_dept = hover_dept.replace(/ /g,'');	//strip spaces
	for(k=0; k < original_thumbs.length; k++){
		$(original_thumbs[k]).find('.'+ hover_dept).attr({src:hover_thumbs[k]})
	}
	
})
//primary and secondart hover for thumbnails

$('.gal_thumb a').mouseover(function(ev){

	//get variables
	var orig_img=$(this).find('img').attr('src');	
	var tooltip_1=$(this).find('img').attr('alt');	
	var tooltip_2=$(this).find('img').attr('longdesc');
	tooltip_2 = tooltip_2.replace(/ /g,'');	//strip spaces
	var coord = mouseCoords(ev);
	if(orig_img.search(/_hov/i)==-1){//if original image is not a hover image already
	
	subdepartment_hover=false; // if the original image is not the hover (has not been selected from the subnav), then the default link will be used on click.
		
	
		//find out if it is a jpg or a gif
		
		$('#tooltip').html("<p><strong>"+ tooltip_1 +"</strong></p><p>"+ tooltip_2 +"</p>");
		$('#tooltip').show();
		if(orig_img.search(/.gif/i)!=-1){
			var hov_img=orig_img.replace(/.gif/i, "_hov.gif");	//append '_hov' to end of filename before extension
		}else{
			var hov_img=orig_img.replace(/.jpg/i, "_hov.jpg");	//append '_hov' to end of filename before extension
		}
	}//end if
	else{
		subdepartment_hover=true; // if the original image is hover (HAS been selected from the subnav), then the subdept  will be used on click.

		
		$('#tooltip').html("<p><strong>"+ tooltip_1 +"</strong></p>");
		$('#tooltip').show();
	}
	$(this).find('img').attr({src: hov_img, alt:''});//assign the hover image
	
	$(this).mouseout(function(){
		$('#tooltip').hide();
		$(this).find('img').attr({src: orig_img, alt:tooltip_1});//replace exisiting image
		});
	});


$('.gal_thumb a').click(function(event){
			   
	if(subdepartment_hover==true){
		event.preventDefault();
		 loc=$(this).attr('href')
   		 urlArray = new Array;
   		 urlArray=loc.split("/");
		         //the 2 in the array locates the subdepartment name so we can replace
		loc=loc.replace(urlArray[2], hover_dept);
		//alert('use subdept href')
		window.location=loc;
		
		
	}else{
		//alert('use default href')
	}
 });


});



