(function(){



var api = {



	init: function()

	{

		if ($.browser.msie) {

			try {

				document.execCommand("BackgroundImageCache", false, true);

			} catch(err){};

			if (typeof(DD_belatedPNG) != 'undefined')

			{

				DD_belatedPNG.fix('#header, #top, #siteTop .info, #siteInfo .add a, #siteInfo .zoom, #siteInfo .directions h4, #trail .view a, #town .content, #sites .marker, #roadTrip h4 span, .twitterbutton, .png, .slide-prev, .slide-next');

			}

		}



		if ($('#gallery').length)

		{

			$('#gallery .slider').jCarouselLite({

				circular: true,

				visible: 10,

				scroll: 1,

				btnNext: '#gallery p.next a',

				btnPrev: '#gallery p.prev a'

			});

			$('#gallery .slider a').click(function(){

				$('#gallery .main').find('img').attr('src', $(this).attr('href')).end();

				$('#gallery .main').find('h3').html($(this).attr('title')).end();

				$('#gallery .main .descrip').html($(this).attr('caption')).end();

				var html = "";

				if ($(this).attr('town')) {

					html = html + '<a href="' + $(this).attr('townpath') + '">' + $(this).attr('town') + '</a><br/>';

				}

				if ($(this).attr('dest')) {

					html = html + '<a href="' + $(this).attr('destpath') + '">' + $(this).attr('dest') + '</a><br/>';

				}

				$('#gallery .main .info').html(html);

				return false;

			});

			//$('#gallery .slider a.img:nth(0)').trigger('click');

		}



		if ($('#sites').length)

		{

			$('#sites .map').scrollFollow({ offset: 10 });

		}



		if ($('#gallery2').length)

		{

			$('a.prev').click(function(){slide(-1); return false;});

			$('a.next').click(function(){slide(1); return false;});

		

			function slide(dir) {

				var curSlide = $('#gallery2 div').index($('#gallery2 .show'));

				$('#gallery2 div').removeClass('show');

				var curSlide = curSlide + dir;

				var slides = $('#gallery2 div');

				if(curSlide >= slides.length) {

					curSlide = 0;

				} else if(curSlide < 0) {

					curSlide = slides.length-1;

				}

				$(slides[curSlide]).addClass('show');

			}

		}
		
		// Just on the homepage		
		if($('body.home').length)
		{
			//alert('home');
			
			// Carousel 
			$('#carousel .slides').cycle({
				prev:    		'#carousel .slide-controls .slide-prev',
	        	next:    		'#carousel .slide-controls .slide-next',
	        	delay:			3000,
	        	pause:			1,
	        	autostop:  		0,
	        	autostopCount: 	5,
				timeout:    	5500,
				fx: 			'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			});
		
		}//end homepage
		
		/* 	
			Dynamic width calculation - was just for IE6 but, with use of div.dropdown-wrap, applied to all
			
			Requires a fixed width on the megadropdown, else column floats don't work: 
			calculates total widths of each dropdown column
			that total is then applied to its parent: the mega dropdown itself.
			
			The swap function allows a function to be run between states: 
			we temporarily turn the megadropdown on and off to allow us to read the widths 
			as generated by their content.
			
			Why? Because we don't know how wide the content in columns will be: it's dynamic.
			
			Make sure to add in any other properties (margin, border, padding) that affect things
		*/
		//if ($.browser.msie && $.browser.version <= 6 ){
			var props = { position: "absolute", visibility: "hidden", display: "block" };
			//var itemWidth = 0;
			/* (width of nav item (107) - borders in menu item graphic + double negative left adjustment) - (padding + borders) */
			var defaultWidth = 73;
			
			$('#nav-main ul li div.mega-dropdown').each(
				function(i){
					//adjustDropdown($('#nav-main ul li div.mega-dropdown'));
					
					adjustDropdown($(this),defaultWidth);
				}
			);
				/*
				$.swap($('#nav-main ul li div.mega-dropdown')[0], props, function(){
				    //itemWidth = $('#nav-main ul li div.mega-dropdown').width();
				     
					var megaDropdown = $('#nav-main ul li div.mega-dropdown div.dropdown-wrap');
					//var megaDropdown = $(this).children('div.dropdown-wrap');
				    var columnWidths = 0;
				    var columnMargins = 0;
				    //var maxHeight = 0;
					megaDropdown.children('div.dropdown-column').each(
						function(i){
							columnWidths += $(this).width();
							// css returns as e.g. 30px, we need just 30
							columnMargins += parseInt($(this).css('margin-right'));
							// height issue: calculate the maxheight
							//columnHeight = $(this).height();
							//if(maxHeight < columnHeight){
							//	maxHeight = columnHeight;
							//}
						}
					);
					//alert(columnMargins);
					var megaDropdownWidth = (columnWidths + columnMargins);
					megaDropdown.css('width', megaDropdownWidth);
				});
				*/
			
			
		//};

		function adjustDropdown(whichDrop,defaultWidth){
			
			$.swap(whichDrop[0], props, function(){
			    //itemWidth = $('#nav-main ul li div.mega-dropdown').width();
			     
				//var megaDropdown = whichDrop;
				var megaDropdown = whichDrop.children('div.dropdown-wrap');
				//$('#nav-main ul li div.mega-dropdown div.dropdown-wrap');
			    var columnWidths = 0;
			    var columnMargins = 0;
			    
				megaDropdown.children('div.dropdown-column').each(
					function(i){
						columnWidths += $(this).width();
						// css returns as e.g. 30px, we need just 30
						columnMargins += parseInt($(this).css('margin-right'));
					}
				);
				var megaDropdownWidth = (columnWidths + columnMargins);
				
				if(megaDropdownWidth < defaultWidth){
					//alert(megaDropdownWidth+' '+defaultWidth);
					megaDropdownWidth = defaultWidth;
				};
				megaDropdown.css('width', megaDropdownWidth);
			});
		}
		
		// Mega dropdowns
		// trigger them on rollover, hide them on rollout
		// only target those menu items that have one
		$('#nav-main ul li div.mega-dropdown').parent().hover(
			function () {
				$(this).children('div.mega-dropdown').show();
				$(this).children('a').css('background-position','0 -45px');
			},
			function () {
				$(this).children('div.mega-dropdown').hide();
				$(this).children('a').css('background-position','0 0');
				/*
				// pesky IE6 specific issue again: doubles padding-bottom
				// but only after having been rolled over - what!
				if ($.browser.msie && $.browser.version <= 6 ){
					$(this).children('div.mega-dropdown').css('padding-bottom', '0px');
				};
				*/
			}
		);
		
		// Close Mega dropdowns
		$('#nav-main ul li div.mega-dropdown div.dropdown-close a').click(
			function() {
			  $(this).parent().parent().parent().hide(); 
			  return false;
			}
		);
		
		

	}//end init

	

};//end api





$(document).ready(api.init);

		

})();
