jQuery.fn.Accordion = function() {
	return this.each(function() {
		$('.drawer.open', this).each( function() {
			$ul = $(this).children('div.info').children('.content');
			$height = $ul.outerHeight();
			$(this).css('height', $height + 'px');
		});
		
		if( !$(this).hasClass('off')) {
			$('.drawer.closed', this).bind("click", openDrawer );
			$('.drawer.open', this).children('a').bind( 'click', closeDrawer  );
		}
	});

	function openDrawer(e) {
		e.preventDefault();
		
		$(this).unbind("click", openDrawer );
		//$(this).addClass("open");
		
		/* 	@hack ie
			This nasty hack is for forms in the accordion in I.E.
			It likes rendering the forms on top of everything else, so
			we hide it via css and use jquery to display the form
			when first rendered via the open function
		*/
		if ( ($(this).children('div.info').children('div.content').children('form').css('display') == 'none') &&
			(!jQuery.browser.msie) ) {
			$(this).children('div.info').children('div.content').children('form').css('display', 'block');
		}
		
		/*	@hack ie
			Continuing the tradition of nasty hacks we check for I.E. and display the form just long enough
			to calculate the height of its parent, then we hide it again until the drawer is fully open
		*/
		if (jQuery.browser.msie) $(this).children('div.info').children('div.content').children('form').css('display', 'block');
		
		$ul = $(this).children('div.info').children('.content');
		$height = $ul.outerHeight();
		
		/* 	@hack ie
			Hide the form for I.E. so it does not render on top of everything else while animating open
		*/
		if (jQuery.browser.msie) $(this).children('div.info').children('div.content').children('form').css('display', 'none');

		$(this).addClass("open");
					
		$(this).animate({ height:$height+"px"},
				800,
				'easeInOutQuad',
				function() {
					// Add the closed class
					$(this).removeClass("closed");
					//$(this).addClass("open");
					$(this).children('a').click(  closeDrawer  );
					
					/*	@hack ie
						For I.E. we need to show the form after the animation is done.
					*/
					if (jQuery.browser.msie) $(this).children('div.info').children('div.content').children('form').css('display', 'block');
				});

		// Closed all open drawers
		$openDrawers = $(this).siblings('.open').each ( function() { animateClosed(this); } );

		return false;
	}

	function closeDrawer(e) {
		e.preventDefault();
		
		$target = $(this).parent();
		//$height = $target.children('div.info ul').height();
		animateClosed($target);
		return false;
	}

	function animateClosed($target) {
		$($target).removeClass("open");
		$($target).addClass("closing");
					
		//log($($target).children('div.info').children('div.content').children('form'));
		//log($($target).children('div.info').children('div.content').children('form')[0]);
		if (jQuery.browser.msie) $($target).children('div.info').children('div.content').children('form').css('display', 'none');
		
		// The css for this site uses 0.9em as the height of a closed accordion.
		// However, using em's causes jquery to jump the window to the top every time
		// this animation runs, causing the page to scroll up if it is not already
		// at the top. This kinda negates the animation since it pulls the user away
		// so abruptly so we use pixels instead.
		$($target).animate({ height:"13px"},
				800,
				'easeInOutQuad',
				function() {
					// Add the closed class
					$(this).removeClass("open");
					$(this).removeClass("closing");
					$(this).addClass("closed");
					$(this).children('a').unbind( 'click', closeDrawer );
					$(this).bind("click", openDrawer );
				});
	}
}








/**
 * Toggle the header div up and down when the user presses the accordion button
 *
 * @use Expects a div with id of 'headerToggleButton' to act as the button
 *
 * @author jON bEEBE
 */
jQuery.fn.exploreHeader = function() {
	return this.each(function(){
		$("#headerToggleButton").click( function() {
			log("processing overlay");
				// Find the parent div (the 'explore' header)
				$parent = $(this).siblings('.desk');

				// If the parent is closed then open it up
				if($parent.hasClass('closed')) {

					try {
						SetCookie('collapse', 'false');
					}
					catch(error) {
						
					}

					$overlay = $parent.children('.overlay');
					$overlay.animate({ opacity:"0", backgroundColor:"#000000" },
						500,
						null,
						function() {
							$(this).removeClass("overlay_on");
							$(this).addClass("overlay_off");
						});

					// Remove the closed class
					$parent.removeClass('closed');

					// Mark the botton as NOT closed
					$(this).removeClass('closed');

					// Animate the div to its large size
					$parent.animate({ height:"388px"},
							500,
							null,
							function() {
								// Add the open class
								$(this).addClass("open");

								// Show the hidden flash movie
								$(this).children('.flash_movie').css('visibility', 'visible');
								$(this).children('.flash_movie').css('display', 'block');
							});
				}
				// The parent is open so close it
				else {
					try {
						SetCookie('collapse', 'true');
					}
					catch(error) {
						
					}

					// If the overlay is not off then fade it off
					$overlay = $parent.children('.overlay');
					if($overlay.css("visibility") != "visible") {
						$overlay.css("opacity", "0");
						$overlay.css("visibility", "visible");
					}

					$overlay.animate({ opacity:"0.8", backgroundColor:"#007bb0" },
						500,
						null,
						function() {
							$(this).addClass("overlay_on");
							$(this).removeClass("overlay_off");
						});

					// Remove the open class
					$parent.removeClass('open');

					// Mark the botton as closed
					$(this).addClass('closed');

					//Hide the flash movie
					$parent.children('.flash_movie').css('visibility', 'hidden');
					$parent.children('.flash_movie').css('display', 'none');

					// Animate the div to its small size
					$parent.animate({ height:"21px"},
							500,
							null,
							function() {
								// Add the closed class
								$(this).addClass("closed");
							});
				}
			} );
	});
};

jQuery.turnOnOverlay = function($target) {
	$target.removeClass('overlay_off');

	// When the page first loads the target is hidden with
	// an opacity of 100%, so we want to initialize these two
	// styles before animating it in
	$target.css("opacity", "0");
	$target.css("visibility", "visible");

	// Animate the overlay on
	$target.animate({ opacity:"0.8" },
			1000,
			null,
			function() {
				$(this).addClass("overlay_on");
			});
}

jQuery.turnOffOverlay = function($target) {
	$target.removeClass('overlay_on');

	$target.animate({ opacity:"0" },
			250,
			null,
			function() {
				$(this).addClass("overlay_off");
				//$(this).children('.flash_movie').css('visibility', 'hidden');
			});
}

/*
 * Part of the external interface to ActionScript
 * For the flash movie to turn the dark overlay on
 */
function turnOnOverlay() {
	//log('trying to turn on overlay');
	$target = $('#explore').children('.overlay');
	$.turnOnOverlay($target);
}
/*
 * Part of the external interface to ActionScript
 * For the flash movie to turn the dark overlay off
 */
function turnOffOverlay() {
	//log('trying to turn off overlay');
	$target = $('#explore').children('.overlay');
	$.turnOffOverlay($target);
}







function hijackFrontPageFork() {
	$('#experienced').click( function(event) { 
			event.preventDefault();
			
			$('#frontexperienced').css('display', 'block');
			$('#frontcollege').css('display', 'none');
			$('#front01').css('display', 'none');
			
			try {
				SetCookie('college', 'FALSE');
			}
			catch(error) {
				
			}
		} );
	$('#college').click( function(event) { 
			event.preventDefault();
			
			$('#frontcollege').css('display', 'block');
			$('#frontexperienced').css('display', 'none');
			$('#front01').css('display', 'none');
			
			try {
				SetCookie('college', 'TRUE');
			}
			catch(error) {
				
			}
		} );	
}








$(document).ready(function () {
	log("The DOM is now loaded and can be manipulated.");
	// Initialize the header functionality
	$('#explore').exploreHeader();
	
	// Initialize any accordions on the page
	$('.accordian').Accordion();
	
	// Do the font page work...
	// This only affects the front page, so don't worry if 
	// this code runs on other pages
	hijackFrontPageFork();
});

function SetCookie(cookieName,cookieValue,nDays) {
	// log("setting cookie '" + cookieName + "' to '" + cookieValue + "'");
	var today = new Date();
	var expire = new Date();
	if (nDays==null || nDays==0) nDays=1;
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName+"="+escape(cookieValue)
		 + ";expires="+expire.toGMTString()
		 + ";path=/";
}
