// Need to presists the Databits values
var state = 'MA';
var gDataSource = '/sor/js/datasource.asp';

$(function() {
	// Get the initial data and render the table and the hidden chart
	var tmp = getDataBits(state);

	// Hookup the trigger to the modal function here.
	
	// This block is for using Simple-Modal, which has an IE bug because it uses jQuery clone() method
//	$("#chartTrigger").click(function (e) {
//		e.preventDefault();
//		$("#databits_chart").modal();
//	});

	// Using jqModal
	$("#databits_chart_container").jqm({modal: true});

	$("#chartTrigger").click(function (e) {
		e.preventDefault();
		$("#databits_chart_container").jqmShow();
	});

	$("#chartClose").click(function (e) {
		e.preventDefault();
		$("#databits_chart_container").jqmHide();
	});
});

// This is the Data Get + Process + Display function
function getDataBits(state) {
	var gURL = gDataSource + '?state=' + state;
	// Get the XML data from the data generator
	$.get(gURL, function(xml) {
		// Now, convert them into simple form of JSON using XML2JSON extension
		var DataBits = $.xml2json(xml);

		// Authorization failure code can go into here
		
		// Clear the table 1st
		clearDataBitsTable();
		
		// Populate the table header
		var tmp = "<tr><th colspan='2'>" + DataBits.DataHeader + "</th></tr>";
		// Put in the header from the XML and then get rid of the blank table header
		$("#databits_data_table thead tr:first").after(tmp).remove();
		// Now Label
//		tmp = "<tr><th>Time Period</th><th>Value</th></tr>";
//		$("#databits_data_table thead tr:last").after(tmp);
		// One more header that needs to go to the very top
		tmp = "<tr><th colspan='2'>TWG Market Statistics</th></tr>";
		$("#databits_data_table thead tr:first").before(tmp);
		
		// Now, push the data into the table		
		for (var i=0; i< DataBits.Data.length; i++) {
			tmp = "<tr><td width='25%'>" + DataBits.Data[i].TIMEPERIOD + "</td><td width='75%' align='right'>" + DataBits.Data[i].VALUE + "</td></tr>";
			$("#databits_data_table tbody tr:last").before(tmp);
		}
		// Remove that 1st/initial blank space
		$("#databits_data_table tbody tr:last").remove();

		//// Styling the Table here 
		// Now, put the css back onto the table
		$("#databits_data_table thead tr:first").addClass("databits_mkt_header");
//		$("#databits_data_table thead tr:nth-child(2)").addClass("databits_header");	
		$("#databits_data_table thead tr:last").addClass("databits_label");
		// even and odd are flipped right now cuz how it's being pushed into table
//		$("#databits_data_table tbody tr:odd td:nth-child(1)").addClass("databits_data_even_noleftborder");
//		$("#databits_data_table tbody tr:odd td:nth-child(2)").addClass("databits_data_even");
//		$("#databits_data_table tbody tr:even td:nth-child(1)").addClass("databits_data_odd_noleftborder");
//		$("#databits_data_table tbody tr:even td:nth-child(2)").addClass("databits_data_odd");
		$("#databits_data_table tbody tr:odd td").addClass("databits_data_even");
		$("#databits_data_table tbody tr:even td").addClass("databits_data_odd");
		
		// Now put the data into XMLStr for Fusion Chart
		var strXML, DollarSign;
		//<chart> element
		//Added animation parameter based on animate parameter			
		//Added value related attributes if show value is selected by the user
		DollarSign = checkDollar(DataBits.Data[0].VALUE.toString());
		//
//		strXML = "<chart caption='" + DataBits.DataHeader + "' formatNumberScale='0' numberPrefix='" + DollarSign + "' animation='1' showValues='1' rotateValues='1' placeValuesInside='1' >";
		strXML = "<chart formatNumberScale='0' numberPrefix='" + DollarSign + "' animation='0' showValues='1' rotateValues='1' placeValuesInside='1' >";
		//Store <categories> and child <category> elements
		strXML = strXML + "<categories>"
		for (var i=0;i<DataBits.Data.length;i++) {
			strXML = strXML + "<category name='" + DataBits.Data[i].TIMEPERIOD.toString() + "' />";
		}
		strXML = strXML + "</categories>";
		strXML = strXML + "<dataset seriesName='" + DataBits.DataHeader + "' >";			
		//Create set elements
		for (var i=0;i<DataBits.Data.length;i++) {
			strXML = strXML + "<set value='" + cleanup4FC(DataBits.Data[i].VALUE.toString()) + "' />";
		}
		//Close <dataset> element
		strXML = strXML + "</dataset>";
		strXML = strXML + "</chart>";

		chart = new FusionCharts("/sor/js/FusionCharts/MSColumn3D.swf", "Chart1Id", "580", "380");
		chart.setDataXML(strXML);
		chart.render("databits_chart");

//		$("#chartTrigger").click(function(event) {
//			showFusionChart(event);
//		});
	});
}

// Reset the Databits Table to some default state so that it can be processed
function clearDataBitsTable() {
	$("#databits_data_table").empty();
	$("<thead><tr><td></td><td></td></tr></thead><tbody><tr><td colspan='2'>&nbsp;</td></tr></tbody>").appendTo("#databits_data_table");
}

//
function showFusionChart(e) {
//	alert("You clicked me!");
//	if ($("#databits_chart_container").is(':hidden')) {
//		$("#databits_chart_container").fadeIn(1200);
//	} else {
//		$("#databits_chart_container").fadeOut(1200);	
//	}
}		
//
function cleanup4FC(str) {
	var tmp1 = str.replace(/\$/g, "");
	var tmp2 = tmp1.replace(/,/g, "");
	return tmp2;
}
//
function checkDollar(str) {
	var i = str.indexOf("\$");
	
	if (i != -1) {
		return "$";
	} else {
		return "";
	}
}


