

var DepartmentTabs = function () {
	//console.log("Creating");
	this.slider = $("#main-col-slider");
	this.container = $("#main-col");
	this.menu = $("#main-col-menu");
	this.active = $("#news_");
	
	me = this;
	$("#dept-nav li").each(function (i, li) {
		//console.log(this);
		$(li).click($.proxy(function (event) {
			// this refers to the DepartmentTabs class
			//console.log(event, this);
			$(".active").removeClass("active");
			link = $(event.currentTarget).addClass("active").children("a");
			$("h1", this.menu).text(link.text());
			if (piwikTracker) {
				try {
					piwikTracker.setDocumentTitle(pwiki_title + " | " + link.text());
					piwikTracker.trackPageView();
				} catch (ex) {}
			}
			$(".visibletab").removeClass("visibletab");
			this.active = $(link.attr("href") + "_", this.slider).addClass("visibletab");
			p = this.active.position();
			this.fit();
			this.slider.animate({
				left: "-" + p.left
			}, 500);
			//return false;
		}, me));
	});
};
DepartmentTabs.prototype.fit = function () {
	this.container.css({height: this.active.height()});
};
function convert_flyouts() {
    $button_bar = $("#header-ext ul:first");
    $("#fly-out-menu ul").each(function() {
        $this = $(this);
        category = $(".category:first", $this).text();
        $flyout = $("<div/>").addClass("mini-flyout").addClass("flyout").hide();
        $catlink = $("<li/>").append(
                $("<a/>").text(category).attr("href", "#")
                .data("flyout", $flyout)
                .click(function (event) {
                    $fout = $(this).data("flyout");
                    if ($fout.is(":visible")) {
                        $fout.fadeOut();
                    } else {
                        $(".mini-flyout").hide();
                        $fout.fadeIn();
                    }
                    return false;
                })
        );
        $button_bar.append($catlink);
        offsets = $catlink.offset();
        $("body").append($flyout);
        $flyout.append($(this).detach());
        $flyout.css({top: offsets.top + 15, left: offsets.left + 15});
    });
    $("#fly-out-menu").remove();
}
$(document).ready(function () {
	tabs = new DepartmentTabs();
	setTimeout("$('#main-col').css({height: $('#news_').height()});", 150);
	$("div.story a.readmore").each(function() {
		$this = $(this);
		$this.click(function() {
			$story = $(this).closest(".story").children(".text");
			$.get("/ajax.php?g=news&id=" + $story.parent().attr("data-id"), function(data) {
				if (data != "") {
					$story.html(data).addClass("selected-news");
					tabs.fit();
				}
				if (console) {
				    console.log(data, $story, tabs);
				}
			});
			$(this).remove();
			return false;
		}).hide();
	});
	$("div.story").hover(function () {
		$("a.readmore", this).stop(true, true).fadeIn();
	}, function () {
		$("a.readmore", this).stop(true, true).fadeOut();
	});
	$("#view-all-meetings").click($.proxy(function (event) {
		$(".out_of_range").fadeIn(400, $.proxy(function () {this.fit();}, this));
		$(event.target).fadeOut();
		
	}, tabs));

	
	/* fix menu floating */
	if ($("#fly-out-menu").hasClass("always-show")) {
	    /* break out into single dropdowns */
	    convert_flyouts();
	    
	} else {
	    offsets = $("#fly-out").offset();
	    if (offsets) {
	        $("#fly-out-menu").css({top: offsets.top + 15, left: offsets.left + 15});
	        $("#fly-out").click(function (event) {
	            if ($("#fly-out-menu").is(":visible")) {
	                $("#fly-out-menu").fadeOut();
	            } else {
	                $("#fly-out-menu").fadeIn();
	            }
	        });
	    }
	    num_uls = $("#fly-out-menu ul").length;
	    max_cols = 5;
	    num_cols = Math.floor((num_uls) / Math.floor((num_uls+max_cols-1)/max_cols));

	    count = 0;
	    w = (880/num_cols);
	    for (var i=0; i< num_cols; i++) {
	        $("#fly-out-menu").append($("<div class='col' id='col-" + i + "'></div>").css({width: w + "px"}));
	    }
	    
	    
	    $("#fly-out-menu ul").each(function () {
	        $("#col-" + (count % num_cols), "#fly-out-menu").append($(this).detach());
	        count = count + 1;
	    });
	    $("#fly-out-menu").append("<div class='break'></div>");
	    
	    /* fix for single column (get rid of 170px width) */
	    if(count == 1) $('#col-0').css('width', 'auto');
	}
	
	/* close menus on click off */
    $('body').click(function (event) {
        if (!$(event.target).closest('.flyout').length && !$(event.target).closest('#fly-out').length) {
            $(".flyout").fadeOut();
        };
    });
	// switch to anchor
	anchor = window.location.hash;
	if (anchor) {
	    setTimeout("$('a[href=" + anchor + "]').click();", 250);
	}
});

