// JavaScript Document
$(document).ready(function(){
	jolly.getPageId();
	jolly.globalLoadFunctions();
	jolly.loadPlugins();
	jolly.pageLoadFunctions();
});

var jolly = function() {
	/*******************************************************************************************
				PRIVATE PROPERTIES AND METHODS
	*******************************************************************************************/
	
	
	/***************************** Dynamic loadings *****************************/
	
	// Plugins. Note: plugin_data can only uses one entry for a page, if both needed, use entry for id, which overrides class	
	var pluginData = {
		/* facilities: {
			type:		'id',
			filestoget:	['js/jquery.flash.min.js', 'js/jquery.sifr.js'],
			callback:	'doSIFR'
		} */
	};
	
	// Functions, based on page id	
	var pageFunctions = {
		home:			['bindHideContent'],
		facilities:		['getScrollData'],
		events:			['getScrollData'],
		forms:			['pdf_newWindow'],
		directions:		['showGoogleMap'],
		pirate:			['getScrollData']
		
	};
	
	// Global functions
	var globalFunctions = ['doSIFR', 'showFirstImage']; //

	/***************************** Private Properties *****************************/
	
	var pageId;
	
	var scrolldata = {};
	
	var directions;
	

	/***************************** Private Methods *****************************/	
	
	// helper for plugin loader
	function createCallback(methodName) {
		return function(){jolly[methodName]()}
	}

	// bind the scroll function to the subnav anchors for facilities
	function localscroll() {						
		// bind the click event
		$('#mainnav ul li a').click(function(){
			// get the hash so we know what element to show
			var hash = $(this).attr('href').split('#');
			hash = hash[1];
			
			// kill any click on same nav
			if (scrolldata.currentdiv == hash) return false;
			
			// highlight the nav
			$('#mainnav ul li a').removeClass('active');
			$(this).addClass('active');
			
			// get the height of the content box
			var contentheight = $('#content').height()
			// get the height of the current div				
			var currentheight = scrolldata[scrolldata.currentdiv].height;
			// get the height of the coming div
			var toshowheight = scrolldata[hash].height;
			
			// show the new div and scroll things				
			$('#content > div.' + hash).css('display', 'block');
			var direction, divtomove, topmargin, amounttomove;				
			if (scrolldata[scrolldata.currentdiv].position < scrolldata[hash].position) {
				// go up
				direction = 'up';
				topmargin = -scrolldata[scrolldata.currentdiv].height
				divtomove = scrolldata.currentdiv;
				// reset the top margins
				$('#content div').css('margin-top', '15px');
				
				$('#content div.' + divtomove).animate({
					marginTop:	topmargin
						}, 500, function(){
							// hide the old div
							$('#content div.' + scrolldata.currentdiv).css('display', 'none');
							
							// set the new current div
							scrolldata.currentdiv = hash;
							
							// change overflow back
							$('#content').css('overflow', 'auto');							
				});
			} else {
				// go down
				direction = 'down';
				amounttomove = -scrolldata[hash].height;
				topmargin = '15px';
				divtomove = hash;
				
				// reset the top margin of the div to move in
				$('#content div.' + divtomove).css('margin-top', amounttomove + 'px');
				$('#content div.' + divtomove).animate({
					marginTop:	topmargin
						}, 500, function(){
							// hide the old div
							$('#content div.' + scrolldata.currentdiv).css('display', 'none');
							
							// set the new current div
							scrolldata.currentdiv = hash;
							
							// change overflow back	
							$('#content').css('overflow', 'auto');									
				});
				
			}

			// now the photos
			if (pageId == 'facilities' || pageId == 'events') {			
				/* $('#photos img:visible').fadeOut(function(){
					$('#photos img.'+hash).fadeIn();
				}); */
				if ($('#photos img.current').length > 0) {
					$('#photos img.current').fadeOut(function(){
						$('#photos img.current').removeClass('current');
						$('#photos img.'+hash).addClass('current').fadeIn();
					});
				} else {
					$('#photos img.'+hash).addClass('current').fadeIn();
				}
									
			}

			// kill default action
			return false;
		});

	}
	
	
	return {

		/*******************************************************************************************
					PUBLIC PROPERTIES AND METHODS
		*******************************************************************************************/
		
		/***************************** Dynamic Loaders *****************************/
	
		// global on-ready functions
		globalLoadFunctions: function() {
//			try {
				for (var i=0; i<globalFunctions.length; i++) {
					//createCallback(globalfunctions[i]);
					jolly[globalFunctions[i]]();
				}
//			} catch(ex) {}	
		},

		// the plugin loader, callbacks below
		loadPlugins: function() { 
//			try {
				// use body id or class? check to see if it has an entry
				var ref = (pluginData[$(document.body).attr('id')])? $(document.body).attr('id'): $(document.body).attr('class');
				
				if (!ref || ref == '') return;
				
				var callback = createCallback(pluginData[ref].callback);
				
				$.plugin('loader', {
					selectors:	['body'],
					files:		pluginData[ref].filestoget,
					callback:	callback	
				});
				$.plugin('loader').get();
				
//			} catch (ex) { /* in case it's not used */ }
		},
		
		// specific page-function loader, methods below
		pageLoadFunctions: function() {
//			try {
				if (pageFunctions[pageId]) {
					var f = pageFunctions[pageId];
					if (f.length > 1) {
						for (var i=0; i<f.length; i++) {
							jolly[f[i]]();
						}
					} else {
						jolly[f]();
					}
				}
//			} catch(ex) {}					
		},

		/***************************** Global Functions, DO NOT use any plugins here as they're not loaded yet *****************************/

		// put the page id into a "global" for access whenever needed
		getPageId: function() {
			pageId = $(document.body).attr('id');
		},

		// get the classes of div#content for use in scrolling
		getScrollData: function() {
			var name;
			var divs = $('#content').children('div').get();
			for (var i=0; i<divs.length; i++) {
				name = $(divs[i]).attr('class');
				scrolldata[name] = new Object();
				scrolldata[name].position = i;
				scrolldata[name].height = $(divs[i]).height();
			}
			
			scrolldata.divcount = divs.length;
			scrolldata.currentdiv = $(divs[0]).attr('class');
			
			// hide everything but the first div
			$('#content div[class!='+scrolldata.currentdiv+']').css('display', 'none');
			// show the ul 
			$('#nav_' + pageId + ' ul').css('display', 'block');			
			// change overflow property on div#content
			//$('#content').css('overflow', 'hidden');
			// highlight the first subnav li if there's no intro
			// get the hash
			var hash = $('#nav_'+pageId+' ul.subnav li:first a').attr('href').split('#');
			hash = hash[1];
			if (hash == scrolldata.currentdiv) {
				$('#nav_'+pageId+' ul.subnav li:first a').addClass('active');
			}

			// show the first picture
			//var classname = $('#content div:visible').attr('class');
			//$('#photos img.'+classname).css('display', 'block');
			$('#content div:visible').addClass('current');
			
			// bind the click event
			localscroll();
		},
		
		doSIFR: function() {
			if ($.browser.msie) return;			
			try {
				$('h2').sifr({ // h1.page_content, 
					path: 'http://webserver/www/jollymx.com/static/2009/',
					font: 'fertigo',
					height:	30
				});
			} catch (ex) {
				// catches exceptions for elements that don't exist on page
			}
		},
				
		
		// same as above, but when coming from inside the page
		bindInpageNav: function() {
			$('#nav_' + pageId + ' ul a').click(function(){				
				var hash = $(this).attr('href').split('#');
				hash = hash[1];
				if (hash != '') {
					$(this).parent().siblings('li').children('a').css('color', '#777');
					$(this).css('color', '#fff');
					//$('#photos img').css('display', 'none');
					$('#photos img').fadeOut('slow', function(){
						$('#content > div').fadeOut('slow', function(){
							$('#photos .' + hash).fadeIn('slow', function(){
								$('#content div.' + hash).fadeIn('slow');
							});	
							$('#content div.' + hash).fadeIn('slow');
						});
					});
					/* $('#content > div').css('display', 'none');
					$('#photos .' + hash).fadeIn('slow',function(){
						$('#content div.' + hash).slideDown();
					}); */
				}
				
				
				// cancel default action
				return false;
			});		
			
		},
		
		// show the first photo in the photos div (display:none; in css)
		showFirstImage: function() {
			try {
				$('#photos img:first-child').addClass('current');
			} catch (ex) {
				// in case there's no image
			}
		},
		
		/***************************** Plugin Callbacks *****************************/
		
		// need something, but don't need anything?
		dummy: function() {
			// do nothing!
		},

		
		/***************************** On-ready Functions, specific pages *****************************/
		
		
		bindHideContent: function(){
			$('#hidecontent').click(function(){
				$('#content').fadeOut();
				$('#weather').fadeOut();
				$(this).css('display', 'none');
				$('#showcontent').css('display', 'block');
				return false;
			});
			$('#showcontent').click(function(){
				$('#content').fadeIn();
				$('#weather').fadeIn();
				$(this).css('display', 'none');
				$('#hidecontent').css('display', 'block');
				return false;
			});
		},
		
		
		pdf_newWindow: function() {
			$('#content a.outerlink').click(function(){
				var url = $(this).attr('href');
				window.open(url, 'pdf', 'scrollbars, resizable');
				return false;	
			});
		},
		
		showGoogleMap: function() {		
			var markerOptions = {
				clickable: false
			};
			var directionsPanel;
						
			if (GBrowserIsCompatible()) {
				var map = new GMap2(document.getElementById("map_canvas"));
				var latlng = new GLatLng(43.1851, -72.2078);
				map.setCenter(latlng, 12);
				//map.setCenter(new GLatLng(43.1857, -72.2060), 12); //43.1657
				directionsPanel = document.getElementById("map_directions");
				directions = new GDirections(map, directionsPanel);
				map.setZoom(10);
				
			} else {
				// do something for these people
				return;				
			}
			
			map.addOverlay(new GMarker(latlng, markerOptions));
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			map.addControl(new GOverviewMapControl());
			
			// bind the get-directions button
			$('#getdirections').click(function() {
				var from = $('#directions_from').val();
				if (from == '') {
					alert('Please enter your starting point.');
					$('#directions_from').focus();
					return;
				}
				var drive = 'from: ' + from + ' to: 43.1851, -72.2078';
				$('#content > div').fadeOut(function(){
					// hide the descriptive para and input stuff
					$('#content p.description, #form_directions').css('display', 'none');
					directions.load(drive);					
					$('#content > div').fadeIn(function(){
						$('p.tryagain').css('display', 'block');
					});
				});
				
			});
			
			$('p.tryagain a').click(function(){
				$('#map_directions').fadeOut(function(){
					$('p.tryagain').css('display', 'none');
					$('#form_directions').fadeIn();
					$('#directions_from').val('').focus();
				});
				
			});
		}

		
		
		
	}
	
}();
