function KeyNav() {
	this.LEFT = 37;
	this.UP = 38;
	this.RIGHT = 39;
	this.DOWN = 40;
	
	$(document).keyup($.proxy(function(event) {
		switch (event.keyCode)
		{
			case this.DOWN:
				this.goDown();
				break;
			case this.UP:
				this.goUp();
				break;
			case this.RIGHT:
				this.goRight();
				break;
			case this.LEFT:
				this.goLeft();
				break;
		}
	}, this));
}

KeyNav.prototype.goDown = function() {
	var next = $("#menu li.selected").next();
	if (next.length) 
		next.children("a").click();
	else 
		$("#menu li").first().children("a").click();
}

KeyNav.prototype.goUp = function() {
	var prev = $("#menu li.selected").prev();
	if (prev.length)
		prev.children("a").click();
	else
		$("#menu li").last().children("a").click();
}

KeyNav.prototype.goRight = function() {
	var selectedMainHref = $("#menu li.selected a").attr("href");
	var next = $(selectedMainHref + " .section_menu li.selected").next();
	if (next.length)
		next.children("a").click();
	else
		$(selectedMainHref + " .section_menu li").first().children("a").click();
}

KeyNav.prototype.goLeft = function() {
	var selectedMainHref = $("#menu li.selected a").attr("href");
	var prev = $(selectedMainHref + " .section_menu li.selected").prev();
	if (prev.length)
		prev.children("a").click();
	else
		$(selectedMainHref + " .section_menu li").last().children("a").click();
}

function ScrollContent() {
	$.localScroll({
		target: $("#viewport"),
		axis:'xy',
		queue:true,
		duration:600,
		hash:true,
		onBefore:function( e, anchor, $target ){
			
			this.isSection = function(id) {
				this.valid = false;
				this.id = id;
				
				$("#menu li a").each($.proxy(function(i, element) {
					if ($(element).attr("hash") == this.id)
						this.valid = true;
				}, this));
				
				return this.valid;
			};
			
			var currTarget = $(e.currentTarget);
			var id = "#" + currTarget.parent().parent().parent().parent().attr("id");
			
			if (this.isSection(id) && !currTarget.parent().hasClass("selected")) {
				this.target = $(id + " .section_content");
				
				$(id + " .section_menu li").each(function() {
					$(this).removeClass("selected");
				});
				
				currTarget.parent().addClass("selected");
			}
			else if ( !$("#menu a#" + currTarget.attr("id")).parent().hasClass("selected") ){
				this.target = $("#viewport");
				
				$("#menu li").each(function() {
					$(this).removeClass("selected");
				});
				
				$("#menu a#" + currTarget.attr("id")).parent().addClass("selected");
			}
			else
				return false;
		},
		onAfter:function( anchor, settings ){
				
		}
	});
}

function findTopMenuIdFromHref(href) {
    this.href = href;
    this.result = "";

    $("#menu li a").each($.proxy(function (i, element) {
        if ($(element).attr("href") == this.href) {
            this.result = $(element).attr("id");
            return;
        }
    }, this));

    return this.result;
}

$(document).ready(function () {

    var kN = new KeyNav();
    var sC = new ScrollContent();

    // update menus to reflect current anchor
    // handles case of a page refresh
    this.anchor = $.url.attr("anchor");
    if (this.anchor) {
        this.topMenu = "";
        this.subMenu = "";

        this.anchor = '#' + this.anchor;

        this.topMenu = findTopMenuIdFromHref(this.anchor);

        if (this.topMenu) {
            $('#' + this.topMenu).click();
            return;
        }

        $(".section_menu li a").each($.proxy(function (i, element) {
            if ($(element).attr("href") == this.anchor) {
                var containerId = '#' + $(element).parent().parent().parent().parent().attr("id");
                this.topMenu = '#' + findTopMenuIdFromHref(containerId);
                $(this.topMenu).click();
                $(element).click();
                return;
            }
        }, this));
    }
});
