/*******************************
       Namespaced Functions
 *******************************/
 
$.extend(lbox, {
	
	// handles chromeframe pop-up
	chromeFrameInstall: function() {
		var chromeOptions = { 
			node: 'banner',
			destination: 'index.html',
			// make this true to use onmissing to create custom install implementation
			preventPrompt: true,
			onmissing: function() {
				if(confirm('This HTML5 site requires Google\'s Chrome Frame plugin to be compatible with IE6 (10 years old!). Proceed to download site?')) {
					window.location = 'http://www.google.com/chromeframe';
				}
				else {
					window.location	= 'http://www.mozilla.org';
				}
			}
		};
		try {
			CFInstall.check(chromeOptions);	
		}
		catch(error) {
			// Chrome frame library not included on this page	
			console.log("Could not initiate chrome frame install:\n" + error);
		}
	},
	
	// cycles through child elements with crossfade
	cycler: function($container, fadeDuration, delay) {
		var $elements = $container.children();
		setTimeout(function() {
			var numElements = $elements.size();
			// make sure all images havent been displayed
			if($elements.filter('.displayed').size() == numElements) {
			 	$elements
					.filter(':hidden')
						.removeClass('displayed')
				;	
			}
			do {
				var randomNumber = Math.floor(Math.random() * (numElements));
				var $selectedElement = $elements.eq(randomNumber);
				// retry if already displayed
				var retry = ($selectedElement.filter('.displayed, .visible').size() == 1) ? true : false;
 			}
			while (retry);	
			// ready to display selected
			$elements
				.filter(':visible')
					.fadeOut(fadeDuration, function() { $(this).removeAttr('style') })
					.removeClass('visible')
					.addClass('displayed')
			;
			$selectedElement
				.fadeIn(fadeDuration)
				.addClass('displayed')
			;
			lbox.cycler($container, fadeDuration, delay);
		}, delay);
	},
	
	// finds current section by examing body tag
	findSection: function() {
		return $('body').attr('id');	
	},
	
	// handles user agent checking
	isiPad: function() {
		return navigator.userAgent.match(/iPad/i) != null;
	},
	isiPhone: function() {
		return navigator.userAgent.match(/iPhone/i) != null;
	},
	
	imageSlide: function(imageIndex, params) {
		var settings = {
			pixelsPerSecond: 4500,
			slowThreshold: 250	
		}
		$.extend(settings, params);
		// shorthand
		var pixelsPerSecond = settings.pixelsPerSecond;
		var slowThreshold = settings.slowThreshold;
		
		// cache jq
		var $marqueeList = $('.marquee ul');
		var $thumb = $('.thumbs li').eq(imageIndex);
		var $imageStem = $('.marquee ul li').eq(imageIndex);
		// swap out thumbnail classes
		$thumb.addClass('active').siblings('.active').removeClass('active');
		$imageStem.addClass('active').siblings('.active').removeClass('active');
		
		var imageIndex =  $thumb.parent().find('li').index($thumb);
		var totalImages = $thumb.parent().children().size();
		
		var currentMargin = parseInt($marqueeList.css('margin-left'), 10);
		var destinationMargin = (imageIndex == 0) 
			? (currentMargin) 
			: parseInt($marqueeList.find('li').eq(imageIndex).position().left, 10)
		;
		// calculate animation duration based on distance
		var pixelDistance = Math.abs(destinationMargin);
		var durationCalculation = parseInt((pixelDistance / pixelsPerSecond) * 1000, 10);
		
		// formulate animation based on rate calculation
		var animationDuration = (durationCalculation > slowThreshold) 
			? durationCalculation  
			: slowThreshold
		;
		var animationEasing = (durationCalculation > slowThreshold) 
			? 'easeInOutCubic' 
			: 'easeOutSine'
		;
		// stop running animations
		if($marqueeList.filter(':animated').exists()) {
			$marqueeList.stop();
		}
		// clear any queued animations
		if(typeof(lbox.timer) != 'undefined') {
			clearTimeout(lbox.timer);	
		}
		// hammer click
		lbox.timer = setTimeout(function() {
			$marqueeList
				.animate({
					marginLeft: (currentMargin - destinationMargin)+'px'
				}, animationDuration, animationEasing)
			;
		}, 40);
		
	},
	
	// Precache images
	precache: function(images, callback) {
		jQuery.each(images, function(index, src) {
			var image = new Image();
			if(typeof(callback) == 'function') {
				// bind callback to load error and abort, so it always fires
				jQuery(image)
					.bind('load', callback)
					.bind('error', callback)
					.bind('abort', callback)
				;
			}
			image.src = src;
		});	
	}
	
});

