jQuery(function($){

	var ti_tt, current_partner;
	var msie = $.browser.msie;
	
	// Fixes transparency for IE6 in the About section
	$('#landing-hero h1, #pnl-signup, #pnl-signup .why').ifixpng();
	
	// Hides the tooltip. IE has a bug re-rendering transparency from hidden state, so we move the tooltip left of stage
	function hideTooltip(){
		msie ? $('div.tooltip').css('left','-9999px') : $('div.tooltip').css('display','none');
		$('div.partner-row a.hover').removeClass('hover');
	}
	
	// Basically, if the partners list div exists (only for Partners page)
	if($('#partners-list').length){
		
		// Invoke the tooltips and append to Body
		var tooltip_lrg = $('<div id="tooltip-lrg" class="tooltip"><div class="top"><div class="desc"></div><div class="news"></div></div><div class="btm"></div>').appendTo('body');
		var tooltip_sml = $('<div id="tooltip-sml" class="tooltip"><div class="top"><div class="desc"></div><div class="news"></div></div><div class="btm"></div>').appendTo('body');
		
		// Apply PNG fix to tooltips for IE6
		if(msie) $('.tooltip:visible div').ifixpng();
		
		// Hide the tooltips by default
		hideTooltip();

		// Prevent propagation of mouseover event on tooltip, so it doesn't hide when we roll over it
		$('.tooltip').mouseover(function(e){
			e.stopPropagation();
		});
		
		// Build the tooltip, based on data hidden, associated with each item (the P in class desc for description, the UL for news)
		function buildTooltip(){
			current_partner.addClass('hover');
			var tooltip = null;
			if(current_partner.parents('.pr-lrg').length){
				tooltip = tooltip_lrg;
			}else{
				tooltip = tooltip_sml;
			}
			
			// Build tooltip
			var partner_content = null;
			var partner_news = null;
			var partner_desc = '';
			
			var partner_content = current_partner.parent().clone().html();
			partner_desc = current_partner.parents('.wrap').find('.desc p').text();
			if(partner_desc) partner_content += ' ' + partner_desc;
			var partner_news = current_partner.parents('.wrap').find('.desc ul').html();
			if(partner_news) partner_news = '<h4>Latest News</h4> <ul>' + partner_news + '</ul>';
			
			// Populate tooltip with content
			tooltip.find('.top .desc').empty().append(partner_content);
			tooltip.find('.top .news').empty().append(partner_news);
			
			// Positioning
			tooltip.css({
				top:parseInt(current_partner.offset().top - (tooltip.outerHeight() - 8)),
				left:parseInt(current_partner.offset().left - (tooltip.width() / 2) + (current_partner.width() / 2))
			});
			
			// Show Tooltip, fade in if not IE (Opacity fade issues)
			msie ? tooltip.show() : tooltip.fadeIn("fast");

			// When we roll over the body, let's hide the tooltip
			$('body').unbind("mouseover").mouseover(function(){
				hideTooltip();
				$('body').unbind("mouseover");
			});
			
		}
		
		// When rolling over a partner, wait a little bit (stops tooltip flicker) then invoke the tooltip for that partner
		$('.pr-lrg h3 a, .pr-med h4 a').mouseover(function(e){
			e.stopPropagation();
			hideTooltip();
			current_partner = $(this);
			clearTimeout(ti_tt);
			ti_tt = setTimeout(function(){ buildTooltip(current_partner) },250);
		}).click(function(e){
			e.preventDefault();
		});
		
	}
	
	// Adds hover functionality in Partners page, so both the top supporter and speech bubble is clickable
	$('#interest-promo').hover(function(){
		$(this).css('cursor','pointer').addClass('hover');
	},function(){
		$(this).removeClass('hover');
	}).click(function(e){
		e.preventDefault();
		location.href = $(this).find('a').attr('href');
	});
	
	
});