var __DEBUG__ = false;
//__DEBUG__ = true;
function debug(message) {
	if (__DEBUG__) {
		try { console.log(message); } catch (e) {}
	}
}


/**
 * Navigation class
 * Used on the home page
 *
 * @version 1.0
 */
var Navigation = new function() {
	var __CLASS__ 	= 'Navigation';	// "class" name
	var __METHOD__	= '()';			// used for debugging
	var __this__	= this;			// reference to itself
	debug(__CLASS__);

	var containers	= null;
	var menus		= null;
	var left 		= null;
	var right		= null;


	/**
	 * Initialization
	 *
	 * @return
	 * @type void
	 */
	function main() {
		__METHOD__ 	= __CLASS__+'.main()';
		debug(__METHOD__);

		left 	= containers.eq(0);
		right	= containers.eq(1);
		//debug(left); debug(right);

		// scroll right column to the bottom
		right.scrollTo(right.find('.page.page-index'), {
			duration: 0,
			onAfter: function(e, target, current, items, position) {
				$('#container').removeClass('invisible');
			}
		});

		// Main menu navigation
		menus.each(function(index) {
			var link = $(this);

			// event handler has been already assigned
			if (link.data('click')) return;

			link.click(function(e) {
				__METHOD__ 	= __CLASS__+'.menus.click()';
				//debug(__METHOD__);

				e.preventDefault();

				var link	= $(this);
				var href 	= link.attr('href');
				var hash	= href.substr(href.lastIndexOf('/')+1);
				//debug(hash);

				$.history.load(hash);

				return false;
			});

			link.data('click', true);
		});
	}


	/**
	 * Navigate to the specific page
	 *
	 * @return
	 * @type void
	 */
	this.navigate = function(hash) {
		__METHOD__ 	= __CLASS__+'.navigate()';
		debug(__METHOD__);

		containers 	= containers || $('#left-column, #right-column');
		left 		= left || containers.eq(0);
		right		= right || containers.eq(1);

		containers.each(function(index, container) {
			container = $(container);

			var target 	= container.find('.page.page-'+ hash);
			debug(target);

			container.scrollTo(target, {
				duration:	800,
				easing:		'easeOutExpo',
				onAfter: 	function(e, target, current, items, position) {
					__METHOD__ 	= __CLASS__+'.container.onAfter()';
					/*debug(e); debug(target); debug(current); debug(items); debug(position); debug(sender.currentTarget.hash);*/
					var page 	= e.closest('.page');
					var index 	= page.parent().find('.page').index(page);
					//debug(page);
					container.trigger('notify', [index]);
				}
			});

			// mark the menu item
			$('.navigation a').removeClass('selected');
			$('a.navigate[href$="/'+ hash +'"]').toggleClass('selected');
		});
	}


	/**
	 * Initialization
	 *
	 * @return
	 * @type void
	 */
	this.init = function() {
		__METHOD__ 	= __CLASS__+'.init()';
		//debug(__METHOD__);

		containers 	= $('#left-column, #right-column');
		menus		= $('a.navigate');

		// if there is no columns or if there is only one page, no need to do anything
		if (containers.length == 0 || containers.eq(0).find('.page').length <= 1)	return;

		main();
	}
}



/**
 * Application class
 * Used to initialize the application
 *
 * @version 1.0
 */
var Application = function() {
	var __CLASS__ 	= 'Application';	// "class" name
	var __this__	= this;
	debug(__CLASS__);


	this.isHome = $('.page-index').length;


	/**
	 * Initialize Google Maps
	 *
	 * @return
	 * @type void
	 */
	function gmaps() {
		__METHOD__ 	= __CLASS__+'.gmaps()';
		debug(__METHOD__);

		var gmap = $('#gmaps #gmap');
		if (gmap.length > 0) {
			gmap.empty();

			// initial location
			gmap.googleMaps({
				geocode: '8-9 Carlisle Street, London W1D 3BP, UK',
				markers: {
					latitude: 51.51469,
					longitude: -0.133868
				},
				depth: 16
			});

			// handle clicks on gmaps links
			var links = $('a.gmaps');
			links.click(function(e) {
				link = $(e.target);

				//debug(link.metadata());
				gmap.googleMaps({
					geocode: link.attr('title'),
					markers: link.metadata(),
					depth: 16
				});

				if (link.attr('target'))
					e.preventDefault();
			});
		}
	}


	/**
	 * Initialize Expandable lists
	 *
	 * @return
	 * @type void
	 */
	function expandables() {
		__METHOD__ 	= __CLASS__+'.expandables()';
		debug(__METHOD__);

		var expandables = $('ul.expandable');
		if (expandables.length == 0)	return;

		expandables.each(function(index, list) {
			list = $(list);
			list.find('li').click(function(e) {
				var item = $(e.target);
				item.siblings().removeClass('expanded').find('div').hide();
				item.toggleClass('expanded').find('div').fadeToggle();
			});
		});
	}


	/**
	 * Initialization
	 *
	 * @return
	 * @type void
	 */
	function init() {
		__METHOD__ 	= __CLASS__+'.init()';
		debug(__METHOD__);

		// hide link outline dots for IE
		if ($.browser.name == 'msie')	$('a').each(function(i, a) { a.hideFocus = true; });

		if (__this__.isHome)	Navigation.init();

		new Slideshow();

		gmaps();

		expandables();
	}


	// a helper function to prepare the date/time format used for tweets
	this.formatDateTime = function(dt) {
		var h 	= dt.getHours();
		var M	= dt.getMonth() + 1;
		var D	= dt.getDate();
		var h 	= dt.getHours();
		var m	= dt.getMinutes();
		//debug(h); debug(M); debug(D);

		var time, date, month, sufix;

		switch (M) {
			case 1:		month = 'Jan'; break;
			case 2:		month = 'Feb'; break;
			case 3:		month = 'Mar'; break;
			case 4:		month = 'Apr'; break;
			case 5:		month = 'May'; break;
			case 6:		month = 'Jun'; break;
			case 7:		month = 'Jul'; break;
			case 8:		month = 'Aug'; break;
			case 9:		month = 'Sep'; break;
			case 10:	month = 'Oct'; break;
			case 11:	month = 'Nov'; break;
			case 12:	month = 'Dec'; break;
			default: 	month = '';
		}
		sufix	= (D%10 == 1) ? 'st' : ((D%10 == 2) ? 'nd' : ((D%10 == 3) ? 'rd' : 'th'));
		date 	= month +' '+ D + sufix;

		m 	= (m < 10) ? '0'+ m : m;
		if (h == 0)			time = 12 +':'+ m +' AM';
		else if (h < 12)	time = h +':'+ m +' AM';
		else				time = (h - 12) +':'+ m +' PM';

		return	time +' '+ date;
	}


	// in case of home page, init all the other pages
	if (__this__.isHome) {
		var loaded 		= 0;
		var pages 		= [
			{ placeholder: '.page-about',		url: '/about' },
			{ placeholder: '.page-services',	url: '/services' },
			{ placeholder: '.page-work',		url: '/work' },
			{ placeholder: '.page-process',		url: '/process' },
			{ placeholder: '.page-team',		url: '/team' },
			{ placeholder: '.page-contact',		url: '/contact' }
		];

		var leftCol		= $('#left-column');
		var rightCol	= $('#right-column');

		leftCol.css('display', 'none');
		rightCol.css('display', 'none');

		// load all the pages
		for (var i=0; i<pages.length; i++) {
			$.ajax({
				url: pages[i].url,
				async: true,
				dataType: 'html',
				success: function(data, status, request) {
					//debug(request.id); debug(request);
					loaded++;
					var left 	= data.substring(data.indexOf('<!--left-->')+11, data.lastIndexOf('<!--/left-->'));
					var right	= data.substring(data.indexOf('<!--right-->')+12, data.lastIndexOf('<!--/right-->'));

					leftCol.find(pages[request.id].placeholder +' .content').replaceWith(left);
					rightCol.find(pages[request.id].placeholder +' .content').replaceWith(right);
					//debug(loaded +': **** left ****');	debug($('#left-column '+ page.placeholder));	debug(left);
					//debug('**** right ****');	debug($('#right-column '+ page.placeholder));	debug(right);

					// if all the pages have been loaded, initialize the app
					if (loaded == pages.length) {
						leftCol.css('display', 'block');
						rightCol.css('display', 'block');
						//$('#container').css('background-image', 'none');
						init();
					}
				}
			}).id = i;	// uniquely identify the request
		}

		// AJAX history - browser back/forward buttons
		$.history.init(
			function(hash) {
				if (hash)
					Navigation.navigate(hash);
				else
					Navigation.navigate('index');
			},
			{ unescape: ",/" }
		);
	}
	else
		init();
}



/* -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */


/**
 * Application entry point
 * Called on document ready - initializes the "application"
 *
 */
function main() {
	new Application();
}



/* -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */


$(document).ready(main);


















