$(document).ready(function(){
	//
	// APPLICATION
	//-------------------------------------------------------------------------------------\
	// Journey v0.1
	//
	//=======================================================================================================\
	//
	// Main
	//
	function application(){
		//
		var app = this;
		//
		//---------------------------------------------------------------\
		//
		// APP ELEMENTS
		//
		//---------------------------------------------------------------\
		//
		// Elements
		//
		app.journeyImageList = $('#journey ul.imageList');
		app.journeyImages = $('li', app.journeyImageList);
		app.journeyMenu = $('#journey div.menu');
		app.journeyMenuItems = $('ul.chapterList li a', app.journeyMenu);
		app.journeyImageFooter = $('#journey div.imageFooter');
		app.journeyImageCountry = $('span.country', app.journeyImageFooter);
		app.journeyImageTitle = $('span.title', app.journeyImageFooter);
		//
		//--------------------------\
		//
		// Constants
		//
		app.journeyCountry = $('span.country', app.journeyMenu).html();
		app.journeyTitle = $('span.title', app.journeyMenu).html();
		app.totalChapters = app.journeyMenuItems.length;
		app.fadeSpeed = 300;		
		//
		//---------------------------------------------------------------\
		//
		// APP METHODS
		//
		//---------------------------------------------------------------\
		//
		// Setup Menu
		//
		app.setupMenu = function(){
			//
			app.journeyMenuItems.each(function(){
				//
				$(this).data('title', $(this).attr('title'));
				$(this).attr('title', '');
				//
				$(this).mouseenter(function(){
					//
					//console.log('I AM '+$($(this).parents('ul')[0]).children().index($(this).parent()));
					//
					var id = $($(this).parents('ul')[0]).children().index($(this).parent());
					var title = $(this).data('title');
					//
					app.showjourney(id, title);
					//
				});
				//
			});
			//
			$('div.elementSB.chapterList').elementSB();
			//
		}
		//---------------------------------------------------------------\
		//
		// Show Journey
		//
		app.showjourney = function(id, title){
			//
			//console.log('app.showjourney '+title);
			app.journeyImageCountry.html(app.journeyCountry);
			app.journeyImageTitle.html(title);
			//
			app.journeyImages.fadeOut(app.fadeSpeed);
			$($('li', app.journeyImageList)[id]).fadeIn(app.fadeSpeed);
			//
		}
		//---------------------------------------------------------------\
		//
		// Reset To Default
		//
		app.resetToDefault = function(){
			//
			app.journeyImageCountry.html(app.journeyCountry);
			app.journeyImageTitle.html(app.journeyTitle);
			//
			app.journeyImages.fadeOut(app.fadeSpeed);
			$('li.default', app.journeyImageList).fadeIn(app.fadeSpeed);
			//
		}
		//---------------------------------------------------------------\
		//
		// Setup Images
		//
		app.setupImages = function(){
			//
			app.journeyImages.hide();
			var ran = Math.floor(Math.random()*app.totalChapters);
			//$(app.journeyMenuItems[ran]).mouseenter();
			app.resetToDefault();
			//
		}
		//---------------------------------------------------------------\
		//
		// Intialize
		//
		app.initialize = function(){
			//
			app.setupMenu();
			app.setupImages();
			//
			$('ul.explorerNav li.about a').click(function(){
				//
				popup.openPopup('aboutcopy');
				//
			});
			$('.popup a.close').click(function(){
				//
				popup.closePopup();
				//
			});
			//
			$('.subMenu').mouseenter(function(){
				//
				$(this).removeClass('open closed').addClass('open');
				//
			});
			//
			$('.subMenu').mouseleave(function(){
				//
				$(this).removeClass('open closed').addClass('closed');
				//
			});
			//
			$('a.warisnav').click(function(){
				//
				popup.openPopup('waris');
				//
			});
			//
		}
		//
	}
	//
	// Initialize
	app = new application();
	app.initialize();
	//
});
