// Start function when DOM has completely loaded 
$(document).ready(function(){ 

	// Open the stock xml file
	$.get("/relaunch09/scripts/tickerimport.php",{},function(xml){
      	
				// Build an HTML string
		topHTMLOutput = '';
		topHTMLOutput += '<span>';
		startHTMLOutput = '';
		startHTMLOutput += '<span style="float: right; width: 90px; font-size: 0.8em;">';
		myHTMLOutput = '';
	 	myHTMLOutput += '<div>';
	  	
		// Run the function for each tag in the XML file
			Name = $(xml).find("name").text();
			CurrentPrice = $(xml).find("CurrentPrice").text();
			Change = $(xml).find("PercentageChange").text();
			Close = $(xml).find("Close").text();
			Volume = $(xml).find("Volume").text();
			High = $(xml).find("High").text();
			Low = $(xml).find("Low").text();
			Day = $(xml).find("Date").text();
			Time = $(xml).find("Time").text();
			// Build row HTML data and store in string
			mydata = BuildHTML(CurrentPrice,Change,Close,Volume,High,Low,Day,Time);
			myHTMLOutput = myHTMLOutput + mydata;
			topdata = BuildTopHTML(CurrentPrice,Change);
			startdata = BuildStartHTML(CurrentPrice,Change);
			topHTMLOutput = topHTMLOutput + topdata;
			startHTMLOutput = startHTMLOutput + startdata;
		topHTMLOutput += '</span>';
		startHTMLOutput += '</span>';
		myHTMLOutput += '</div>';
		
		// Build an HTML string
	  	
			// Build row HTML data and store in string
		
		// Update the DIV called Content Area with the HTML string
		$("#startstockticker").append(startHTMLOutput);
		$("#stockheader").append(topHTMLOutput);
		$("#stockticker").append(myHTMLOutput);
	});
});

function BuildStartHTML(CurrentPrice,Change){

		var newtime ='';
		var time = Time.split(":");
		var hours = time[0];
		var minutes = time[1];
		var newhours = parseInt(hours);

		var PM = minutes.search('pm');
		if (PM == -1 || hours == 12){
			newhours = time[0];
		} else {
			newhours += 12;
		}

		minutes = minutes.slice(0, 2);
		// Build HTML string and return
		topoutput = '';
		topoutput += '(' +  newhours + ':' + minutes + 'h)<br/>';
		topoutput += CurrentPrice + ' &euro; ';
		if(Change >= 0){
		topoutput += '<span class="green">+' + Change + '%</span>';
		}else{
		topoutput += '<span class="red">' + Change + '%</span>';
		}
		return topoutput;
	}
							
function BuildTopHTML(CurrentPrice,Change){
		// Build HTML string and return
		topoutput = '';
		topoutput += CurrentPrice + '&nbsp;&nbsp;';
		if(Change >= 0){
		topoutput += '<span class="green">+' + Change + '</span>';
		}else{
		topoutput += '<span class="red">' + Change + '</span>';
		}
		return topoutput;
	}
								
function BuildHTML(CurrentPrice,Change,Close,Volume,High,Low,Day,Time){
		var newstring = ".";
		var newtime ="";
		var day = Day.replace("/", newstring);
		var day = day.replace("/", newstring);

		var time = Time.split(":");
		var hours = time[0];
		var minutes = time[1];
		var newhours = parseInt(hours);

		var PM = minutes.search('pm');
		if (PM == -1){
			newhours = time[0];
		} else {
			newhours += 12;
		}

		minutes = minutes.replace("pm", newtime);
		minutes = minutes.replace("am", newtime);
		
		// Build HTML string and returns Stockdata
		output = '';
		output += '<div class="right">' + CurrentPrice + '</div>Aktueller Kurs<div class="nofloat"></div>';
		if(Change >= 0){
		output += '<div class="right green">' + Change + '</div>Veränderung<div class="nofloat"></div>';
		}else{
		output += '<div class="right red">' + Change + '</div>Veränderung<div class="nofloat"></div>';
		}
		output += '<div class="right">' + Close + '</div>Vortag<div class="nofloat"></div>';
		output += '<div class="right">' + Volume + '</div>Volumen<div class="nofloat"></div>';
		output += '<div class="right">' + High + '</div>Tageshoch<div class="nofloat"></div>';
		output += '<div class="right">' + Low + '</div>Tagestief<div class="nofloat"></div>';
		output += '<div class="black">' + day + ' um ' + newhours + ':' + minutes + '</div><div class="nofloat"></div>';
		return output;
	}