var HomeTeaserPanel;
(function($){
	var $panes, $navItems, settings, slideshow;
	
	var b = function(options) {
		settings = options;
		
		$panes = $('.home-teaser .content .pane');
		$navItems = $('.home-teaser .nav a');
		
		slideshow = new Slideshow({
			target: $('.slideshow')[0]
		});
		
		$navItems.click(function(e){
			e.preventDefault();
			var index = $navItems.index(this);
			activatePane(index);
		});
		
		$panes.each(function(i, item){
			if (!$(this).hasClass('none')) {
				activatePane(i);
			}
		});
	}
	
	function activatePane(index) {
		$navItems.removeClass('active');
		$($navItems[index]).addClass('active');
		$panes.hide();
		$($panes[index]).show();
		
		var id = $($panes[index]).attr('id');
		
		slideshow.stop();
		
		switch (id) {
			case 'pane-images':
				slideshow.start();
			break;
			
			case 'pane-video':
				enableVideo();
			break;
			
			case 'pane-my-tunisia':
				enableMyTunisia();
			break;
		}
	}
	
	function enableVideo() {
		swfobject.embedSWF(
			settings.videoPlayerURL + "?v=1.0.0", 
			"video-player-alt", 546, 296, "9.0.0",
			"", 
			{ options: settings.videoPlayerOptions, debug: 1 },
			{ menu: "false", scale: "noScale", allowFullscreen: "true", allowScriptAccess: "always", bgcolor: "#000000" },
			{ id: "video-player-alt" }
		);
	}
	
	function enableMyTunisia() {
		swfobject.embedSWF(
			settings.myTunisiaFlashURL + "?v=1.0.0", 
			"my-tunisia-alt", 546, 296, "9.0.0",
			"", 
			{ },
			{ menu: "false", scale: "noScale", allowFullscreen: "true", allowScriptAccess: "always", bgcolor: "#FFFFFF" },
			{ id: "my-tunisia-alt" }
		);
	}
	
	HomeTeaserPanel = b;
})(jQuery);

var Slideshow;
(function($){
	var nextIndex = 0,
	currentIndex,
	cycleAnimId,
	autoplay = true, 
	navShown = false,
	duration = 3000,
	templates = {},
	total,
	$this,
	$images,
	$nav;
	
	templates.nav = 
	'<div class="slideshow-nav"><div class="slideshow-nav-pad clearfix">' +
		'<a class="slideshow-nav-prev">Prev</a>' +
		'<span class="slideshow-nav-pager"><span class="slideshow-current-page">' + 1 + '</span> of <span class="slideshow-total"></span></span>' +
		'<a class="slideshow-nav-next">Next</a>' +
		'<span> | Auto Play: </span>' +
		'<a class="slideshow-nav-autoplay">' + (autoplay ? 'On' : 'Off') + '</a>' +
		'<span class="slideshow-caption"></span>' +
	'</div></div>';
	
	var b = function(settings) {
		$this = $(settings.target);
		$images = $this.find('img');
		$images.hide();
		$nav = $(templates.nav).appendTo($this);
		$nav.hide();
		total = $images.length;
		$this.find('.slideshow-total').html(total);
		$this.bind('mouseover', function(event){
			showNav();
		});
		$(document).bind('mouseover', checkForMouseout);
		$this.find('.slideshow-nav-prev').bind('click', function(e){
			e.preventDefault();
			var next = currentIndex - 1;
			if (next < 0) {
				next = total - 1;
			} else if (next == total) {
				next = 0;
			}
			nextIndex = next;
			rotateImages();
		});
		$this.find('.slideshow-nav-next').bind('click', function(e){
			e.preventDefault();
			nextIndex++;
			if (nextIndex == total) nextIndex = 0;
			rotateImages();
		});
		$this.find('.slideshow-nav-autoplay').bind('click', function(e){
			e.preventDefault();
			toggleAutoplay();
			if (autoplay) {
				setTimeout(function() {
					nextIndex++;
					if (nextIndex == total) nextIndex = 0;
					rotateImages();
				}, duration);
			}
		});
	}
	
	b.prototype.start = function() {
		rotateImages();
	}
	
	b.prototype.stop = function() {
		clearInterval(cycleAnimId);
		currentIndex = null;
		nextIndex = 0;
		$images.hide();
	}
	
	var rotateImages = function() {
		clearInterval(cycleAnimId);
		if (currentIndex) {
			$($images[currentIndex]).fadeOut('normal');
		}
		$this.find('.slideshow-current-page').html(nextIndex + 1);
		$this.find('.slideshow-caption').html($($images[nextIndex]).attr('title'));
		$($images[nextIndex]).fadeIn('normal', function() {
			if (total == 1) return;
			currentIndex = nextIndex;
			if (!autoplay) return;
			cycleAnimId = setInterval(function(){
				nextIndex++;
				if (nextIndex == total) nextIndex = 0;
				rotateImages();
			}, duration);
		});
	}
	
	var checkForMouseout = function(event) {
		var el = event.target;
		while (true) {
			if (el == $this[0]) {
				return true;
			} else if (el == document) {
				hideNav();
				return false;
			} else {
				el = $(el).parent()[0];
			}
		}
	}
	
	var showNav = function() {
		if (navShown) return;
		$nav.slideDown();
		navShown = true;
	}
	
	var hideNav = function() {
		if (!navShown) return; 
		$nav.slideUp();
		navShown = false;
	}
	
	var toggleAutoplay = function() {
		clearInterval(cycleAnimId);
		autoplay = autoplay ? false : true;
		var status = autoplay ? 'On' : 'Off';
		$this.find('a.slideshow-nav-autoplay').html(status);
	}
	
	Slideshow = b;
})(jQuery);