/*

 * JAVASCRIPT FOR THE SLIDER / TABS
 */
 
/*
	jQuery Coda-Slider v2.0 - http://www.ndoherty.biz/coda-slider
	Copyright (c) 2009 Niall Doherty
	This plugin available for use in all personal or commercial projects under both MIT and GPL licenses.
*/
$(document).ready(function() {
	// LETS MAKE THE SLIDER NOW
	$('#slider').codaSlider({
		autoSlide: true,
		autoSlideInterval: 4000,
		autoHeight: false,
		autoSlideStopWhenClicked: true
    });
});

$(function(){
	// Remove the coda-slider-no-js class from the body
	$("body").removeClass("coda-slider-no-js");
		
	// Preloader
	$(".coda-slider").children('.panel').hide().end().prepend('<p class="loading">Loading...<br /><img src="' + cmsContextPath + '/images/ajax-loader.gif" alt="loading..." /></p>');
});

var sliderCount = 1;

$.fn.codaSlider = function(settings) {

	settings = $.extend({
		autoHeight: true,
		autoHeightEaseDuration: 1000,
		autoHeightEaseFunction: "easeInOutExpo",
		autoSlide: false,
		autoSlideInterval: 7000,
		autoSlideStopWhenClicked: true,
		crossLinking: true,
		dynamicArrows: true,
		dynamicArrowLeftText: "&#171; left",
		dynamicArrowRightText: "right &#187;",
		dynamicTabs: true,
		dynamicTabsAlign: "center",
		dynamicTabsPosition: "top",
		externalTriggerSelector: "a.xtrig",
		firstPanelToLoad: 1,
		panelTitleSelector: "h2.title",
		slideEaseDuration: 1000,
		slideEaseFunction: "easeInOutExpo"
	}, settings);
	
	return this.each(function(){
		
		// Uncomment the line below to test your preloader
		// alert("Testing preloader");
		
		var slider = $(this);
		
		// If we need arrows
		if (settings.dynamicArrows) {
			slider.parent().addClass("arrows");
			slider.before('<div class="coda-nav-left" id="coda-nav-left-' + sliderCount + '"><a href="#">' + settings.dynamicArrowLeftText + '</a></div>');
			slider.after('<div class="coda-nav-right" id="coda-nav-right-' + sliderCount + '"><a href="#">' + settings.dynamicArrowRightText + '</a></div>');
		};
		
		var panelWidth = slider.find(".panel").width();
		var panelCount = slider.find(".panel").size();
		var panelContainerWidth = panelWidth*panelCount;
		var navClicks = 0; // Used if autoSlideStopWhenClicked = true
		
		// Slider dynamic values
		var dynNewTop = 249;
		var dynNewLeft = 170;
		var dynNewWidth = 700;
		var dynNewButtonWidth = "";
		var noRightMarginForButton = false;
		var useMediumButton =  false;
		var useSmallButton =  false;
		if(panelWidth==466){
			if(panelCount==3){
				dynNewLeft = 5;
				dynNewButtonWidth = ' style="width: 151px;"' ;
				noRightMarginForButton = true;
				useSmallButton =  true;
			}else{
				dynNewLeft = 40;
				dynNewWidth = 700;
			}
			dynNewTop = 126;
			$(".panel-text", slider).css({ width: 400 });
		}else if(panelWidth==706){
			if(panelCount==4){
				dynNewLeft = 5;
				dynNewButtonWidth = ' style="width: 173px;"' ;
				useMediumButton =  true;
				noRightMarginForButton = true;
			}else if(panelCount==3){ 
				dynNewLeft = 60;
			}else{
				dynNewLeft = 140;
			}
			dynNewTop = ($.browser.msie?206:207);
			dynNewWidth = 700;
			$(".panel-text", slider).css({ width: 650 });
		}else{ //Default is 946
			if(panelCount==5){
				dynNewLeft = 5;
				dynNewWidth = 940;
				noRightMarginForButton = true;
			}else if(panelCount==4){ 
				dynNewLeft = 70;
				dynNewWidth = 840;
			}else{
				dynNewLeft = 170;
				dynNewWidth = 700;
			}
			dynNewTop = 249;
			$(".panel-text", slider).css({ width: 900 });
		}
		
		// Surround the collection of panel divs with a container div (wide enough for all panels to be lined up end-to-end)
		$('.panel', slider).wrapAll('<div class="panel-container"></div>');
		// Specify the width of the container div (wide enough for all panels to be lined up end-to-end)
		$(".panel-container", slider).css({ width: panelContainerWidth });
		
		// Specify the current panel.
		// If the loaded URL has a hash (cross-linking), we're going to use that hash to give the slider a specific starting position...
		if (settings.crossLinking && location.hash && parseInt(location.hash.slice(1)) <= panelCount) {
			var currentPanel = parseInt(location.hash.slice(1));
			var offset = - (panelWidth*(currentPanel - 1));
			$('.panel-container', slider).css({ marginLeft: offset });
		// If that's not the case, check to see if we're supposed to load a panel other than Panel 1 initially...
		} else if (settings.firstPanelToLoad != 1 && settings.firstPanelToLoad <= panelCount) { 
			var currentPanel = settings.firstPanelToLoad;
			var offset = - (panelWidth*(currentPanel - 1));
			$('.panel-container', slider).css({ marginLeft: offset });
		// Otherwise, we'll just set the current panel to 1...
		} else { 
			var currentPanel = 1;
		};
			
		// Left arrow click
		$("#coda-nav-left-" + sliderCount + " a").click(function(){
			navClicks++;
			if (currentPanel == 1) {
				offset = - (panelWidth*(panelCount - 1));
				alterPanelHeight(panelCount - 1);
				currentPanel = panelCount;
				if(useSmallButton){
					slider.siblings('.coda-nav').find('a.small_a_current').removeClass('small_a_current').parents('ul').find('li:last a.small_a').addClass('small_a_current');
				}else if(useMediumButton){
					slider.siblings('.coda-nav').find('a.medium_a_current').removeClass('medium_a_current').parents('ul').find('li:last a.medium_a').addClass('medium_a_current');
				}else{
					slider.siblings('.coda-nav').find('a.current').removeClass('current').parents('ul').find('li:last a').addClass('current');
				}
			} else {
				currentPanel -= 1;
				alterPanelHeight(currentPanel - 1);
				offset = - (panelWidth*(currentPanel - 1));
				if(useSmallButton){
					slider.siblings('.coda-nav').find('a.small_a_current').removeClass('small_a_current').parent().prev().find('a.small_a').addClass('small_a_current');
				}else if(useMediumButton){
					slider.siblings('.coda-nav').find('a.medium_a_current').removeClass('medium_a_current').parent().prev().find('a.medium_a').addClass('medium_a_current');
				}else{
					slider.siblings('.coda-nav').find('a.current').removeClass('current').parent().prev().find('a').addClass('current');
				}
			};
			$('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
			if (settings.crossLinking) { location.hash = currentPanel }; // Change the URL hash (cross-linking)
			return false;
		});
			
		// Right arrow click
		$('#coda-nav-right-' + sliderCount + ' a').click(function(){
			navClicks++;
			if (currentPanel == panelCount) {
				offset = 0;
				currentPanel = 1;
				alterPanelHeight(0);
				if(useSmallButton){
					slider.siblings('.coda-nav').find('a.small_a_current').removeClass('small_a_current').parents('ul').find('a.small_a:eq(0)').addClass('small_a_current');
				}else if(useMediumButton){
					slider.siblings('.coda-nav').find('a.medium_a_current').removeClass('medium_a_current').parents('ul').find('a.medium_a:eq(0)').addClass('medium_a_current');
				}else{
					slider.siblings('.coda-nav').find('a.current').removeClass('current').parents('ul').find('a:eq(0)').addClass('current');
				}
			} else {
				offset = - (panelWidth*currentPanel);
				alterPanelHeight(currentPanel);
				currentPanel += 1;
				if(useSmallButton){
					slider.siblings('.coda-nav').find('a.small_a_current').removeClass('small_a_current').parent().next().find('a.small_a').addClass('small_a_current');
				}else if(useMediumButton){
					slider.siblings('.coda-nav').find('a.medium_a_current').removeClass('medium_a_current').parent().next().find('a.medium_a').addClass('medium_a_current');
				}else{
					slider.siblings('.coda-nav').find('a.current').removeClass('current').parent().next().find('a').addClass('current');
				}
			};
			$('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
			if (settings.crossLinking) { location.hash = currentPanel }; // Change the URL hash (cross-linking)
			return false;
		});
		
		// If we need a dynamic menu
		if (settings.dynamicTabs) {
			var dynamicTabs = '<div class="coda-nav" id="coda-nav-' + sliderCount + '"><ul></ul></div>';
			switch (settings.dynamicTabsPosition) {
				case "bottom":
					slider.parent().append(dynamicTabs);
					break;
				default:
					slider.parent().prepend(dynamicTabs);
					break;
			};
			ul = $('#coda-nav-' + sliderCount + ' ul');
			
			// Create the nav items
			$('.panel', slider).each(function(n) {
				if(useSmallButton){
					ul.append('<li class="tab' + (n+1) + '"><a class="small_a" ' + dynNewButtonWidth + ' href="#' + (n+1) + '">' + $(this).find(settings.panelTitleSelector).text() + '</a></li>');												
				}else if(useMediumButton){
					ul.append('<li class="tab' + (n+1) + '"><a class="medium_a" ' + dynNewButtonWidth + ' href="#' + (n+1) + '">' + $(this).find(settings.panelTitleSelector).text() + '</a></li>');												
				}else{
					ul.append('<li class="tab' + (n+1) + '"><a ' + dynNewButtonWidth + ' href="#' + (n+1) + '">' + $(this).find(settings.panelTitleSelector).text() + '</a></li>');												
				}
			});
			navContainerWidth = slider.width() + slider.siblings('.coda-nav-left').width() + slider.siblings('.coda-nav-right').width();
			ul.parent().css({ width: navContainerWidth });
			switch (settings.dynamicTabsAlign) {
				case "center":
					ul.css({ width: ($("li", ul).width() + 2) * panelCount });
					break;
				case "right":
					ul.css({ float: 'right' });
					break;
			};
		};
			
		// If we need a tabbed nav
		$('#coda-nav-' + sliderCount + ' a').each(function(z) {
			// What happens when a nav link is clicked
			$(this).bind("click", function() {
				navClicks++;
				if(useSmallButton){
					$(this).addClass('small_a_current').parents('ul').find('a.small_a').not($(this)).removeClass('small_a_current');
				}else if(useMediumButton){
					$(this).addClass('medium_a_current').parents('ul').find('a.medium_a').not($(this)).removeClass('medium_a_current');
				}else{
					$(this).addClass('current').parents('ul').find('a').not($(this)).removeClass('current');
				}
				offset = - (panelWidth*z);
				alterPanelHeight(z);
				currentPanel = z + 1;
				$('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
				if (!settings.crossLinking) { return false }; // Don't change the URL hash unless cross-linking is specified
			});
		});
		
		// External triggers (anywhere on the page)
		$(settings.externalTriggerSelector).each(function() {
			// Make sure this only affects the targeted slider
			if (sliderCount == parseInt($(this).attr("rel").slice(12))) {
				$(this).bind("click", function() {
					navClicks++;
					targetPanel = parseInt($(this).attr("href").slice(1));
					offset = - (panelWidth*(targetPanel - 1));
					alterPanelHeight(targetPanel - 1);
					currentPanel = targetPanel;
					// Switch the current tab:
					if(useSmallButton){
						slider.siblings('.coda-nav').find('a.small_a').removeClass('small_a_current').parents('ul').find('li:eq(' + (targetPanel - 1) + ') a.small_a').addClass('small_a_current');
					}else if(useMediumButton){
						slider.siblings('.coda-nav').find('a.medium_a').removeClass('medium_a_current').parents('ul').find('li:eq(' + (targetPanel - 1) + ') a.medium_a').addClass('medium_a_current');
					}else{
						slider.siblings('.coda-nav').find('a').removeClass('current').parents('ul').find('li:eq(' + (targetPanel - 1) + ') a').addClass('current');
					}
					// Slide
					$('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
					if (!settings.crossLinking) { return false }; // Don't change the URL hash unless cross-linking is specified
				});
			};
		});
			
		// Specify which tab is initially set to "current". Depends on if the loaded URL had a hash or not (cross-linking).
		if (settings.crossLinking && location.hash && parseInt(location.hash.slice(1)) <= panelCount) {
			if(useSmallButton){
				$("#coda-nav-" + sliderCount + " a:eq(" + (location.hash.slice(1) - 1) + ")").addClass("small_a_current");
			}else if(useMediumButton){
				$("#coda-nav-" + sliderCount + " a:eq(" + (location.hash.slice(1) - 1) + ")").addClass("medium_a_current");
			}else{
				$("#coda-nav-" + sliderCount + " a:eq(" + (location.hash.slice(1) - 1) + ")").addClass("current");
			}
		// If there's no cross-linking, check to see if we're supposed to load a panel other than Panel 1 initially...
		} else if (settings.firstPanelToLoad != 1 && settings.firstPanelToLoad <= panelCount) {
			if(useSmallButton){
				$("#coda-nav-" + sliderCount + " a:eq(" + (settings.firstPanelToLoad - 1) + ")").addClass("small_a_current");
			}else if(useMediumButton){
				$("#coda-nav-" + sliderCount + " a:eq(" + (settings.firstPanelToLoad - 1) + ")").addClass("medium_a_current");
			}else{
				$("#coda-nav-" + sliderCount + " a:eq(" + (settings.firstPanelToLoad - 1) + ")").addClass("current");
			}
		// Otherwise we must be loading Panel 1, so make the first tab the current one.
		} else {
			if(useSmallButton){
				$("#coda-nav-" + sliderCount + " a:eq(0)").addClass("small_a_current");
			}else if(useMediumButton){
				$("#coda-nav-" + sliderCount + " a:eq(0)").addClass("medium_a_current");
			}else{
				$("#coda-nav-" + sliderCount + " a:eq(0)").addClass("current");
			}
		};
		
		// Set the height of the first panel
		if (settings.autoHeight) {
			panelHeight = $('.panel:eq(' + (currentPanel - 1) + ')', slider).height();
			slider.css({ height: panelHeight });
		};
		
		// Trigger autoSlide
		if (settings.autoSlide) {
			slider.ready(function() {
				setTimeout(autoSlide,settings.autoSlideInterval);
			});
		};
		
		function alterPanelHeight(x) {
			if (settings.autoHeight) {
				panelHeight = $('.panel:eq(' + x + ')', slider).height()
				slider.animate({ height: panelHeight }, settings.autoHeightEaseDuration, settings.autoHeightEaseFunction);
			};
		};
		
		function autoSlide() {
			if (navClicks == 0 || !settings.autoSlideStopWhenClicked) {
				if (currentPanel == panelCount) {
					var offset = 0;
					currentPanel = 1;
				} else {
					var offset = - (panelWidth*currentPanel);
					currentPanel += 1;
				};
				alterPanelHeight(currentPanel - 1);
				// Switch the current tab:
				if(useSmallButton){
					slider.siblings('.coda-nav').find('a.small_a').removeClass('small_a_current').parents('ul').find('li:eq(' + (currentPanel - 1) + ') a.small_a').addClass('small_a_current');
				}else if(useMediumButton){
					slider.siblings('.coda-nav').find('a.medium_a').removeClass('medium_a_current').parents('ul').find('li:eq(' + (currentPanel - 1) + ') a.medium_a').addClass('medium_a_current');
				}else{
					slider.siblings('.coda-nav').find('a').removeClass('current').parents('ul').find('li:eq(' + (currentPanel - 1) + ') a').addClass('current');
				}
				// Slide:
				$('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
				setTimeout(autoSlide,settings.autoSlideInterval);
				
				// Position ul relative to the panel
				ul = $('#coda-nav-1 ul');
				ul.css({ top: ($('#slider').position().top + dynNewTop) });
				ul.css({ left: ($('#slider').position().left + dynNewLeft) });
				ul.css({ width: ($('#slider').position().left + dynNewWidth) });
				
				if(noRightMarginForButton){
					$(".coda-nav ul li a").css({ marginRight: 0 });
				}
			};
		};
		
		// Kill the preloader
		$('.panel', slider).show().end().find("p.loading").remove();
		slider.removeClass("preload");
		
		sliderCount++;
		
	});
};

/*
 * Rotate
 */
$.extend($.ui.tabs.prototype, {
	rotation: null,
	rotate: function(ms, continuing) {

		var self = this, o = this.options;
		
		var rotate = self._rotate || (self._rotate = function(e) {
			clearTimeout(self.rotation);
			self.rotation = setTimeout(function() {
				var t = o.selected;
				self.select( ++t < self.anchors.length ? t : 0 );
			}, ms);
			
			if (e) {
				e.stopPropagation();
			}
		});
		
		var stop = self._unrotate || (self._unrotate = !continuing ?
			function(e) {
				if (e.clientX) { // in case of a true click
					self.rotate(null);
				}
			} :
			function(e) {
				t = o.selected;
				rotate();
			});

		// start rotation
		if (ms) {
			this.element.bind('tabsshow', rotate);
			this.anchors.bind(o.event + '.tabs', stop);
			rotate();
		}
		// stop rotation
		else {
			clearTimeout(self.rotation);
			this.element.unbind('tabsshow', rotate);
			this.anchors.unbind(o.event + '.tabs', stop);
			delete this._rotate;
			delete this._unrotate;
		}
	}
});
