/*******************************************************************************
 *		INITIALIZE JAVASCRIPT CLASSES
 *******************************************************************************
 *      Author:      	Farshad Ali
 *      Email:        	me@farshadali.com
 *      Website:    	http://www.farshadali.com
 *
 *      File:           init.js
 *      Version:     	[1.0]
 *      Copyright:  	(c) 2009 - Farshad Ali 
 *******************************************************************************
 *  VERION HISTORY:
 *  v1.0 [DATE] - Initialize JS
 
*******************************************************************************

 *  DESCRIPTION:
 *  Use this file to initialize all your javascript functions for the page

********************************************************************************/
$(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() {
	$("#header-links li").hover(function(){
		$(this).animate({"opacity":1},{"duration":"fast"})							
	},
	function(){
		$(this).animate({"opacity":0.6},{"duration":"fast"})							
	})
	/* ---------------------------- */
	/* 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");
		},function(){
		if($(this).is(":not(.active)")){ swapImageOff($(this).find('.img-over')); }
		$(this).find('menu').css({visibility: "hidden"});
	});
	/* ---------------------------- */
	/* 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;
}


