/**
* Tab Switcher JS
* @author mjames
* @
* default options 
*	container: tabContainer
*/

NewMind.registerNameSpace("NewMind.extensions");

NewMind.extensions.tabSwitcher = function () {

	var $panels, $container, $parentContainer, horizontal, $scroll; //global var's
	// var $navigation;

	/**
	* 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().parent();
		$panels = $(container + ' > div.tab');
		//attempt at adding nav height but its not really needed since we dont need
		//to set the height of the parent container around the tabs.
		//$navigation = $($parentContainer.find('.tabNavigation'));

		// 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');

		//removing the scroll buttons for Bath site, dont need them
		//$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)
		.parents('ul:first')
		  .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');

		// 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();
		//alert(sID+' '+tabHeight);
		$container.height(tabHeight);
		$container.parent().height(tabHeight);
		//$container.parent().parent().height(tabHeight);
		//	$container.parent().parent().parent().height(tabHeight);
		//$container.parent().parent().height(tabHeight+20);
		//alert($container.parent().parent().attr("class"));
		//this height setting doesn't include the navigation and isn't really required
		//$parentContainer.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 moveAvailSearch = function () {
		if (($('.ctl_Booking_AvailSearch').length > 0) && ($('#tabAvail').length > 0)) {
			$('.ctl_Booking_AvailSearch').appendTo('#tabAvail');
		}
	};

	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);
					}
				}
				if (el === undefined) {
					$('ul.tabNav a:first').click().focus();
				}
				else {
					$(el).click().focus();
				}

			} else {
				$('ul.tabNav a:first').click().focus();
			}
			resizeContainer($('#tabContainer div.tab:first')[0].id);
		},

		/**
		* 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);
		},

		moveAvailSearch: function (e) {
			moveAvailSearch();
		}
	};
} ();
