// JavaScript Document
$(document).ready(function(){
	
	CCD.getPageId();
	
	CCD.globalLoadFunctions();
	
//	CCD.loadPlugins();
	
	CCD.pageloadFunctions();	
	
	

});



var CCD = 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 plugin_data = {};
	
	
	var loadfunctions = {
		'directions':		['showGoogleMap']
	};
	
	// Global functions
	var globalfunctions = ['bindExternalLinks', 'loadMultibox']; //, 'nav_dropdown'
	
	
	/***************************** Private Properties *****************************/
	
	var pageId;
	
	// for google maps
	var directions;
	var drive = '';
	
	/***************************** Private Methods *****************************/	
	
	// helper for plugin loader
	function createCallback(methodName) {
		return function(){CCD[methodName]();};
	}
	
	
	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]);
					CCD[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 = (plugin_data[$(document.body).attr('id')])? $(document.body).attr('id'): $(document.body).attr('class');
				
				if (!ref || ref === '') return;
				
				var callback = createCallback(plugin_data[ref].callback);
				
				$.plugin('loader', {
					selectors:	['body'],
					files:		plugin_data[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 (loadfunctions[pageId]) {
					var f = loadfunctions[pageId];
					if (f.length > 1) {
						for (var i=0; i<f.length; i++) {
							KVD[f[i]]();
						}
					} else {
						CCD[f]();
					}
				}
//			} catch(ex) {}					
		},

		/***************************** Global Functions, DO NOT use any plugins *****************************/
		
		sitepath: '',
		
		// temp for dev and live: sets filepaths
		setSitepath: function() {
			//var sitepath;
			if (location.host.indexOf('webserver') != -1) {
				CCD.sitepath = 'http://webserver/www/claremontcycle.com/';
			} else {
				CCDD.sitepath = 'http://www.claremontcycle.com/';
			}
			//var sitepath = this.sitepath;
			jQuery.each(plugin_data, function(){
				var l = this.filestoget.length;
				for (var i=0; i<l; i++) {
					this.filestoget[i] = CCD.sitepath + this.filestoget[i];
				}
			});
		},
		
		// put the page id into a "global" for access whenever needed
		getPageId: function() {
			pageId = $(document.body).attr('id');
		},
		
		// binds the facebox script
		loadFacebox: function() {
			$('a[rel=facebox]').facebox({
				closeImage: CCD.sitepath + 'graphics/closelabel.gif' 
			});
		},
		
		// binds multibox
		loadMultibox: function() {
			$('a[rel*=multibox]').multibox();	
		},
		
		/***************************** Plugin Callbacks *****************************/
		
		// when you don't need one
		dummy: function() {
			// do nothing
		},
		
		/***************************** Public Methods *****************************/

		
		/* google maps stuff */
		getDirections: function() {
			var from = document.getElementById("directions_from").value; alert(from);
			if (from === "") return;
			drive = from + " to Claremont Cycle Depot, Claremont, NH";
			$('#map_directions>p').slideUp(1000, function(){
				$('#form_directions').slideUp(1000, function(){
					$('#map_canvas').slideUp(1000, function(){
						directions.load(drive);
					});
				});
			});			
		},

		showGoogleMap: function() {
			// hide the static and display the gmap stuff
			$('#static_map').css('display', 'none');
			$('#map_canvas').css('display', 'block');
			$('#form_directions').css('display', 'block');
			
			var icon = new GIcon(G_DEFAULT_ICON);
			icon.image = "http://webserver/www/claremontcycle.com/graphics/mapmarker_sign.png";
			//icon.shadow = "";
			icon.iconSize = new GSize(41, 38);
			//icon.shadowSize = new GSize(91, 62);
			icon.iconAnchor = new GPoint(15, 36);
			//icon.infoWindowAnchor = new GPoint(31, 8);
			
			var markerOptions = {
				clickable: false,
				icon: icon
			};
			var directionsPanel;
						
			if (GBrowserIsCompatible()) {
				var map = new GMap2(document.getElementById("map_canvas"));
				var latlng = new GLatLng(43.3683, -72.3777);
				map.setCenter(latlng, 12);
				directionsPanel = document.getElementById("google_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.3683, -72.3777';
				$('#normal_directions').slideUp(function(){
					directions.load(drive);
					$('#google_directions').slideDown();
					$('p.tryagain').css('display', 'block');
				});
				$('#canceldirections').fadeIn();
			});
			
			$('p.tryagain a').click(function(){
				$('#google_directions').slideUp(function(){
					$('p.tryagain').css('display', 'none');
					$('#form_directions').slideDown();
					$('#directions_from').val('').focus();
				});
				
			});
			
			$('#canceldirections').click(function(){
				$('p.tryagain').css('display', 'none');
				$('#google_directions').slideUp(function(){
					$('#normal_directions').slideDown();
				});
			});
		},
		
		bindExternalLinks: function() {
			$('p.read_more a').click(function(){
				var href = $(this).attr('href');
				var newwin = window.open(href, 'External', 'location,menubar,resizable,scrollbars,status,toolbar');
				return false;
			});
			$('a.externallink').click(function(){
				var href = $(this).attr('href');
				var newwin = window.open(href, 'External', 'location,menubar,resizable,scrollbars,status,toolbar');
				return false;
			});
		}
		
	}; // end return
}();
