/** The functions in this file are related to the compare bank rate chart layers */

var activeLink = null;
var oldLink = null;
var contentFile = null;
$(document).ready(function(){
	if (getParam("print") != "y") {
		// Get all the links with dynamic calls
		var actionLinks = $('.action_btn > a');
		applyLinkAction(actionLinks);
	}
});

function applyLinkAction(links){
	if(links.length > 0){
		links.each(function() {
			$(this).click(function(e) {
				if (!$(this).hasClass('active')) {
					e.preventDefault();
					if(null != activeLink){
						$(activeLink).removeClass('active');
					}
					
					var id = $(this).attr("id");
					if (id == "tfgCompare") {
						setTaxFreeDropDown();
					}
					if (id == "gicCompare") {
						unsetTaxFreeDropDown();
					}
					
					if($(this) != $(activeLink)) { activeLink = $(this); }
					activeLink = $(this);
					contentFile = $(this).attr("href");
					selectRow($(this).attr("id"));  
					$.ajax({
						url: contentFile,
						data: "someData=value&example=foo",
						success: handleResponse,
						error: handleError
					});
				} else {
					e.preventDefault();
					if($(this) != $(activeLink)) { activeLink = $(this); }
					$(activeLink).parent().parent().parent().find('.chart_container').css('display', 'none');
					$(activeLink).removeClass('active');
					deselectRow($(this).attr("id"));
				}
			});
		});
	}
}

function handleResponse(html) {
	// this is added to strip out author tags
	var dom = $('<div>' + html + '</div>');
	$(dom).find("link").remove();
	$(dom).find("script").remove();
	$(dom).find("[class^=mgnl]").remove();
	html = $(dom).html();
	
	var container = $(activeLink).parent().parent().parent().find('.chart_container');
	container.find('.chart_content').html(html);
	$(activeLink).addClass('active');
	container.css('display', 'block'); //turn on the correct div
	attachEvents(); 
	if($(activeLink).attr("id") == "cdCompare" || $(activeLink).attr("id") == "gicCompare" || $(activeLink).attr("id") == "tfgCompare"){
		$('.sbox').selectbox({debug: true}); //rebuilds select boxes in product tabs
	}
	
	attachExitLinkEvents();
}

function handleError(){
	var container = $(activeLink).parent().parent().parent().find('.chart_container');
	container.find('.chart_content').html("<p>Information not available</p>");
	$(activeLink).addClass('active');
	container.css('display', 'block');
}

function selectRow(id) {
	var thisBox = id + "Box";
	var leftCol = "rateCol";
	var rightCol = "Rate";
	if(thisBox.indexOf("Compare") != -1) { leftCol = "compareCol"; rightCol = "Compare"; }
	//turn off the border for the chart-box
	$(".chart_content").css("border-top","0px");
	//add border to content boxes except the selected one
	$(".content_box[id*="+rightCol+"]").css("border-bottom-width","1px").css("background-color","#f4f4f4");
	$(".content_box[id*="+thisBox+"]").css("border-bottom-width","0").css("background-color","#FFF");
}
function deselectRow(id) {
	var thisBox = id + "Box";
	var leftCol = "rateCol";
	var rightCol = "Rate";
	if(thisBox.indexOf("Compare") != -1) { leftCol = "compareCol"; rightCol = "Compare"; }
	//turn off the border for the chart-box
	$(".chart_content").css("border-top","1px");
	//add border to content boxes except the selected one
	$(".content_box[id*="+rightCol+"]").removeAttr("style");
}
