// ETWP Javascript Minimiser 1.5.0.0
// [Cached Minimiser Output]

// Included: D:\Websites\ETWP Sites\CORNWALL\visitcornwall.com\javascript\tabSwitcher.js

/**
 * Tab Switcher JS
 * @author mjames
 * @
 * default options 
 *	container: tabContainer
 */

//var NewMind = window.NewMind || {} ; //namespace
NewMind.registerNameSpace("NewMind.extensions");

NewMind.extensions.tabSwitcher = function() {

	var $panels, $container, $parentContainer, horizontal, $scroll; //global var's

	/**
	* Setup Tab Switcher
	* @param {Object} container for tabs switcher to load in
	* @param {Object} scroll horizontal or vetical
	*/
	var setup = function(container, scrollHorizontal) {

		$container = $(container);
		$parentContainer = $container.parent().parent();
		$panels = $(container + ' > div.tab');

		// if false, we'll float all the panels left and fix the width 
		// of the container
		horizontal = scrollHorizontal;

		// float the panels left if we're going horizontal
		if (horizontal) {
			$panels.css({
				'float': 'left',
				'position': 'relative' // IE fix to ensure overflow is hidden 
			});

			// calculate a new width for the container (so it holds all panels)
			// add 1px per panel for browser compatibility (FF2)
			$container.css('width', $panels[0].offsetWidth * $panels.length + $panels.length);
			//$container.css('width', $panels[0].offsetWidth * $panels.length);
		}

		$scroll = $parentContainer.find('.productTabs').css('overflow', 'hidden');

		$scroll.before('<span class="scrollButtons left">&lt;<span class="buttonImg">&nbsp;</span></span>') // changed from img's to spans due to ie pngs being naff and then the png fix breaks them further
				.after('<span class="scrollButtons right">&gt;<span class="buttonImg">&nbsp;</span></span>');
	};

	/**
	* Binds Tab Click Events
	*/

	var bindNav = function() {
		// bind the navigation clicks to update the selected nav:
		$parentContainer.find('.tabNavigation a').click(NewMind.extensions.tabSwitcher.selectNav);


		// add a show map event to the map link tab
		var jqMapContainer = $('div.dynMapContainer');
		if (jqMapContainer.length > 0) {
			var strMapId = jqMapContainer[0].id;
			$parentContainer.find('.tabMap a').one('click', function() {
				toggleSearchResultsMap(strMapId);
			});

		}
	};

	/**
	* Removes selected class from tabs and assigns to new tab and tabcontainer - finally fires scroll
	*/
	var selectNav = function() {
		$(this)
		.parent().parent()
		  .find('li, a')
			.removeClass('selected')
		  .end()
		.end()
		.addClass('selected')
		.parent()
		.addClass('selected');

		scroll();
	};

	var trigger = function(data) {
		// within the .navigation element, find the A element
		// whose href ends with ID ($= is ends with)
		var el = $parentContainer.find('.tabNavigation a[href$="' + data.id + '"]').get(0);

		$('div.tab').removeClass('selected');
		$(data.id).addClass('selected');

		if (data.id === 'tabAvailability') {
			$('#bookAvailSearch').removeClass('hidden').addClass('selected');
		} else {
			$('#bookAvailSearch').removeClass('selected').addClass('hidden');
		}

		// we're passing the actual element, and not the jQuery instance.
		selectNav.call(el);
		resizeContainer(data.id);
	};

	/**
	* Does the actual scrolling
	*/
	var scroll = function() {
		// offset is used to move to *exactly* the right place
		var offset = parseInt((horizontal ? $container.css('paddingTop') : $container.css('paddingLeft')) || 0, 10) * -1;

		var scrollOptions = {
			target: $scroll, // the element that has the overflow
			items: $panels, 		  // can be a selector which will be relative to the target
			navigation: '.tabNav a', // selectors are NOT relative to document, i.e. make sure they're unique
			prev: 'span.left',
			next: 'span.right',
			axis: 'x', // allow the scroll effect to run both directions
			onAfter: trigger, // our final callback  
			offset: offset,
			duration: 500, // duration of the sliding effect
			easing: 'swing'
		};


		$parentContainer.serialScroll(scrollOptions);

		// now apply localScroll to hook any other arbitrary links to trigger 
		// the effect
		$.localScroll(scrollOptions);

		// finally, if the URL has a hash, move the slider in to position, 
		/* this is crashing browsers - need to bug fix 
		scrollOptions.duration = 1; // pageload so dont animate
		$.localScroll.hash(scrollOptions);
		*/

	};

	/**
	* Adjust the tab container height to the height of the tab
	* @param {Object} sID - Tab ID to get height from
	*/
	var resizeContainer = function(sID) {
		var tabHeight = $('#' + sID).outerHeight();
		$container.height(tabHeight);
		$container.parent().height(tabHeight);
	};

	/**
	*When provided the tabcontainer, spins through the tab's and removes any empty ones
	*@param {string} sContainer - Tab Container
	*/
	var trimTabs = function(sContainer) {

		$(sContainer + ' > div.tab').each(function() {
			var tab = $(this);
			if (tab.children().size() === 0 || (tab.children().size() === 1 && String(tab.children()[0].tagName).toLowerCase() === "script")) {
				$('.tabNavigation a[href$="' + tab[0].id + '"]').parent().remove();
				tab.remove();
			}
		});
	};

	var bookAvailSetup = (function() {
		//$("#tabAvailability").append($("#bookAvailSearch"))
		$('#bookAvailSearch').ajaxStart(function() {
			var container = $("#availabilityCalendarTable");
			if (container.length > 0) {
				var height = container.height();
				var width = container.width();

				$('<div id="availLoad" style="display: none;" ><h3>Updating...</h3></div>')
			        .appendTo(container)
			        .height(height)
			        .width(width)
			        .fadeIn('fast');
			}
		}).addClass('hidden');

		//moving this into tabs is causing too many bugs so it will sit below		
		//$("#tabAvailability").append($("#bookAvailSearch")).append($("#formProvider"));


		if ($("#formProvider form").length > 0) {
			$("#formProvider form").unbind("submit");
			$("#formProvider form").submit(function() {
				if (NewMind.ETWP.Forms.CheckForm(this)) {
					$(this).find('div.submit span.field').append('<img src="/images/ajax-loader-small.gif" alt="loading..." class="ajaxloader"/>');
					NewMind.Site.AjaxForms.postback(this);
				}
				return false;
			});
		}
		else if (($("#bookAvailSearch").length > 0) && ($("#bookAvailSearch").html().length === 0)) {
			$("#bookAvailSearch").remove();
		}
	});

	return {
		/**
		* Setup the tab switcher
		* @param {Object} setup params (containerID, Horizontal)
		*/
		init: function(params) {

			//load values and set to default if needed

			var sContainer = params.container ? params.container : "#tabContainer";
			var bHorizontal = params.scrollHorizontal ? params.scrollHorizontal : true;


			setup(sContainer, bHorizontal); //set up the coda slider
			bindNav(); // bind nav links

			if (window.location.hash) { //see if we should be open on a specific tab if not load first
				var id = window.location.hash.substr(1);
				var el = $parentContainer.find('.tabNavigation a[href$="' + id + '"]').get(0);

				if (el === undefined) { //our hash id may actually be an element not a tab, in this case we need to find the correct tab and update data
					var actualTab = $parentContainer.find('#' + id).parents('div.tab');
					if (actualTab.length > 0) {
						id = actualTab.get(0).id;
						el = $parentContainer.find('.tabNavigation a[href$="' + id + '"]').get(0);
					}
					$('ul.tabNav a:first').click().focus();
				}
				else {
					$(el).click().focus();
				}

			} else {
				$('ul.tabNav a:first').click().focus();
			}
			resizeContainer($('#tabContainer div.tab:first')[0].id);

			bookAvailSetup();
		},

		/**
		* Exposed SelectNav
		* @param {Object} d
		*/
		selectNav: function(d) {
			selectNav(d);
		},

		/**
		* Exposed Container Resize for binding to other events
		* @param {Object} e
		*/
		resizeContainer: function(e) {
			resizeContainer($(this).parents('div.tab :has(.selected)')[0].id);
		},


		/**
		* Removes any empty tabs from the tab collection
		* @param {Object} sContainer - tabContainer to trim
		*/
		trimTabs: function(sContainer) {
			trimTabs(sContainer);
		},

		bookAvailSetup: function() {
			bookAvailSetup();
		}
	};
} ();

