// modified http://paulirish.com/2009/markup-based-unobtrusive-comprehensive-dom-ready-execution/
// only fires on body class, not body class + id (working off strictly wordpress body_class)

IPMA = {
	// all pages
	common: {
		init: function(){

			// Project Manager Search Form
			if($("#pm_search_form").length > 0)
				$("#search-results").tablesorter({"headers": {3: {"sorter":false} } });

			if (Modernizr.touch) {
				$(function() {
				
					// Mobile only, not iPad
					if(!screen || screen.width < 768)
					{
						$('#nav-main > ul').each(function() {
			    			$(this).find('ul.sub-menu li a').each(function() {
					    		$(this).prepend('&ndash; ');
					    	});
	
			    			$(this).find('ul.sub-menu li li a').each(function() {
					    		$(this).prepend('&ndash; ');
					    	});
						});
					}

				    $('#nav-main > ul').each(function() {

				        var $select = $('<select />');

				        $(this).find('> li > a').each(function() {
				            var $option = $('<option />');
				            $option.attr('value', $(this).attr('href')).html($(this).html());
				            $select.append($option);
				        });

				        $select.append('<option value="" selected="selected">&mdash;</option>');

				        $(this).find('li a').each(function() {
				            var $option = $('<option />');
				            
				            $option.attr('value', $(this).attr('href')).html($(this).html());
				            $select.append($option);
				        });

				        ($select).appendTo($(this).parent());
				    });

				    $('#nav-main select').prepend('<option value="" selected="selected">Menu</option>');

					$('#nav-main select').change(function() {
						window.location = $('#nav-main option:selected').val();
					});
				});

				$('#main img').each(function() {
				    var maxWidth = 280; // Max width for the image
				    var maxHeight = 600;    // Max height for the image
				    var ratio = 0;  // Used for aspect ratio
				    var width = $(this).width();    // Current image width
				    var height = $(this).height();  // Current image height

				    // Check if the current width is larger than the max
				    if(width > maxWidth){
				        ratio = maxWidth / width;   // get ratio for scaling image
				        $(this).css("width", maxWidth); // Set new width
				        $(this).css("height", height * ratio);  // Scale height based on ratio
				        height = height * ratio;    // Reset height to match scaled image
				    }

				    // Check if current height is larger than max
				    if(height > maxHeight){
				        ratio = maxHeight / height; // get ratio for scaling image
				        $(this).css("height", maxHeight);   // Set new height
				        $(this).css("width", width * ratio);    // Scale width based on ratio
				        width = width * ratio;    // Reset width to match scaled image
				    }
				});
			}

			$('#nav-main').hover(function(){
				$('#nav-main-bar').removeClass('hidden');
			},
			function(){
				$('#nav-main-bar').addClass('hidden');
			});

			$('#nav-main #menu-about-us').hover(function(){
				$('#nav-main-bar').addClass('menu-about-us');
			},
			function(){
				$('#nav-main-bar').removeClass('menu-about-us');
			});

			$('#nav-main #menu-membership').hover(function(){
				$('#nav-main-bar').addClass('menu-membership');
			},
			function(){
				$('#nav-main-bar').removeClass('menu-membership');
			});

			$('#nav-main #menu-certification').hover(function(){
				$('#nav-main-bar').addClass('menu-certification');
			},
			function(){
				$('#nav-main-bar').removeClass('menu-certification');
			});

			$('#nav-main #menu-awards').hover(function(){
				$('#nav-main-bar').addClass('menu-awards');
			},
			function(){
				$('#nav-main-bar').removeClass('menu-awards');
			});

			$('#nav-main #menu-education-training').hover(function(){
				$('#nav-main-bar').addClass('menu-education-training');
			},
			function(){
				$('#nav-main-bar').removeClass('menu-education-training');
			});

			$('#nav-main #menu-research').hover(function(){
				$('#nav-main-bar').addClass('menu-research');
			},
			function(){
				$('#nav-main-bar').removeClass('menu-research');
			});

			$('#nav-main #menu-young-crew').hover(function(){
				$('#nav-main-bar').addClass('menu-young-crew');
			},
			function(){
				$('#nav-main-bar').removeClass('menu-young-crew');
			});

			$('#nav-main #menu-resources').hover(function(){
				$('#nav-main-bar').addClass('menu-resources');
			},
			function(){
				$('#nav-main-bar').removeClass('menu-resources');
			});

			jQuery(".menu-item .arrow").live("click", function(e)
			{
				var $submenu = jQuery(this).nextAll(".sub-menu");

				if($submenu.is(":visible"))
				{
					$submenu.parent("li")
						.addClass("shrunk")
						.removeClass("expanded");

					if(jQuery("html").is(".ie7") || jQuery("html").is(".ie6")) // IE hack
					{	$submenu.hide();
						jQuery("#sidebar")[0].className = jQuery("#sidebar")[0].className;
					}
					else $submenu.slideUp("fast");
				}

				else // $submenu.is(":hidden")
				{
					$submenu.parent("li")
						.addClass("expanded")
						.removeClass("shrunk");

					if(jQuery("html").is(".ie7") || jQuery("html").is(".ie6")) // IE hack
					{	$submenu.show();
						jQuery("#sidebar")[0].className = jQuery("#sidebar")[0].className;
					}
					else $submenu.slideDown("fast");
				}
			});

			// Unobfuscate email addresses
			if(Base64 != undefined)
			$("a[mail]").each(function(n, el)
			{
				var $email = $(el);
				var addy = Base64.decode($email.attr("mail"));
				$email
					.html(addy)
					.attr("href", "mailto:"+addy);
			});
		},
		finalize: function(){ }
	},
	// Home page
	home: {
		init: function(){

			$('#cycle').cycle({
			    timeout: 6000,
			    fx: 'scrollRight',
			    cleartypeNoBg: true,
			    pager: '#cycle-nav'
			});

		}
	},
	// Search Results
	search: {
		init: function(){

			$("#search-results").tablesorter();

			$("#search-results tbody tr").click(function(){
				window.location = $(this).find('a:last').attr('href');
			});

		}
	}
}

UTIL = {
	fire : function(func,funcname, args){
		var namespace = IPMA;  // indicate your obj literal namespace here
		funcname = (funcname === undefined) ? 'init' : funcname;
		if (func !== '' && namespace[func] && typeof namespace[func][funcname] == 'function'){
			namespace[func][funcname](args);
		}
	},
	loadEvents : function(){

		// hit up common first.
		UTIL.fire('common');

		// do all the classes too.
		$.each(document.body.className.split(/\s+/),function(i,classnm){
			UTIL.fire(classnm);
		});

		UTIL.fire('common','finalize');
	}
};

// kick it all off here
$(document).ready(UTIL.loadEvents);
