// JavaScript Document for home page

// server_root must be initialized at the vbey top.
$(function(){
	$("#home-rates a").each(function(i){
		$(this).find("img").mouseover(function(){
			$(this).fadeTo(100,0.5);
		});
		$(this).find("img").mouseout(function(){
			$(this).fadeTo(100,1);
		});
		$(this).click(function(){
			home_rate_selected(this);	
			return false;
		});
	});
	$("#home-loans-links a").each(function(i){
		$(this).find("img").mouseover(function(){
			$(this).fadeTo(100,0.5);
		});
		$(this).find("img").mouseout(function(){
			$(this).fadeTo(100,1);
		});
		$(this).click(function(){
			home_loan_selected(this);	
			return false;
		});
	});
});

function home_rate_selected(elm){
	$("#home-ratedata-wrap div").addClass("hidden");	
	$("#" + $(elm).attr("id").replace('-1','')).removeClass("hidden");
	$("#home-rates img").each(function(i){
		$(this).attr("src", $(this).attr("src").replace("-on.","-off."));
	});	
	$(elm).find("img").attr("src", $(elm).find("img").attr("src").replace("-off.","-on."));
	
}

function home_loan_selected(elm){
	
	$("#home-loans .main div.d").addClass("hidden");	
	$("#" + $(elm).attr("id").replace('-1','')).removeClass("hidden");
	$("#home-loans-links img").each(function(i){
		$(this).attr("src", $(this).attr("src").replace("-on.","-off."));
	});	
	$("#home-loans div.bg").each(function(i){
		var loantype = $(elm).attr("id").split("-")[1];
		if(loantype.indexOf("conforming") > -1 ){
			var side = (i == 0 ? "_l" : "_r");
			$(this).find("img").attr("src", $(this).find("img").attr("src").replace(/[\-]{1,1}[a-zA-Z\_]{1,}[\-]{1,1}/, "-conform" + side + "-"));
		}else if(loantype.indexOf("jumbo") > -1 ){
			var side = (i == 0 ? "_l" : "_r");
			$(this).find("img").attr("src", $(this).find("img").attr("src").replace(/[\-]{1,1}[a-zA-Z\_]{1,}[\-]{1,1}/, "-jumbo" + side + "-"));
		} else {
			$(this).find("img").attr("src", $(this).find("img").attr("src").replace(/[\-]{1,1}[a-zA-Z\_]{1,}[\-]{1,1}/, "-" + loantype + "-"));
		}
	});
	$(elm).find("img").attr("src", $(elm).find("img").attr("src").replace("-off.","-on."));
}

$(function(){
	 $.ajax(	{dataType:"html",
			url:root + "php/ajax_calls.php?cmd=getstockdata&rand="+Math.random(),
			success:processstockdata,error:function(reg,txtstatus,err){
			} });
	
	 $.ajax(	{dataType:"xml",
			url:root + "php/ajax_calls.php?cmd=getbonddata&rand="+Math.random(),
			success:processbonddata,error:function(reg,txtstatus,err){
			} });
	$.ajax(	{dataType:"html",
			url:root + "php/ajax_calls.php?cmd=getmortgagedata&rand="+Math.random(),
			success:processmortgagedata,error:function(reg,txtstatus,err){
			} });
	
});

function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
  var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
  newnumber = ( /[\.]{1,1}[0-9]{1,1}$/.test(newnumber) ) ? newnumber + "0" : newnumber; 
  return newnumber; // Output the result to the form field (change for your purposes)
}

function processmortgagedata(pdata,txtstatus){
	$("#temp_mortgage").append(pdata);
	$("#temp_mortgage tr").each(function(i){
		if (i <= 2){
			for(var j=1; j<=2; j++){
				$("td",$("#mortgagerates tr").eq(i)).eq(j).text( $.trim($("td",$(this)).eq(j).text()) + "" + ((j==1)? "%" : ""));
			}
		}
	});
	$("#ftmortgage td").text("Source: Freddie Mac.");
}

function processstockdata(data,txtstatus){
	//var rx = /<script[\s\S]*?<\/script>/gi;
	//data = data.replace(rx,"");
	//alert("stock data here ");
	$("#temp_stock").append(data);
	//alert("stock data appended");
	var nv = {ref983582:"djia", ref626307:"sp500", ref13756934:"nasdaq"}
	var ar = Array({name:"djia",ref:"983582"},{name:"sp500", ref:"626307"},{name:"nasdaq", ref:"13756934"});
	var refstr = ""
	for (var i= 0; i <=2; i++){
		refstr = refstr + "#ref_" + ar[i].ref + "_l" + ((i==2) ? "" : ",");
	}
	//alert(refstr);
	$("#temp_stock").find(refstr).each(function(i){
		//alert( $(this).html() );
		var id = $(this).attr("id");
		var idx = nv["ref" + id.split("_")[1]];
		$("#" + idx + " td").removeClass("numup numdown");
		$("#" + idx + " td:nth-child(2)").text($(this).text());
		chg = $("#" + id.replace("_l","_c"),$("#temp_stock")).text();
		elm = $("#" + idx + " td:nth-child(3)").eq(0);
		numupdown =  ((chg > 0) ? "numup" : ((chg < 0) ? "numdown" : ""));
		/* elm.text(numupdown.replace("numup","+").replace("numdown","") + 
				$("#" + id.replace("_l","_cp"),$("#temp_stock")).text().replace(")","").replace("(","") 
				);  this was change percentage showing*/
		elm.text(chg);
		elm.addClass( numupdown );
		$("#ftequity td:nth-child(1)").text("Source: Google Finance");
		//$("#ftequity td:nth-child(2)").text("Google Finance");
	})
		
}

function processbonddata(data, txtstatus){
	 var rx = /[0-9\,]{0,10}[\.]{1,1}[0-9]{0,10}/;
	 datalen = parseInt($("BID_CURVE_DATE", $(data)).size());
	 data_list = $("G_NEW_DATE", $(data));
	 data_last =data_list.eq((datalen -1));
	 var k = 1;
	 while( !rx.test(data_last.find("BC_30YEAR").text()) && k <= datalen ){
		k = k + 1;													 
	 	data_last = data_list.eq((datalen -k));
	 }
	 k = k + 1;
	 data_prev = data_list.eq((datalen-k));
	 while( !rx.test(data_prev.find("BC_30YEAR").text()) && k <= datalen){
		k = k + 1;													 
	 	data_prev = data_list.eq((datalen -k));
	 }
	 //bidcurvedate_last = $("BID_CURVE_DATE", $(data)).eq((datalen-1)).text();
	 //$("G_NEW_DATE", $(data)).each(function(i){
	//	if ( $(this).find("BID_CURVE_DATE").text() == bidcurvedate_last ){
			trea30yr = parseFloat(data_last.find("BC_30YEAR").text());
			trea10yr = parseFloat(data_last.find("BC_10YEAR").text());
			trea5yr = parseFloat(data_last.find("BC_5YEAR").text());
			//alert(data_prev.find("BC_10YEAR").text());
			//data_prev = $("G_NEW_DATE", $(data)).eq((datalen-2));
			chg30 = trea30yr - ( parseFloat(data_prev.find("BC_30YEAR").text()) );
			chg10 = trea10yr - ( parseFloat(data_prev.find("BC_10YEAR").text()) );
			chg5 = trea5yr - ( parseFloat(data_prev.find("BC_5YEAR").text()) );
			$("#TREASURYY5 td,#TREASURYY10 td,#TREASURYY30 td").removeClass("numup numdown");
			$("#TREASURYY5 td:nth-child(2)").text(roundNumber(trea5yr,2) + "%");
			$("#TREASURYY10 td:nth-child(2)").text(roundNumber(trea10yr,2) + "%");
			$("#TREASURYY30 td:nth-child(2)").text(roundNumber(trea30yr,2) + "%");
			$("#TREASURYY5 td:nth-child(3)").each(function(i){
				numupdown =  ((chg5 > 0) ? "numup" : ((chg5 < 0) ? "numdown" : ""));
				$(this).text(numupdown.replace("numup","+").replace("numdown","") + roundNumber(chg5,2) );	
				$(this).addClass( numupdown );
			});
			$("#TREASURYY10 td:nth-child(3)").each(function(i){
				numupdown = ((chg10 > 0) ? "numup" : ((chg10 < 0) ? "numdown" : ""));
				$(this).text(numupdown.replace("numup","+").replace("numdown","") + roundNumber(chg10,2));	
				$(this).addClass( numupdown );
			});
			$("#TREASURYY30 td:nth-child(3)").each(function(i){
				numupdown = ((chg30 > 0) ? "numup" : ((chg30 < 0) ? "numdown" : ""));
				$(this).text(numupdown.replace("numup","+").replace("numdown","") + roundNumber(chg30,2) );	
				$(this).addClass( numupdown );
			});
			$("#ftbond td:nth-child(1)").text("Updated " + data_last.find("BID_CURVE_DATE").text() + ", U.S. Treasury");
			//$("#ftbond td:nth-child(2)").text($(this).find("NEW_DATE").text());
	//	}
	 //});
}