/* 	tour wrappers
 * 	-dynamically imports headers and footers.
 */
var tourWrapper = {
	/***********************/
	/*hardcoded properties */
	/***********************/
	HEADER_DIV: "tourHeadContainer",
	FOOTER_DIV: "tourFooter",
	TOURNAMENT_HEADER_DIV: "tourLSTounament",
	TOURNAMENT_NAV_DIV: "tourLSNav",
	
	/********************/
	/*dynamic properties*/
	/********************/
	protocolToken: "http",
	tournId: "",
	headElement: "",
	
	/*********************/
	/*tourWrapper methods*/
	/*********************/
	tourOutputHTML: function(element,tourHTML){
		element.innerHTML = tourHTML;
	},
	
	tourOutputCSS: function (tourCSS){
		setTimeout( function() { this.headElement.innerHTML = this.headElement.innerHTML + tourCSS; }, 50 );
	},
	
	readFile: function(fileURL){
		var response = "";
		if (fileURL == ""){
			return response;
		}
		var req = new Ajax.Request(fileURL,
			{
				method:'get',
				asynchronous: false,
				onSuccess: function(transport){	
					response = transport.responseText || "";		
				},
				onFailure: function(transport){
				},
  				onException: function(transport, exception) {
    				throw exception;
  				}				
			}
		)
		return response;
	},
	
	setTourTournament: function(tournamentId){
		this.tournId = tournamentId;
	},
	
	setProtocolToken: function(token){
		protocolToken = token;
	},
	
	setHeadElement: function(){
		var headElements = document.getElementsByTagName("head");
		this.headElement = headElements[0];
	},
	
	
	setCustomHeader: function(headerURL){
		var headerHTML = '<div style="width: 1000px">' + this.readFile(headerURL) + '</div>';
		
		this.tourOutputHTML(document.getElementById(this.HEADER_DIV),headerHTML);
	},
	
	setTourHeader: function(){
		var headerURL = "";
		var tourType = "";
		var headerHTML = "";
		var headerCSS = "";
		if (this.tournId.length == 0){
			tourType = "r";//defaoult to PGA Tour 
		}
		if (this.tournId.charAt(0) == "h"|| this.tournId.charAt(0) == "s"){
			tourType = this.tournId.charAt(0);
		} else {
			tourType = "r";
		}
		
		headerCSS = '<link rel="stylesheet" type="text/css" href="http://i.cdn.turner.com/pgatour/.element/ssi/css/3.0/global/global.css" /><link rel="stylesheet" type="text/css" href="http://i.cdn.turner.com/pgatour/.element/ssi/css/3.0/global/' + tourType + '/main_' + tourType + '.css" /><link rel="stylesheet" type="text/css" href="http://i.cdn.turner.com/pgatour/.element/css/3.0/sect/' + tourType + '/homepage_' + tourType + '.css" />';
		headerURL = "/.element/ssi/sect/3.0/" + tourType + "/.header/ceiling_" + tourType + ".html";
		headerHTML = '<div style="width: 1000px">' + this.readFile(headerURL) + '</div>';
		
		//TODO: process html for https
		//TODO: process the javascript from the header that needs to execute
		this.tourOutputHTML(document.getElementById(this.HEADER_DIV),headerHTML);
		//this.tourOutputCSS(headerCSS);
		
	},

	setCustomTournamentHeader: function(tournHeaderURL){
		if (this.tournId.length != 0){
			var tournamentHeaderHTML = "";
			tournamentHeaderHTML =  this.readFile(tournHeaderURL);
			//TODO: process html for https
			this.tourOutputHTML(document.getElementById(this.TOURNAMENT_HEADER_DIV),tournamentHeaderHTML);
		}
	},
	
	setTourTournamentHeader: function(){
		if (this.tournId.length != 0){
			var tournamentHeaderURL = "/.element/ssi/sect/3.0/tournaments/"+ this.tournId + "/.header/tourn_header.html";
			var tournamentHeaderHTML = "";
			tournamentHeaderHTML =  this.readFile(tournamentHeaderURL);
			//TODO: process html for https
			this.tourOutputHTML(document.getElementById(this.TOURNAMENT_HEADER_DIV),tournamentHeaderHTML);
		}
	},

	setCustomTournamentNav: function(navURL){
		if (this.tournId.length != 0){
			var tournamentNavHTML = "";
			tournamentNavHTML = this.readFile(navURL);
			//TODO: process html for https
			this.tourOutputHTML(document.getElementById(this.TOURNAMENT_NAV_DIV),tournamentNavHTML);
		}
	},
	
	
	setTourTournamentNav: function(){
		if (this.tournId.length != 0){
			var tournamentNavURL = "/.element/ssi/sect/3.0/tournaments/"+ this.tournId + "/.rail/page_nav_flip.html";
			var tournamentNavHTML = "";
			tournamentNavHTML = this.readFile(tournamentNavURL);
			//TODO: process html for https
			this.tourOutputHTML(document.getElementById(this.TOURNAMENT_NAV_DIV),tournamentNavHTML);
		}
	},
	
	setCustomFooter: function(footerURL){
		var footerHTML = "";
		footerHTML = this.readFile(footerURL);
		//TODO: process html for https
		//TODO: process the javascript from the footer that needs to execute
		this.tourOutputHTML(document.getElementById(this.FOOTER_DIV),footerHTML);
		//this.tourOutputCSS(footerCSS);
	},	

	setTourFooter: function(){
		var footerURL = "/.element/ssi/global/3.0/footer/footer.html";
		var footerHTML = "";
		var footerCSS = '<link rel="stylesheet" type="text/css" href="http://i.cdn.turner.com/pgatour/.element/ssi/css/3.0/global/global.css" />';
		
		footerHTML = this.readFile(footerURL);
		//TODO: process html for https
		//TODO: process the javascript from the footer that needs to execute
		this.tourOutputHTML(document.getElementById(this.FOOTER_DIV),footerHTML);
		//this.tourOutputCSS(footerCSS);
	}

}
