var cfe = cfe || {};
cfe.active_menu_bar = null;
cfe.active_menu_pane = null;

cfe.open_menu = function(event, close, id) {
	var override = typeof(id) != 'undefined' ? true : false;
	close = typeof(close) != 'undefined' ? close : true;
    $('#menu').animate({top: "20px"});
	var bar = null;
	if (override) {
		bar = $('#' + id + ' > div.menu-bar');
		bar.children('span.menu-links').css('display', 'block');
	} else {
		bar = $(this);
	}
    var pane = bar.next();
    if (cfe.active_menu_pane == null || cfe.active_menu_bar.parent().attr('id') != bar.parent().attr('id')) {
        cfe.close_menu(false, false);
        cfe.active_menu_bar = bar;
        cfe.active_menu_pane = pane;
        if (cfe.active_menu_bar.attr('id') == 'menu-bar-top') {
            cfe.active_menu_bar.css('background', '#ffcf06');
        } else {
            cfe.active_menu_bar.css('background', '#ffcf06 url(\'../img/gradient_down.png\') repeat-x');
        }
        cfe.active_menu_bar.children('span.menu-arrow').css('background', 'url(\'../img/menu_arrow_opened.png\')');
        cfe.active_menu_bar.children('div.menu-title').css('backgroundPosition', '0 -20');
        cfe.active_menu_pane.slideDown('350');
        $('#dimmer').css('display', 'block');
    } else {
		if (close) {
			cfe.close_menu(true, true);
		}
    }
};

cfe.close_if_active = function() {
	if (cfe.active_menu_pane != null && (cfe.active_menu_pane.parent().attr('id') == $(this).parent().attr('id'))) {
		cfe.close_menu(false, true);
	} else {
		hash = $(this).children('.menu-links').children('.menu-link').first().attr('href').substring(1);
		window.location.hash = hash;
	}
}

cfe.close_menu = function(hover, brighten) {
    if (cfe.active_menu_bar) {
    	cfe.deselect_menu(cfe.menu_state[cfe.active_menu_bar.parent().attr('id')]);
	}
    if (cfe.active_menu_pane !== null) {
        if (!hover) {
            cfe.active_menu_bar.css('background', 'url(\'../img/menu_bar_gray.png\') repeat-x');
            cfe.active_menu_bar.children('span.menu-arrow').css('background', 'url(\'../img/menu_arrow_closed.png\')');
            cfe.active_menu_bar.children('div.menu-title').css('backgroundPosition', '0 0');
            cfe.active_menu_bar.children('span.menu-links').css('display', 'none');
        } else {
            cfe.active_menu_bar.children('span.menu-arrow').css('background', 'url(\'../img/menu_arrow_closed_hover.png\')');
        }
        cfe.active_menu_pane.slideUp('350');
        cfe.active_menu_bar = null;
        cfe.active_menu_pane = null;
    }
    if (brighten) {
    	window.location.hash = "";
        $('#dimmer').css('display', 'none');
    }
};

cfe.menu_bar_in = function () {
    if (cfe.active_menu_pane == null || cfe.active_menu_pane.parent().attr('id') != $(this).next().parent().attr('id')) {
        if ($(this).attr('id') == 'menu-bar-top') {
            $(this).css('background', '#ffcf06');
            $(this).children('div.menu-title').css('backgroundPosition', '0 -20px');
        } else {
            $(this).css('background', '#ffcf06 url(\'../img/gradient_down.png\') repeat-x');
            $(this).children('div.menu-title').css('backgroundPosition', '0 -20px');
        }
        $(this).children('span.menu-arrow').css('background', 'url(\'../img/menu_arrow_closed_hover.png\')');
        $(this).children('span.menu-links').css('display', 'block');
    }
};

cfe.menu_bar_out = function () {
    if (cfe.active_menu_pane == null || cfe.active_menu_pane.parent().attr('id') != $(this).next().parent().attr('id')) {
        $(this).css('background', 'url(\'../img/menu_bar_gray.png\') repeat-x');
        $(this).children('span.menu-arrow').css('background', 'url(\'../img/menu_arrow_closed.png\')');
        $(this).children('div.menu-title').css('backgroundPosition', '0 0');
        $(this).children('span.menu-links').css('display', 'none');
    }
};

cfe.hide_dimmer = function() {
    cfe.close_menu(false, true);
    $('#dimmer').css('display', 'none');
};

cfe.move_menu = function() {
    var buffer = 15;
    cfe.close_menu();
    var menu_top = $('#menu').offset().top;
    var menu_bottom = menu_top + $('#menu').height();
    var video_top = $(this).offset().top;
    var video_bottom = video_top + $(this).height();
    if ((menu_top >= video_top && menu_top <= video_bottom) || (menu_bottom >= video_top && menu_bottom <= video_bottom)) {
        var height = menu_bottom-menu_top+buffer;
        if (height < video_top) {
            $('#menu').animate({top: video_top-height + "px"});
        } else {
            $('#menu').animate({top: video_bottom+buffer + "px"});
        }
    }
};

cfe.link_menu = function() {
	href = $(this).attr('href');
	if (href.substring(0,1) != '#') {
		window.location = href;
	}
	return false;
}

/* URL Handling **********************************************************/
cfe.menu_state = {
	"in-the-classroom": "courses",
	"outside-the-classroom": null,
	"news-and-happenings": null,
	"people": null,
	"support-cfe": null
}

cfe.parse_url = function(url) {
	url_segments = url.split('/');
	return (url_segments.length >= 3) ? url_segments : false;
}

cfe.link = function(event) {
	event.preventDefault()
	if (cfe.active_menu_bar) {
		cfe.deselect_menu(cfe.menu_state[cfe.active_menu_bar.parent().attr('id')]);
	}
	hash = $(this).attr('href').substring(1);
	window.location.hash = hash;
}

cfe.hashchange = function() {
	if (cfe.active_menu_bar) {
		cfe.deselect_menu(cfe.menu_state[cfe.active_menu_bar.parent().attr('id')]);
	}
    var hash = window.location.hash;
    if (hash.length <= 1) {
        cfe.close_menu(false, true);
    } else {
        cfe.load_page();
    }
}

cfe.load_page = function() {
    var hash = window.location.hash.substring(1);
	var segments = cfe.parse_url(hash);
	cfe.select_menu(segments[1]);
	cfe.menu_state[segments[0]] = segments[1];
	
	if (segments != false) {
		var menu_id = segments[0];
		var link_id = segments[1];
        if (segments[2] == 'static') {
            var url = 'index.php';
            for (var i = 3; i < segments.length; i++) {
                url += '/' + segments[i];
                pre_data = '<div class="submenu-links" style="display:none;"></div><div class="menu-content">';
                post_data = '</div><div class="gradient-up white"></div>';
            }
        } else {
            var url = 'index.php/content/index/';
            for (var i = 2; i < segments.length; i++) {
                url += segments[i] + '/';
                pre_data = '';
                post_data = '';
            }
        }
		$('#' + menu_id + ' > div.menu-pane > div.submenu-links').css('display', 'none');
		$('#' + menu_id + ' > div.menu-pane > div.menu-content').html('<center><img src="http://cfe.umich.edu/img/ajax-loader.gif" width="32px" height="32px" alt="Loading..." /><br /><p>Loading...</p></center>');
		cfe.open_menu(null, false, menu_id);
		$.ajax({
			url: url,
			success: function(data) {
				var pane = $('#' + menu_id + ' > div.menu-pane');
				pane.html(pre_data + data + post_data);
				var submenu = pane.children('div.menu-content').children('div.submenu-links');
				if (submenu.length) {
					pane.children('div.submenu-links').html(submenu.html()).css('display', 'block');
				}
			}
		});
	}
}

cfe.select_menu = function(id) {
	$('#ml-' + id).addClass('ml-selected');
}

cfe.deselect_menu = function(id) {
	$('#ml-' + id).removeClass('ml-selected');
}

$(document).ready(function() {
	var hash = window.location.hash.substring(1);
	if (hash) {
		cfe.load_page(hash);
	}

    $('.menu-bar').hover(cfe.menu_bar_in, cfe.menu_bar_out);
    $('.menu-bar').bind('click', cfe.close_if_active);
    $('.menu-link').bind('click', cfe.link_menu);
    $('#dimmer').bind('click', cfe.hide_dimmer);
    $('#wall td').bind('click', cfe.move_menu);
	$('a.hash').bind('click', cfe.link);
    //$(window).bind('hashchange', cfe.hashchange);
    $(window).hashchange(cfe.hashchange);
	
	
});
$(window).load(function () {
	var menu_top = $('#menu').offset().top;
	$('#menu').animate({top: menu_top-100 + "px"}).delay(500).animate({top: menu_top + "px"});
});
