
function calcDownPayment(){
    var down = $("#ListPrice").val()* ($(this).val() / 100);
    $("#DownPayment").val(down.toFixed(0));
}
function calcPayments(){
    //check for required fields
    if($("#ListPrice").val() == '' || $("#ListPrice").val() == 0) { alert('Please Enter A Price!'); $("#ListPrice").focus(); return false; }
    if($("#InterestRate").val() == '' ) { alert('Please Enter An Interest Rate!'); $("#InterestRate").focus(); return false; }

    var total = ($("#ListPrice").val() - $("#DownPayment").val())*1; //Principal Required
    var r = ($("#InterestRate").val() / 100) / $("#PaymentFreq").val(); // effective rate
    var n = $("#PaymentFreq").val() * $("#Amortization").val();  // number of payments
    var a = [total*Math.pow((1+r),n)*r] / [Math.pow((1+r),n)-1]; //calc monthly payment
    $('#MortgageAmount').html('$' + total.toFixed(0));
    $('#MonthlyPayment').html('$' + a.toFixed(0));
    $('#TotalPayments').html(n);
}
var map = null;
var currentTab = null;
var pinID = 1;
function init(openTab)
{
	if (!openTab) { openTab = 'mainDescription'; }
    $("#tabs li.tab a").bind("click",
        function() {
            var UrlSegments = $(this).attr('href').split('/');
            loadTab(UrlSegments[UrlSegments.length - 1]);
            this.blur();
            return false;
        });

    loadTab(openTab);
}
function loadTab(name) {
    
    if (currentTab != 'mainDescription')
    	saveTab(currentTab);
    $("#tabs li.tab").removeClass("current");
    $('#tabs li#' + name + '_tab').addClass("current");
    $("#tab_group #ContentHolder").html($("#" + name + " div.contents").html());
    $("#" + name + " div.contents a.thickbox").attr("rel", "");
	$("#tab_group #ContentHolder a.thickbox").click(TB_CustomInit);
    var funcName = 'init_' + name;
    try {
        this[funcName]();
    } catch(e) {
    }
    currentTab = name;
}
function TB_CustomInit() {
	var t = this.title || this.name || null;
	var g = this.rel || false;
	TB_show(t,this.href,g);
	this.blur();
	return false;
}
function saveTab(name) {
    $("#" + name + " div.contents").html($("#tab_group #ContentHolder").html());
}
function init_mainDescription() {
    $(".thumbnail").hover( function() { 
    	$("#ViewPort a").href($(this).href());
    	$("#ViewPort img").attr('src', $("img", this).attr('src'));
    	/*$("#ViewPort a").unclick();
    	$("#ViewPort a").click(TB_CustomInit);*/
    }, 
    	function() {});
}
function init_calculator() {
    $("#QuickSelect").bind("change", calcDownPayment);
    $("#MortgageCalc form").bind("submit", function() { calcPayments(); return false; });
}

function init_map() {
	if ($("#map div.contents").html() == "") {
		var url = '/residential-listings/map/' + mapLongitude + '/' + mapLatitude;
		$("#ContentHolder").html('<div id="map_loading" style="height: 100px; width: 515px; text-align: center; pading-top: 100px;"><img src="/chaseshuswap/images/ajax-loader.gif" /></div><iframe id="map_iframe" src="'+ url +'" style="height: 320px; width: 515px;" frameborder="0" scrolling="no"></iframe>');		
	}
}
function map_loaded() {
	$("#map_loading").addClass("hidden");
}

