function displaySection(header) {
	var t = header[0].tagName.substr(1);
	header.addClass("show").nextAll().each(function () {
		if (jQuery(this).is(":header")) {
			var x = jQuery(this)[0].tagName.substr(1);
			if (t >= x) {
                return false;
            }
		}
		jQuery(this).addClass("show");
	});
	jQuery(".content > div:not(.show), .content :header:not(.show)").hide();
	jQuery(".show:hidden").show();
}

function updateState() {
    var h = window.location.hash,
        header;
	jQuery(".show").removeClass("show");
	if (h && h !== "#") {
		header = jQuery(".content a" + h).parent();
		if (header.is(":visible")) {
            return false;
        }
		if (header[0]) {
            displaySection(header);
        }
		else {
            displaySection(jQuery(".content :header:first"));
        }
	}
	else {
        displaySection(jQuery(".content :header:first"));
    }
}

function swapPanels(panel) {
	panel = jQuery(panel);
	var cur = jQuery("#current_panel").empty().removeClass(),
        classes = panel.attr("class"),
        color = classes.substr(classes.indexOf("color"), 6);
	cur.addClass(color);
	panel.children(":hidden").clone().prependTo(cur).show();
}

function panels() {
	jQuery(".panel").addClass("side_panel").click(function () {
        swapPanels(this);
    }).children(":not(:header)").hide();
	jQuery(".panel:first").before("<div id='current_panel'></div>");
    var loc = window.location.hash,
        h;
    loc = loc.substr(loc.indexOf("#") + 1);
	h = jQuery(loc);
	if (loc && h.length) {
		swapPanels(h.parents(".panel"));
	}
	else {
        swapPanels(".panel:first");
    }
}

function validate() {
	jQuery.validator.addMethod("phoneUS", function (phone_number, element) {
		phone_number = phone_number.replace(/\s+/g, ""); 
		return this.optional(element) || phone_number.length > 9 && phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}jQuery/);
    }, "Please specify a valid phone number");
	jQuery(".validate").validate({
		debug: true,
		errorElement: "div",
		errorPlacement: function (error, element) {
			error.appendTo(element.parent("p"));
		},
		rules: {
			name: {
                required: true,
                minlength: 2
			},
			email: {
                required: true,
                email: true
			},
			phone: {
                required: true,
                phoneUS: true
			}
		}
	});
}

function openPlayer() {
	var newwindow = window.open("/audioplayer", "name", "height=355,width=460,location=no,resizeable=no,menubar=no,status=no,toolbar=no");
	if (window.focus) {
        newwindow.focus();
    }
	return false;
}

function attachNavEvents(parent, myClass) {
    jQuery(parent + " ." + myClass).hover(function () {
			jQuery(this).before('<div class="nav-' + myClass + '" style="display:none"></div>');
			jQuery("div.nav-" + myClass).fadeIn(200);
		}, function () {
			jQuery("div.nav-" + myClass).fadeOut(200, function () {
				jQuery(this).remove();
			});
		});
}

jQuery(function () {
    var calendar_even_rows = jQuery("#calendar li:even"),
        calendar_odd_rows = jQuery("#calendar li:odd");
	jQuery(".nav").children("li").each(function () {
	    var current = "nav current-" + (jQuery(this).attr("class")),
            parentClass = jQuery(".nav").attr("class");
	    if (parentClass !== current) {
	        jQuery(this).children("a").css({backgroundImage: "none"});
	    }
	});
	attachNavEvents(".nav", "zero");
	attachNavEvents(".nav", "one");
	attachNavEvents(".nav", "two");
	attachNavEvents(".nav", "three");
	attachNavEvents(".nav", "four");
	attachNavEvents(".nav", "five");
	attachNavEvents(".nav", "six");
	attachNavEvents(".nav", "seven");
	if (jQuery("div.toc").length) {
        jQuery(".content > div, .content :header").hide();
        jQuery(window).hashchange(updateState);
        updateState();
    }
	if (jQuery(".validate").length) {
        validate();
    }
	if (jQuery(".panel").length) {
        panels();
    }
    
    if (calendar_even_rows.length) {
        calendar_even_rows.addClass("color2");
    }
	if (calendar_odd_rows.length){
        calendar_odd_rows.addClass("color3");
    }
});