var home = {
	/**
	 * Events
	 */
	init: function(){
		var self = this;
		
		$("#hpBars li").hover(
			function(){
				$(this).addClass("hover");
			},
			function(){
				$(this).removeClass("hover");
			}
		);
		
		$("#whatWeOfferExpand").bind("click",function(){
			self.whatWeOffer();
		});
		$("#whatWeOfferCloseBtn").bind("click",function(){
			self.whatWeOffer(-1);
		});
		
	},
	
	/**
	 * Opens and closes content panel
	 *
	 * @param dir {Int} the direction
	 */
	whatWeOffer: function(dir){
		if (dir == -1){
			$("#whatWeOfferCloseBtn").hide();
			$("#whatWeOfferContent").slideUp('fast',function(){
				$("#whatWeOfferExpand").show();
			});
			$("#hpVideoBox").removeClass("open");
		} else {
			$("#whatWeOfferExpand").fadeOut(100);
			$("#hpVideoBox").addClass("open");
			$("#whatWeOfferContent").slideDown('normal',function(){
				var boxHeight = $("#hpVideoBox").outerHeight();
				$("#whatWeOfferCloseBtn").css("top",boxHeight).show();
			});
		}
	}
};

var programsMenu = {
	/**
	 * Events
	 */
	init: function(buttonOnly){
		var self = this;
		$("#programsNavBtn").bind("click",function(evt){
			self.show(evt);
		});
		if (!buttonOnly){
			$("body").bind("click",function(evt){
				self.hide(evt);
			});
		}
	},
	/**
	 * Hides menu 
	 *
	 */
	hide: function(evt,close){
		var self = this;
		var $clicked = $(evt.target);
		if ($clicked.is("#programsNavBtn")){
			return false;
		} else {
			if ($.browser.msie && is_home){
				//ie7 fix for position relative
				$("#hpVideoBox").addClass("posRel");
				$("#whatWeOfferExpand").removeAttr("style");
			}
			$("#programs_menu").slideUp(100,function(){
				$("#programsNavBtn").removeClass("open");
				$(this).remove();
			});
		}
	},
	/**
	 * Shows menu 
	 *
	 */
	show: function(evt){
		var self = this;
		var $clicked = $(evt.target);
		if ($("#programs_menu").length){
			if ($clicked.is("#programsNavBtn")){
				return false;
			}
			$("#programs_menu").remove();//don't add it twice
		} 
		var $footerProgramsUL = $("#programsFooterNav");
		var $menuNav = $("<div />").attr("id","programs_menu");
		//get pos
		var pos = $("#programsNavBtn").position();
		
		//update dom elements
		$footerProgramsUL
			.find("a:first")
			.addClass("first")
			.end()
			.find("a:last")
			.addClass("last")
		;
		if ($.browser.msie && is_home){
			//ie7 fix for position relative
			var $vidBox = $("#hpVideoBox");
			$vidBox.removeClass("posRel");
			var x = ($vidBox.width() + $vidBox.position().left) - 135;
			var y = $("#hpLogo").position().top + $("#hpLogo").height() + 6;
			$("#whatWeOfferExpand").css({"left":x,"top":y});
		}
		
		//build new menu
		$menuNav
			.css({
				display:"none",
				top:(pos.top + 23),
				left:(pos.left - 123) //123 is width of menu - width of button
			})
			.html('<ul>'+$footerProgramsUL.html()+'</ul>')
			.appendTo("#header")
			.bind("mouseleave",function(evt){
				self.hide(evt);
			})
			.slideDown('fast')
		;
		//swap button
		$("#programsNavBtn")
			.css("outline","none")
			.addClass("open")
		;
	}
};

var common = {
	
	breadcrumb: function(){
		var $last = $("#breadcrumbLast");
		if ($last.length){
			var width = $last.width();
			$("#breadcrumbLastArrow").css("left",width/2);
		}
	}
	
};

$(function(){
	home.init();
	programsMenu.init();
	common.breadcrumb();
});