var preview_position_top = 0;
var preview_position_left = 0;

// Extend jQuery with wait
$.fn.wait = function(time, type) {
        this.queue('fx', []);
		time = time || 1000;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                $(self).dequeue();
            }, time);
        });
    };

// select box hack
in_select = false;

// Nav Items
arr_nav_items = new Array("home", "goals", "court_surfacing", "add_ons", "other", "support_resources", "contact", "cart");

// Preload images
$.preloadImages(path + "images/design/navigation/button_home_active.gif",
			path + "images/design/navigation/button_goals_active.gif",
			path + "images/design/navigation/button_add_ons_active.gif",
			path + "images/design/navigation/button_other_active.gif",
			path + "images/design/navigation/button_support_resources_active.gif",
			path + "images/design/navigation/button_contact_active.gif",
			path + "images/design/navigation/button_cart_active.gif",
			path + "images/design/navigation/add_ons_left_active.gif",
			path + "images/design/navigation/add_ons_right_active.gif",
			path + "images/design/cart/add_to_cart_active.gif");

function init_navigation(){	
	
	$("#navigation_section_home").hover(function(){select_navigation('home')},reset_navigation);
	$("#navigation_button_home").click(function(){select_navigation('home')},reset_navigation);
	
	// Basketball Goals
	$("#navigation_section_goals").hover(function(){select_navigation('goals')},reset_navigation);
	$("#navigation_button_goals").click(function(){select_navigation('goals')},reset_navigation);

	// Court Surfacing
	$("#navigation_section_court_surfacing").hover(function(){select_navigation('court_surfacing')},reset_navigation);
	$("#navigation_button_court_surfacing").click(function(){select_navigation('court_surfacing')},reset_navigation);

	// Basketball Goal Add-Ons
	$("#navigation_section_add_ons").hover(function(){select_navigation('add_ons')},reset_navigation);
	$("#navigation_button_add_ons").click(function(){select_navigation('add_ons')},reset_navigation);
	
	// Other
	$("#navigation_section_other").hover(function(){select_navigation('other')},reset_navigation);
	$("#navigation_button_other").click(function(){select_navigation('other')},reset_navigation);
	
	// Support and Resources
	$("#navigation_section_support_resources").hover(function(){select_navigation('support_resources')},reset_navigation);
	$("#navigation_button_support_resources").click(function(){select_navigation('support_resources')},reset_navigation);

	// Contact
	$("#navigation_section_contact").hover(function(){select_navigation('contact')},reset_navigation);
	$("#navigation_button_contact").click(function(){select_navigation('contact')},reset_navigation);
	
	// Cart
	$("#navigation_section_cart").hover(function(){select_navigation('cart')},reset_navigation);
	$("#navigation_button_cart").click(function(){select_navigation('cart')},reset_navigation);
	
	$("#navigation select").focus(function(){in_select = true;});
	$("#navigation select").blur(function(){in_select = false;});
	$("#navigation select").change(function(){in_select = false;});
	

	$('.navigation_drop_down').bind("mouseout", function(e){
		reset_navigation();
    });
	
	
	$(':not(#cart_summary) .navigation_table tr:not(.no_hover)').hover(
		function(){
			
			$(this).children().addClass('highlight');
			
			
			// Preview Pane
			el = $('#preview_pane');
			var self = this;
			setTimeout(function(){
				if($(self).children().hasClass('highlight')){
					//el.fadeIn(200).load(path + 'includes/preview_pane.html');
					el.fadeIn(200).load(path + 'pop/preview_pane.php?sku=' + $(self).attr('preview') + '&path=' + path);
				}
			}, 1000);
			$(this).wait().mousemove(function(e){
				el.css('left', e.pageX);
				el.css('top', e.pageY + 25);
			});
			
		},
		function(){
			$(this).children().removeClass('highlight');
			
			// Preview Pane
			$('#preview_pane').html('');
			$('#preview_pane').fadeOut(100);
			
		}).click(function(){
			document.location = $(this).find('a').attr('href');
		});
	
	
	init_cart_summary();	

	reset_navigation();
	
	// Select current page if set
	if(typeof(current_page) != "undefined") select_navigation(current_page);
	
}

// Pull in cart summary
function init_cart_summary(){
	$('#cart_summary').load(path + 'cart/cart_summary.php?path=' + path);
}

function reset_navigation(){
	
	/*
	$('#navigation select').each(function(){
    	this.blur();  
    });
	*/
	
	if(in_select == false){
	
		$('select').css('visibility', 'visible');
		
		$('#navigation .section .drop_down').hide();
		//$('#navigation .section .drop_down').fadeOut(100);
		
		for(i=0;i<arr_nav_items.length;i++){
			
			nav_item = arr_nav_items[i];
			
			drop_down = $('#navigation_drop_down_' + nav_item);
			button = $('#navigation_button_' + nav_item);
			shadow = $('#navigation_shadow_' + nav_item);
			section = $('#navigation_section_' + nav_item);
			
			section.css("zIndex",4);
			button.css("zIndex",3);
			drop_down.css("zIndex",2);
			shadow.css("zIndex",1);
			
			$('#navigation_button_' + nav_item).css('backgroundImage','url(' + path + 'images/design/navigation/button_' + nav_item + '_inactive.gif)');
			
		}
	}
	
	if(parent.window.open_navigation !== undefined) parent.window.open_navigation(false);

}

function select_navigation(nav_item){
	
	if(in_select == false){
	
		reset_navigation();
		
		if(nav_item != 'home'){
			drop_down = $('#navigation_drop_down_' + nav_item);
			shadow = $('#navigation_shadow_' + nav_item);
			drop_down.show();
			//drop_down.fadeIn(200);
			drop_down.css("zIndex",6);
			shadow.css("zIndex",5);
			$('select').css('visibility', 'hidden');
			$('#navigation select').css('visibility', 'visible');
		}
		
		button = $('#navigation_button_' + nav_item);
		section = $('#navigation_section_' + nav_item);
		
		button.css("backgroundImage","url(" + path + "images/design/navigation/button_" + nav_item + "_active.gif)");
		
		section.css("zIndex",8);
		button.css("zIndex",7);
		
	}
	
	
	if(parent.window.open_navigation !== undefined) parent.window.open_navigation(true);
	
}

function search_installers(el){
	state = $('#' + el).val();
	if(state == "XX"){
		$('#' + el)[0].focus();
		alert("Please choose a state before searching");
	}else{
		str_state = $('#' + el + ' :selected').text();
		if(parent !== undefined) parent.tb_show("Installer Search", parent.path + "pop/installer_search.php?str_state=" + str_state + "&state=" + state + "&height=500&width=705&TB_iframe=true",false);
		else tb_show("Installer Search", parent.path + "pop/installer_search.php?str_state=" + str_state + "&state=" + state + "&height=500&width=705&TB_iframe=true",false);
	}
}

function search_locations(el){
	state = $('#' + el).val();
	if(state == "XX"){
		$('#' + el)[0].focus();
		alert("Please choose a state before searching");
	}else{
		str_state = $('#' + el + ' :selected').text();
		tb_show("Location Search",path + "pop/location_search.php?str_state=" + str_state + "&state=" + state + "&height=488&width=810&TB_iframe=true",false);
	}
}



function search_transit_time(el){
	
	zip = $.trim($('#' + el).val());
	
	// Validate zip
	var valid = "0123456789";
	
	error = false;
	
	if (zip.length!=5){
		error = true;
	}else{
		for (var i=0; i < zip.length; i++){
			temp = zip.substring(i, i+1);
			if (valid.indexOf(temp) == "-1"){
				error = true;
				break;
			}
		}
	}
	
	if(error){
		alert("The zip code you entered was not 5 digits");
	}else{
		tb_show("Delivery Date",path + "pop/transit_time.php?zip=" + zip + "&height=488&width=810&TB_iframe=true",false);
	}
}

function download_installation_instructions(el){
	file = $('#' + el).val();
	if(file != 'XX') document.location = path + "docs/user_manuals/" + file;
	else alert('Please choose a model');
}

function toggle_sub_nav(el){
	el = $(el);
	
	if(el.hasClass('level_4')) selected = 'level_4_selected';
	else if(el.hasClass('level_3')) selected = 'level_3_selected';
	else if(el.hasClass('level_2')) selected = 'level_2_selected';
	else if(el.hasClass('level_1')) selected = 'level_1_selected';
	
	section = el.next();
	
	if(section.css('display') == 'none'){
		section.slideDown("fast");
		el.addClass(selected);
	}else{
		section.slideUp("fast");
		el.removeClass(selected);
	}
	
}
