// func to populate tables
function getTable(identify){
	// show loading
	$("#"+identify+"_loading").show();
	$.ajax({
		type: "GET",
		url: "../xml/"+identify+".xml",
		dataType: "xml",
		success: function(xml) {			
			// to handle rowspan entries - create a temp value for the first row
			var titleRow = "";
			var firstRow = "";
			var firstRow_dd = "";
			var firstRow_sd = "";
			var show_dd = true;
			var show_sd = true;
			var show_24 = false;
			var secondRow = "";
			// populate the table
			$(xml).find('record').each(function(){
				// get text
				var market_text = $(this).find('market').text();
				var exchange_hours_text = $(this).find('exchange_hours').text();
				var quoting_hours_text = $(this).find('quoting_hours').text();
				var max_ostake_text = $(this).find('max_ostake').text();
				var hours_text = $(this).find('hours').text();
				var underlying_text = $(this).find('underlying').text();
				var min_imr_text = $(this).find('min_imr').text();
				var max_cgsl_text = $(this).find('max_cgsl').text();
				var spread_text = $(this).find('spread').text();
				var contract_text = $(this).find('contract').text();
				var dealing_day_text = $(this).find('dealing_day').text();
				var settlement_text = $(this).find('settlement').text();
				
				if (titleRow ==""){ // title row?
					
					titleRow+= '<table><thead><tr>';
					titleRow+= '<th style="width:60px">'+tidyString(market_text)+'</th>';
					titleRow+= '<th style="width:70px">'+tidyString(exchange_hours_text)+'</th>';
					titleRow+= '<th style="width:70px">'+tidyString(quoting_hours_text)+'</th>';
					
					//alert("MOS"+max_ostake_text);
					if (max_ostake_text !=""){
						titleRow+= '<th>'+tidyString(max_ostake_text)+'</th>';
					}
					
					temp24 = '<th>'+tidyString(hours_text)+'</th>';
					
					tempTitle = '<th>'+tidyString(underlying_text)+'</th>';
					tempTitle+= '<th>'+tidyString(min_imr_text)+'</th>';
					tempTitle+= '<th>'+tidyString(max_cgsl_text)+'</th>';
					tempTitle+= '<th>'+tidyString(spread_text)+'</th>';
					tempTitle+= '<th>'+tidyString(contract_text)+'</th>';
					tempTitle+= '<th style="width:130px">'+tidyString(dealing_day_text)+'</th>';
					tempTitle+= '<th style="width:130px">'+tidyString(settlement_text)+'</th>';
					tempTitle+= '</tr></thead><tbody>';
				
				} else if (firstRow == ""){ // is this the first row? Hold in temp
					firstRow = '<tr>';
					firstRow+= '<td>'+tidyString(market_text)+'</td>';
					firstRow+= '<td>'+tidyString(exchange_hours_text)+'</td>';
					firstRow+= '<td>'+tidyString(quoting_hours_text)+'</td>';
					if (max_ostake_text !=""){
						firstRow+= '<td>'+tidyString(max_ostake_text)+'</td>';
					}
					if (hours_text.length > 0 ){ // if there's 24 hours text - show it
						// sort out the table titles
						titleRow+=temp24;
						// sort out the first row
						firstRow+= '<td>'+tidyString(hours_text)+'</td>';
						show_24 = true;
					}
					// sort out the table titles
					titleRow+=tempTitle;
					tableHtml = titleRow;
					//
					firstRow+= '<td>'+tidyString(underlying_text)+'</td>';
					firstRow+= '<td>'+tidyString(min_imr_text)+'</td>';
					firstRow+= '<td>'+tidyString(max_cgsl_text)+'</td>';
					firstRow+= '<td>'+tidyString(spread_text)+'</td>';
					firstRow+= '<td>'+tidyString(contract_text)+'</td>';
					// hold the two to check in variables
					firstRow_dd = tidyString(dealing_day_text);
					firstRow_sd = tidyString(settlement_text);
				} else if (secondRow ==""){ // check for blank row content
					secondRow = "done";
					if (dealing_day_text.length <1 ){ // if there's no content then have the first entry span all rows
						firstRow+= '<td rowspan="100">'+firstRow_dd+'</td>';
						show_dd = false;
					} else { // just enter row as normal
						firstRow+= '<td>'+firstRow_dd+'</td>';
					}
					if (settlement_text.length <1 ){ // if there's no content then have the first entry span all rows
						firstRow+= '<td rowspan="100">'+firstRow_sd+'</td>';
						show_sd = false;
					} else { // just enter row as normal
						firstRow+= '<td>'+firstRow_sd+'</td>';
					}
					firstRow+= '</tr>';
					// add to tableHtml
					tableHtml+= firstRow;
					// now add current row
					tableHtml+= '<tr>';
					tableHtml+= '<td>'+tidyString(market_text)+'</td>';
					tableHtml+= '<td>'+tidyString(exchange_hours_text)+'</td>';
					tableHtml+= '<td>'+tidyString(quoting_hours_text)+'</td>';
					if (max_ostake_text !=""){
						tableHtml+= '<td>'+tidyString(max_ostake_text)+'</td>';
					}
					if (show_24){
						tableHtml+= '<td>'+tidyString(hours_text)+'</td>';
					}
					tableHtml+= '<td>'+tidyString(underlying_text)+'</td>';
					tableHtml+= '<td>'+tidyString(min_imr_text)+'</td>';
					tableHtml+= '<td>'+tidyString(max_cgsl_text)+'</td>';
					tableHtml+= '<td>'+tidyString(spread_text)+'</td>';
					tableHtml+= '<td>'+tidyString(contract_text)+'</td>';
					if (show_dd){
						tableHtml+= '<td>'+tidyString(dealing_day_text)+'</td>';
					}
					if (show_sd){
						tableHtml+= '<td>'+tidyString(settlement_text)+'</td>';
					}
					tableHtml+= '</tr>';
				} else { // add rest to table
					tableHtml+= '<tr>';
					tableHtml+= '<td>'+tidyString(market_text)+'</td>';
					tableHtml+= '<td>'+tidyString(exchange_hours_text)+'</td>';
					tableHtml+= '<td>'+tidyString(quoting_hours_text)+'</td>';
					if (max_ostake_text !=""){
						tableHtml+= '<td>'+tidyString(max_ostake_text)+'</td>';
					}
					if (show_24){
						tableHtml+= '<td>'+tidyString(hours_text)+'</td>';
					}
					tableHtml+= '<td>'+tidyString(underlying_text)+'</td>';
					tableHtml+= '<td>'+tidyString(min_imr_text)+'</td>';
					tableHtml+= '<td>'+tidyString(max_cgsl_text)+'</td>';
					tableHtml+= '<td>'+tidyString(spread_text)+'</td>';
					tableHtml+= '<td>'+tidyString(contract_text)+'</td>';
					if (show_dd){
						tableHtml+= '<td>'+tidyString(dealing_day_text)+'</td>';
					}
					if (show_sd){
						tableHtml+= '<td>'+tidyString(settlement_text)+'</td>';
					}
					tableHtml+= '</tr>';
				}
				
			});
			
			// end the table HTML
			tableHtml+= '</tbody></table>';
			// if binary markets show the asterix Text 
			if (identify == "binary_markets"){
				tableHtml+= "<p>&#42; The 'Min IMR' is the buy price paid if you have bought a binary. Otherwise the Min IMR is 100-sell price if you have sold the binary.</p>";
				tableHtml+= "<p>&#42; Maximum position size per market is &pound;20. However, if you trade in two markets that give you the same effective exposure, then the maximum position size will be the sum of both these up to &pound;20. For example, buying &pound;10 wall street down, and selling &pound;10 wall street up will mean the &pound;20 position limit will have been reached.</p>";
			}
			//
			$("#"+identify+"_loading").hide();
			$("#"+identify+"_show").empty().append(tableHtml);
			$("tr:nth-child(even)").addClass("odd");
			// add mouseover events
			
			$("tr").mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});
			$("#"+identify+"_show").show();
		
			// move to it's anchor
			$.scrollTo("#"+identify,800);
			$("#"+identify+"_show a").click(function(){
				$.scrollTo(this.hash, 800);
				return false;
			});
		}
	});
}
//
function tidyString(theString){
	theString = theString.replace('&', '&amp;');
	theString = theString.replace('£', '&pound;');
	return theString;
}

//
// when ready start it
$(document).ready(function() {
	$(".titlebar").click(function() {
		if ($("#"+this.id+"_show").is(":visible")){
			$("#"+this.id+"_show").toggle();
		} else {
			getTable(this.id);
		}
		$("#"+this.id+" img").toggle();
	});
});