// Scoller
function Scroller(link, speed) {

	if (!speed) var speed = 500;

	$(link).click(function() {
	   var elementClicked = $(this).attr("href");
	   var destination = $(elementClicked).offset().top;
	   $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-30}, speed );
	   return false;
	});
}

// Fancybox Overlays
function Overlay(link) {

	$(link).fancybox({
			'height'        :   600,
			'transitionIn'	:	'fade',
			'transitionOut'	:	'fade',
			'speedIn'		:	800, 
			'speedOut'		:	400, 
			'overlayColor'  :   '#fff',
			'overlayOpacity':   0.8,
			'overlayShow'	:	true,
			'titlePosition' :   'over',
			'width'         :   800
	});		
}


// Carousel
function Carousel(container, timeouts) {
	
	var container = $(container);
	var img_height = container.children(':first-child').height();
	
	container.parent().css('height', img_height);
	container.css('height', img_height);
	
	// Create the Navigation
	var navigationHTML =  '<div id="slideshow-navigation">';
		navigationHTML += '<a id="previous" href="#">Previous</a>';
		navigationHTML += '<a id="next" href="#">Next</a>';
		navigationHTML += '<div id="indicators"></div>';
		navigationHTML += '</div>';
	container.before(navigationHTML);

	var navigation = $('#slideshow-navigation');
	
	// Hide the Forward and Back buttons
	navigation.children('a').hide();
	
	if (timeouts == true) {
		
		// 	Slideshow Settings
		var timeouts = [
			// 2000,
			4000,
			// 3000,
			// 4000,
			100,
			100,
			100,
			4000,
			100,
			100,
			100,
			4000,
			100,
			100,
			100,
			2000,
			2000,
			// 3000,
			// 2000,
			3000,
			4000
		];
		
		function calculateTimeout(currElement, nextElement, opts, isForward) { 
		    var index = opts.currSlide; 
		    return timeouts[index]; 
		}
		
		container.cycle({
		    fx: 'fade',
			next: '#next',
			nowrap: 1,
			pager: '#indicators',
			pause: 1,
			pauseOnPagerHover: 1,
			prev: '#previous',
			speed: 500,
			sync: 1,
			timeoutFn: calculateTimeout
		});
		
	} else {
		
		container.cycle({
		    fx: 'fade',
			next: '#next',
			nowrap: 0,
			pager: '#indicators',
			pause: 1,
			pauseOnPagerHover: 1,
			prev: '#previous',
			speed: 500,
			sync: 1
		});
	};
	
	
	// Show and hide the Navigation on hover
	container.hover(
		function () {
			navigation.children('a').stop(true,true).fadeIn('slow');
		},
		function () {
			navigation.children('a').stop(true,true).fadeOut('slow');
		}
	);
	navigation.children('a').hover(
		function () {
			$(this).stop(true,true).show();
			container.cycle('pause');
		},
		function () {
			$(this).stop(true,true).show();
			container.cycle('resume');
		}
	);
	
	// Remove the focus outline on Navigation items when clicked
	navigation.find('a').keypress(function() {
		this.blur();
		this.hideFocus = false;
		this.style.outline = null;
	});
	navigation.find('a').mousedown(function() {
		this.blur();
		this.hideFocus = true;
		this.style.outline = 'none';
	});
	
	
}
