$(document).ready(function() {
	
	/* Table formatting */
	$('#products').addClass('js_active');
	//$('#products th .button').wrap('<div></div>').after('<span class=\"column_pointer\"></span>');
	$('#products .monthly_cost .conditions').wrap('<div class=\"bubble_wrap\"></div>');

	$('#products tbody tr').not('.divider').hover(function(){
		$('#products td.position div.number > span').show();
		$('#products tbody tr.hover').removeClass('hover');
		$('.button').removeClass('hover');
		$('#products tbody tr .position .number .clone').remove();
		//$(this).addClass('hover').find('.button').addClass('hover');
		
		/* Row highlight */
		var row = $(this).position();
		$('#row_highlight').show().find('a').unbind('click').external(
		{
		toolbar:    1,
		scrollbars: 1,
		location:   1,
		statusbar:  1,
		menubar:    1,
		resizable:  1,
		width:      800,
		height:     600,
		left:       100,
		top:        150
		}
		).attr('href',$(this).find('.monthly_cost .new_button').attr('href')).css('height',$(this).innerHeight()-3);
		$('#products #row_highlight').css('top',row.top).css('left',row.left).css('height',$(this).innerHeight()-3);

		if (!$.browser.msie || $.browser.version > 6) {
			/* Cufon highlight */
			$(this).find('.position .number .cufon-alt').clone().addClass('clone').appendTo($(this).find('.position .number'));
			Cufon.replace($(this).find('.position .number .clone'));
			$(this).find('td.position div.number > span').eq(0).hide();
			$(this).addClass('hover');
		}
		
	},
	function(){
		$(this).removeClass('hover');
	});
	
	$('#products').hover(function(){
			},
	function(){
		$('#products #row_highlight').hide();
		$('.clone').remove();
		$('.cufon').show();

	});
	//Price info tooltip
	var in_progress=false;
	$('#products .monthly_cost .conditions_link').mouseover(function(){
		if (in_progress==false) {
			in_progress=true;
			$(this).parent().find('.conditions .cost').empty();
			$(this).parent().find('.conditions').prepend($(this).prev('.cost').clone().append('a month')).fadeIn('fast');
		}
	});
	$('#products .monthly_cost .conditions_link').mouseout(function(){
		$(this).parent().find('.conditions').fadeOut('fast',function(){$(this).find('.cost').remove();in_progress=false});
	});
	
	if (!$('#sticky').length || !$('#sticky table thead').length) {
	$('<div id=\"sticky\"><table></table></div>').prependTo('#products').hide().css('height', 'auto');
	$('#products table thead').clone().appendTo('#sticky table');
	$('<div id="sticky_shadow"></div>').appendTo('#sticky').hide();
	}

	$('#products table').stickyheader();
	productPopup();
});

function productPopup() {
	$('.more_info').addClass('open');
	/* Product info pop up */
	$('.more_info.open').fancybox({
	'width':735,
	'height':453,
	'onStart': function(){
		$('.product_info.product').show();
		$('#fancy_bg').addClass('info_box');
		$('#info_lightbox').css('position','static');
		$('#fancy_close').empty().append('Close<span></span>');
		$('#sticky').hide();
		$('.product_thumbnail .provider_logo').eq(1).clone().removeClass('hidden').appendTo('#image_wrapper');
		$('#lightbox_nav img').click(function(){
			$('#info_holder').fadeOut();
			$('#image_wrapper').empty();
			$(this).next().clone().removeClass('hidden').appendTo('#image_wrapper');
			var slide = $(this).parent().attr('class').substring(1);
			$('.product_info').hide();
			$('#'+slide+'.product_info').fadeIn();
			$('#info_holder').fadeIn();
			return false;
		});
		$("#fancy_ajax a[href^='/go/']").external({
			toolbar:    1,
			scrollbars: 1,
			location:   1,
			statusbar:  1,
			menubar:    1,
			resizable:  1,
			width:      800,
			height:     600,
			left:       100,
			top:        150
		});

	},
	'onClosed': function(){
			$('#products table').stickyheader();
			$('#sticky').hide();
			}
	});
}

