//
// dropdown.js
// Copyright (c) Connect Group Ltd. All rights reserved.
//
// $Id$
//

var opendd = null;
var selectedLink = null;

$(window).bind("resize load", function() {
	$("div#mask").css("height", getMaskHeight());
});

$(document).ready(function() {
	selectedLink = $("div#primary-navigation ul li a.selected");

	var mask = $('<div id="mask"></div>');
	$("body").append(mask);
	mask.fadeTo(0, 0);

	$("div#drop-downs div.primary-nav-dropdown").hide();

	$("div#primary-navigation ul li a").each(function() {
		var element = $("div#drop-downs div." + this.id + "-dropdown");
		if (element.length == 1) {
			element.data("link", $(this));
			$(this).click(function() {
				toggleDropDown(element);
				return false;
			});
		}
	});

	$("div#mask").click(function() {
		toggleDropDown(null);
	});

	$("div#drop-downs div div.close-nav a").click(function() {
		toggleDropDown(null);
		return false;
	});

	$('a[rel="find-a-dealer"]').click(function(e) {
		e.preventDefault();
		toggleDropDown($("div#drop-downs div.find-dealer-dropdown"));
		return false;
	});
});
//
// keyHandler(e)
// Key handler for the drop downs.
// PARAMS
//  e: The key event.
//
function keyHandler(e) {
	if (e.keyCode == 27) {
		toggleDropDown(null);
	}
}
//
// toggleDropDown(next)
// Toggles the state of the currently open drop down.
// PARAMS
//  next: The next drop down to open.
// REMARKS
// This method manages the drop downs and the mask that overlays the page when a drop down is open.
// This method behaves as follows:
// * If there is no currently open drop down, the next drop down is opened and the mask is displayed.
// * If there is a next drop down, the drop down is closed and the new one opened.
// * If there is no next drop down, the drop down is closed and the mask hidden.
// This method also ensures the correct link that initiates a drop down is marked as "selected."
//
function toggleDropDown(next) {
	if ((opendd != null && next == null) || (opendd == next && opendd != null)) {
		$(document).unbind('keydown.dropDown', keyHandler);
		
		opendd.slideUp("slow", function() {
			opendd = null;
			selectedLink.addClass("selected");
			$("div#mask").css("z-index", -1).fadeTo("slow", 0);
		}).data("link").removeClass("selected");
	}
	else if (opendd != null && opendd != next) {
		opendd.slideUp("slow", function() { opendd = next; next.slideDown("slow").data("link").addClass("selected"); }).data("link").removeClass("selected");

	} else {
		if (next != null) {
			var openDropDown = function() {
				selectedLink.removeClass("selected");
				$("div#mask").css("z-index", 1).fadeTo("slow", 0.6, function() {
					next.data("link").addClass("selected");
					next.slideDown("slow", function() {
						opendd = next;
						//only for ie fire ie6 so that hidden div background image shows
						// if($.browser.msie && $.browser.version == "6.0"){
						// 	IEPNGFix.update();
						// }
						$(document).bind('keydown.dropDown', keyHandler);
						$('fieldset input:not(:hidden):first', next).focus();
					});
				});
			}

			if ($(window).scrollTop() > $('div#wrapper div#header').height()) {
				$('html,body').animate({ scrollTop: 0 }, 2000, openDropDown);
			} else {
				openDropDown();
			}
		}
	}
}
//
// getMaskHeight()
// Gets the height of the mask element.
//
function getMaskHeight() {
	return $("div#wrapper").height() > $(window).height() ? $("div#wrapper").height() : $(window).height()
}
