// JavaScript Document
$().ready( function ()
{ 
  $('.ww_more').click( function ( ev )
  {
    ev.preventDefault();
	fadeBG.currentImgArr = [
				'images/news/IMG_5247.jpg',
				'images/news/IMG_5298_5295.jpg',
				'images/news/IMG_5326.jpg',
				'images/news/IMG_5344.jpg',
				'images/news/IMG_5359.jpg',
				'images/news/IMG_5369.jpg',
				'images/news/IMG_5386.jpg',
				'images/news/IMG_5389.jpg'
				
			];
	fadeBG.currentCaptionArr = [
								 '',
								 '',
								 '',
								 '',
								 '',
								 '',
								 '',
								 ''
								 ];
	fadeBG.preloadImages( fadeBG.currentImgArr );
	fadeBG.fadeInPopUp();
  }); // end willye white _more click
  
  
    $('.jell_more').click( function ( ev )
  {

    ev.preventDefault();
	fadeBG.currentImgArr = [
				'images/news/jell_1.jpg',
				'images/news/jell_2.jpg',
				'images/news/jell_3.jpg',
				'images/news/jell_4.jpg',
				'images/news/jell_5.jpg'
				
			];
	fadeBG.currentCaptionArr = [
								 '',
								 '',
								 '',
								 '',
								 ''
								 ];
	fadeBG.preloadImages( fadeBG.currentImgArr );
	fadeBG.fadeInPopUp();
  }); // end jell _more click
  
    $('.gpmf_2010').click( function ( ev )
  {

    ev.preventDefault();
	fadeBG.currentImgArr = [
				'images/news/gpmf_2009_AF.jpg',
				'images/news/gpmf_2010_gatefold.jpg',
				'images/news/gpmf_2010_season_brochure.jpg',
				'images/news/gpmf_banner1.jpg',
				'images/news/gpmf_banner2.jpg',
				'images/news/gpmf_crowd.jpg'
				
			];
	fadeBG.currentCaptionArr = [
								 '',
								 '',
								 '',
								 ''
								 ];
	fadeBG.preloadImages( fadeBG.currentImgArr );
	fadeBG.fadeInPopUp();
  }); // end gpmf_2010 click
  

  
  
  
}); // end dom ready

$id = function (el)
{
	return document.getElementById(el);
} // end id

id = function (el)
{
	return document.getElementById(el);
} // end id

cl = function ( msg ) 
{
	//console.log( typeof console )
	if ( typeof console == 'object' ) {
		console.log( msg )
	}
}; // end cl

pr = function ( obj ) 
{
	if ( typeof console == 'object' ) { // don't bother unless this exists
		for( i in obj ) {
			cl( i + ' is: ' + obj[i] );	
		}
	} //
}; // end pr

winDim = {
	getWidth: function ()
    {
		var x = 0;
        if (self.innerHeight) {
			x = self.innerWidth;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			x = document.documentElement.clientWidth;
        } else if (document.body) {
			x = document.body.clientWidth;
		}
		return x;
	}, // end get wid
	getHeight: function ()
	{
		var y = 0;
		if (self.innerHeight) {
			y = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
 			y = document.documentElement.clientHeight;
		} else if (document.body) {
			y = document.body.clientHeight;
		}
		return y;
	} // end get ht
};

fadeBG = {
	windowHeight: 600,
	windowWidth: 800,
	contentTop: 0,
	currentImgArr: [],
	currentCaptionArr: [],
	currentImgNum: 0,
	boxHt: 400,
	getDocHeight: function ()
	{
		var db = document.body;
		var de = document.documentElement
		return Math.max(
			Math.max(db.scrollHeight, de.scrollHeight),
			Math.max(db.offsetHeight, de.offsetHeight),
			Math.max(db.clientHeight, de.clientHeight)
		);
	},
	preloadImages: function ( a )
	{
	  for ( i in a ) {
		im = new Image();
		im.src = a[i];
	  }
	}, // end preload images
	
	fadeInPopUp: function ( elID, closeID, callback )
	{
		var clickedEl = this;
		$(clickedEl).unbind('click');
		var scrollTop = $().scrollTop();
		if ( !id('fade_container') ) { // if the fade container is not in the dom, add it
			var html = '<div id="fade_container"></div>';
			$('body').prepend( html );
		}
		
		$('#fade_container').css({ opacity: 0, 'z-index': 300, 
			width: '100%', height: fadeBG.getDocHeight(), position: 'absolute', left: 0, top: 0,
			backgroundColor: '#000000', display: 'block'
		}).fadeTo( 750, 0.6, function () {
			
			var html = '\
			  <div id="current_image_container" style="height: ' + fadeBG.boxHt + 'px;">\
			  <div id="image_container" style="width: 100%; text-align: center;" >\
			      <img id="current_image" style="text-align: center;" src="' + fadeBG.currentImgArr[ fadeBG.currentImgNum ] + '" />\
				</div>\
			<table width="650" height="20" border="0" cellspacing="0" cellpadding="0" style="margin-top:15px;">\
				  <tr>\
    <td><p id="image_caption" style="text-align: left;">' + fadeBG.currentCaptionArr[ fadeBG.currentImgNum ] + '</p></td>\
    <td valign="middle" width="150" style="text-align:right;"><p class="slideshow_ctrls"><span id="prev_button">Previous</span>&nbsp;&nbsp;|&nbsp;&nbsp;<span id="next_button">Next</span></p></td>\
    <td valign="middle" width="18"  style="padding-left:10px;"><p><span id="close_container"><img src="images/close_btn.gif" alt="close" name="close" width="18" height="18" border="0" id="close" /></span></td>\
  </tr>\
</table>\
				</div>\
			';
			$( '#fade_container' ).after( html );
			id( 'current_image_container' ).style.marginLeft = ( -1 * id( 'current_image_container' ).offsetWidth / 2 ) + 'px';
			//id( 'image_container' ).style.marginTop = ( ((fadeBG.boxHt / 2) - 50) -  (id( 'image_container' ).offsetHeight / 2)  ) + 'px'; // 50 px for the close button atop
			$( '#next_button' ).click( fadeBG.nextImage );
			$( '#prev_button' ).click( fadeBG.prevImage );
			var scrollTop = $().scrollTop();
			var centeringAdd =  Math.round( (winDim.getHeight() - id("current_image_container").offsetHeight ) / 2 );
		 	var contentTop = ( scrollTop + centeringAdd ) + 'px';
			if( typeof callback == 'function') {
				callback();
			}
			
			id("current_image_container").style.top = contentTop;
			
			$( '#close_container' ).click( function( ev )
			{	
				$('#close_container').unbind('click');
				
				ev.preventDefault();
				fadeBG.fadeOutPopUp( );
			}); // end click
		});
	}, // fade in pop up
	fadeOutPopUp: function ( )
	{
		$( '#current_image_container' ).remove();
        $( id('fade_container') ).fadeOut(750, function()
		{
			$( id('fade_container') ).remove();	
		});
		
	}, // fade out pop up
	
	nextImage: function( ) 
	{
	  var numImages = fadeBG.currentImgArr.length;
	  ( fadeBG.currentImgNum === ( numImages - 1 ) ) ? ( fadeBG.currentImgNum = 0 ) : ( fadeBG.currentImgNum += 1 );
	  fadeBG.swapImages();
	}, // end next image 
	
	prevImage: function( ) 
	{
	  var numImages = fadeBG.currentImgArr.length;
	  ( fadeBG.currentImgNum === 0 ) ? ( fadeBG.currentImgNum = ( numImages - 1 ) ) : ( fadeBG.currentImgNum -= 1 );
	  fadeBG.swapImages();
	}, // end prev image 
	
	swapImages: function ()
	{
	 $('#current_image').fadeOut( '500', function () {
	    id( 'current_image' ).src = fadeBG.currentImgArr[ fadeBG.currentImgNum ];
		im = new Image( );
		im.src = fadeBG.currentImgArr[ fadeBG.currentImgNum ];
		var imHt = im.height;
		//id( 'image_container' ).style.marginTop = ( ((fadeBG.boxHt / 2) ) -  (imHt / 2)  ) + 'px'; // 50 px for the close button atop
		$('#current_image').fadeIn( '500' );
	  });
	  $('#image_caption').fadeOut( '500', function () {
	    id( 'image_caption' ).innerHTML = fadeBG.currentCaptionArr[ fadeBG.currentImgNum ];
		id( 'current_image_container' ).style.marginLeft = ( -1 * id( 'current_image_container' ).offsetWidth / 2 ) + 'px';
		$( '#image_caption' ).fadeIn( '500' );
	  });
	} // end fade image
	
	
} // end fadeBG object