/*****************************************************************************
 *
 * Copyright © 2011 Mordue Engineering
 *
 * All rights reserved
 *
 ****************************************************************************/

(function () {
	$(document).ready (function () {
		/*
		 * Primary navigation drop downs
		 */
		$('ul#primary_nav li.has_dropdown').hover (
			function () { $(this).addClass ('active'); },
			function () { $(this).removeClass ('active'); }
		);
		
		/*
		 * The feature slideshow
		 */
		var featureHeading = $('#feature h3');
		var featureDescription = $('#feature p');
		
		// Store the panel data for showing on demand
		var featureData = {};
		$('ul#features li').each (function (i, item) {
			featureData[i] = {
				'heading': $(item).find ('h3').text (),
				'description': $(item).find ('p').text (),
				'link': $(item).find ('a').attr ('href')
			};
		});
		
		// Now hide the data (don't remove it for the sake of search engines)
		$('ul#features h3')
			.add ('ul#features p')
			.hide ();
		
		$('ul#features').Slideshow (
			{
				cycleInterval: 5000,
				nextButton: $('a#feature_forward'),
				prevButton: $('a#feature_back'),
				onChangeBegin: function (obj) {	
					featureHeading.text (featureData[obj._index].heading);
					featureDescription.text (featureData[obj._index].description);
					$('#feature_link').attr ('href', featureData[obj._index].link);
				}
			}
		);


		/*
		 * The project shortcuts
		 * We do an array size check to ensure compatibility with IE
		 */
		var shortcuts = $('#project_shortcuts');
		if (shortcuts.length)
			shortcuts.Carousel ({
			'panelWidth': 700,
			'panelHeight': 50,
			'length': 600,
			'direction': 'vertical'
		});
		
		
		/*
		 * Projects page
		 */
		
		// Change the category
		var setCategory = function (category) {
			// Show the correct items
			$('#projects_list > li')
				.show ()
				.filter (function () { return $(this).attr ('alt') != category; })
				.hide ();
				
			// Change the heading
			var heading;
			switch (category) {
				case 'residential':
					heading	= 'Residential projects';
					break;
				case 'commercial':
					heading	= 'Commercial & industrial projects';
					break;
				case 'health':
					heading	= 'Health & education projects';
					break;
				case 'others':
					heading	= 'Other projects';
					break;
				default:
					heading	= 'There are no projects in that category';
			}
			$('#project_heading').text (heading);
			
			// Update the category select
			$('#category_selection a')
				.removeClass ('current')
				.filter (function () { return $(this).attr ('alt') == category; })
				.addClass ('current');
		};
		
		// Get the current category
		var getCategory = function () {
			return $('#projects_list').attr ('alt');
		};
		
		// Set up manual changing of the category
		$('#category_selection a').click (function () {
			var category = $(this).attr ('alt');
			setCategory (category);
			return false;
		});
		
		// Initialize
		// The initial category is set by the HTTP request
		setCategory (getCategory ());
		
		// PrettyPhoto initialisation
		$("a[rel^='prettyPhoto']").prettyPhoto({
			'theme':	'dark_rounded'
		});
		
		// Bit of a hack to get pretty photo to work properly
		// Simulate the first thumbnail being clicked
		$('ul.actions li.action_photos a').click (function () {
			var el = $(this)
				.parent ()
				.parent ()
				.parent ()			
				.find ('.gallery li')
				.eq (0)
				.find ('a')
				.click ();

				return false;
		});
		
	});
})(jQuery);
