
/**
 * 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");
							});
				}
			} );
	});
};

/**
 * This function simulates the overlay fade that should happen when the user starts or stops the flash movie
 *
 * @author jON bEEBE
 */
jQuery.fn.simulateOverlayFade = function() {
	return this.each(toggleOverlay);

	function toggleOverlay(){
		$(this).children('.flash_movie').click( function() {
				// Find the overlay object within this objects siblings
				$target = $(this).siblings('.overlay');

				// If the overlay is off then animate it on
				if($target.hasClass('overlay_off')) {
					jQuery.turnOnOverlay($target);
				}
				// The overlay is on, animate it off
				else {
					jQuery.turnOffOverlay($target);
				}
			} );
	}
}

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');
			});
}


function turnOnOverlay() {
	//log('trying to turn on overlay');
	$target = $('#explore').children('.overlay');
	$.turnOnOverlay($target);
}
function turnOffOverlay() {
	//log('trying to turn off overlay');
	$target = $('#explore').children('.overlay');
	$.turnOffOverlay($target);
}

$(document).ready(function () {
	//log("initializing the accordion header");
	$('#explore').exploreHeader();
	//$('#explore').simulateOverlayFade();

	// Test the toggle functions
	// turnOnOverlay();
	// turnOffOverlay();
});

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=/";
}


function log(msg) {
	try {
		console.log(msg);
	}
	catch(e) {
		
	}
}
