var form, result;

$(function() {
	form   = new FormDisplay();
	result = new ResultDisplay();
	
	$('.tooltips').click(function(e){return false;}).tooltip({
		track: true, 
		delay: 0, 
		showURL: false, 
    	showBody: " - ", 
  		fixPNG: true, 
		opacity:0.85, 
		top: -25,
		left:-325
	});
});

function FormDisplay()
{
	/* Assign critical elements by ID */
	this.tab1   = $('#form_tab_1');
	this.block1 = $('#form_body_1');
	this.type1  = $('#property_type_1');
	
	this.condo1 = this.block1.find(".table_row[title='Condo Fee']");
	this.rent1a = this.block1.find(".table_row[title='Rental Income 1']");
	this.rent1b = this.block1.find(".table_row[title='Rental Income 2']");
	
	this.tab2   = $('#form_tab_2');
	this.block2 = $('#form_body_2');
	this.type2  = $('#property_type_2');
	
	this.condo2 = this.block2.find(".table_row[title='Condo Fee']");
	this.rent2a = this.block2.find(".table_row[title='Rental Income 1']");
	this.rent2b = this.block2.find(".table_row[title='Rental Income 2']");
	
	/* Add onclick to tab anchors */
	this.tab1.find('a').click(function(e){form.display(1);return false;});
	this.tab2.find('a').click(function(e){form.display(2);return false;});
	
	/* Add onchange to property_type select boxes */
	this.type1.change(function(){form.changePropertyType(1)});
	this.type2.change(function(){form.changePropertyType(2)});
	
	/* Set the form for the current property_type */
	show = (this.block1.attr('class') == 'hide') ? 2 : 1;
	this.changePropertyType(show);
}

FormDisplay.prototype.display = function(key)
{
	if (key == 2) {
		this.tab1.find('img').attr('src', '/images/calculator/payment_tab_0.gif');
		this.tab2.find('img').attr('src', '/images/calculator/afford_tab_1.gif');
		
		this.block1.hide();
		this.block2.show();
	} else {
		this.tab2.find('img').attr('src', '/images/calculator/afford_tab_0.gif');
		this.tab1.find('img').attr('src', '/images/calculator/payment_tab_1.gif');
		
		this.block2.hide();
		this.block1.show();
	}
}

FormDisplay.prototype.changePropertyType = function(key)
{
	if (key == 2) {
		propertyType = this.type2.val();
		this.type1.val(propertyType);
	} else {
		propertyType = this.type1.val();
		this.type2.val(propertyType);
	}
	
	if (propertyType == 'Condominium') {
		this.rent1a.hide();
		this.rent1b.hide();
		this.condo1.show();
		
		this.rent2a.hide();
		this.rent2b.hide();
		this.condo2.show();
	} else if (propertyType == 'Two Family') {
		this.condo1.hide();
		this.rent1b.hide();
		this.rent1a.show();
		
		this.condo2.hide();
		this.rent2b.hide();
		this.rent2a.show();
	} else if (propertyType == 'Three Family') {
		this.condo1.hide();
		this.rent1a.show();
		this.rent1b.show();
		
		this.condo2.hide();
		this.rent2a.show();
		this.rent2b.show();
	} else {
		this.condo1.hide();
		this.rent1a.hide();
		this.rent1b.hide();
		
		this.condo2.hide();
		this.rent2a.hide();
		this.rent2b.hide();
	}
	
		
	
}

function ResultDisplay()
{
	this.tab1   = $('#result_tab_1');
	this.tab2   = $('#result_tab_2');
	this.tab3   = $('#result_tab_3');
	this.tab4   = $('#result_tab_4');
	this.block1 = $('#result_body_1');
	this.block2 = $('#result_body_2');
	this.block3 = $('#result_body_3');
	this.block4 = $('#result_body_4');
	
	/* Add onclick to tab anchors */
	this.tab1.find('a').click(function(e){result.display(1);return false;});
	this.tab2.find('a').click(function(e){result.display(2);return false;});
	this.tab3.find('a').click(function(e){result.display(3);return false;});
	this.tab4.find('a').click(function(e){result.display(4);return false;});
}

ResultDisplay.prototype.display = function(key)
{
	switch (key) {
		case 4:
			this.tab1.find('img').attr('src', '/images/calculator/results_with_mass_tab_0.gif');
			this.tab2.find('img').attr('src', '/images/calculator/complete_pay_sched_tab_0.gif');
			this.tab3.find('img').attr('src', '/images/calculator/vs_30yr_tab_0.gif');
			this.tab4.find('img').attr('src', '/images/calculator/next_steps_tab_1.gif');
			
			this.block1.hide();
			this.block2.hide();
			this.block3.hide();
			this.block4.show();
			break;
		case 3:
			this.tab1.find('img').attr('src', '/images/calculator/results_with_mass_tab_0.gif');
			this.tab2.find('img').attr('src', '/images/calculator/complete_pay_sched_tab_0.gif');
			this.tab3.find('img').attr('src', '/images/calculator/vs_30yr_tab_1.gif');
			this.tab4.find('img').attr('src', '/images/calculator/next_steps_tab_0.gif');
			
			this.block1.hide();
			this.block2.hide();
			this.block4.hide();
			this.block3.show();
			break;
		case 2:
			this.tab1.find('img').attr('src', '/images/calculator/results_with_mass_tab_0.gif');
			this.tab2.find('img').attr('src', '/images/calculator/complete_pay_sched_tab_1.gif');
			this.tab3.find('img').attr('src', '/images/calculator/vs_30yr_tab_0.gif');
			this.tab4.find('img').attr('src', '/images/calculator/next_steps_tab_0.gif');
		
			this.block1.hide();
			this.block3.hide();
			this.block4.hide();
			this.block2.show();
			break;
		default:
			this.tab1.find('img').attr('src', '/images/calculator/results_with_mass_tab_1.gif');
			this.tab2.find('img').attr('src', '/images/calculator/complete_pay_sched_tab_0.gif');
			this.tab3.find('img').attr('src', '/images/calculator/vs_30yr_tab_0.gif');
			this.tab4.find('img').attr('src', '/images/calculator/next_steps_tab_0.gif');
			
			this.block2.hide();
			this.block3.hide();
			this.block4.hide();
			this.block1.show();
			break;
	}
}