/*jslint devel: true, plusplus: true, white: true, maxlen: 120, maxerr: 10, indent: 4 */
/*globals document, window, jQuery */

var TAHP = window.TAHP || { };

TAHP.PortfolioLinks = (function (module) {
	"use strict";
	
	// Private
	
	function p_imgHover(e)
	{
		var $target = jQuery(e.currentTarget);
		$target.children('.p_tooltip').animate(
		{
			opacity: 1
		}, 200);
	}
	function p_imgUnhover()
	{
		jQuery('.p_tooltip').animate(
		{
			opacity: 0
		}, 200);
	}
	function p_thumbClick(e)
	{
		var $target = jQuery(e.currentTarget),
			largeImg = "_img/clip-" + $target.attr('data-id') + ".png",
			$mainImg = $target.parents('.p_tooltip').prev();
		
		$target.parents('.p_tooltip_main').find('ul li img').removeClass('selected');
		$target.addClass('selected');
		$mainImg.attr('src', '_img/clip-loading.gif');
		jQuery.preload([largeImg],
		{
			onComplete: function()
			{
				$mainImg.attr('src', largeImg);
			}
		});
		
		return false;
	}
	
	// Public
	
	module.init = function()
	{
		var $thumbs = jQuery('.p_tooltip_main ul li img'),
			i;
			
		// Preload images
		jQuery.preload(
		[
			'clip-loading.gif'
		], 
		{
			base:'_img/'
		});

		// Main image hover event listeners
		jQuery('.portfolio_img').parent().hoverIntent(p_imgHover, p_imgUnhover);
		
		// Highlight the main image's matching thumbnail
		for (i = 0; i < $thumbs.size(); i++)
		{
			if (jQuery($thumbs[i]).attr('data-id') === jQuery($thumbs[i]).parents().find('.portfolio_img').attr('data-id'))
			{
				jQuery($thumbs[i]).addClass('selected');
			}
		}
		
		// Set tooltip opacity to 0 and show
		jQuery('.p_tooltip').animate(
		{
			opacity: 0
		}, 0).show();
		
		// Thumb image hover and click event listeners
		$thumbs.click(p_thumbClick);
	};
	
	return module;
} (TAHP.PortfolioLinks || {}));
