// File: readXML.js
var $j = jQuery.noConflict();


// Start function when DOM has completely loaded 
$j(document).ready(function(){ 



	// Open the xml file
	$j.get("/tour-import/tour.xml",{},function(xml){
      	
		// Build an HTML string
		myHTMLOutput = '';
	 	myHTMLOutput += '<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tourdates" id="event-list">';
	  	//myHTMLOutput += '<th>Date</th><th>Location</th><th>Venue</th>';
	  	$jcount=0;
		// Run the function for each show tag in the XML file
		$j('show',xml).each(function(i) {
			if ($jcount < 10){
			showRecordKey = $j(this).find("recordKey").text();
			showCity = $j(this).find("city").text().substring(0, 250);
			showState = $j(this).find("stateAbbreviation").text();
			showCountry = $j(this).find("country").text();
			showDate = $j(this).find("date").text();
			showVenue = $j(this).find("venueName").text().substring(0, 250);
			showVenueAddress = $j(this).find("venueAddress").text();
			showVenueZip = $j(this).find("venueZip").text();
			showVenuePhone = $j(this).find("venuePhone").text();
			showVenueURI = $j(this).find("venueURI").text();
			showTicketURI = $j(this).find("ticketURI").text();
			showTicketPrice = $j(this).find("ticketPrice").text();
			showTimeSet = $j(this).find("timeSet").text();
			showTimeDoors = $j(this).find("timeDoors").text();
			showDescription = $j(this).find("description").text();
			showAgeLimit = $j(this).find("ageLimit").text();
			
			// Build row HTML data and store in string
			mydata = BuildshowHTML(showRecordKey,showCity,showState,showCountry,showDate,showVenue,showVenueAddress,showVenueZip,showVenuePhone,showVenueURI,showTicketURI,showTicketPrice,showTimeSet,showTimeDoors,showDescription,showAgeLimit);
			myHTMLOutput = myHTMLOutput + mydata;
			}
			$jcount++;
		});
		myHTMLOutput += '</table>';
		
		// Update the DIV called Content Area with the HTML string
		$j("#tour_content").append(myHTMLOutput);
	});
});
 
 
 
 function BuildshowHTML(showRecordKey,showCity,showState,showCountry,showDate,showVenue,showVenueAddress,showVenueZip,showVenuePhone,showVenueURI,showTicketURI,showTicketPrice,showTimeSet,showTimeDoors,showDescription,showAgeLimit){
	
	// Check to see if their is a "post" attribute in the name field
	if ((showTimeSet) != undefined){
		showTimeSetHTML = "<strong>(" + showTimeSet + ")</strong>";
	}
	else
	{
		showTimeSetHTML = "";
	}



showTimeSetArray = showTimeSet.split(":");

atoj1= showTimeSetArray[0];
atoj2 = showTimeSetArray[1];
atoj3= showTimeSetArray[2];

if(atoj1==0){atoj4="am";atoj1=12}
else if(atoj1 <= 11){atoj4="am"}
else if(atoj1 == 12){atoj4="pm";atoj1=12}
else if(atoj1 >= 13){atoj4="pm";atoj1-=12}

showTimeSetHTML = ""+atoj1+":"+atoj2+atoj4+"";






var monthnames = new Array();
monthnames[1]="JAN";
monthnames[2]="FEB";
monthnames[3]="MAR";
monthnames[4]="APR";
monthnames[5]="MAY";
monthnames[6]="JUNE"
monthnames[7]="JUL";
monthnames[8]="AUG";
monthnames[9]="SEP";
monthnames[10]="OCT";
monthnames[11]="NOV";
monthnames[12]="DEC";

showDateArray = showDate.split("-");

theday = showDateArray[2];
themonth = parseInt( showDateArray[1], 10 );
theyear = showDateArray[0];


//var formattedDate = theday + " " + monthnames[themonth] + " " + theyear;
var formattedDate = monthnames[themonth] + " " + theday;




if (showVenueAddress != ""){
	showMap = '- <a href="http://maps.google.com/?q=' + showVenueAddress + '%2C+' + showCity + '%2C+' + showState + '%2C+' + showCountry + '" target="_blank" class="map-link">MAP</a>';
} else { showMap = ''; }

if (showTicketURI != ""){
	showTicketLink = '- <a href="' + showTicketURI + '" target="_blank" class="ticket-link">BUY TICKETS</a>';
} else { showTicketLink = ''; }

if (showVenueURI != ""){
	showVenueLink = '- <a href="' + showVenueURI + '" target="_blank" class="ticket-link">VENUE WEBSITE</a>';
} else { showVenueLink = ''; }

if (showTicketURI != ""){
	showVenueHTML = '<a href="' + showTicketURI + '" target="_blank">' + showVenue + '</a>';
} else { showVenueHTML = showVenue; }

	
	// Build HTML string and return
	output = '';
	output += '<tr>';
	output += '<td class="event-date">'+ formattedDate +'</td>';
	output += '<td class="event-location">'+ showCity + ', ' + showState +'</td>';
	output += '<td class="event-venue">'+ showVenueHTML +'</td>';
	output += '</tr>';
	return output;
}
	 
