/* ---------------------------- 
[Global Javascript]

Project: 	Green & Associates
Version:	1.0
Last change:	10/1309/30/09 [templates created, ef]
Assigned to:	Eric Mayfield
Primary use:	Web

------------------------------- */
	
/* ---------------------------- */
/* This preloads all "_over.jpg" images with the class "img-over"
/* ---------------------------- */

$(window).bind('load', function() {
    var preload = new Array();
    $(".img-over").each(function() {
        s = $(this).attr("src").replace(/\.(.+)$/i, "_over.$1");
        preload.push(s)
    });
    var img = document.createElement('img');
    $(img).bind('load', function() {
        if(preload[0]) {
            this.src = preload.shift();
        }
    }).trigger('load');
});

/* ---------------------------- */
/* Image swapping & dropdowns
/* ---------------------------- */

$(document).ready(function() {
	/* ---------------------------- */
	/* This handles the dropdowns
	/* ---------------------------- */
	$(" #header-navigation menu ").css({display: "none",position:'absolute'});
	$(" #header-navigation>li.active").each(function(){swapImageOn($(this).find('.img-over'));});
	$(" #header-navigation>li").hover(function(){
		if($(this).is(":not(.active)")){ swapImageOn($(this).find('.img-over')); }
		$(this).find('menu').css({visibility: "visible", display: "none"}).fadeIn("fast");
		if(getFlashMovie("products")){
			getFlashMovie("products").setHoverDisabled()  // To Fix Flash hover bug on sub menu rollover
		}
		},function(){
		if($(this).is(":not(.active)")){ swapImageOff($(this).find('.img-over')); }
		$(this).find('menu').css({visibility: "hidden"});
		if(getFlashMovie("products")){
			getFlashMovie("products").setHoverEnabled() // To Fix Flash hover bug on sub menu rollover
		}
	});
	$("window").scroll(function(){
		if(getFlashMovie("products")){
			getFlashMovie("products").setHoverEnabled() // To Fix Flash hover bug on sub menu rollover
		}						
	});
	
	/* ---------------------------- */
	/* This swaps all "_over.jpg" images with the class "img-over"
	/* ---------------------------- */
	$(".img-over").each(function() {
        if ($(this).attr("src").match(/_over\.(.+)$/i)) {
            $(this).removeClass("hover");
        }
    });
});

function swapImageOn(element){
	if(element.attr("src")){
		s = element.attr("src").replace(/\.(.+)$/i, "_over.$1");
		element.attr("src", s);
		return true;
	}
	return false;
}

function swapImageOff(element) {
	if(element.attr("src")){
		s = element.attr("src").replace(/_over\.(.+)$/i, ".$1");
		element.attr("src", s);
		return true;
	}
	return false;
}


/* ---------------------------- */
/* Get Flash File
/* ---------------------------- */
function getFlashMovie(movieName) {   
 	var isIE = navigator.appName.indexOf("Microsoft") != -1;   
	return (isIE) ? window[movieName] : document[movieName];  
}
