// JavaScript Document

(function($){
	$.fn.itemPopup = function() {
		function showPopup(popup) {
			var link = popup.find('.imgContainer'),
				img = $('<img class="popupThumb" />'),
				trigger = popup.parents('tr').find('.popupTrigger'),
				thumbs = popup.find('.popupThumbsGallery > a'),
				t = trigger.position().top,
				p = popup.height(); //popup.hasClass('withoutThumb') ? 60 : 372,
				s = $(window).scrollTop(),
				w = $(window).height(),
				top_pos = (t + p) > (s + w) ? (s + w - p) : t - 10;
 
			popup.css({top: top_pos, left: trigger.position().left + 80}).
			find('.pointerArrow').css({top: t + (popup.hasClass('withoutThumb') ? 0 : 10) - top_pos});
			
			if ( link.html() == '' ) {
				link.append(img.attr('src', link.attr('rel'))).removeAttr('rel');
			}
			if ( !popup.data('loaded') ){
				thumbs.each(function() {
					var urls = $(this).attr('rel').split('|');
					$(this).attr('href',urls[1]).addClass('popupThumbsChange').append('<img src="' + urls[0] + '" />');
				});
				popup.data('loaded',true);
			}
			popup.fadeIn(200);
		}
		
		$('.popupThumbsChange').live('click', function() {
			$(this).parent().children().removeClass('active');
			$(this).addClass('active').parents('.itemPopup').find('.imgContainer img').attr('src',$(this).attr('href'));
			return false;
		});
		
		return this.each(function() {
			var popup = $(this);
			var timer;
			var trigger = $(this).parents('tr').find('.popupTrigger');

			trigger.bind('mouseenter', function(){
				clearTimeout(timer);
				timer = setTimeout(function() { 
					showPopup(popup); 
				}, 500);
			}).bind('mouseleave', function() {
				clearTimeout(timer);
				timer = setTimeout(function() { 
					popup.fadeOut(200); 
				}, 500);
			});
			popup.bind('mouseenter', function() {
				clearTimeout(timer);
			}).bind('mouseleave', function(){
				clearTimeout(timer);
				timer = setTimeout(function() { 
					popup.fadeOut(200); 
				}, 500);
			});
		});
	};
	
	$(function() {
		$('.itemPopup').itemPopup();
		$('.itemListing tbody tr').hover(
			function() { 
				$(this).addClass('hover'); 
			}, 
			function() { 
				$(this).removeClass('hover'); 
			}
		);
	});
})(jQuery);

var footerActiveTab = [];
var footerTransTab = [];
var footerTime = [];
for (i = 2; i <= 5; i++)
{
	footerActiveTab['footer'+i] = null;
	footerTime['footer'+i] = null;
};

footerTransTab['footerAbout'] = 'footer2';
footerTransTab['footerServices'] = 'footer4';
footerTransTab['footerExternal'] = 'footer5';
footerTransTab['specialSites'] = 'footer3';

function footerTimeout()
{
	for (i = 2; i <= 5; i++)
	{
		if (footerActiveTab['footer'+i] != null)
		{
			$j(footerActiveTab['footer'+i]).next('ul:hidden').slideDown('fast');
			footerActiveTab['footer'+i] = null;
		};
	};
}

function footerMouseEnter()
{
	footerActiveTab[this.id] = this;
	footerTime[this.id] = setTimeout('footerTimeout()', 600);
}

function footerMouseLeave()
{
	hide_id = footerTransTab[this.id];

	footerActiveTab[hide_id] = null;
	if (footerTime[hide_id])
		clearTimeout(footerTime[hide_id]);
	$j(this).find('ul:visible').slideUp('fast');
}

