// JavaScript Document
if(typeof(TW) === "object") {
	//Load dependencies	
	bam.loadSync(
		bam.homePath + "bam.xml.js", 
		bam.homePath + "bam.url.js", 
		bam.homePath + "bam.validation.js", 
		bam.homePath + "bam.datetime.js",
		bam.homePath + "bam.datagrid.js",
		bam.homePath + "bam.collections.js",
		bam.homePath + "bam.ajaxHelper.js"
	);
	
	//Adding dao to TW namespace
	$.extend(TW, {dao: (function(){
		//Built-in Poller prototype class
		var Poller = function() {
			this.start = function() {
				if(!!this.interval && $.isFunction(this.onRefresh)) {
					this.timerId = setInterval(this.onRefresh, this.interval);				
				}
			};
			this.stop = function() {
				if(!!this.timerId) {
					clearTimeout(this.timerId);	
				}
			};
		};
		
		var _self = {
			//Tiger's Scorecard					 
			shotByShot: bam.ajaxHelper.createInstance({ //Need to Pole every 30- sec (server-refresh every 30 sec)
				interval: 20000,
				onRefresh: null,
				dataType: "xml",
				init: function() { Poller.call(this); }, //Inherit from Poller
				Load: function(eventId, cb) {
					var that = this;
					that.url = "/xml/" + eventId + "/playbyplay.xml";
					that.makeRequest(function(jObj) {
						that.DataCache = jObj;						
						if($.isFunction(cb)){cb();}
				  	});
				},
				getRound: function(roundNo) {
					var _out = null;
					if(!!this.DataCache) {
						_out = this.DataCache.player[0].rounds[0].round.getNodesByAttribute("roundId", String(roundNo));						
					}
					return _out;
				}
		  	}),
			//Page Summary
			summary: bam.ajaxHelper.createInstance({ //Need to Pole every 1 min
				interval: 60*1000,
				onRefresh: null,
				dataType: "xml",
				init: function() { Poller.call(this); }, //Inherit from Poller
				Load: function(eventId, cb) {					 
					var that = this;
					that.url = "/xml/" + eventId + "/Scorecard.xml";
					that.makeRequest(function(jObj) {
						that.DataCache = jObj;						
						if($.isFunction(cb)){cb();}
				  	});
				},
				getSummary: function(roundNo) {
					var _out = null;
					if(!!this.DataCache) {
						var c = this.DataCache.Player[0];
						_out = {
							overallPos: 	c.CurrentPosition,
							overallToPar: 	(isNaN(c.ParRelativeScore) || c.ParRelativeScore.toString().length === 0) ? 
                                c.ParRelativeScore : parseInt(c.ParRelativeScore,10),
							throughRound: 	(isNaN(this.DataCache.CurrentRound) || this.DataCache.CurrentRound.toString().length === 0) ? 
                                this.DataCache.CurrentRound : parseInt(this.DataCache.CurrentRound, 10),
							currentToPar: 	(isNaN(c.ParRelativeScoreToday) || c.ParRelativeScoreToday.toString().length === 0) ? 
                                c.ParRelativeScoreToday : parseInt(c.ParRelativeScoreToday,10),
							throughHole: 	c.NumberOfHolesPlayed,
							startingHole: 	c.StartingHole
						};
					}
					return _out;
				}
		  	}),
			stats: bam.ajaxHelper.createInstance({ //Need to Pole every 1 min
				interval: 30*1000, //30 seconds
				onRefresh: null,
				dataType: "xml",
				init: function() { Poller.call(this); }, //Inherit from Poller
				Load: function(eventId, cb) {
					var that = this;
					that.url = "/xml/" + eventId + "/stats_tiger.xml";
					that.makeRequest(function(jObj) {
						that.DataCache = jObj;						
						if($.isFunction(cb)){cb();}
				  	});
				},
				getStats: function() {
					var _out = null;
					if(!!this.DataCache) {
						_out = this.DataCache.Player[0].Stat;						
					}
					return _out;
				}
			 }),
			 leaderboard: bam.ajaxHelper.createInstance({ //Need to Pole every 1 min
				interval: 30*1000, //30 seconds
				onRefresh: null,
				dataType: "xml",
				init: function() { Poller.call(this); }, //Inherit from Poller
				Load: function(eventId, cb) {
					var that = this;
					that.url = "/xml/" + eventId + "/Leaderboard.xml";
					that.makeRequest(function(jObj) {
						that.DataCache = jObj;
						if($.isFunction(cb)){cb();}
				  	});
				},
				getLeaderboard: function() {
					var _out = null;
					if(!!this.DataCache) {
						_out = this.DataCache.Player;						
					}
					return _out;
				}
			 })
		};
		
								 
		return _self;
	})()});
}