function readyFunction(aConfig){

	// Config vars
	//aConfig["buyer_id"]
	//aConfig["site_number"]
	//aConfig["ticket_query_string"]
	//aConfig["team_name"]
	//aConfig["domain_name"]
	//aConfig["phone_number"]
	//aConfig["site_name"]

	 $("#search-for").focus();

	preparePrimaryTNDisplay(aConfig);
	loadSideTNDisplay(aConfig);

	applyStyles();
}

function applyStyles(){
	// ------------------------------------------------------------------------------------------------------------
	// Fancy Fonts
	// ------------------------------------------------------------------------------------------------------------
	
	Cufon.replace('h1',{forceHitArea: true});  
	Cufon.replace('h2',{forceHitArea: true});
	Cufon.replace('h3',{forceHitArea: true});	
	$(".header p").addClass("post-cufon");
	Cufon.replace('.header p',{forceHitArea: true});	

	// ------------------------------------------------------------------------------------------------------------
	// Add n-th-child classes to various elements
	// ------------------------------------------------------------------------------------------------------------

	$("table tr:nth-child(even)").addClass("nth-child-even");
	$("table tr:nth-child(odd)").addClass("nth-child-odd");			
			
	$("tr th:first-child").addClass("first-child");
	$("tr th:last-child").addClass("last-child");
		
	$("tr td:first-child").addClass("first-child");
	$("tr td:last-child").addClass("last-child");
			
	$("ul li:first-child").addClass("first-child");
	$("ul li:last-child").addClass("last-child");		
}

// Open rel="external" links in a new window/tab.
$(function(){
	$('a[rel*=external]').click( function() {
		window.open(this.href);
		return false;
	});
});

function cleanTNSortURL(url){
	
	newURL = '';
	
	jQuery.url.setUrl(window.location);

	if( jQuery.url.segment(0) == "schedule"  ||  jQuery.url.segment(0) == "search"){

		newURL =  "/" + jQuery.url.segment(0) + "/" +  jQuery.url.setUrl(url).param("kwds") + "/sort-" +  jQuery.url.param("sortcol") + "-" + jQuery.url.param("sortord");
	}
	
	else if( jQuery.url.segment(0) == "tickets" ||  jQuery.url.segment(0) == "venues" ||  jQuery.url.segment(0) == "events" ){

		newURL =  "/" + jQuery.url.segment(0) + "/" +   jQuery.url.segment(1) + "/" +   jQuery.url.segment(2) + "/sort-" +  jQuery.url.setUrl(url).param("sortcol") + "-" + jQuery.url.param("sortord");

		if(jQuery.url.attr("anchor") != null){
			newURL += "/#" + jQuery.url.attr("anchor");
		}
	}
	
	return newURL;
}

function cleanTNURL(url){
	
	if(url != null){	
		url = url.replace("ResultsTicket.aspx","/tickets");
		url = url.replace("ResultsVenue.aspx","/venues");
		url = url.replace("ResultsEvent.aspx","/events");

		url = url.replace("?","/");
		url = url.replace("&amp\;","/");
		url = url.replace("&","/");
		
		url = get2dir(url,"event");
		url = get2dir(url,"evtid");
		url = get2dir(url,"venid");
		url = get2dir(url,"sortcol");
		url = get2dir(url,"sortord");
		url = get2dir(url,"kwds");
		url = get2dir(url,"stprvid");
		url = get2dir(url,"city");
		url = get2dir(url,"vname");
		url = get2dir(url,"pid");
	}
	return url;
}

function get2dir(url,string){
	return url.replace("/" + string + "=","/");
}

function monthNumber2monthString(monthNumber){
	var monthsToStr= new Array(13);
	monthsToStr[1] = 'Jan';
	monthsToStr[2] = 'Feb';
	monthsToStr[3] = 'Mar';
	monthsToStr[4] = 'Apr';
	monthsToStr[5] = 'May';
	monthsToStr[6] = 'Jun';
	monthsToStr[7] = 'Jul';
	monthsToStr[8] = 'Aug';
	monthsToStr[9] = 'Sep';
	monthsToStr[10] = 'Oct';
	monthsToStr[11] = 'Nov';
	monthsToStr[12] = 'Dec';
	return monthsToStr[monthNumber];
}

function preparePrimaryTNDisplay(aConfig){
	
	$(".LoadingTickets").hide("slow");

	// ------------------------------------------------------------------------------------------------------------
	// No Results Found?
	// ------------------------------------------------------------------------------------------------------------
	$("#primary-tn-results-area .tn_results_notfound").replaceWith("<h3>There is currently no online inventory available for this event.</h3><p>Please contact " + aConfig["site_name"] +" at " + aConfig["phone_number"] + " or <a href=\"mailto:sales@" + aConfig["domain_name"]  + "\">sales@" + aConfig["domain_name"] + " </a>to check availability for this event.</p>");
	$("#primary-tn-results-area .tn_results_notfound_form").replaceWith("");	


	// ------------------------------------------------------------------------------------------------------------
	// Buy Tickets Header
	// ------------------------------------------------------------------------------------------------------------

	// Remove tr.tn_results_header_divider
	$("#primary-tn-results-area tr.tn_results_header_divider").replaceWith("");

	// Remove the image from the "Click Here to See Venue Map" area	
	$("#primary-tn-results-area td.tn_results_header_maplink a img").replaceWith("");
	
	// Fix formatting of "click to see venue map" link	
	$("#primary-tn-results-area td.tn_results_header_maplink a").each(function(){
		if($(this).text().indexOf("venue map") !== -1){
			$(this).text("Click Here to See Venue Map");	
		}
	});

	// Replace td.tn_results_header_title with <th><h3>
	$("#primary-tn-results-area td.tn_results_header_title").each(function(){
		$(this).replaceWith(function(){
			return "<th colspan=6><h3>" + $(this).html() + "</h3></th>";
		});
	});

	// Clean the URL's in the results header
	$("#primary-tn-results-area table.tn_results_header a").each(function(){
		$(this).attr("href", cleanTNURL($(this).attr("href")) );		
	});

	// ------------------------------------------------------------------------------------------------------------
	// Buy Tickets Area
	// ------------------------------------------------------------------------------------------------------------

	// Convert td's in tr.tn_results_colhead to th's
	$("#primary-tn-results-area td.tn_results_colhead").each(function(){
		$(this).replaceWith(function(){
			return "<th>" + $(this).html() + "</th>";
		});
	});

	// Clean the header sorting link urls
	$("#primary-tn-results-area table.tn_results th a").each(function(){
		$(this).attr("href", cleanTNSortURL($(this).attr("href")) );		
	});
	
	// Remove images from headers
	$("#primary-tn-results-area table.tn_results th img").replaceWith("");		

	// Remove tr.tn_results_divider
	$("#primary-tn-results-area tr.tn_results_divider").replaceWith("");
	
	// Remove column head class
	$("#primary-tn-results-area tr.tn_results_colhead").removeClass("tn_results_colhead");
	
	// Remove alternating row classes
	$("#primary-tn-results-area tr.tn_results_alternate_row").removeClass("tn_results_alternate_row");
	$("#primary-tn-results-area tr.tn_results_standard_row").removeClass("tn_results_standard_row");
	
	// Remove valign from all tr
	$("#primary-tn-results-area table.tn_results tr").removeAttr("valign");
	
	// Update style for "See Tickets" buttons	
	$("#primary-tn-results-area .tn_results_tickets_text a").text("See Tickets");
	$("#primary-tn-results-area .tn_results_tickets_text a").addClass("single-line-button");

	
	// Update style for "Purchase Tickets" buttons
	$("#primary-tn-results-area .tn_results_ticket_purchase a").text("Buy Tickets");
	$("#primary-tn-results-area .tn_results_ticket_purchase a").addClass("single-line-button");

	
	//  Split Day, Date and Time into seperate columns	
	$("#primary-tn-results-area th a").each(function(){
		if($(this).text() == "Date/Time"){
			$(this).parents("th").attr("colspan","2");
			$(this).text("Schedule");
		}		
	});

	//  Split Day, Date and Time into seperate columns
	$("#primary-tn-results-area .tn_results_datetime_text").each(function(){ 		
		var $col_day = $(this).children(".tn_results_day_text").text();
		var $col_date = $(this).children(".tn_results_date_text").text();
		var $col_time = $(this).children(".tn_results_time_text").text();		
		
		$(this).replaceWith("<td class=\"tn_results_date_daydate_text\">" + $col_day + "<br />" + $col_date + "</td><td class=\"tn_results_date_time_text\">" + $col_time + "</td>");		
	});
	
	// Remove spans in ticket section and row tables
	$("#primary-tn-results-area table.tn_results_ticket_sectionrow span").each(function(){
		$(this).replaceWith(function(){
			return $(this).html();
		});		
	});
	
	// Put a space after "Row:" and "Section:"
	$("#primary-tn-results-area table.tn_results_ticket_sectionrow").each(function(){
		$(this).html($(this).html().replace("Section:","Section: "));
		$(this).html($(this).html().replace("Row:","Row: "));
	});
	
	// Remove empty .tn_results_ticket_notes	
	$("#primary-tn-results-area table.tn_results_ticket_sectionrow td.tn_results_ticket_notes").each(function(){
		if($.trim($(this).html()) == ""){$(this).parent("tr").replaceWith("");}
	});

	// Change the golden star link
	$("#primary-tn-results-area img.tn_results_ticket_highlight").each(function(){
		if($(this).attr("src") == "tn_images/tn_results_highlight_off.gif"){$(this).replaceWith("");}else{$(this).attr("src","/images/design/gold_star.png");}
	});


	// Clean the URLs 
	$("#primary-tn-results-area table.tn_results td a").each(function(){
		$(this).attr("href", cleanTNURL($(this).attr("href")) );		
	});
}

function loadSideTNDisplay(aConfig){

	$.getJSON('/tickets/side-module-ajax/kwds/' + aConfig["ticket_query_string"] + '/bid/' + aConfig["buyer_id"]+ '/sitenumber/' + aConfig["site_number"], function(data) {

		$('#side-ticket-network-content').hide();
		$('#side-ticket-network-content').html(data.html);		
		
		prepareSideTNDisplay(aConfig["team_name"]);
		applyStyles();

		$('#side-ticket-network-content').slideDown("fast");
	});
}

function prepareSideTNDisplay(team){
	
	// ------------------------------------------------------------------------------------------------------------
	// Side Area Ticket Display
	// ------------------------------------------------------------------------------------------------------------
	
	$(".side .tn_results_header").replaceWith("");	
	$(".side tr.tn_results_divider").replaceWith("");	
	
	
	$(".side tr.tn_results_colhead").replaceWith("<tr><th>Date</th><th>Opponent</th><th></th></tr>");

	$(".side .tn_results_tickets_text a").text("Tickets");
	$(".side .tn_results_tickets_text a").addClass("single-line-button");
	
	$(".side .tn_results_tickets_text").addClass("tn_custom_side_tickets");
	$(".side .tn_custom_side_tickets").removeClass("tn_results_tickets_text");

	var sideEventCounter = 0;


	// Loop through each row of the results table
	$(".side table.tn_results tr").each(function(){
		// If this isn't a header...
		if($(this).find("th").size() == 0){		
			
			if(sideEventCounter < 8){
	
				sideEventCounter++;	

				// Find out if we should display the team they're playing (if a sport) or the city (if some other event)	
				
				var $firstItem = $(this).find("td.tn_results_event_text a:first-child").text();
				var $secondItem = $(this).find("td.tn_results_event_text a:last-child").text();
				
				$firstItem = $firstItem.replace("Spring Training: ","");
				$secondItem = $secondItem .replace("Spring Training: ","");

				var $hasVs = $(this).children(".tn_results_event_text").text().indexOf("Vs.");
				
				// If it's a sport, display the name of the opponent and remove the information on the location of the event
				if($firstItem != $secondItem && $hasVs !== -1){					
					if($firstItem.indexOf(team) !== -1){
						var $col_event_contents =  $secondItem;
					}else{
						var $col_event_contents =  "@ " + $firstItem;
					}
				}else{
					var $col_event_contents = $firstItem;
				}
			
				var $col_tickets_content = cleanTNURL($(this).children("td.tn_custom_side_tickets").html());
			
				var $col_day = $(this).find(".tn_results_datetime_text .tn_results_day_text").text();
				var $col_date = $(this).find(".tn_results_datetime_text .tn_results_date_text").text();
				var $col_time = $(this).find(".tn_results_datetime_text .tn_results_time_text").text();		
			
				var col_date_arr = $col_date.split("/");			
			
				var $col_date_contents =  monthNumber2monthString(col_date_arr[0]) + " " + col_date_arr[1];		
			
				$(this).replaceWith("<tr><td class=\"date\">" + $col_date_contents + "</td><td class=\"event\">" + $col_event_contents + "</td><td class=\"tickets\">" + $col_tickets_content + "</td></tr>");	
			}else{$(this).replaceWith("");}
		}
	});
}
