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

var jolly = function() {
	
	/***************************** Dynamic loadings *****************************/	
	
	// Functions, based on page id	
	var pageFunctions = {
		home:			['bindHideContent'],
		facilities:		['changeNav'],
		events:			['changeNav'],
		forms:			['pdf_newWindow'],
		directions:		['showGoogleMap'],
		pirate:			['changeNav']
		
	};
	
	// Global functions
	var globalFunctions = ['doCufon', 'showFirstImage']; //doSIFR

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

	/***************************** Private Methods *****************************/	
	
	// get the data needed for scrolling and reclass subnav lis, divs and imgs
	function getScrollData() { 
		var name, href;
		var index = 0;
		var count = 0;
		var photocount = 0;
		$('#content').children('div').each(function(){
			// if has class 'all', use that, otherwise number the divs and add that class
			if ($(this).hasClass('all')) {
				name = 'all';
			} else {
				name = 'div' + index;
				$(this).attr('class', name);
				index++;
			}			
			scrolldata[name] = {};
			scrolldata[name].name = name;
			scrolldata[name].index = index;
			scrolldata[name].height = $(this).height();
			scrolldata[name].current = false;
			
			// hide em all, show the proper one later in showDiv()
			$(this).addClass('hide');
		});		
		// add the new classes to the subnav
		$('#nav_'+pageId+' .subnav').children('li').each(function(){				
			if (!$(this).hasClass('all')) {
				$(this).removeClass().addClass('div' + count);
				count++;					
			}				
		});
		// relate them to the divs
		$('#nav_'+pageId+' .subnav').children('li').each(function(){
			name = $(this).attr('class');
			href = $(this).children('a').attr('href'); 
			scrolldata[name].href = href;
		});		
		// relate them to any photos
		$('#photos img').each(function(){
			$(this).addClass('div'+photocount);
			photocount++;
		});
		
		// hide 'all' subnav
		$('.subnav li.all').css({display: 'none'});
	}
	
	// shows the proper div on first load
	function showDiv() {		
		for (div in scrolldata) {			
			if (scrolldata[div].href == location.href) { // for subnav pages called directly
				scrolldata[div].current = true; 
				doScroll($('.subnav li.'+scrolldata[div].name+' a'));
			} else { // for top-level pages
				scrolldata[div].current = false;
				try {
					// show 'all' if it exists
					scrolldata['all'].current = true;					
					$('#content div.all').removeClass('hide').addClass('current');
				} catch (e) {
					// show first div if it doesn't
					scrolldata['div0'].current = true;
					$('#content div.div0').removeClass('hide').addClass('current');
					// and the nav
					$('.subnav li.div0').addClass('current');
				}
			}
		}
	}
		
	function bindLocalScroll() {
		$('#mainnav .subnav a').click(function(){ 
			if ($(this).parent('li').hasClass('current')) return false;
			doScroll(this);
		
			return false;
		});
	}

	// does the actual scrolling for subnav items, passed <a> node
	function doScroll(el) {
		var i, div;
		// get the class
		var name = $(el).parent('li').attr('class');
		// get the height of the content box
		var contentheight = $('#content').height();
		// get the height of the current div				
		var currentheight = $('#content div.current').height(); 
 		var currentdivname = $('#content div.current').attr('class').slice(0, -8); // take off "current"
		// show the new div and scroll things				
		$('#content > div.' + name).removeClass('hide').addClass('current');
		var direction, divtomove, topmargin, amounttomove;	
		if (scrolldata[currentdivname].index < scrolldata[name].index) {
			// go up
			topmargin = -scrolldata[currentdivname].height;
			// reset the top margins
			$('#content div').css({marginTop: '15px'});
			
			$('#content div.'+currentdivname).animate({
				marginTop: topmargin
				}, 500, function(){
					// hide the old div
					$('#content div.'+currentdivname).removeClass('current').addClass('hide');
									
					// set the new current div
					scrolldata[currentdivname].current = false;
					scrolldata[name].current = true;
					
					// change overflow back
					$('#content').css('overflow', 'auto');
				
				});
		} else {
			// go down
			amounttomove = -scrolldata[name].height;
			topmargin = '15px';
			divtomove = name;
			
			// 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.' + currentdivname).removeClass('current').addClass('hide');
						
						// set the new current div
						scrolldata[currentdivname].current = false;
						scrolldata[name].current = true;
						
						// change overflow back	
						$('#content').css('overflow', 'auto');									
			});
		}
		 			
		// now the photos
		if (pageId == 'facilities' || pageId == 'events') {			
			if ($('#photos img.current').length > 0) {
				$('#photos img.current').fadeOut(function(){
					$('#photos img.current').removeClass('current');
					$('#photos img.'+name).addClass('current').fadeIn();
				});
			} else {
				$('#photos img.'+name).addClass('current').fadeIn();
			}									
		}

		// change the nav highlight
		$('#mainnav .subnav li').removeClass('current');
		$(el).parent('li').addClass('current');
	}
	
	return {

		/*******************************************************************************************
					PUBLIC PROPERTIES AND METHODS
		*******************************************************************************************/
		
		/***************************** Dynamic Loaders *****************************/
	
		// global on-ready functions
		globalLoadFunctions: function() {
			try {
				for (var i=0; i<globalFunctions.length; i++) {
					jolly[globalFunctions[i]]();
				}
			} catch(ex1) {}	
		},
		
		// 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(ex2) {}					
		},

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

		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
			}
		},
		
		// font replacement for h2
		doCufon: function() {			
			try {
				// if the scripts are already loaded, use them
				Cufon.replace(document.getElementsByTagName('h2'));
			} catch (ex) {
				// otherwise go recursive
				setTimeout(function(){return jolly.doCufon();}, 250);
			}
		},
		
		// 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
			}
		},
		
		
		/***************************** On-ready Functions, specific pages *****************************/
		
		// does the scrolling stuff
		changeNav: function() {
			if ($('nav_'+pageId+' .subnav')) {
				getScrollData(); // get the data
				showDiv(); // show the proper div onload
				bindLocalScroll(); // change the subnav to do scrolling
				//jolly.showScrolldata(); // to show in Firebug dom for troubleshooting
			}				
		},
		
		/* scrolldata: {},	
		showScrolldata: function() {
			jolly.scrolldata = scrolldata;	
		}, */
				
		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();
				});
				
			});
		}
		
	}
	
}();
