var noActiveNavWidth = "228px";

/* NODE WIDTHS FOR ACTIVE STATES */
var indoorWidths = { indoor: "514px", outdoor: "131px", store: "131px", inspiration: "131px" };
var outdoorWidths = { indoor: "131px", outdoor: "514px", store: "131px", inspiration: "131px" };
var storeWidths = { indoor: "131px", outdoor: "131px", store: "514px", inspiration: "131px" };
var inspireWidths = { indoor: "131px", outdoor: "131px", store: "131px", inspiration: "514px" };

var widths = new Array();
widths['indoor'] = indoorWidths;
widths['outdoor'] = outdoorWidths;
widths['store'] = storeWidths;
widths['inspiration'] = inspireWidths;


function clearNode(obj) {
	obj.css("background-color", "transparent");
	obj.children('img').attr("src",obj.children('img').attr("src").replace("-on","-off"));
}
function styleNode(obj) {
	obj.css("background-color", "#D04527");
	obj.children('img').attr("src",obj.children('img').attr("src").replace("-off","-on"));
}

function hideIndoorDropdown(obj) {
	if($('#dropdown-indoor').css("display") != "block") {
		clearTimeout(indoorTimer);
		indoorTimer = setTimeout("hideIndoorDropdown($('#navNode-indoor'))",10);
		return;
	}
	
	var obj = $('#navNode-indoor');
	$('#dropdown-indoor').queue(function(){
		$('#dropdown-indoor').hide(0);
		$('#dropdown-indoor').dequeue();
	}); 
	if(activeSection != 'indoor') {
		clearNode(obj);
	};
	$('#dropdown-indoor').queue(function(){
		if(lastActive=='indoor' && currentActive=='indoor') {
			resetNavNodeWidths();
		}
		$('#dropdown-indoor').dequeue();
	});
}

function hideOutdoorDropdown(obj) {
	if($('#dropdown-outdoor').css("display") != "block") {
		clearTimeout(outdoorTimer);
		outdoorTimer = setTimeout("hideOutdoorDropdown($('#navNode-outdoor'))",10);
		return;
	}
	var obj =  $('#navNode-outdoor');
	$('#dropdown-outdoor').queue(function(){
		$('#dropdown-outdoor').hide(0);
		$('#dropdown-outdoor').dequeue();
	});
	if(activeSection != 'outdoor') {
		clearNode(obj);
	};
	$('#dropdown-outdoor').queue(function(){
		if(lastActive=='outdoor' && currentActive=='outdoor') {
			resetNavNodeWidths();
		}
		$('#dropdown-outdoor').dequeue();
	});
}

function hideInspirationDropdown(obj) {
	if($('#dropdown-inspiration').css("display") != "block") {
		clearTimeout(inspirationTimer);
		inspirationTimer = setTimeout("hideInspirationDropdown($('#navNode-inspiration'))",10);
		return;
	}
	
	var obj = $('#navNode-inspiration');
	$('#dropdown-inspiration').queue(function(){
		$('#dropdown-inspiration').hide(0);
		$('#dropdown-inspiration').dequeue();
	});
	if(activeSection != 'inspiration') {
		clearNode(obj);
	};
	$('#dropdown-inspiration').queue(function(){
		if(lastActive=='inspiration' && currentActive=='inspiration') {
			resetNavNodeWidths();	
		}
		$('#dropdown-inspiration').dequeue();
	});
	
}

function hideStoreDropdown(obj) {
	if($('#dropdown-store').css("display") != "block") {
		clearTimeout(storeTimer);
		storeTimer = setTimeout("hideStoreDropdown($('#navNode-store'))",10);
		return;
	}
	
	var obj = $('#navNode-store');
	$('#dropdown-store').queue(function(){
		$('#dropdown-store').hide(0);
		$('#dropdown-store').dequeue();
	});
    
	if(activeSection != 'store') {
		clearNode(obj);
	};
	$('#dropdown-store').queue(function(){
		if(lastActive=='store' && currentActive=='store') {
            resetNavNodeWidths();	
		}
		$('#dropdown-store').dequeue();
	});
	
}

function resetNavNodeWidths(){
    if (activeSection != null && activeSection.length > 0){
        var currentWidths = widths[activeSection];
        $('#navNode-indoor').css("width", currentWidths['indoor']);
        $('#navNode-outdoor').css("width", currentWidths['outdoor']);
        $('#navNode-store').css("width", currentWidths['store']);
        $('#navNode-inspiration').css("width", currentWidths['inspiration']);	
    } else {
        $('#nav-nodes .nav-node').css("width", noActiveNavWidth);
    }
}

function calculateNavColumnHeightIndoor() {
    var byProduct_indoor_height = $('#dropdown-indoor .dropDown-byProduct-column').height();
    var byCollection_indoor_height = $('#dropdown-indoor .dropDown-byDesigner-column').height();
    var byMaterial_indoor_height = $('#dropdown-indoor .dropDown-bySetting-column').height();
    var highValue = Math.max(byProduct_indoor_height,byCollection_indoor_height,byMaterial_indoor_height);
    if(byProduct_indoor_height) {
            $('.indoorPart').height(highValue) ;
            $("#dropdown-indoor .nav-col").height(highValue);
            calculateNavColumnHeightIndoor = function(){};
    }
}
function calculateNavColumnHeightOutdoor() {
    var byProduct_outdoor_height = $('#dropdown-outdoor .dropDown-byProduct-column').height();
    var byCollection_outdoor_height = $('#dropdown-outdoor .dropDown-byDesigner-column').height();
    var byMaterial_outdoor_height = $('#dropdown-outdoor .dropDown-bySetting-column').height();
    var highValue = Math.max(byProduct_outdoor_height,byCollection_outdoor_height,byMaterial_outdoor_height);
    if(byProduct_outdoor_height) {
            $('.outdoorPart').height(highValue);
            $("#dropdown-outdoor .nav-col").height(highValue);
            calculateNavColumnHeightOutdoor = function(){};
    }
}

var indoorTimer = null;
var outdoorTimer = null;
var inspirationTimer = null;
var storeTimer = null;
var lastActive = '';
var currentActive = '';
var indoorOver = 0;
var outdoorOver = 0;
var inspirationOver = 0;
var storeOver = 0;

$(function() {
	$('#navNode-indoor').hoverIntent(
		function() {
            indoorOver = 1;
            currentActive = 'indoor';
            if (indoorTimer) { clearTimeout(indoorTimer); }
            $(this).queue(function(){
                styleNode($(this));
                $(this).dequeue();
            });
            
            var widths = indoorWidths;

            $('#navNode-outdoor').css('width', widths.outdoor);
            $('#navNode-store').css('width', widths.store);
            $('#navNode-inspiration').css('width', widths.inspiration);
            
            $(this).animate({width: widths.indoor}, 200, function() {
                if( indoorOver ) {
                    $('#dropdown-indoor').queue(function(){
                        $(this).slideDown(200,calculateNavColumnHeightIndoor);
                        $('[id^=dropdown]').not(this).slideUp(0);
                        $(this).dequeue();
                    });
                }else {
                    if(activeSection != 'indoor') {
                        clearNode($(this));
                    }
                    //resetNavNodeWidths();
                }
            });
            
            lastActive='indoor';
		},
		function() {
			indoorOver = 0;
			indoorTimer = setTimeout("hideIndoorDropdown($('#navNode-indoor'))",200);
		}
	);
    /*
	$("#dropdown-indoor").hoverIntent(
		function() {
			if (indoorTimer) { clearTimeout(indoorTimer); }
		},
		function() {
			indoorTimer = setTimeout("hideIndoorDropdown($('#navNode-indoor'))",200);
		}
	);
    */
	
	$('#navNode-outdoor').hoverIntent(
		function() {
			outdoorOver = 1;
			currentActive = 'outdoor';
			if (outdoorTimer) { clearTimeout(outdoorTimer); }
			$(this).queue(function(){
				styleNode($(this));
				$(this).dequeue();
			});

			var widths = outdoorWidths;
            $('#navNode-indoor').animate({width: widths.indoor},300)
			$('#navNode-store').animate({width: widths.store},300);
            $('#navNode-inspiration').animate({width: widths.inspiration},300);
			
            $(this).animate({width: widths.outdoor}, 300, function() {
				if( outdoorOver ) {
					$('#dropdown-outdoor').queue(function(){
						$(this).slideDown(200,calculateNavColumnHeightOutdoor);
						$('[id^=dropdown]').not(this).slideUp(0);
						$(this).dequeue();
					});
				} else {
					if(activeSection != 'outdoor') {
						clearNode($(this));
					}
					clearNode($(this));
					//resetNavNodeWidths();
				}
			});	
			lastActive='outdoor';
		},
		function() {
			outdoorOver = 0;
			outdoorTimer = setTimeout("hideOutdoorDropdown($('#navNode-outdoor'))",200);
		}
	);
    
    /*
	$("#dropdown-outdoor").hoverIntent(
		function() {
			if (outdoorTimer) { clearTimeout(outdoorTimer); }
		},
		function() {
			outdoorTimer = setTimeout("hideOutdoorDropdown($('#navNode-outdoor'))",200);
		}
	);	
    */
	
	$('#navNode-inspiration').hoverIntent(
		function() {
			inspirationOver = 1;
			currentActive = 'inspiration';
			if (inspirationTimer) { clearTimeout(inspirationTimer); }
			$(this).queue(function(){
				styleNode($(this));
				$(this).dequeue();
			});
			
			var widths = inspireWidths;
            $('#navNode-indoor').animate({width: widths.indoor},300);
            $('#navNode-outdoor').animate({width: widths.outdoor},300)
			$('#navNode-store').animate({width: widths.store},300);
			
            $(this).animate({width: widths.inspiration}, 300, function() {
				if( inspirationOver ) {
					$('#dropdown-inspiration').queue(function(){
						$(this).slideDown(200);
						$('[id^=dropdown]').not(this).slideUp(0);
						$(this).dequeue();
					});
				}else {
					if(activeSection != 'inspiration') {
						clearNode($(this));
					}
					//resetNavNodeWidths();
				}
			});			
			
			lastActive='inspiration';
		},
		function() {
			inspirationOver = 0;
			inspirationTimer = setTimeout("hideInspirationDropdown($('#navNode-inspiration'))",200);
		}
	);
    
    /*
	$("#dropdown-inspiration").hoverIntent(
		function() {
			if (inspirationTimer) { clearTimeout(inspirationTimer); }
		},
		function() {
			inspirationTimer = setTimeout("hideInspirationDropdown($('#navNode-inspiration'))",200);
		}
	);
    */
	
    $('#navNode-store').hoverIntent(
		function() {
			storeOver = 1;
			currentActive = 'store';
			if (storeTimer) { clearTimeout(storeTimer); }
			$(this).queue(function(){
				styleNode($(this));
				$(this).dequeue();
			});
			
			var widths = storeWidths;
            $('#navNode-indoor').animate({width: widths.indoor},300);
            $('#navNode-outdoor').animate({width: widths.outdoor},300)
			$('#navNode-inspiration').animate({width: widths.inspiration},300);
			
            $(this).animate({width: widths.store}, 300, function() {
				if( storeOver ) {
					$('#dropdown-store').queue(function(){
						$(this).slideDown(200);
						$('[id^=dropdown]').not(this).slideUp(0);
						$(this).dequeue();
					});
				}else {
					if(activeSection != 'store') {
						clearNode($(this));
					}
                    clearNode($(this));
                    
					//resetNavNodeWidths();
				}
			});			
			
			lastActive='store';
		},
		function() {
			storeOver = 0;
			storeTimer = setTimeout("hideStoreDropdown($('#navNode-store'))",200);
		}
	);
    
    /*
    $("#dropdown-store").hoverIntent(
        function() {
            if (storeTimer) { clearTimeout(storeTimer); }
        },
        function() {
            storeTime = setTimeout("hideStoreDropdown($('#navNode-store'))",200);
        }
    );
    */
    
	$(".navBar-bottomShadow").mouseover(function() {

	});
		
	
	$('ul.dropDown-columnNodes li.new').hoverIntent(
		function() {
			$(this).css("background-color", "#4B4E4E");
		},
		function() {
			$(this).css("background-color", "#902812");
		}
	);

    	$('ul.dropDown-columnNodes li.sale').hoverIntent(
		function() {
			$(this).css("background-color", "#4B4E4E");
		},
		function() {
			$(this).css("background-color", "#902812");
		}
	);
});
