var $j = jQuery.noConflict();
// Tag Cloud 
$j(document).ready(function() {	

	$j.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase()); 
	if ($j.browser.chrome) { 
		$j('#TagCloud').addClass('chrome'); 
	} else {
		$j('#TagCloud').removeClass('chrome');
	}
		// Tag Opacity Effect 
	$j('.tag.large').delay(200).fadeTo(2800, 0.5);
	$j('.tag.medium').delay(1200).fadeTo(1200, 0.5, function() {
		// complete class added when effect is done
		$j('#TagCloud').addClass('complete');
		// Tooltip Position Settings
		$j('.tag').each(function() {
			// left position setup			
			var tagLeftPos = $j(this).position().left	
			var tooltipWidth = $j(this).prev('.tagTooltip').innerWidth();
			var tagWidth = $j(this).innerWidth();
			var widthTotal = tooltipWidth - tagWidth
			var halfWidth = widthTotal/2	
			// top position	setup		
			var tagTopPos = $j(this).position().top	
			var tooltipHeight = $j(this).prev('.tagTooltip').innerHeight();
			// adds positions to tooltip			
			$j(this).prev('.tagTooltip').css({left:tagLeftPos, top:tagTopPos}).each(function() {
				$j(this).css({left:tagLeftPos-halfWidth, top:tagTopPos-tooltipHeight});
			});
		});
	});	
	
	// Tag Tooltip
	$j('.tagContent').each(function() {
		var tag = $j('.tag', this);
		var tagTooltip = $j('.tagTooltip', this);
		var HideTagDelay = 500;
		var HideTagDelayTimer = null;
		$j('.tag').append('<span class="left border"></span><span class="right border"></span>');
		$j('.border').hide();
		$j([tag.get(0), tagTooltip.get(0)]).mouseover(function () {
			if ($j('#TagCloud').is('.complete')) {
				$j('.tagTooltip, .tag').not(this).addClass('hide');
				tag.removeClass('hide');
				tagTooltip.removeClass('hide');
				$j('#TagCloud .hide:visible').each(function () {
					$j(this).not('.tag').removeClass('hide').fadeOut(200);
					$j(this).not('.tagTooltip').removeClass('hide').fadeTo(200, 0.5).children('span.border').hide();
				});
				if (HideTagDelayTimer) clearTimeout(HideTagDelayTimer);
				if (tagTooltip.is(':animated,:visible')) {
					return;
				} else {
					tag.fadeTo(200, 1.0).children('span.border').fadeIn();			
					tagTooltip.fadeIn(200);		
				}
				return false;
			}
		}).mouseout(function () {
			if ($j('#TagCloud').is('.complete')) {
				if (HideTagDelayTimer) clearTimeout(HideTagDelayTimer);
				HideTagDelayTimer = setTimeout(function () {
					HideTagDelayTimer = null;
					tag.fadeTo(200, 0.5).children('span.border').hide();
					tagTooltip.fadeOut(200);			
				}, HideTagDelay);
				return false;
			}
		});
	});	

	// Tag Layout 
	$j('#TagCloud').masonry({
		columnWidth: 1, 
		itemSelector: '.tag'
	});
});


