/**
 * @author hugh.maclachlan
 * 
 * Built on top of the JQuery library
 * Plugin to display homepage fixtures/results/live match information.
 */

(function($) {
	//server time is stored as a global variable. we only need to get this once for all instances of the matches panel
	var serverTime = null;
	
	//store the settings for the matchesPanel here
	var s;
	
	//the HTML element that is to contain our fixtures and results
	var objMatchesPanel;
	
	//these variables are used to listen to the ajax and make sure all our xmls have been loaded before processing
	var xmlLoadTimer;
	var matchXmlsLoaded = 0;
	 
	//arrays for storing our match details info
	var results=[];
	var fixtures=[];
	
	
	var liveMatch = -1;
	
	//default values of the matchesPanel settings variables 
	var defaults = {
		serverTimeXML: null,
		clubId: null,
		liveLinkImage: ""
	};
	
	$.fn.matchesPanel = function(o) {
		objMatchesPanel = this; 
		
		this.settings = $.extend({}, defaults, o || {});
        s = this.settings;
		      
        //use an ajax call to retrieve server time from xml
		$.ajax({
			url: s.serverTimeXML,
			success: function(data) {
				var xml; 
				try //Internet Explorer 
				{ 
					xml = new ActiveXObject("Microsoft.XMLDOM"); 
					xml.async = false; 
					xml.loadXML(data); 
				} 
				catch(e) 
				{ 
					xml = data; 
				}

				$("date", xml).each(function(){
					serverTime = new Date(parseInt($(this).attr("timestamp")));
				});
				doMatchesPanel();
			},
			error: function() {
				doMatchesPanel();
			}
		});
		
		function doMatchesPanel() {
			return objMatchesPanel.each(function() {
				new $mp(objMatchesPanel, o);
        	});	
		};     
    };
	
	//converts our matchtime value from our xml into a js date
	function performTSToDate(timestamp) {
		//timestamp = "20100501150000";
		//functino to convert wierdy datestamp to epoch using regexp
		//input has to be in this format: 20100501150000
		var regex=/^([0-9]{4})([0-1][0-9])([0-3][0-9])([0-2][0-9])([0-5][0-9])([0-5][0-9])?$/;
		var results = regex.exec(timestamp);
		return new Date(results[1],results[2]-1,results[3],results[4],results[5],results[6]);
	}
	
	//construct our Matches Panel
	$.matchesPanel = function(e, o, index){
		if (serverTime != null){
			$mp.findLiveMatch();	
		}
		
		if (liveMatch > 0) {
			$("#mpLive" + liveMatch).show();
			$(".mpFixture").remove();
		} else {	
			$(".mpFixture").show();
			$(".mpTime").remove();
		}
	};
	
	/*
	 * 
	 */
	$.matchesPanel.findLiveMatch = function() {
		$(".mpFixture").each(function(index) {
			var matchNum = index + 1;
			
			var elTime = $(this).find(".mpTime");
			
			var matchDateTime = performTSToDate(elTime.html());
			
			var timeDiff = Math.floor((matchDateTime.getTime() - serverTime.getTime()) / 60000);
		    if ((timeDiff <= 30) && (timeDiff >=-180)) {
				liveMatch = matchNum;
				return false;
			} else {
				$("#mpLive" + (index+1).toString()).remove();
			}
			
			elTime.remove();
		});
	}
	
	
	//shortcut variable for internal use
    var $mp = $.matchesPanel;

})(jQuery);


