$(document).ready(function () {
	check_comparison_readiness();
	$('.compare a').click( function (e) {
		e.preventDefault();
		flyout_dropdown($(this));
	});
	$('#bankingTypes a').click(function (e) {
		e.preventDefault();
		var serviceID = $(this).attr('href').replace(/.*serviceIDs=[^,]+,([^&]+)/, "$1");
		var cellIndex = parseInt($(this).parent().parent().parent().parent().attr('cell'));
		compare(cellIndex, serviceID);
	});
	if ($('.banking th').length < 3) { // only one service
		$(".banking .compare[cell='2']").hide(); // hide compare arrow
	} else { // 2 services
		$('.colTwo .articleContent').html( $('#noCoparison').html() ); // show compare instructions
	}
});

function check_comparison_readiness() {
	var limit = 2;
	$("form[name='banking']").submit(function (e) {
		if ($("input[name='toCompare\[\]']:checked").length != limit) {
			alert("Please select " + limit + " services to compare.");
			e.preventDefault();
			return false;
		}
	});
	$("input[name='toCompare\[\]']").click(function() {
		if ( $("input[name='toCompare\[\]']:checked").length > limit ) {
			alert("You can only compare 2 services at a time.");
			$(this).attr('checked', '');
		}
	});
}

function compare(cellIndex, serviceID) {
	var kidsNum = $('.roundCornersTable tr th').length;
	$(".banking .compare").show();
	if (kidsNum < 3) add_extra_column();
	fill_in_column_content(cellIndex, serviceID);
	$('.compare a').click( function (e) {
		e.preventDefault();
		flyout_dropdown($(this));
	});
	//$('.compare .special').remove();
	$('.colTwo .articleContent').html( $('#noCoparison').html() );
}

function add_extra_column() {
	var i = 0;
	$('.roundCornersTable tr').each(function () {
		if (i == 0) { // header
			$(this).find('th:last').before(document.createElement('th'));
			$(this).children().slice(1,2).html('<span class="compare" cell="1"><a href=""><img src="../images/icons/arrow.gif" border="0" alt="Compare" align="absmiddle" /></a></span>');
			$(this).children().slice(1).css('width', '39%');
			$(this).children().slice(2).css('width', '39%');
			//$(this).children().slice(1).html('hello')
			//alert($(this).children().slice(1).parent().html());
		} else { // reg. cell
			$(this).find('td:last').before(document.createElement('td'));
		}
		i++;
	});
}

function fill_in_column_content(cellIndex, serviceID) {
	var trNum = $('.roundCornersTable tr').length;
	var i = 1;
	$('#bankingTypes').appendTo('body').hide();
	ajaxResponse = $.ajax({
		type: "GET",
		url: "../templates/ajax/get_banking_services.php",
		data: "typeID=<?pv($myBankingServiceID)?>&serviceID=" + escape(serviceID),
		error: function () {
			alert(" - AJAX response problems. Please try again.");
		},
		async: false
	}).responseText;
	var ajaxDataArray = ajaxResponse.split("\n");
	$('.roundCornersTable tr').each(function () {
		if (i == 1) { // header
			$(this).children().slice(cellIndex, cellIndex + 1).html( ajaxDataArray[1] + ' &nbsp; <span class="compare" cell="' + cellIndex + '"><a href=""><img src="../images/icons/arrow.gif" border="0" alt="Compare" align="absmiddle" /></a></span>' );
		} else if (i < trNum) { // regular cell
			$(this).children().slice(cellIndex, cellIndex + 1).html(  ajaxDataArray[i] );
			$(this).children().slice(cellIndex, cellIndex + 1).attr('align', 'center');
		} else { // last cell
			if (ajaxDataArray[i] > '') {
				$(this).children().slice(cellIndex, cellIndex + 1).html('<a href="' + ajaxDataArray[i] + '" target="_blank"><img src="../images/buttons/apply.gif" border="0" alt="Apply" /></a>');
			} else {
				$(this).children().slice(cellIndex, cellIndex + 1).html('&nbsp;');
			}
			$(this).children().slice(cellIndex, cellIndex + 1).attr('align', 'center');
		}
		i++;
	});

}

function flyout_dropdown(objLink) {
	$('#bankingTypes').appendTo(objLink);
	$('#bankingTypes').show('slow', function () {
		$(document).one("click", function() {
			$('#bankingTypes').hide();
			$(document).unbind('click');
		});
	});
}