/*
 * Brittain marketing website general scripts
 *
 */
 
var numScrollItems = 0;
var currentScrollItem = 1;
var slideScrollType = 1;

(function($){
	
	$(function() {
		initialise();
	});
	
	function initialise() {
		// INITIALISE
		createToggleButton();
		highlightCurrentSection();
		createSlideScrollComponent();
	}
	
	function highlightCurrentSection() {
		// Sets the current top level menu item to its highlighted colour
		$('#nav .active a:first').css('color','#74abda');
	}
	
	function createToggleButton() {
		/*
		* 	Attaches a click event to the toggle button in the footer
		*	that opens and closes the hiden panel
		*/
		
		$('#toggleButton').bind('click',function(event) {
			$('#footerPanel').slideToggle('slow', function() {
				// Animation complete.
				
				// Modify the open/close image in the button to reflect the open state of the panel
				if ($('#footerPanel').is(':visible')) {
					$('#toggleButton').css({backgroundPosition: 'center -16px'});
				} else {
					$('#toggleButton').css({backgroundPosition: 'center 0px'});	
				}
				
			});

		});
	}
	
	function updatePagination() {
		/*
		* 	Changes the currently highlighted page number
		*/
		
		var elem = currentScrollItem;
		
		// adjust the current item number for 2 paragraph elements
		if (slideScrollType == 2 && elem > 1) {
			elem = (elem + 1) / 2;
		}
		
		// Clear current highlighted element
		$('#slideScrollPagination .slideScrollPaginationDigit').removeClass('current');
		
		// Set the current element
		$('#slideScrollPagination .slideScrollPaginationDigit:nth-child('+elem+')').addClass('current');
		
	}
	
	function createSlideScrollComponent() {
		/*
		* 	Initialises the SlideScroll component.
		*	NB. There are two version wrapped in here. 'client endorsements' uses 2 paragraphs for each item, the regular version uses only 1.
		*/
		
		var elem = 0;

		if ($('#slideScrollContentWrapper').is(":visible")) {
			
			// Set the component type
			var strHeading = $('#slideScrollHeading').text();
			if (strHeading == 'what our clients say') {
				slideScrollType = 2;
			}
			
			// Add the quote marks and the Next/Prev buttons
			if (slideScrollType == 2) {
				$('#slideScrollContentWrapper').addClass('closeQuote');
				$('#slideScrollContentWrapper').append('<div id="slideScrollQuoteTop"></div>');
			}
			
			$('#slideScrollContentWrapper').append('<div id="slideScrollLeft"><a href="#slideLeft" title="previous"></a></div>');
			$('#slideScrollContentWrapper').append('<div id="slideScrollRight"><a href="#slideRight" title="next"></a></div>');
			
			// Calculate the number of items.
			// NB> For the 'our reputation page' there are 2 items for each quote
			if (slideScrollType == 2) {
				numScrollItems = $('#slideScrollContent p').length;
			} else {
				numScrollItems = $('#slideScrollContent table').length;
			}
			
			// Show the first item
			if (slideScrollType == 2) {
				$('#slideScrollContent p:nth-child(1)').show();
				$('#slideScrollContent p:nth-child(2)').show();
			} else {
				$('#slideScrollContent table:nth-child(1)').show();
			}
			
			// Add the pagination
			$('#slideScrollBody').append('<div id="slideScrollPagination"></div>');
			elem = numScrollItems;
			if (slideScrollType == 2) {
				elem = elem / 2;
			}
			for(i=1; i<=elem; i=i+1) {
				$('#slideScrollPagination').append('<span class="slideScrollPaginationDigit">' + i + '</span>');
			}
			updatePagination();
			
			// Create the button handlers
			if (slideScrollType == 2) {
				$('#slideScrollRight a').bind('click',function(event) {
					$('#slideScrollContent p:nth-child('+currentScrollItem+')').fadeOut();
					$('#slideScrollContent p:nth-child('+(currentScrollItem+1)+')').fadeOut('normal', function() {
						// Animation complete.
						currentScrollItem = currentScrollItem + 2;
						if (currentScrollItem > numScrollItems) {
							currentScrollItem = 1;	
						}
						$('#slideScrollContent p:nth-child('+currentScrollItem+')').fadeIn();
						$('#slideScrollContent p:nth-child('+(currentScrollItem+1)+')').fadeIn();
						updatePagination();
					});
				});
				
				$('#slideScrollLeft a').bind('click',function(event) {
					$('#slideScrollContent p:nth-child('+currentScrollItem+')').fadeOut();
					$('#slideScrollContent p:nth-child('+(currentScrollItem+1)+')').fadeOut('normal', function() {
						// Animation complete.
						currentScrollItem = currentScrollItem - 2;
						if (currentScrollItem < 1) {
							currentScrollItem = numScrollItems - 1;	
						}
						$('#slideScrollContent p:nth-child('+currentScrollItem+')').fadeIn();
						$('#slideScrollContent p:nth-child('+(currentScrollItem+1)+')').fadeIn();
						updatePagination();
					});
				});
			} else {
				$('#slideScrollRight a').bind('click',function(event) {
					$('#slideScrollContent table:nth-child('+currentScrollItem+')').fadeOut('normal', function() {
						// Animation complete.
						currentScrollItem = currentScrollItem + 1;
						if (currentScrollItem > numScrollItems) {
							currentScrollItem = 1;	
						}
						$('#slideScrollContent table:nth-child('+currentScrollItem+')').fadeIn();
						updatePagination();
					});
				});
				
				$('#slideScrollLeft a').bind('click',function(event) {
					$('#slideScrollContent table:nth-child('+currentScrollItem+')').fadeOut('normal', function() {
						// Animation complete.
						currentScrollItem = currentScrollItem - 1;
						if (currentScrollItem < 1) {
							currentScrollItem = numScrollItems;	
						}
						$('#slideScrollContent table:nth-child('+currentScrollItem+')').fadeIn();
						updatePagination();
					});
				});
			}
		}
	}
	
	
})(jQuery);
