$(document).ready(function() {

});

$(window).bind('load', function() {
	crossfadeHeader();
});

/**
 * Declare bootstrap functions for pages.
 */
var routes = {
	home: function() {
		iMacSlideshow();
	},
	our_work: function() {

	},
	expertise: function() {

	},
	contact: function() {
		// contact page map
		var $map = $('#contact-map .map');
		if($map.length) {
			// Remove the background loading image.
			$map.css({
				width: '360px',
				height: '300px',
				backgroundImage: 'none'
			});
			$map.jmap('init', {
				'mapCenter': [-37.833005,145.056181],
				'mapZoom': 16,
				'mapShowjMapsIcon': false
			});
			$map.jmap('AddMarker', {
				'pointLatLng': [-37.833005,145.056181],
				'pointHTML': '<strong>Inlight Media</strong><p>306/685 Bourke Rd, Camberwell 3124</p>'
			}, function() {
				$('img[src*=http://maps.gstatic.com/intl/en_ALL/mapfiles/marker.png]').live('mouseover', function(event) { $(this).hide(); });
			});
		}

		// contact page form
		$form = $('#contact-form');
		if ($form.length) {

			$('#response').ajaxSend(function(event, XMLHttpRequest, ajaxOptions) {
				$('#response').html('Sending...');
			});

			$('#response').ajaxError(function(event, XMLHttpRequest, ajaxOptions) {
				$('#response').html("Arghhhh and unforeseeable error has occurred.");
			});

			$form.ajaxForm(function(response) {
				var msg = '';
				// Please note these are replicated in the contact page - for non JS users.
				switch (response) {
					case 'sent':
						msg = 'Thank you for your message, we\'ll be in touch soon.';
						break;
					case 'failed':
						msg = 'Arghhhh an unforeseeable error has occurred.';
						break;
					case 'invalid':
						msg = 'All fields need to be completed';
						break;
					default:
						break;
				}

				$('#response').html(msg);
			});
		}
	},
	ski_lodge_promo: function() {
		// @TODO: refactor form code...
		// page form
		$form = $('#contact-form');
		if ($form.length) {

			$('#response').ajaxSend(function(event, XMLHttpRequest, ajaxOptions) {
				$('#response').html('Sending...');
			});

			$('#response').ajaxError(function(event, XMLHttpRequest, ajaxOptions) {
				$('#response').html("Arghhhh and unforeseeable error has occurred.");
			});

			$form.ajaxForm(function(response) {
				var msg = '';
				// Please note these are replicated in the contact page - for non JS users.
				switch (response) {
					case 'sent':
						msg = 'Thank you for your message, we\'ll be in touch soon.';
						break;
					case 'failed':
						msg = 'Arghhhh an unforeseeable error has occurred.';
						break;
					case 'invalid':
						msg = 'All fields need to be completed';
						break;
					default:
						break;
				}

				$('#response').html(msg);
			});
		}
	}
};

/**
 * Swaps the heading colours, then callbacks to bootstrap(page_name).
 */
function crossfadeHeader() {
	colours = [];
	colours['blue'] = ['/img/background-header-blue.jpg', '/img/background-header-wrapper-blue.jpg', '/img/logo-blue.png'];
	colours['purple'] = ['/img/background-header-purple.jpg', '/img/background-header-wrapper-purple.jpg', '/img/logo-purple.png'];
	colours['green'] = ['/img/background-header-green.jpg', '/img/background-header-wrapper-green.jpg', '/img/logo-green.png'];
	colours['orange'] = ['/img/background-header-orange.jpg', '/img/background-header-wrapper-orange.jpg', '/img/logo-orange.png'];

	$colourPlaceholder = $('#header-new');
	if ($colourPlaceholder.exists()) {
		var urls = colours[$colourPlaceholder.attr('class')];

		// preload images for new header
		$(urls).imagepreloader({
			after_all: function() {
				// Change header+logo then run page bootstrap code.
				$oldHeader = $('#header');
				$newHeader = $oldHeader.clone();
				$newHeader.attr('class', $colourPlaceholder.attr('class'));
				$colourPlaceholder.remove();
				$newHeader.hide();
				$oldHeader.after($newHeader);

				// Adjust the newHeader's logo.
				$newHeader.find('#logo img').attr('src', urls[2]);

				// Fade in the new header.
				$newHeader.fadeIn(800, function() {
					$oldHeader.remove();
					runBootstrap();
				});
			}
		});
	}
	else {
		runBootstrap();
	}
}

function runBootstrap() {
	// Bootstrap is used to load page specific functionality.
	var url = window.location.pathname;
	bootstrap(url.replace(/-/g,'_'));
}

function iMacSlideshow() {
	var $data = $([
		{
			name: 'Victorian Multicultural Commission - http://multicultural.vic.gov.au/',
			url: '/our-work#vmc',
			image: '/img/project-vmc.png'
		},
		{
			name: 'Nightvibes - http://nightvibes.com.au',
			url: '/our-work#nightvibes',
			image: '/img/project-nightvibes.png'
		},
		{
			name: 'Your Printing Quotes - http://yourprintingquotes.com.au',
			url: '/our-work#ypq',
			image: '/img/project-ypq.png'
		},
		{
			name: 'Rivetz Online Store - http://rivetz.com.au',
			url: '/our-work#rivetz',
			image: '/img/project-rivetz.png'
		},
		{
			name: 'BVH Accountants - http://bvhaccountants.com.au',
			url: '/our-work#bvh',
			image: '/img/project-bvh.png'
		},
		{
			name: 'Abco Water Systems - http://abco.net.au',
			url: '/our-work#abco',
			image: '/img/project-abco.png'
		},
		{
			name: 'Airlie Bay Accommodation - http://www.airliebay.com.au/',
			url: '/our-work#airlie',
			image: '/img/project-airlie.png'
		}
	]);


	var slider_urls = [];
	$data.each(function(index, value) {
		slider_urls[index] = value.image;
	});

	$(slider_urls).slider({
		container_id: 'screen',
		before_each: function() {
 			var src = $(this).attr('src');
			var index = slider_urls.indexOf(src);
			var current = $data[index];

			$('#screen').attr('href', current.url);
			$('#projects .name-wrapper .name').fadeOut(500, function() {
				$('#projects .name').html(current.name);
				$('#projects .url').attr('href', current.url);
				$('#projects .name-wrapper .name').fadeIn(500);
			});
		}
	});
}



