var image_path = 'images/home_nav/';
var content_image_path = 'images/home_content/';
var first_nav_button=0;
var last_nav_button=4;
var nav_buttons = Array('stay_connected','account_mgmt','opp_mgmt','contact_mgmt','issue_tracking');
var preloaded_images = Array();
var last_hash = '';

$(function(){

	for (i=0; i<nav_buttons.length; i++) {
		preloadimage(image_path+nav_buttons[i]+'_off.png');
		preloadimage(image_path+nav_buttons[i]+'_on.png');
		preloadimage(image_path+nav_buttons[i]+'_roll.png');
		preloadimage(content_image_path+nav_buttons[i]+'.png');
	}
	preloadimage(image_path+'edge_left_off.png');
	preloadimage(image_path+'edge_left_on.png');
	preloadimage(image_path+'edge_right_off.png');
	preloadimage(image_path+'edge_right_on.png');
	
	preloadimage(image_path+'seperator_off.png');
	preloadimage(image_path+'seperator_right_on.png');
	preloadimage(image_path+'seperator_left_on.png');
	
	// SWITCH TO ROLLOVER IMAGE ON MOUSEOVER

	$(".homenavbutton").mouseover(function() { 
		var button = $(this).attr('id');
		var onoff = $(this).attr('onoff');
		
		if (onoff=='off') {
			$(this).attr('src',image_path+button+'_roll.png');
		}
	});

	// REMOVE ROLLOVER IMAGE ON MOUSEOUT
	
	$(".homenavbutton").mouseout(function() { 
		
		var button = $(this).attr('id');
		var onoff = $(this).attr('onoff');
		
		if (onoff=='off') {
			$(this).attr('src',image_path+button+'_off.png');
		}
	});
	
	// TURN OFF ALL BUTTONS
	
	$(".homenavbutton").click(function() {
		var button = $(this).attr('id');
		var onoff = $(this).attr('onoff');
		var nav_order = $(this).attr('nav_order');
		
			
		window.location.hash = '#' + button;
		last_hash = '#' + button;

		$(".homenavbutton").each(function() { 
			var navbutton = $(this).attr('id');
			var navbuttonorder = $(this).attr('nav_order');
			$(this).attr('onoff','off');
			$(this).attr('src',image_path+navbutton+'_off.png');
			$("#"+navbutton+"_seperator").attr('src',image_path+'seperator_off.png');
			if (navbuttonorder == first_nav_button) {
				$("#"+navbutton+"_edge").attr('src',image_path+'edge_left_off.png');
			}
			else if (navbuttonorder == last_nav_button) {
				$("#"+navbutton+"_edge").attr('src',image_path+'edge_right_off.png');
			}
			
		});
		
		$(this).attr('onoff','on'); // Track whether on or off
		$(this).attr('src',image_path+button+'_on.png'); // Switch to on button
		$("#"+button+"_seperator").attr('src',image_path+'seperator_left_on.png'); // Switch to on button
	
		if (nav_order != first_nav_button) {
			var previous_button = $(".homenavbutton[nav_order='"+(nav_order-1)+"']").attr('id');
			$("#"+previous_button+"_seperator").attr('src',image_path+'seperator_right_on.png');
		}
		
		if (nav_order == first_nav_button) {
			$("#"+button+"_edge").attr('src',image_path+'edge_left_on.png');
		}
		else if (nav_order == last_nav_button) {
			$("#"+button+"_edge").attr('src',image_path+'edge_right_on.png');
		}
		
		$("#home_content_image").fadeOut(150, function () {
			$("#home_content_image").attr('src',content_image_path+button+'.png'); // Swap to new content image
        	$("#home_content_image").fadeIn(150);
     	});
		
		
	});
	
	if (window.location.hash != '') {
		checkbackbutton();
	}
	
	setInterval("checkbackbutton()",700);
	
});

function checkbackbutton() {
	
	if (window.location.hash != last_hash) {
		last_hash = window.location.hash;
		var button = window.location.hash.replace(/#/,'');
		
		if (button=='')
			button = 'account_mgmt';
		
		$('#'+button).click();
		
	}
}

function preloadimage(imagefile) {
	preloaded_images[imagefile] = new Image(); 
	preloaded_images[imagefile].src = imagefile;
	
}