$(document).ready(function()
{
	// Add some splits to the navigation items that are too long vertically
	$('a.nav-760').parent().addClass('split');
	
	// Add in help tips
	var helpBox = $('<div class="helpBox"></div>');
	helpBox.load('/_content/Nav-Tips/Health-Cover');
	$('a.nav-760').parent().append(helpBox);

	// Buy now buttons.
	$('.pod_price_container a.pod_buy_button').click(function()
	{
		var _h1 = $('#content h1');
		if( _h1.length > 0 )
		{
			_gaq.push(['_trackEvent', 'BuyButton', _h1[0].innerHTML, 'Clicked']);
			setTimeout('document.location = "' + $(this).attr('href') + '"', 100)
			return false;
		}
	});

	
	// Set up the hover action on the tiered navigation.
	$('#navigaton ul.level1 > li').hover(
		function() {
			var item = $(this);
			var itemLink = item.children('a:first');
			var itemLevel2 = item.children('.level2:first');

			if( itemLink.hasClass('active') )
			{
				itemLink.addClass('activeHover').removeClass('active');
			}
			item.addClass('hover');

			if( itemLevel2.length > 0 )
			{
				if( itemLevel2.find('.level3').length == 0 )
				{
					// For display purposes, set the width of the level2 unordered list to be at least 10 pixels wider than the level 1 parent item.
					itemLevel2.css('min-width', parseInt(item.width() + 10, 10) + 'px');

					// Disable floating of level 2 items so they are all the same width and a bottom border can be applied.
					itemLevel2.children().css('float', 'none').css('border-bottom', 'solid 1px #d6d6d6');
				}

				// Add rounded corners support for IE via the htc file using the css behavior property.
				// This has to be applied here rather than in the stylesheet because of a rendering issue in IE8 that causes the rendered rounded corners to appear for a split second in the incorrect position and then reposition themselves.
				//itemLevel2.css('behavior', 'url(/_stylesheet/PIE.htc)');

				// Fade in the level 2 children.
				itemLevel2.show(); //fadeIn('fast');
			}
			else
			{
				item.addClass('nochildren');
			}

			// see comments below
			//item.css('behavior', 'url(/_stylesheet/PIE.htc)');
		},
		function() {
			var item = $(this);
			var itemLink = item.children('a:first');
			var itemLevel2 = item.children('.level2:first');

			item.removeClass('hover').removeClass('nochildren');
			if( itemLink.hasClass('activeHover') )
			{
				itemLink.addClass('active').removeClass('activeHover');
			}
			// Clear the behavior css property.
			//item.css('behavior', '');	

			if( itemLevel2.length > 0 )
			{
				itemLevel2.hide();
				// Clear the behavior css property.
				itemLevel2.css('behavior', '');				
			}
		}
	);
});

/*
 * Functions to open Product Selectors
 *
 */
function open_ps_help_me_choose()
{
	window.open("/PS/Help-Me-Choose-Questions/", "ps_window", "width=1000, height=810, location=no, status=yes, scrollbars=yes, resizable=yes, toolbar=no");
}

function open_ps_join_now(vCode)
{
	// Log click with google analytics
	pageTracker._trackPageview('/PS/Already-Know');

        var post_data = '';
        if( vCode != undefined )
        {
             post_data = '?ps_product=' + vCode;
        }

	// Open product selector.
	window.open("/PS/Already-Know-Questions/" + post_data, "ps_window", "width=1000, height=810, location=no, status=yes, scrollbars=yes, resizable=yes, toolbar=no");
}

function open_ps_combined_cover()
{
	var vCode = undefined;

	var hospitalCover = $("input[name='hospital']:radio:checked").val();
	var necessitiesCover = $("input[name='necessities']:radio:checked").val();

	if( (undefined == hospitalCover) && ('lite' == necessitiesCover) )
	{
		vCode = 'v01';
	}
	else if( (undefined == hospitalCover) && ('necessities' == necessitiesCover) )
	{
		vCode = 'v02';
	}
	else if( (undefined == hospitalCover) && ('complete' == necessitiesCover) )
	{
		vCode = 'v03';
	}
	if( ('silver' == hospitalCover) && (undefined == necessitiesCover) )
	{
		vCode = 'v04';
	}
	else if( ('silver' == hospitalCover) && ('lite' == necessitiesCover) )
	{
		vCode = 'v05';
	}
	else if( ('silver' == hospitalCover) && ('necessities' == necessitiesCover) )
	{
		vCode = 'v06';
	}
	else if( ('silver' == hospitalCover) && ('complete' == necessitiesCover) )
	{
		vCode = 'v07';
	}
	else if( ('gold' == hospitalCover) && (undefined == necessitiesCover) )
	{
		vCode = 'v08';
	}
	else if( ('gold' == hospitalCover) && ('lite' == necessitiesCover) )
	{
		vCode = 'v09';
	}
	else if( ('gold' == hospitalCover) && ('necessities' == necessitiesCover) )
	{
		vCode = 'v10';
	}
	else if( ('gold' == hospitalCover) && ('complete' == necessitiesCover) )
	{
		vCode = 'v11';
	}

	open_ps_join_now(vCode);
}

function compare_products_popup( element )
{
    // compare buttons 
    element.fancybox({
		'overlayShow': true,
		'overlayOpacity': 0.9,
		'autoDimensions': false,
		'width': 800,
		'height': '95%',
		'zoomSpeedIn': 0,
		'zoomSpeedOut': 0,
		titleShow: false,
		'onComplete': function()
		{

			// if the scrollheight is smaller or equal to the hover size then do nothing but add the styles.
			if(  $('.hover').height() < $('#fancybox-content > div').height() )
			{
				$('.hover table').addClass('styled').addClass('curved_corners');
				return;
			}

			// Height of the table is determined by the size of the hover - 
			var _contentHeight = $('.hover').height() - $('.hover table').outerHeight(true);

			// the table needs to be the window height - the content height.
			var _tableHeight = $('#fancybox-content > div').height() - _contentHeight - 60;

			$('.hover table').tableScroll({height: _tableHeight, width: $('.hover').width()}); 

			setTimeout(function()
			{
				var _columns = $('.tablescroll_head th');
				var _bodyColumns = $('.tablescroll_body tbody > tr td');

				_columns.each(function(index)
				{
					if( index < _columns.length - 1 )
					{
						var _column = $(this).width(
						$(_bodyColumns[index]).outerWidth() - parseInt($(this).css('paddingRight')) - parseInt($(this).css('paddingLeft')) - 2 );
					}
					else
					{
						$(this).width('auto');
					}
				});
			}, 200);
		}
	});
}
