
// preload images function
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

// preload images
$.preloadImages("http://www.ecologicalservicesltd.com/images/templates/search_btn_over.gif");

$(function() {   
	
	// insert nav lense div
	$('#header_wrapper').prepend('<div id="nav_lense"></div>');
	// set opacity
	$('#nav_lense').css({ 'opacity':'0','display':'block' }).hide(); 
	//define fadedUp variable
	var menuActive=false;
	var hidden=false;
	var showing=false;
	
	// hide dropdown lense
	function hideLense() {
		if(!menuActive) { return; }
		menuActive=false;
		if(hidden) { return; }
		hidden = true;
		$('#nav_lense').fadeTo(10, 0, function() {
			$(this).hide();
		});
	}
	// show dropdown lense
	function fadeUp() {
		if(menuActive!=false) { return; }
		menuActive=true;
		if(showing) { return; }
		$('#nav_lense').show();
		showing = true;      
		hidden = false;
		$('#nav_lense').fadeTo('fast', 0.7, function() {
			showing=false;
		});
	}
	
	// navigation rollovers
	$('#services_nav').hover(
		function(){
			$(this).children('a').css({ 'background':'transparent url("http://www.ecologicalservicesltd.com/images/templates/grad_nav_services.gif") bottom center no-repeat' });
			if (jQuery.browser.msie) {
				if(parseInt(jQuery.browser.version) != 6) {
					fadeUp();
					clearTimeout(initiateHide);
				} else {
					$(this).find('#services_nav_cont').css({ 'display':'block' });
				}
			} else {
				fadeUp();
				clearTimeout(initiateHide);
			}
		},
		function(){
			$(this).children('a').css({ 'background':'transparent url("http://www.ecologicalservicesltd.com/images/templates/grad_nav_services.gif") top center no-repeat' });
			if (jQuery.browser.msie) {
				if(parseInt(jQuery.browser.version) != 6) {
					initiateHide = setTimeout(hideLense,10);
				} else {
					$(this).find('#services_nav_cont').css({ 'display':'none' });
				}
			} else {
				initiateHide = setTimeout(hideLense,10);
			}
		}
	);
	$('.standard_drop').hover(
		function(){
			var bgimg = $(this).children('a').css( 'background-image' );
			//alert(bgimg);
			$(this).children('a').css({ 'background':'transparent '+bgimg+' bottom center no-repeat' });
			if (jQuery.browser.msie) {
				if(parseInt(jQuery.browser.version) != 6) {
					fadeUp();
					clearTimeout(initiateHide);
				} else {
					$(this).children('ul').css({ 'display':'block' });
				}
			} else {
				fadeUp();
				clearTimeout(initiateHide);
			}
		},
		function(){
			var bgimg = $(this).children('a').css( 'background-image' );
			$(this).children('a').css({ 'background':'transparent '+bgimg+' top center no-repeat' });
			if (jQuery.browser.msie) {
				if(parseInt(jQuery.browser.version) != 6) {
					initiateHide = setTimeout(hideLense,10);
				} else {
					$(this).children('ul').css({ 'display':'none' });
				}
			} else {
				initiateHide = setTimeout(hideLense,10);
			}
		}
	);
	
	// search button rollover
	$('.search_button').hover(
		function(){
			$(this).attr('src','http://www.ecologicalservicesltd.com/images/templates/search_btn_over.gif');
		},
		function(){
			$(this).attr('src','http://www.ecologicalservicesltd.com/images/templates/search_btn.gif');
		}
	);
	
	// swap homepage images
	$('#m_case_studies ul').find('img').click(
		function(){
			// get image path of clicked image and replace large image
			var home_image_path = $(this).attr('src');
			$('#m_case_studies img').not('ul img').attr('src',home_image_path);
			return false;
		}
	);
	
	// swap images
	$('#m_images ul').find('img').click(
		function(){
			// get image path of clicked image and replace large image
			var image_path = $(this).attr('src');
			$('#m_images img').not('ul img').attr('src',image_path);
			// get id of clicked image and remove '_text"
			var image_id = $(this).attr('id');
			var image_id_stripped = image_id.replace('_text','');
			// get image annotation
			var image_ann = $('#'+image_id_stripped).text();
			$('#img_annotation').text(image_ann);
		}
	);
	$('#m_images ul').find('a').click(
		function(){
			return false;
		}
	);
	
});