//-------------------------------
// Class
//-------------------------------

function LiveBoxscoreApp() {

	
	var _self = this;

	//-------------------------------
	// Constants
	//-------------------------------
	
	var XML_PATH = {"type" : "live", "url" : "http://www.nba.com/blazers/islive.xml" };
	//var XML_PATH = {"type" : "live", "url" : "xml/islive.xml" };
	var LOAD_DATA = "load_data";
	
	//-------------------------------
	// Properties
	//-------------------------------
	
	var _liveBoxscoreDataModel;
	var _liveBoxscoreView;
	
	var _current;
	
	//-------------------------------
	// Constructor
	//-------------------------------
	
	$j(function() {
		_liveBoxscoreDataModel = new LiveBoxscoreDataModel();
		_liveBoxscoreView = new LiveBoxscoreView();
		
		$j(_liveBoxscoreDataModel).bind(_liveBoxscoreDataModel.DATA_COMPLETE, onBoxscoreDataComplete);
		$j(_self).bind(LOAD_DATA, onLoadData);

	});
	
	//-------------------------------
	// Public Methods
	//-------------------------------
	
	_self.deploy = function() {
		$j(_self).trigger(LOAD_DATA, [XML_PATH]);
		_current = "";
	}
	
	//-------------------------------
	// Private Methods
	//-------------------------------
	
	
	function checkLive(data) {
		var type = $j(data).find("type").text();
		var url = ($j(data).find("game").attr("code").match("xml/")) ? $j(data).find("game").attr("code") : "http://www.nba.com/games/game_component/dynamic/" + $j(data).find("game").attr("code") + "/boxscore.xml";
		
		var boxscoreUrl = {"type" : "boxscore", "url" : url };
		
		switch(type) {
			case "video" :
				if(_current != "video") _liveBoxscoreView.video(data);
				break;
			case "ingame" :
				if(_current != "ingame") {
					$j(_self).trigger(LOAD_DATA, [boxscoreUrl]);
					_liveBoxscoreView.ingame(data);
				}
				setTimeout(function() { $j(_self).trigger(LOAD_DATA, [boxscoreUrl]); }, 10000);
				break;
		}
		
		_current = type;
		
	}
	
	//-------------------------------
	// Listeners
	//-------------------------------
	
	function onBoxscoreDataComplete(event, data, type) {
		
		switch(type) {
			case "boxscore" :
				_liveBoxscoreView.boxscore(data);
				break;
			case "live" :
				//alert("deployed");
				checkLive(data);
				setTimeout(function() { $j(_self).trigger(LOAD_DATA, [XML_PATH]); }, 10000);
				break;
		}
		
	}
	
	function onLoadData(event, path) {
		
		_liveBoxscoreDataModel.load(path);
	}

}
