// JavaScript Document
//site operations
$(document).ready(init);
$("#show_menu").live("click", function(e) {
	e.preventDefault();
	dropmenu();
});
$("#close_menu").live("click", function(e) {
	e.preventDefault();
	closemenu();
});

function init() {
	$("a").each(function () {
		if ($(this).is("[href^='tel:']")) {
			$(this).addClass("tel");
		}
	});
	
	$("#nav li a").live("click", function(e) {
		if($(this).parent("li").find('ul').size() > 0) {
			e.preventDefault();
			handleNavClick($(this).parent("li").attr("id"));
		} else {
			//do nothing
		}
	});
	
	$(".nextnav ul li a").live("click", function(e) {
		if($(this).parent("li").find('ul').size() > 0) {
			e.preventDefault();
			handleNavClick($(this).parent("li").attr("id"));
		} else {
			//do nothing
		}
	});
	
	$(".back_arrow a").live("click", function(e) {
		e.preventDefault();
		handleNavClose($(this).attr("rel"));
	});
	
	$(".back_title").live("click", function(e) {
		e.preventDefault();
		handleNavClose($(this).attr("rel"));
	});

	$("#dd1").live("click", function (e) {
		e.preventDefault();
		if ($("#dd1").hasClass("active")) {
			hidecatmenu();
		} else {
			showcatmenu();
		}
	});

}

function showcatmenu() {
	$("#dd1").addClass("active");
	$("#dropdown1").css("display", "block");
}

function hidecatmenu() {
	$("#dd1").removeClass("active");
	$("#dropdown1").css("display", "none");
}

function dropmenu() {
	$("#show_menu").attr("class", "close").attr("title", "Close Site Navigation").html("Close").attr("id","close_menu");
	$("#quick_nav").before("<div id='overlay'></div>");
	$("#overlay").height($("#everything_wrapper").height());
	$("#navigation").slideDown("fast");
}

function closemenu() {
	$("#close_menu").removeClass("close").attr("title", "Show Site Navigation").html("Menu").attr("id","show_menu");
	$("#navigation").slideUp("fast", function() {
		$("#overlay").fadeOut("fast", function() {
			$("#overlay").remove();
			var browserWidth = parseInt($("#everything_wrapper").width());
			$("#navigation").width(browserWidth);
			$("#navigation").css("marginLeft", "0px");
			$(".nextnav").remove();
		});
	});
}

function handleNavClick(listid) {
	var theListHTML = $("#"+listid+" ul").html();
	var theDivHTML = $("#nextnav").html();
	var browserWidth = parseInt($("#everything_wrapper").width());
	
	$("#navigation_wrapper").width(browserWidth);	
	$("#navigation").animate({width: "+="+browserWidth+""}, 10, function() {
		//build the element
		$("<div>").addClass("nextnav").attr("id", "next_"+listid).appendTo("#navigation").html(theDivHTML);
		$("#next_"+listid+" ul").html(theListHTML);
		$("#next_"+listid+" .nav_title").html("<a class='back_title' rel='"+listid+"' href='#'>"+$("#"+listid+" a:not(#"+listid+" li a)").text()+"</a>");
		$("#next_"+listid+" .back_arrow a").attr("rel", listid);
		
		//set the styles
		$("#next_"+listid+" ul").width(browserWidth-28);
		$("#next_"+listid+"").css("display", "block");
		$("#next_"+listid+" li:last a").addClass("last");
		
		//show the element
		$("#navigation").animate({marginLeft: "-="+browserWidth+""}, 300, function() {
			resizeHelps();
		});
	});
}

function handleNavClose(listid) {
	var browserWidth = parseInt($("#everything_wrapper").width());
	
	$("#navigation_wrapper").width(browserWidth);
	$("#navigation").animate({marginLeft: "+="+browserWidth+""}, 300, function() {
		//delete the element
		$("#next_"+listid+"").remove();
		//hide the extra nav width
		$("#navigation").animate({width: "-="+browserWidth+""}, 10, function() {});
	});
}
