/*
 -----------------------------------------
   lightboxes.js
 -----------------------------------------

 * @version 0.3
 * @author LBi - http://www.lbi.com/en
 * @requires jQuery Core 1.3.2 - http://www.jquery.com/
*/

/*jslint eqeqeq: true, undef: true */
/*global $, jQuery, BTT, window, document, swfobject, confirm, prompt  */


/**
 * Lightboxes is an object that enables the display of content within a lightbox
 * lightboxes take on two forms
 * 1) Content is hidden on the page and is revealed when a trigger is activated
 * 2) Content is pulled into the page from an HTML snippet and revealed within a lightbox
 * @namespace Lightboxes stuff
 * @member BTT
 */
BTT.LightboxesCarousel = {

	classNames : {
		openLightboxClass : 'JS_openLightboxCarousel',
		closeLightboxClass : 'JS_closeLightboxCarousel',
		buttonClass : 'button',
		buttonSmallClass : 'buttonSmall',
		closeButtonClass : 'buttonClose',
		lightboxWrapClass : 'layoutArea layoutAreaLightbox',
		lightboxActiveClass : 'layoutAreaLightboxActive',
		contentWrapClass : 'contentArea',
		carouselImageContainer : 'carouselImageContainer',
		imagePanel : 'imagePanel',
		contentLoaderClass : 'contentAreaLightboxLoading',
		contentPreloadClass : 'contentAreaLightboxPreload',
		contentHighlightClass : 'contentAreaHighlight contentAreaPanel',
		contentErrorClass : 'contentAreaLightboxError',
		hiddenElementClass : 'hidden',
		accessibleInputClass : 'JS_refreshLightboxHelper',
		navigation: 'lightBoxNavigation',
		navigationPrevious: 'lightBoxNavigationPrevious',
		navigationNext: 'lightBoxNavigationNext'
	},
	lightboxParams : {
		nameSpace : 'lightbox',
		defaultTrigger : 'body',
		defaultContainer : 'body',
		minWidth : 500,
		minHeight : 100,
		height : 800,
		autoHeight : true,
		minSpacing : 20,
		scrollPosition : 0,
		scrollSpeed : 1000
	},
	ajaxParams : {
		method : 'GET',
		cache : false
	},
	/**
	* createLightboxesArrays is a function that creates an array of each different type of lightbox trigger on a page
	* each trigger has a class starting with value stored in function's parametr
	* return value of the function is an array of lightbox triggers, URLs and IDs
	* If there is no lightbox trigger on the page, the function returns false
	*/
	createLightboxesArray : function(className){
		// get all the triggers on the page
		var lightboxTriggers = $('*[class*="'+ className +'"]'),
			lightboxesArray = [],
			tempInput = '',
			tempArray = [],
			returnValue = false,
			regExpClass = BTT.LightboxesCarousel.classNames.openLightboxClass+'\\(([^\\)]*)\\)',
			i = 0;
		regExpClass = new RegExp(regExpClass);
		// loop through all the triggers in the page and assign them into array based upon what is within the class attribute
		for(i = 0; i < lightboxTriggers.length; i=i+1){
			tempInput = lightboxTriggers[i].className;
			// tempURL & tempID is the bit of the classname between the parentheses
			tempInput = tempInput.match(regExpClass);
			if ((tempInput instanceof Array) && (tempInput[1] > '')) {
				var tempUrl = '',
					tempId = '',
					tempWidth = null,
					tempAction = null,
					j = 0;
				tempArray = tempInput[1].split('#');
				if ((tempArray.length > 1) && (tempArray[0] > '')) {
					tempUrl = tempArray[0];
					tempId = tempArray[1];
				} else {
					//this testing is necessary because hash sign is not required for introducing the id name
					if (tempArray[0].split('/').length > 1) {
						tempUrl = tempArray[0];
					} else {
						tempId = tempArray[0];
					}
				}
				tempArray = tempInput[1].split(',');
				if (tempArray.length > 1) {
					for (j = 1; j < tempArray.length; j=j+1) {
						if (parseInt(tempArray[j], 10) === (tempArray[j] - 0)) {
							tempWidth = parseInt(tempArray[j], 10);
						} else if (tempArray[j] === 'ajaxSubmit') {
							tempAction = tempArray[j];
						}
					}
				}
				
				//push parsed string into the array
				lightboxesArray.push({
					lightboxTrigger: lightboxTriggers[i],
					lightboxUrl: tempUrl,
					lightboxId: tempId,
					lightboxWidth: tempWidth,
					lightboxAction: tempAction
				});
			}
		}
		if (lightboxesArray.length > 0) {
			returnValue = lightboxesArray;
		}
		//console.log("ReturnValue: Create LightboxArray", returnValue);
		return returnValue;
	},
	
	
	/**
    * createLightbox is a function that creates lightbox with specified content
    * @returns The lightbox that was created
    */
	createLightbox : function(content,options){


		var lightbox = null,
			returnValue = false,
			currentClass = '',
			isOnPage = content.parents(BTT.LightboxesCarousel.lightboxParams.defaultContainer).length > 0;
		if (typeof options === undefined) {
			options = {};
		}
		if ((typeof options.trigger === undefined) || !(options.trigger instanceof $)) {
			options.trigger = $(BTT.LightboxesCarousel.lightboxParams.defaultTrigger);
		}
		if (content instanceof $) {
			lightbox = content;
			currentClass = lightbox.attr('class');
			if ((currentClass === undefined) || (currentClass.indexOf(BTT.LightboxesCarousel.classNames.lightboxWrapClass) === -1)) {
				lightbox.wrap($('<div/>')
					.addClass(BTT.LightboxesCarousel.classNames.lightboxWrapClass)
					.append(
						$('<div/>')
							.addClass(BTT.LightboxesCarousel.classNames.contentWrapClass)
							.addClass(BTT.LightboxesCarousel.classNames.contentHighlightClass)
							.attr('aria-live', 'assertive')
					)		
				);
				lightbox = lightbox.parent().parent();
			}
			if (!isOnPage) {
				$(BTT.LightboxesCarousel.lightboxParams.defaultContainer).append(lightbox);
			} else {
				lightbox.appendTo(BTT.LightboxesCarousel.lightboxParams.defaultContainer);
				if (!lightbox.hasClass(BTT.LightboxesCarousel.classNames.lightboxActiveClass)) {
					lightbox.addClass(BTT.LightboxesCarousel.classNames.hiddenElementClass);
				}
			}
			if ((typeof options.className !== undefined) && (options.className !== '')) {
				lightbox.addClass(options.className);
			}
			lightbox.prepend(
				$('<a />')
					.addClass(BTT.LightboxesCarousel.classNames.hiddenElementClass)
					.attr('name', 'accessibleTarget')
					.html(BTT.Messages.Lightboxes.accessibleDynamicContent)
			);
			
			//BTT.LightboxesCarousel.enableClose(lightbox,options.trigger,!isOnPage);
			
			returnValue = lightbox;
		}
		return returnValue;
	},
	
	
	
	/**
    * showHideLightbox is a function that shows or hides lightbox content within a page
    */
	showHideLightbox : function(lightbox,lightboxTrigger,lightboxWidth){
		var returnValue = false;
		if ((lightbox instanceof $) && (lightboxTrigger instanceof $)) {
			if (lightbox.hasClass(BTT.LightboxesCarousel.classNames.lightboxActiveClass)) {
				lightbox
					.removeClass(BTT.LightboxesCarousel.classNames.lightboxActiveClass)
					.addClass(BTT.LightboxesCarousel.classNames.hiddenElementClass);
				lightboxTrigger.focus();
			} else {
				lightboxTrigger.blur();
				lightbox
					.removeClass(BTT.LightboxesCarousel.classNames.hiddenElementClass)
					.addClass(BTT.LightboxesCarousel.classNames.lightboxActiveClass);
				$('.' + BTT.LightboxesCarousel.classNames.lightboxActiveClass + ' > a').focus();
				BTT.LightboxesCarousel.positionLightbox(lightbox,lightboxWidth);
				$(window).resize(function() {
					BTT.LightboxesCarousel.positionLightbox(lightbox,lightboxWidth);
				});
			}
			returnValue = true;
		}
		return returnValue;
	},
	
	
	/**
	* enableClose is a function that adds a close button to a lightbox and allows the lightbox to be closed when the 'esc' key is pressed
	*/
	enableClose : function(lightbox,lightboxTrigger,lightboxDestroy){
		var currentLightbox = null,
			closeButton = $('<div/>');
		if (lightbox === '') {
			lightbox = $('.'+BTT.LightboxesCarousel.classNames.lightboxActiveClass);
		}
		currentLightbox = lightbox;
		if ($('.'+BTT.LightboxesCarousel.classNames.closeButtonClass,lightbox).length === 0) {
			closeButton
				.addClass(BTT.LightboxesCarousel.classNames.buttonClass)
				.addClass(BTT.LightboxesCarousel.classNames.closeButtonClass)
				.append(
					$('<a/>')
						.addClass(BTT.LightboxesCarousel.classNames.closeLightboxClass)
						.attr('href','#')
						.text(BTT.Messages.Lightboxes.close)
				);
				
			$('.'+BTT.LightboxesCarousel.classNames.contentWrapClass+':first', lightbox).prepend(closeButton);
			$(document).bind('keyup.'+BTT.LightboxesCarousel.lightboxParams.nameSpace, {
					lightbox : lightbox,
					lightboxTrigger : lightboxTrigger,
					lightboxDestroy : lightboxDestroy
				},
				function(closeEvent){
					if (closeEvent.keyCode === 27){
						if (closeEvent.data.lightbox.hasClass(BTT.LightboxesCarousel.classNames.lightboxActiveClass)) {
							BTT.LightboxesCarousel.showHideLightbox(closeEvent.data.lightbox, closeEvent.data.lightboxTrigger);
							if (closeEvent.data.lightboxDestroy) {
								closeEvent.data.lightbox.remove();
							}
						}
					}
				}
			);
		} else {
			currentLightbox = $('.'+BTT.LightboxesCarousel.classNames.closeButtonClass, lightbox).siblings();
		}
		$('.'+BTT.LightboxesCarousel.classNames.closeLightboxClass, currentLightbox).bind('click.'+BTT.LightboxesCarousel.lightboxParams.nameSpace, {
				lightbox : lightbox,
				lightboxTrigger : lightboxTrigger,
				lightboxDestroy : lightboxDestroy
			},
			function(closeEvent){
				var returnValue = true;
				returnValue = !BTT.LightboxesCarousel.showHideLightbox(closeEvent.data.lightbox, closeEvent.data.lightboxTrigger);
				if (!$(this).hasClass('*[class*="'+ BTT.AJAX.Forms.classNames.submitButtonsClass +'"]')) {
					closeEvent.stopImmediatePropagation();
					$(BTT.AJAX.Forms.defaultFormElement).unbind('submit.form-plugin');
				}
				if (closeEvent.data.lightboxDestroy) {
					closeEvent.data.lightbox.remove();
				}
				return returnValue;
			}
		);
	},
	
	
	/**
	* extendFunctionality call functions which should be run on AJAX lightbox ready
	*/
	extendFunctionality : function() {
		
		//intialise form validation
		if (typeof BTT.Validation !== "undefined") {
			BTT.Validation.init();
		}
		// initialise utilities
		if (typeof BTT.Utilities !== "undefined") {
			BTT.Utilities.fileUpload.init('.' + BTT.LightboxesCarousel.classNames.lightboxActiveClass);
			// accessibility improvement
			BTT.Utilities.hideElementsFromTabIndex();
			// intialise videos in lightboxes
			BTT.Utilities.viddlerVideo.init();
			// intialise tabs in lightboxes
			BTT.Utilities.tabs.init();
		}
		// run omniture tracking
		if (typeof BTT.Omniture !== "undefined") {
			BTT.Omniture.init();
		}
		// run media manager intialisation
		if (typeof BTT.MediaManager !== "undefined") {
			BTT.MediaManager.init();
		}
		// keyword suggestion
		if (typeof BTT.Keywords !== "undefined") {
			BTT.Keywords.autoComplete.init();
		}
		// intialise AjaxForms; parameter presents call from lightbox
		if (typeof BTT.AJAX !== "undefined") {
			BTT.AJAX.init(true);
		}
		//intialise rich textareas with Tiny MCE
		if (typeof BTT.RichText !== "undefined") {
			BTT.RichText.init();
		}
		//intialise interactive map
		if (typeof BTT.Maps !== "undefined") {
			BTT.Maps.init();
		}
		
	},
	
	
	/**
	* positionLightbox is a function that positions a lightbox vertically on screen and adjusts when window changes size
	*/
	positionLightbox : function(lightbox,width,changePosition) {
		var currentLightboxWrap = $('.'+BTT.LightboxesCarousel.classNames.lightboxActiveClass),
			wrapMinWidth =  $(window).width(),
			wrapMinHeight = $(BTT.LightboxesCarousel.lightboxParams.defaultContainer).outerHeight(),
			wrapSpacingBottom = 0,
			currentLightbox = $('.'+BTT.LightboxesCarousel.classNames.lightboxActiveClass+' > .'+BTT.LightboxesCarousel.classNames.contentWrapClass+':first'),
			lightboxWidth = BTT.LightboxesCarousel.lightboxParams.minWidth,
			lightboxHeight = BTT.LightboxesCarousel.lightboxParams.height,
			lightboxMinHeight = BTT.LightboxesCarousel.lightboxParams.minHeight,
			lightboxOffset = BTT.LightboxesCarousel.lightboxParams.minSpacing;
		if ((typeof width !== undefined) && (width > 0)) {
			lightboxWidth = width;
		}
		if (BTT.LightboxesCarousel.lightboxParams.autoHeight){
			lightboxHeight = 'auto';
		}
		if ($('.' + BTT.LightboxesCarousel.classNames.contentLoaderClass, lightbox).length > 0) {
			lightboxMinHeight = $('.' + BTT.LightboxesCarousel.classNames.contentLoaderClass, lightbox).css('minHeight');
			if (parseInt(lightboxMinHeight,10) < BTT.LightboxesCarousel.lightboxParams.minHeight) {
				lightboxMinHeight = BTT.LightboxesCarousel.lightboxParams.minHeight;
			}
		} else if ($('div > div.'+BTT.LightboxesCarousel.classNames.contentWrapClass,currentLightbox).outerHeight() > BTT.LightboxesCarousel.lightboxParams.minHeight) {
			lightboxMinHeight = $('div > div.'+BTT.LightboxesCarousel.classNames.contentWrapClass,currentLightbox).outerHeight();
		}
		currentLightbox.css({
			width:		lightboxWidth,
			minWidth:	BTT.LightboxesCarousel.lightboxParams.minWidth,
			minHeight:	lightboxMinHeight,
			height:		lightboxHeight
		});
		if ((changePosition === undefined) || (changePosition === true)) {
			if (($(window).height() - currentLightbox.outerHeight()) > (lightboxOffset * 2)) {
				lightboxOffset = ($(window).height() - currentLightbox.outerHeight()) / 2;
			}
			lightboxOffset = lightboxOffset + $(window).scrollTop();
		} else {
			lightboxOffset = parseInt(currentLightbox.css('top'), 10);
		}
		if (wrapMinHeight < (currentLightbox.outerHeight() + lightboxOffset + BTT.LightboxesCarousel.lightboxParams.minSpacing)) {
			wrapMinHeight = currentLightbox.outerHeight() + lightboxOffset + BTT.LightboxesCarousel.lightboxParams.minSpacing;
			wrapSpacingBottom = BTT.LightboxesCarousel.lightboxParams.minSpacing + 'px';
		}
		currentLightboxWrap.css({
			minWidth:	wrapMinWidth,
			minHeight:	wrapMinHeight,
			paddingBottom: wrapSpacingBottom
		});
		if ((changePosition === undefined) || (changePosition === true)) {
			currentLightbox.css({
				top:		lightboxOffset // * 100 / currentLightboxWrap.outerHeight()+'%'
			});
		}
	},
	
	/**
    * ajaxLoad is a function that loads content from specified source by AJAX
    */
	ajaxLoad : function(contentUrl,contentId,lightbox,trigger,width,action){
		var	returnValue = false,
			lightboxAction = action,
			ajaxContent = null,
			ajaxResponse = {
				content : null,
				error : false
			},
			closeButtons = null;

		if (contentUrl > '') {
			$('.'+BTT.LightboxesCarousel.classNames.closeButtonClass, lightbox).siblings().remove();
			$('.'+BTT.LightboxesCarousel.classNames.contentWrapClass+':first', lightbox)
				.addClass(BTT.LightboxesCarousel.classNames.contentLoaderClass);
			ajaxContent = $.ajax({
				url: contentUrl,
				type: BTT.LightboxesCarousel.ajaxParams.method,
				cache: BTT.LightboxesCarousel.ajaxParams.cache,
				success: function( response ){
					
					
					
					//alert( "LB success: " + enumerator(response)  )
					
					
					if (contentId > '') {
						if (typeof BTT.Omniture !== "undefined") {
							BTT.Omniture.omnitureInLightbox(response);
						}
						ajaxResponse.content = $('#'+contentId, $(response));
					} else {
						ajaxResponse.content = $(response);
					}
					if (ajaxResponse.length === 0) {
						ajaxResponse.error = true;
						ajaxResponse.content = $('<h1/>')
							.text(BTT.Messages.Lightboxes.ajaxWrongId);
					}
					if (BTT.LightboxesCarousel.changeLightboxContent(ajaxResponse.content,lightbox) !== false) {
						if (ajaxResponse.error) {
							lightbox.addClass(BTT.LightboxesCarousel.classNames.contentErrorClass);
						} else {
							// Sending the form behind the lightbox
							if ((typeof lightboxAction !== undefined) && (lightboxAction === 'ajaxSubmit')) {
								var formOptions = {};
								formOptions.data = [];
								formOptions.data[trigger.attr('name')] = trigger.attr('value');
								trigger.parents('form').ajaxSubmit(formOptions);
							}
						}
					}
					$('.'+BTT.LightboxesCarousel.classNames.accessibleInputClass).attr('checked', !$('.'+BTT.LightboxesCarousel.classNames.accessibleInputClass).attr('checked'));
				},
				error: function(err1, err2, err3) {
					
					//alert( "LB error: " + enumerator(err1)  )
					//alert( "LB error: " + enumerator(err2)  )
					//alert( "LB error: " + enumerator(err3)  )
					
					ajaxResponse.error = true;
					ajaxResponse.content = $('<h1/>')
						.text(BTT.Messages.Lightboxes.ajaxWrongPage);
					if (BTT.LightboxesCarousel.changeLightboxContent(ajaxResponse.content,lightbox) !== false) {
						lightbox.addClass(BTT.LightboxesCarousel.classNames.contentErrorClass);
					}
					$('.'+BTT.LightboxesCarousel.classNames.accessibleInputClass).attr('checked', !$('.'+BTT.LightboxesCarousel.classNames.accessibleInputClass).attr('checked'));
				},
				complete: function(xmlHttp) {
					// xmlHttp is a XMLHttpRquest object
					//alert("COMPLETE: " + xmlHttp.status);
				}

			});
			returnValue = lightbox;
		}
		return returnValue;
	},
	
	

	/**
    * changeLightboxContent is a function that changes content in specified Lightbox
    */
	changeLightboxContent : function(content,lightbox,changePosition){
		var returnValue = false,
			preload = null,
			mediaToLoad = 0,
			currentlyLoadedArray = [];
		if (content instanceof $) {
			if ((typeof lightbox === undefined) || !(lightbox instanceof $)) {
				lightbox = $('.'+BTT.LightboxesCarousel.classNames.lightboxActiveClass);
			}
			if (lightbox.length > 0) {
				preload = $('<div />')
					.addClass(BTT.LightboxesCarousel.classNames.contentPreloadClass)
					.css('width',BTT.LightboxesCarousel.lightboxParams.minWidth)
					.append(content)
					.bind('allLoaded.preloader', function(e, data) {
						var newMinHeight = preload.outerHeight()+'';
						if (mediaToLoad > 0) {
							currentlyLoadedArray.push(data.image);
						}
						if(currentlyLoadedArray.length === mediaToLoad) {
							$('.'+BTT.LightboxesCarousel.classNames.contentWrapClass+':first', lightbox)
								.removeClass(BTT.LightboxesCarousel.classNames.contentLoaderClass);
							preload.removeClass(BTT.LightboxesCarousel.classNames.contentPreloadClass);
							BTT.LightboxesCarousel.enableClose(lightbox,$(BTT.LightboxesCarousel.lightboxParams.defaultTrigger),true);
							//BTT.LightboxesCarousel.enableNavigation(lightbox,$(BTT.LightboxesCarousel.lightboxParams.defaultTrigger),true);
							BTT.LightboxesCarousel.extendFunctionality();
							BTT.LightboxesCarousel.positionLightbox(lightbox,BTT.LightboxesCarousel.lightboxParams.minWidth,changePosition);
							$('.'+BTT.LightboxesCarousel.classNames.accessibleInputClass)
								.attr('checked', !$('.'+BTT.LightboxesCarousel.classNames.accessibleInputClass)
									.attr('checked')
								);
						}
					});
				$('.'+BTT.LightboxesCarousel.classNames.contentWrapClass+':first', lightbox)
					.append(preload);
				mediaToLoad = $('img, object',preload).length;
				if (mediaToLoad === 0) {
					preload.trigger('allLoaded.preloader');
				}
				$('img, object',preload).bind('load.preloader error.preloader', function(event) {
					if (event.type === 'load') {
						preload.trigger('allLoaded.preloader',{image : $(this), error : false});
					} else {
						preload.trigger('allLoaded.preloader',{image : $(this), error : true});
					}
				});
				returnValue = lightbox;
			}
		}
		return returnValue;
	},
	createAccessibleInput : function() {
		var accessibleInput = $('<input/>')
			.attr('tabindex','-1')
			.attr('type', 'checkbox')
			.addClass(BTT.LightboxesCarousel.classNames.hiddenElementClass)
			.addClass(BTT.LightboxesCarousel.classNames.accessibleInputClass);
		$(BTT.LightboxesCarousel.lightboxParams.defaultContainer).prepend(accessibleInput);
	},
	
	
	/* enable the user to navigate to previous and next images
	-----------------------------------------------------------*/
	
	enableNavigation : function(lightboxNumber, lightboxesArray, lightbox, lightboxTrigger) {
			var currentLightbox = null,
				nextButton = $('<div />'),
				previousButton = $('<div />');	
			if (lightbox === '') {
				lightbox = $('.'+BTT.LightboxesCarousel.classNames.lightboxActiveClass);
			}
			currentLightbox = lightbox;
			if ($('.'+BTT.LightboxesCarousel.classNames.navigationNext,currentLightbox).length === 0) {
				if (lightboxNumber < (lightboxesArray.length - 2))
				{
					nextButton
						.addClass(BTT.LightboxesCarousel.classNames.buttonClass)
						.addClass(BTT.LightboxesCarousel.classNames.buttonSmallClass)
						.addClass(BTT.LightboxesCarousel.classNames.navigationNext)
						.append(
							$('<a/>')
								.addClass(BTT.LightboxesCarousel.classNames.navigation)						
								.text(BTT.Messages.Lightboxes.navigateNext)							
						);
						$('.'+BTT.LightboxesCarousel.classNames.contentWrapClass+':first', currentLightbox).prepend(nextButton);
				}
			}
			if ($('.'+BTT.LightboxesCarousel.classNames.navigationPrevious,currentLightbox).length === 0) {
				if (lightboxNumber > 1 && lightboxNumber < lightboxesArray.length)
				{
					previousButton
						.addClass(BTT.LightboxesCarousel.classNames.buttonClass)
						.addClass(BTT.LightboxesCarousel.classNames.buttonSmallClass)
						.addClass(BTT.LightboxesCarousel.classNames.navigationPrevious)
						.append(
							$('<a/>')
								.addClass(BTT.LightboxesCarousel.classNames.navigation)
								.text(BTT.Messages.Lightboxes.navigatePrevious)							
						);
					
					$('.'+BTT.LightboxesCarousel.classNames.contentWrapClass+':first', currentLightbox).prepend(previousButton);
				}
				
			} //else {
			//	currentLightbox = $('.'+BTT.LightboxesCarousel.classNames.closeButtonClass, lightbox).siblings();
			//}
			$('.'+BTT.LightboxesCarousel.classNames.navigationNext, currentLightbox).bind('click.'+BTT.LightboxesCarousel.lightboxParams.nameSpace, {
					lightbox : lightbox,
					lightboxTrigger : lightboxTrigger
				},
				function(clickEvent){
					var returnValue = true;
					
					clickEvent.stopImmediatePropagation();
					//clickeEvent.preventDefault();
		
					lightbox = BTT.LightboxesCarousel.ajaxLoad(lightboxesArray[lightboxNumber + 2].lightboxUrl,lightboxesArray[lightboxNumber + 2].lightboxId,lightbox,$(lightboxesArray[lightboxNumber + 2].lightboxTrigger),lightboxesArray[lightboxNumber + 2].lightboxWidth,lightboxesArray[lightboxNumber + 2].lightboxAction);
					lightbox = BTT.LightboxesCarousel.enableNavigation(lightboxNumber+ 2,lightboxesArray,lightbox, $(lightboxesArray[lightboxNumber + 2].lightboxTrigger));
				
					
					return returnValue;
				}
			);
			$('.'+BTT.LightboxesCarousel.classNames.navigationPrevious, currentLightbox).bind('click.'+BTT.LightboxesCarousel.lightboxParams.nameSpace, {
					lightbox : lightbox,
					lightboxTrigger : lightboxTrigger
				},
				function(clickEvent){
					var returnValue = true;
					
					clickEvent.stopImmediatePropagation();
					//clickeEvent.preventDefault();
		
					lightbox = BTT.LightboxesCarousel.ajaxLoad(lightboxesArray[lightboxNumber - 2].lightboxUrl,lightboxesArray[lightboxNumber - 2].lightboxId,lightbox,$(lightboxesArray[lightboxNumber - 2].lightboxTrigger),lightboxesArray[lightboxNumber - 2].lightboxWidth,lightboxesArray[lightboxNumber - 2].lightboxAction);
					lightbox = BTT.LightboxesCarousel.enableNavigation(lightboxNumber- 2,lightboxesArray,lightbox, $(lightboxesArray[lightboxNumber - 2].lightboxTrigger));
				
					
					return returnValue;
				}
			);
			return currentLightbox;
			
			
	},
	
	// set up the lightbox functionality
	init : function(){
		
		var lightboxesArray = [],
			currentLightbox = null,
			currentUrl = '',
			currentId = '',
			options = {
				trigger : null,
				className : ''
			}
		;
		
		// create arrays of all lightboxes and associated triggers on a page
		lightboxesArray = BTT.LightboxesCarousel.createLightboxesArray(BTT.LightboxesCarousel.classNames.openLightboxClass);
		
		
		
		if (lightboxesArray !== false) {
			
			
			//set up an event on each trigger to show it's associated lightbox content
			$(lightboxesArray).each(function(i){
				//console.log("lightBox enum : ", i);
				//console.log("lightBox value : ", lightboxesArray[i]);
				
				currentLightbox = null;
				options.trigger = $(lightboxesArray[i].lightboxTrigger);
				currentUrl = lightboxesArray[i].lightboxUrl;
				currentId = lightboxesArray[i].lightboxId;
				options.width = lightboxesArray[i].lightboxWidth;
				options.action = lightboxesArray[i].lightboxAction;
				
				// Open lightbox from trigger, passing the identifier of the lightbox as a closure
				if ((currentUrl === '') && (currentId > '')) {
					currentLightbox = BTT.LightboxesCarousel.createLightbox($('#'+currentId),options);
					currentLightbox = BTT.LightboxesCarousel.enableNavigation(i,lightboxesArray, currentLightbox);
				}
				options.trigger.bind('click.'+BTT.LightboxesCarousel.lightboxParams.nameSpace, {
						lightbox : currentLightbox,
						lightboxUrl : currentUrl,
						lightboxId : currentId,
						lightboxTrigger : options.trigger,
						lightboxClassName : options.className,
						lightboxWidth : options.width,
						lightboxAction : options.action
					},
					function(triggerEvent){
						var lightbox = triggerEvent.data.lightbox,
							options = {
								trigger : triggerEvent.data.lightboxTrigger,
								className : triggerEvent.data.lightboxClassName,
								width : triggerEvent.data.lightboxWidth,
								action : triggerEvent.data.lightboxAction
							}
						;
						if (lightbox === null) {
							
							lightbox = $('<div/>')
								.addClass(BTT.LightboxesCarousel.classNames.contentAreaLightboxLoading);
							lightbox = BTT.LightboxesCarousel.createLightbox(lightbox,options);
							
							lightbox = BTT.LightboxesCarousel.ajaxLoad(triggerEvent.data.lightboxUrl,triggerEvent.data.lightboxId,lightbox,options.trigger,options.width,options.action);
							
							lightbox = BTT.LightboxesCarousel.enableNavigation(i,lightboxesArray, lightbox, options.trigger);
							
							
							
						}
						
						
						if (lightbox !== false) {
							if (BTT.LightboxesCarousel.showHideLightbox(lightbox,options.trigger,options.width)) {
								triggerEvent.preventDefault();
							}
						}
						
					}
				);
			});
			
			
			BTT.LightboxesCarousel.createAccessibleInput();
		}
	}
};
	
/* Initialise when the DOM is ready
-------------------------------- */
$(document).ready(function(){
				   
	if ((BTT.IE === undefined) || (BTT.IE.runAjaxAndLightboxes === undefined) || BTT.IE.runAjaxAndLightboxes) {
		BTT.LightboxesCarousel.init();		
	}
	
});

