/**
 * Required scripts for the site
 */

/**
 * Object to control revealing main and sub menus
 */
var MainMenus = {
	/**
	 * Sets up the menus
	 */
	setup : function () {
		/* Find all the main menus */
		var menus = $('div.menu-container').find('li');
		
		/* Attach hovers on the menus */
		$.each(menus, function (idx, elem) {
			$(elem).hover(function (event) {
				/* See if there are sub menus */
				var lm = $(elem);
				$(elem).children('ul')
					.stop(true, false)
					.css({'display' : 'block'})
					.animate({'opacity' : 1}, 300);
			}, function (event) {
				/* See if there are sub menus */
				var lm = $(elem);
				$(elem).children('ul')
					.stop(true, false)
					.animate({'opacity' : 0}, 300, function () {
						$(elem).children('ul').css({'display' : 'none'});
					});
			});
		});
		
		/* Make sure the opacity of subs is 0 by default */
		menus.find('ul').css({'opacity' : 0});
	}
}


/**
 * Scripts specific to the bars page
 */
var BarPage = {
	/* Settings for the popup windows */
	settings : {
		width : 450,
		height : 600
	},
	/**
	 * Runs when a sidebar link is clicked
	 */
	onSidebarLinkClick : function (event) {
		/* Fix IE */
		if (!event.target) {
			event.target = event.srcElement;
		}
		
		/* Gather the link info */
		var url = $(event.target).parent().attr('href'),
			topCalculation = ((screen.height / 2) - (BarPage.settings.height / 2)),
			leftCalculation = ((screen.width / 2) - (BarPage.settings.width / 2)),
			features = 'resizable=0,width='+BarPage.settings.width+'px,height='+BarPage.settings.height+'px,top='+topCalculation+'px,left='+leftCalculation+'px,menubar=0,toolbar=0,scrollbars=1';
		
		/* Open a new window */
		window.open(url, 'barDetail', features, false);
		
		/* Get rid of the usual link action */
		return false;
	},
	/**
	 * Sets up special elements on the bar page
	 */
	setup : function () {
		/* See if we need to run */
		if ($('div#bar').size() <= 0) {
			return;
		}
		
		/* We need to hook the sidebar items to open new resized windows */
		var dom = $('div#bar'),
			links = dom.find('div.sidebar').find('a');
		links.click(BarPage.onSidebarLinkClick);
	}
}


/**
 * Scripts specific to the jobs page
 */
var JobsPage = {
	/* Settings for the popup */
	settings : {
		
	},
	/**
	 * Function to react to clicking of the link
	 */
	onLinkClick : function (event) {
		/* Fix IE */
		if (!event.target) {
			event.target = event.srcElement;
		}
		
		/* Gather the info and launch */
		var url = $(event.target).attr('href'),
			topCalculation = ((screen.height / 2) - (BarPage.settings.height / 2)),
			leftCalculation = ((screen.width / 2) - (BarPage.settings.width / 2)),
			features = 'resizable=0,width='+BarPage.settings.width+'px,height='+BarPage.settings.height+'px,top='+topCalculation+'px,left='+leftCalculation+'px,menubar=0,toolbar=0';
	
		/* Open a new window */
		window.open(url, 'barDetail', features, false);
		
		/* Override default functionality */
		return false;
	},
	/**
	 * Sets up the page elements
	 */
	setup : function () {
		/* See if we need to run */
		if ($('div#page-jobs').size() <= 0) {
			return;
		}
		
		/* Hook clicking of any announcements */
		var dom = $('div.announcement-list').find('h2'),
			links = dom.find('a');
		links.click(JobsPage.onLinkClick);
	}
}


/**
 * Newsletter signup snippet
 */
var NewsletterSignup = {
	/**
	 * Handler for when the link is clicked
	 */
	onToggleClick : function (event) {
		/* Handle IE */
		if (!event.target) {
			event.target = event.srcElement;
		}
		
		/* Position the element */
		var elem = $('div#newsletter-signup-snippet');
		var off = $(event.target).offset();
		elem.css({
			'top' : off.top + 15,
			'left' : off.left
		});
		elem.toggle();
		$(event.target).toggleClass('tab-on');
		return false;
	},
	/**
	 * Runs the setup to hook the toggle link
	 */
	setup : function () {
		/* Find the menu item */
		var link = $('a[href=#newsletter-signup]');
		link.click(NewsletterSignup.onToggleClick);
		var close = $('a[href=#close]');
		close.click(function () {
			link.click();
			return false;
		});
	}
}


/* Listen for page load */
$(function () {
	MainMenus.setup();
	BarPage.setup();
	JobsPage.setup();
	NewsletterSignup.setup();
});

