jQuery(document).ready(function($) {
	var galleryopen = false;
	var neveropened = true;
	function embedFlash() {
		var params = {};
		params.wmode = "transparent";
		params.allowfullscreen = true;
		swfobject.embedSWF("/wp-content/themes/chdirector/media/gallery.swf", "flashembed", "900", "480", "9.0.28", "/wp-content/themes/chdirector/js/swfobject/expressInstall.swf", null, params, null, flashLoaded);
	}
	function flashLoaded(e) {
		if( e.success == true ) {
			neveropened = false;
			$('div#gallery').animate({
				opacity : 1,
				height: '100%'
			}, 1000);
		} else {
			$('div#flashembed').html('Sorry, the gallery failed to load. Please try refreshing the page.');
		}
	}

	function centerWrap(callback) {
		var centerOffset = ( $(window).height() - $('div#wrapper').height() ) / 2;
		if( centerOffset > 0 ) {
		$('div#wrapper')
			.stop()
			.animate({
				top: centerOffset
			}, 1000, 'easeInOutCirc', function() {
				if (typeof callback == 'function') callback(); else return;
			});
		} else {
			$('div#wrapper').stop().animate({ top : '0px' });
		}
	}
	$('div#wrapper')
		.prepend('<div id="gallerypanel"><a id="gallerybar" href="/photo-gallery" title="View the photo gallery"></a><div id="gallery"><div id="flashembed">Loading&hellip;</div></div></div>');
	$('a#gallerybar')
		.click(function(){
			if( galleryopen != true ) {
				$("a#gallerybar").attr("title", "View the site content.");
				$(this)
				.parent()
				.animate({
					width : '100%'
				}, 1000, 'easeInOutCirc');
						$("a#gallerybar")
							.animate({
								backgroundPosition : "-120px 0px"
							}, 500);
				if( $('div#container').height() > 480 ) {
					$('div#container').slideUp(1000, function(){
						centerWrap(function() {
							if( neveropened != false ) {
								embedFlash();
							} else {
								$('div#gallery').animate({
									opacity : 1,
									height: '100%'
								}, 500);
							}
						});
					});
				} else {
					$("a#gallerybar")
						.animate({
							backgroundPosition : "-120px 0px"
						}, 500);
					centerWrap(function() {
						if( neveropened != false ) {
							embedFlash();
						} else {
							$('div#gallery').animate({
								opacity : 1,
								height: '100%'
							}, 500);
						}
					});
				}
				galleryopen = true;
			} else {
				$("a#gallerybar").attr("title", "View the photo gallery.");
				$('div#gallery').animate({
					opacity: 0.01,
					height: '1px'
				}, 700);
				$("a#gallerybar")
					.animate({
						backgroundPosition : "0px 0px"
					}, 500);
				$('div#gallery')
				.parent()
				.animate({
					width : '59px'
				}, 700, 'easeInCirc', function(){
					$('div#container').slideDown(1000, function() {
						centerWrap();
					});
				})
				.css({
					'z-index' : '699'
				});
				galleryopen = false;
			}
			return false;
		});
});
