/**
 * The homepage-specific scripts
 */
var Home = {
	/* The ids of elements */
	ids : {
		gallery : 'home-header'
	},
	/**
	 * Gallery-specific functions
	 */
	gallery : {
		/* The reference to the timer */
		timer : null,
		/* The amount of time in the timer */
		timerLength : 9000,
		/* The link objects */
		links : [],
		/* The page containers */
		pages : [],
		/* The page width */
		pageWidth : 0,
		/* The selected index */
		selectedIndex : -1,
		/**
		 * Selects a gallery given the id
		 * @param Integer The gallery index to select
		 */
		doSelect : function (idx) {
			/* See if the index is currently selected */
			if (idx == Home.gallery.selectedIndex) {
				return;
			}
			
			/* Remove the active class from the menus */
			$('#'+Home.ids.gallery+' div.links').find('a').removeClass('on');
			$('#'+Home.ids.gallery+' div.panes-wrapper div.panes div.pane-container')
				.stop(true, false)
				.animate({
					left : -Home.gallery.pageWidth * idx
				}, 1000);
			Home.gallery.links[idx].addClass('on');
			Home.gallery.selectedIndex = idx;
		},
		/**
		 * Runs when the menu button is clicked
		 * @param Object The event data
		 */
		onMenuClick : function (event) {
			/* Remove the timer */
			if (Home.gallery.timer) {
				clearInterval(Home.gallery.timer);
				Home.gallery.timer = null;
			}
			
			/* Get the target of the click */
			if (!event.target) {
				event.target = event.srcElement;
			}
			
			/* Find the index of the tag */
			var clicked_link = $(event.target),
				clicked_index = -1;
			for (var idx = 0; idx < Home.gallery.links.length; idx++) {
				if (clicked_link.attr('href') == Home.gallery.links[idx].attr('href')) {
					clicked_index = idx;
				}
			}
			
			/* Select the index */
			Home.gallery.doSelect(clicked_index);
			return false;
		},
		/**
		 * Runs when the timer elapses to change the menu
		 */
		onMenuTimer : function () {
			/* Select the next or first element */
			var new_index = ((Home.gallery.selectedIndex + 1) >= Home.gallery.links.length) ? 0 : Home.gallery.selectedIndex + 1;
			Home.gallery.doSelect(new_index);
		}
	},
	/**
	 * Sets up the homepage scripts
	 */
	setup : function () {
		/* Get the elements */
		var container = $('#' + Home.ids.gallery),
			panes_container = container.find('div.panes'),
			links_container = container.find('div.links');
		
		/* Locate all of the links */
		var links = links_container.find('a'),
			link;
		Home.gallery.links = new Array();
		$.each(links, function (idx, elem) {
			link = $(elem);
			link.click(Home.gallery.onMenuClick);
			Home.gallery.links.push(link);
		});
		
		/* Locate the panes */
		var panes = panes_container.find('div.pane'),
			pane;
		Home.gallery.pages = new Array();
		Home.gallery.pageWidth = panes_container.width();
		$.each(panes, function (idx, elem) {
			pane = $(elem);
			pane.css({
				position : 'absolute',
				top : 0,
				left : (Home.gallery.pageWidth * idx) + 'px'
			});
			Home.gallery.pages.push(pane);
		});
		
		/* Call the first item */
		Home.gallery.doSelect(0);
		
		/* Set up the timer for auto-rotation */
		Home.gallery.timer = setInterval(Home.gallery.onMenuTimer, Home.gallery.timerLength);
	}
};
$(function () {
	Home.setup();
});

