
$(function () {
	
	/* -- IMAGE REPLACEMENT -- */
	$('#body .replaceable').each(function () {
		
		var src = $(this).metadata().src;
		var width = ($(this).metadata().hasOwnProperty('width')) ? $(this).metadata().width + 'px' : "auto";
		var height = ($(this).metadata().hasOwnProperty('height')) ? $(this).metadata().height + 'px' : "auto";
		
		var container = $(document.createElement("div"));
		container.addClass('replaced').css({
			width:width,
			height:height,
			margin:0,
			'text-indent':'-2000px',
			overflow:'hidden',
			background:'transparent url(' + src + ') left top no-repeat'
		});
		
		$(this).wrap(container);
		
	});
	
	
	/* -- NAVIGATION -- */
	
	var navigation = $('#navigation');
	var navigationMenu = $('#navigationMenu');
	
	/* Navigation Height */
	var standardNavigationHeight = 82;
	var computedHeight = standardNavigationHeight + $('#secondaryNavigation').height() + $('#productNavigationContainer').height() + $('#body').height();
	
	navigation.height(computedHeight);
	$('#navigation li ul').height(computedHeight);
	
	/* Navigation Hover */
	
	navigationMenu.hover(function () {
		navigation.show().trigger('menushow');
	}, function () {
		navigation.hide().trigger('menuhide');
	});
	
	$('#navigation > li').hover(function () {
		$(this).addClass('active').find('ul').show().find('li').stop().animate({left:'0'}, 175);
	}, function () {
		$(this).removeClass('active').find('ul li').stop().animate({left:'-105px'}, 75, 'swing', function () { $(this.parentNode).hide(); });
	});
	
	$(".list ul li a").wrapInner("<span></span>");
	
	
	
	/* -- ADVISOR -- */
	
	$('.advisor > li').each(function () {
		
		// Count Items
		var numberItems = $(this).find('li').length;
		if (numberItems > 0 && !$(this).hasClass('availability')) {
			$(this).find('> a').append(' (' + numberItems + ')');
		}
		
		// Wrap inside <span></span>
		$(this).find('> a').wrapInner('<span></span>');
		
		// Store original height metadata
		$(this).find('ul').each(function () {
			$(this).addClass(' {originalHeight:' + $(this).height() + '}');
		});
		
		// Block Clickability if href = #
		var aTag = $(this).find('> a');
		if (aTag.attr('href') == "#") {
			aTag.click(function () {
				this.blur();
				return false;
			});
		}
		
	});
	
	
	$('.advisor > li').hover(function () {
		
		$(this).addClass('active');
		
		var ulElement = $(this).find('ul');
		
		if (ulElement && ulElement.length > 0) {
			ulElement.height('0px').stop();
			ulElement.animate({height:ulElement.metadata().originalHeight + 'px'}, 200, 'swing');
		}
		
	}, function () {
		$(this).removeClass('active').find('ul').stop().animate({height:'0px'}, 200, 'swing');
	});
	
	
});

function animateAdvisor () {
	
	var advisorTopics = $('.advisor > li').hide();
	var topicDelay = 100;
	
	if (advisorTopics.length > 0) {
		
		$('.advisor').css({top:0});
		
		$('#advisorHeader').animate({top:0}, 100, function () {
			
			advisorTopics.each(function (i) {
				$(this).animate({opacity:1.0}, topicDelay*i).fadeIn('fast');
			});
		
		});
		
	}
	
}
