$(document).ready(function(){
	//
	// APPLICATION
	//-------------------------------------------------------------------------------------\
	// Home v0.1
	//
	//=======================================================================================================\
	//
	// Main
	//
	function application(){
		//
		var app = this;
		//
		//---------------------------------------------------------------\
		//
		// APP ELEMENTS
		//
		//---------------------------------------------------------------\
		//
		// Elements
		//
		app.photoStage = $('ul.packageList');
		app.photos = $('li', app.photoStage);
		app.currentPhoto = 0;
		app.totalPhotos = app.photos.length;
		app.autoInterupt = false;
		//
		app.nav_destinations = $('#exploreNav .destinations');
		app.nav_experiences = $('#exploreNav .experiences');
		app.nav_a_destinations = $('#exploreNav a.destination');
		app.nav_a_experiences = $('#exploreNav a.experience');
		//
		//--------------------------\
		//
		// Constants
		//
		
		//
		//---------------------------------------------------------------\
		//
		// APP METHODS
		//
		//---------------------------------------------------------------\
		//
		// Intialize
		//
		app.initialize = function(){
			//
			// prep stage
			app.photos.hide();
			//app.photos.css('visibility', 'hidden');
			app.currentPhoto = Math.floor(Math.random()*app.totalPhotos);
			//$(app.photos[app.currentPhoto]).css('visibility', 'visible');
			$(app.photos[app.currentPhoto]).fadeIn(app.fadeSpeed);
			window.setInterval(function(){app.autoNextPhoto();}, (app.switchSpeed));
			//console.log('Start At Image = '+app.currentPhoto);
			//
			$('.popup a.close').click(function(){
				//
				popup.closePopup();
				//
			});
			$('.header .subTitle a.about').click(function(){
				//
				popup.openPopup('aboutcopy');
				//
			});
			$('#slideshow a.prev').click(function(){
				//
				app.previousPhoto('manual');
				//
			});
			$('#slideshow a.next').click(function(){
				//
				app.nextPhoto('manual');
				//
			});
			$('#exploreNav a.destination').click(function(){
				//
				app.openNav('destination');
				//
			});
			$('#exploreNav a.experience').click(function(){
				//
				app.openNav('experience');
				//
			});
			//
			$('ul.regions a', app.nav_destinations).each(function(){
				//
				var thisA = $(this);
				//
				thisA.click(function(){
					//
					var thisA = $(this);
					var type = thisA.attr('type');
					app.openRegion(type);
					//
				});
				//
			});
			//
			var defaultRegion =  $($('ul.regions a', app.nav_destinations)[0]).attr('type');
			app.openRegion(defaultRegion);
			//
		}
		//
		//---------------------------------------------------------------\
		//
		// GALLERY
		//
		//---------------------------------------------------------------\
		//
		//---------------------------------------------------------------\
		//
		// Auto Next Photo
		//
		app.autoNextPhoto = function(type){
			//
			if(!app.autoInterupt){
				//
				app.nextPhoto('auto');
				//
			}
			//
		}
		//
		//---------------------------------------------------------------\
		//
		// Next Photo
		//
		app.nextPhoto = function(type){
			//
			//console.log('app.switchPhoto : app.currentPhoto = '+app.currentPhoto);
			//
			if(type != 'auto'){
				//
				app.autoInterupt = true;
				//
			}
			//
			if(app.currentPhoto < (app.photos.length-1)){
				// move to next photo
				$(app.photos[app.currentPhoto]).fadeOut(app.fadeSpeed);
				//$(app.photos[app.currentPhoto+1]).css('visibility', 'visible');
				$(app.photos[app.currentPhoto+1]).fadeIn(app.fadeSpeed);
				//$('span.name embed', $(app.photos[app.currentPhoto+1])).css('height', '28px');
				//$('span.name object', $(app.photos[app.currentPhoto+1])).css('width', '200px');
				//$(app.photos[app.currentPhoto]).css('visibility', 'hidden');
				//$(app.photos[app.currentPhoto+1]).css('visibility', 'visible');
				app.currentPhoto++;
				//
			}else{
				// on last photo
				$(app.photos[app.currentPhoto]).fadeOut(app.fadeSpeed);
				//$(app.photos[0]).css('visibility', 'visible');
				$(app.photos[0]).fadeIn(app.fadeSpeed);
				//$('span.name embed', $(app.photos[0])).css('height', '28px');
				//$('span.name object', $(app.photos[0])).css('width', '200px');
				//$(app.photos[app.currentPhoto]).css('visibility', 'hidden');
				//$(app.photos[0]).css('visibility', 'visible');
				app.currentPhoto = 0;
				//
			}
			//
		}
		//
		//---------------------------------------------------------------\
		//
		// Previous Photo
		//
		app.previousPhoto = function(type){
			//
			//console.log('app.switchPhoto : app.currentPhoto = '+app.currentPhoto);
			//
			if(type != 'auto'){
				//
				app.autoInterupt = true;
				//
			}
			//
			if(app.currentPhoto > 0){
				// move to prev photo
				$(app.photos[app.currentPhoto]).fadeOut(app.fadeSpeed);
				$(app.photos[app.currentPhoto-1]).fadeIn(app.fadeSpeed);
				app.currentPhoto--;
				//
			}else{
				// on last photo
				$(app.photos[app.currentPhoto]).fadeOut(app.fadeSpeed);
				$(app.photos[(app.photos.length-1)]).fadeIn(app.fadeSpeed);
				app.currentPhoto = (app.photos.length-1);
				//
			}
			//
		}
		//
		//---------------------------------------------------------------\
		//
		// NAVIGATION
		//
		//---------------------------------------------------------------\
		//
		//---------------------------------------------------------------\
		//
		// Open Nav
		//
		app.openNav = function(type){
			//
			app.nav_destinations.hide();
			app.nav_experiences.hide();
			app.nav_a_destinations.removeClass('on');
			app.nav_a_experiences.removeClass('on');
			//console.log(type);
			if(type == 'destination'){
				app.nav_a_destinations.addClass('on');
				app.nav_destinations.show();
			}else if(type == 'experience'){
				app.nav_a_experiences.addClass('on');
				app.nav_experiences.show();
			}
			//
		}
		//
		//---------------------------------------------------------------\
		//
		// Open Region
		//
		app.openRegion = function(region){
			//
			//console.log('app.openRegion('+region+')');
			//
			//
			$('ul.regions a', app.nav_destinations).each(function(){
				//
				var thisA = $(this);
				var type = thisA.attr('type');
				//
				if(type == region){
					thisA.addClass('on');
				}else{
					thisA.removeClass('on');
				}
				//
			});
			//
			$('div.countries li', app.nav_destinations).each(function(){
				//
				var thisLI = $(this);
				var type = thisLI.attr('class');
				//
				if(type == region || type ==  'all'){
					thisLI.show();
				}else{
					thisLI.hide();
				}
				//
			});
			//
		}
		//
	}
	//
	// Initialize
	app = new application();
	app.switchSpeed = 8000;
	app.fadeSpeed = 500;
	app.initialize();
	//
});
