/*
#####################################################
# 
# Drago
# JavaScript setup routines
#
# Hand-crafted by Phenotype (phenotype.net)
#
#####################################################
*/

	////////////////////////////////////////////////////////////////////
	//	Initialise setup routines
	////////////////////////////////////////////////////////////////////
	
	// Called when DOM is ready
	$(document).ready(domSetup);
	
	// Called when entire page is loaded
	window.onload = pageSetup;
	
	// Called when DOM is unloaded
	$(window).unload(domUnload);

	////////////////////////////////////////////////////////////////////
	//	Define setup routines
	////////////////////////////////////////////////////////////////////
	
	/* 
	domSetup()
	
	All JavaScripts requiring initialisation on DOM LOAD should be called
	from this routine
	-----------------------------------------------------------------------
	*/
	
	function domSetup() {

		// Setup dropdown menus
		$('#navigation ul.dropdown-menu').superfish({
			delay: 1000,
			dropShadows: false,
			autoArrows: false,
			animation: {'height' : 'show' },
			speed: 'fast'
			/*pathClass: 'selected'*/
		});
		
		// Setup image lazy-loading
		$('img').lazyload({
			threshold		: 600,
			failurelimit	: 100,
			container		: $('#content'),
			placeholder 	: "assets/templates/drago/images/template/common/loading.gif",
			background		: "#ffffff",
			effect      	: "fadeIn",
			effectspeed		: 500
		});
		
		// Setup slideshow sliders
		/*if($('.slideshow')[0]) {
			contentSlider({ slides: '.slide', container: '.slideshow-container', scroller: '.slideshow-scroller', parent: '.slideshow', navigation: '.slideshow-navigation.thumbnails' }, { orientation: "horizontal", buttons: false }, { easing: 'easeInQuad', duration: 200 });
		}
		
		if($('.book-preview')[0]) {
			contentSlider({ slides: '.slide', container: '.slideshow-container', scroller: '.slideshow-scroller', parent: '.book-preview', navigation: '.slideshow-navigation.thumbnails' }, { orientation: "horizontal", buttons: false }, { easing: 'easeInQuad', duration: 200 });
		}*/
		
		$('ul.slideshow-container:not(#book-listing ul.slideshow-container, #event-listing ul.slideshow-container)').smaugSlide({
			baseClassName: 'slideshow',
			slideHeight: 338,
			easing: 'easeOutQuad',
			activeClassName: 'selected',
			secondaryNavigation: 'ul.slideshow-navigation.thumbnails'
		});
		
		$('#book-listing ul.slideshow-container, #event-listing ul.slideshow-container').smaugSlide({
			baseClassName: 'slideshow',
			slideHeight: 386,
			easing: 'easeOutQuad',
			activeClassName: 'selected',
			secondaryNavigation: 'ul.slideshow-navigation.thumbnails'
		});
		
		////////////////////////////////////////////////////////////////////
		//	Cufon
		////////////////////////////////////////////////////////////////////
		
		Cufon.replace('#navigation ul a:not(#navigation ul ul a)', { fontFamily: 'Century Gothic' });
		Cufon.replace('#content h2:not(#content .module.events h2.title, #content .module.books-index h2, #content .module.resources-index h2)', { fontFamily: 'Century Gothic' });
		Cufon.replace('#content h3', { fontFamily: 'Century Gothic' });
		Cufon.replace('#content h4:not(#content .post .meta h4.date, #content .module.events h4.date)', { fontFamily: 'Century Gothic' });
		Cufon.replace('#content .module.index ul.pagination, #content ul.ajaxSearch_paging', { fontFamily: 'Century Gothic' });
		Cufon.replace('ul.slideshow-slide-controls', { fontFamily: 'Century Gothic' });
		Cufon.replace('#content .book-buy a', { fontFamily: 'Century Gothic' });
		Cufon.replace('#summary h4', { fontFamily: 'Century Gothic' });
		
		////////////////////////////////////////////////////////////////////
		//	/Cufon
		////////////////////////////////////////////////////////////////////
		
		////////////////////////////////////////////////////////////////////
		//	Forms
		////////////////////////////////////////////////////////////////////
		
			// Clear default values on focus
			$('input.text, textarea').formclear();
			
			// Form submit buttons
			$('form a.button').bind('click.default', function(){
				// Clear any remaining submitted flags
				$('.submitted').removeClass('submitted');
				// Add submitted flag
				$(this).parents('.form:first').addClass('submitted');
				// Submit the parent form
				$(this).parents('form:first').submit();
				// Show wait cursor
				$('*').css({'cursor' : 'wait'});
				// Don't visit link specified
				return false;
			});
			
			// Quick Navigation and Quick Filter menus
			$('.quick-navigation select, .quick-filters select').bind('change.default', function(){
				
				var selected = $(this).children(':selected').val();
				if(selected != -1){
					window.location = selected;
				}
				
			});
			
			// Get selected Quick Filters
			if($('.quick-filters')[0]){
				var show = $.query.get('show');
				var sort = $.query.get('sort');
				
				if(show != 'null'){					
					// Select from list
					$('#show option[name='+show+']').attr({selected : 'selected'});
				}
				
				if(sort != 'null'){					
					// Select from list
					$('#sort option[name='+sort+']').attr({selected : 'selected'});
				}
			}
		
		////////////////////////////////////////////////////////////////////
		//	/Forms
		////////////////////////////////////////////////////////////////////
		
		// Local scroll
		$.localScroll();
		
		// Setup external links
		externalLinks();
		
	} // End domSetup()
	
	/* 
	pageSetup()
	
	All JavaScripts requiring initialisation on PAGE LOAD should be called
	from this routine (all images and elements should be loaded and ready to
	manipulate by this point)
	-----------------------------------------------------------------------
	*/
	
	function pageSetup() {
	
	// Paint drips animation
	setTimeout(
		function(){
			// Spray
			$('#paint').show();
			// Drip
			$('#mask').animate({'top' : 400}, 12000, 'easeOutQuad', function(){
				$(this).hide();
			});
		},
		3000
	);
	
	////////////////////////////////////////////////////////////////////
	//	Google Maps
	////////////////////////////////////////////////////////////////////
	
	// API key
	// ABQIAAAA8oUCFXVAAGXZuThzjjj5dhQF2S_CnP842lciOB2Ve7HfhyOc1xRiIoyES8HBdB6KslIr1Rdkxsuudg;
		
		if($('#map')[0] && GBrowserIsCompatible()){

			// Declare vars
			var map;
			var marker;
			
			// Initialise geocoder
			var geocoder = new GClientGeocoder();

			// Geocode address
			geocoder.getLatLng($('#geocode').val(), function(point) {
				if(point) {
					
					// Initialise map
					map = new GMap2(document.getElementById("map"));
					
					// Centre map
					map.setCenter(point, 13);
					
					// Set UI
					map.setUIToDefault();
					
					// Add marker
					marker = new GMarker(point);
					map.addOverlay(marker);
					
				}
			});
			
			// Info window stuff

		}
		
		////////////////////////////////////////////////////////////////////
		//	/Google Maps
		////////////////////////////////////////////////////////////////////
	
	} // End pageSetup()
	
	/* 
	domUnload()
	
	Called when page/DOM is unloaded
	-----------------------------------------------------------------------
	*/
	
	function domUnload() {
	
		// Unload Google Map, if one exists
		if($('#map')[0] && GBrowserIsCompatible()){
			GUnload();
		}
	
	} // End domUnload()