
(function($){

	"use strict";

	/* DOM ready. */
	$(document).ready(function(){

		setupLeadIndex();
		setupServicesTab();
		setupServicesIndex();

	});

	/**
	 * Setup Lead index.
	 */
	function setupLeadIndex()
	{
		/* Setup each items behavior. */
		$('#main > div.lead > div.about li')
			/* Store images. */
			.find('h3 img')
				.each(function(){
					var img = new Image();
					img.src = this.src.replace(/^(.+)(\.[a-z]+)$/, '$1_on$2');
					$(this).data('images', {
						defaultSrc: this.src,
						activeSrc: img.src
					});
				})
			.end()

			/* Expand clickable area. */
			.css('cursor', 'pointer')
			.click(function(){
				var url = $(this).find('a').attr('href');
				if (url) {
					location.href = url;
				}
				return false;
			})

			.hover(
				function(){
					$(this).addClass('on');
					var $img = $(this).find('h3 img');
					$img.attr('src', $img.data('images').activeSrc);
				},
				function(){
					$(this).removeClass('on');
					var $img = $(this).find('h3 img');
					$img.attr('src', $img.data('images').defaultSrc);
				}
			)
		;
	}

	/**
	 * Setup Servcies tab.
	 */
	function setupServicesTab()
	{
		$('#tab')
			/* Store images. */
			.find('img')
				.each(function(){
					var img = new Image();
					img.src = this.src.replace(/^(.+)(\.[a-z]+)$/, '$1_on$2');
					$(this).data('images', {
						defaultSrc: this.src,
						activeSrc: img.src
					});
				})
			.end()

			/* Setup tab. */
			.idTabs(function(id, list, set){
				/* Switch active tab. */
				$('a', set).each(function(){
					var src;
					if (this.hash === id) {
						src = $('img', this).data('images').activeSrc;
					} else {
						src = $('img', this).data('images').defaultSrc;
					}
					$('img', this).attr('src', src);
				});

				/* Show & hide tab contents. */
				for (var i in list) {
					$(list[i]).stop(false, true).hide();
				}
				$(id).fadeIn('fast');
			})
		;
	}

	/**
	 * Setup Services index.
	 */
	function setupServicesIndex()
	{
		/* Store items. */
		var $items = $('#purpose li');

		/* Set flat heights each 3 items. */
		$.each(array_chunk($items, 3), function(){
			$(this).flatHeights();
		});

		/* Add Find by 'Contents' items. */
		$.merge($items, $('#contents li'));

		/* Setup each items behavior. */
		$items
			/* Expand clickable area. */
			.css('cursor', 'pointer')
			.click(function(){
				var url = $(this).find('a').attr('href');
				if (url) {
					location.href = url;
				}
				return false;
			})

			/* Set hover handler. */
			.hover(
				function(){
					$(this).find('img.image').stop().animate({
						opacity: 0.5
					}, 'fast');
				},
				function(){
					$(this).find('img.image').stop().animate({
						opacity: 1
					}, 'fast');
				}
			)
		;
	}

})(jQuery);



