/* Author:
stayce kavanaugh, mystayce.com
www.bareapps.com
*/

$(document).ready(function() {
	//When page loads...
	$(".page_content").hide(); //Hide all content
    $("#container").show();
	$("ul.menu li:first").addClass("active hidden"); //Activate first tab
	$(".page_content:first").show(); //Show first tab content

	//On Click Event
	$("ul.menu li, .fade_content").click(function() {
		$("p.copy").addClass("hidden"); //Remove any "home" class
		$("ul.menu li").removeClass("active"); //Remove any "active" class
		$("ul.menu li, .hideHome").removeClass("hidden"); //Remove any "home" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".page_content").hide(); //Hide all tab content
		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID page content
		return false;
	});
	$("ul.menu li#home_link").click(function() {
		$('p.copy').removeClass('hidden'); //remove "hidden" class to copyright
		$('.hideHome').addClass('hidden'); //remove "hidden" class to copyright
		$(this).addClass('hidden'); //Add "hidden" class to selected tab
		$('ul.menu').css('padding-left', '55px'); // make ul padding left 55 to center the menu
	});
	
	$('#refresh_rates').live('click', function() {
		get_treas_rates();
		$('#rates').fadeOut();
		$('#rates').fadeIn();
		return false;
	});
	
	get_treas_rates();

});



var get_treas_rates = function() {
	var currentTimeAndDate = new Date();
	currentTimeAndDate.format("m/d/yy, h:MM TT Z");
	
	$.ajax({
	  type: "GET",
	  url: "/xml2json.php",

	  // change dataType to 'text' so that jquery doesn't try to parse xml
	  dataType: "text",
	  success: function(xml) {
		
		var 
		
		two_yr_swap				= $(xml).find('TWO_YR').text(),
		two_yr_swap_n			= $(xml).find('cb\\:value').first().text(),
		three_yr_swap_n			= $(xml).find('cb\\:value').text(),
		
		three_yr_swap		= $(xml).find('THREE_YR').text(),
		five_yr_swap		= $(xml).find('FIVE_YR').text(),
		seven_yr_swap		= $(xml).find('SEVEN_YR').text(),
		ten_yr_swap			= $(xml).find('TEN_YR').text(),
		swap_release_date	= $(xml).find('DATE').text(),
		
		libor_30_day 		= $(xml).find('LIBOR_30DAY').text(),
		libor_90_day		= $(xml).find('LIBOR_90DAY').text(),
		libor_180_day		= $(xml).find('LIBOR_180DAY').text(),
		prime_rate			= $(xml).find('PRIME_RATE').text(),
		
			recent_two_yr		= $(xml).find('BC_2YEAR').last().text(),
			recent_three_yr		= $(xml).find('BC_3YEAR').last().text(),
			recent_five_yr		= $(xml).find('BC_5YEAR').last().text(),
			recent_seven_yr		= $(xml).find('BC_7YEAR').last().text(),
			recent_ten_yr		= $(xml).find('BC_10YEAR').last().text();
			
			
		$('#treas_2_yr ').text(recent_two_yr);	
	    $('#treas_3_yr ').text(recent_three_yr);
		$('#treas_5_yr ').text(recent_five_yr);
		$('#treas_7_yr ').text(recent_seven_yr);
		$('#treas_10_yr').text(recent_ten_yr);
		
		// LIBOR & PRIME RATES FROM WSJ
		$('#libor_30_day ').text(libor_30_day);
		$('#libor_90_day ').text(libor_90_day);
		$('#libor_180_day ').text(libor_180_day);
		$('#prime_rate ').text(prime_rate);
		
		//SWAP RATES
		$('#swap_2_yr ').text(two_yr_swap);
		$('#swap_3_yr ').text(three_yr_swap);
		$('#swap_5_yr ').text(five_yr_swap);
		$('#swap_7_yr ').text(seven_yr_swap);
		$('#swap_10_yr').text(ten_yr_swap);
		$('#x_treas_timestamp').text(swap_release_date);
		
		console.log(two_yr_swap_n);
		$('span#x_timestamp').html(currentTimeAndDate.format("m/d/yy, h:MM TT Z")).fadeIn();
		
		//format the treas numbers
		
		FormatRates();
	  },
	  error: function(xhr, testStatus, error) {
		alert('Treasury data currently unavailable.');
	  }
	});
};

var FormatRates = function() {
	
	var ratesCollection = $('.rate');
	
	$(ratesCollection).each(function(){
		var rateUnformated		= $(this).text().replace(/[^\d.,]+/,'');
		var rateFormated 		= parseFloat(rateUnformated).toFixed(2);
		$(this).text(rateFormated);
	});
	
	$('.treas-rate').each(function(){
		var treasrateUnformated	= $(this).text().replace(/[^\d.,]+/,'');
		var treasrateFormated 	= parseFloat(treasrateUnformated).toFixed(3);
		$(this).text(treasrateFormated);
	});
	
}







