 /*
	 * If the JSON object is not available redirect to the old website
	 */
	if(typeof JSON === 'undefined') { 
	  alert("You are viewing this in a very old browser, therefore some things will not work or appear as you might expect.");
	} 
	
/*
 * DAVIDWOODFIELD API
 */
var DAVIDWOODFIELD = (function() {
	
	var API = {};
	
	/*
	 * Functions
	 */
	
	/*
	 * getJSON()
	 *
	 * General function for loading JSON data into a page
	 * accepts, JSON file to load
	 */
	function getJSON(file, methodToRun) {

		$.ajax({
			url: file,
			dataType: 'json',
			cache: false,
			beforeSend: function() {

				/*
				 * Show loading
				 */
				$('.inner-content').addClass('loading');

			},
			success: function(json) {
				methodToRun(json);
			}, 
			error: function(one,two,three) {
				
				alert('unable to load data at this time');
			},
			complete: function() {
				/*
				 * Hide loading
				 */
				$('.inner-content').removeClass('loading');
			}
		});

	} 
	
	/*
	 * saveJSON()
	 *
	 * General function for saving JSON data into a file
	 * accepts, JSON file to save to & the jsonstring
	 */
	function saveJSON(file, jsonString, methodToRun) {

		$.ajax({
			url: '../CMS/saveJSON.php',
			data: {
				filename: file,
				jsonstring: jsonString
			},
			type: 'POST',
			cache: false,
			beforeSend: function() {

				/*
				 * Show loading
				 */
				$('body').addClass('loading');

			},
			success: function(data) {
				methodToRun(data);
			}, 
			error: function(one,two,three) {
				
				alert('unable to save data at this time');
			},
			complete: function() {
				/*
				 * Hide loading
				 */
				$('body').removeClass('loading');
			}
		});

	}
	
	/*
	 * getMatchReport()
	 *
	 * Function to load a match report
	 */
	function getMatchReport(id, fixtures, whichTeam) {
	
	   var $matchReport,
	       $matchReportTitle,
	       $matchReportText,
	       $closeMatchReport,
	       isFound = false;
	  
	  /*
	   * Load match report
	   */
	  getJSON(fixtures, function(json) {
	     
	     if(json) {
	      
	      for(var i = 0, l = json.fixtures[whichTeam].length; i < l; i++) {
	        
	        /*
	         * Get the right fixture
	         */
	        if(json.fixtures[whichTeam][i].id === id) {
	          
	          isFound = true;
	          
	          /*
	           * Build match report
	           */
	          if($('#match-report').length > 0) {
	            $matchReport = $('#match-report');
	          } else {
	            $matchReport = $('<div id="match-report" class="match-report"></div>'); 
	            $('body').append($matchReport); 
	          }
	          
	          $matchReport.empty();
	          
	          $matchReportTitle = $('<h4 />');
			      $matchReportTitle.addClass('match-report-title');
			      $matchReportTitle.text(json.fixtures[whichTeam][i].homeTeam + ' ' + json.fixtures[whichTeam][i].result.homeTeamGoals + ' - ' + json.fixtures[whichTeam][i].result.awayTeamGoals + ' ' + json.fixtures[whichTeam][i].awayTeam);
			  
			      $matchReport.append($matchReportTitle);
			  
			      $matchReportText = $('<textarea />');
			      $matchReportText.addClass('match-report-text');
			      $matchReportText.val(json.fixtures[whichTeam][i].result.matchReport);
	          
	          $matchReport.append($matchReportText); 
	          
	          $closeMatchReport = $('<a href="#" id="close-match-report" class="close-match-report"><span>Close Match Report</span></a>');
	          $matchReport.append($closeMatchReport);
	          /*
	           * Show light box
	           */
	          $matchReport.lightbox_me({
	            centered: true
	          }); 
	          
	          break;
	           
	        } else {
	          isFound = false;
	        } 
	        
	      }
	       
	     } else {
	      alert('Can\'t load match report');
	     }  
	     
	     if(!isFound) {
	      alert('cannot find match report id');
	     }
	     
	  });
	   
	}  
	
	/*
	 * API
	 */
	API.getJSON = getJSON;
	API.saveJSON = saveJSON;
	API.getMatchReport = getMatchReport;
	
	/*
	 * Return PUBLIC API
	 */
	return API;
	
}());

/*
 * Things to do on page load
 */
$(function() {
	
	
	
	/*
	 * Close match report link
	 */
	$('body').delegate('#close-match-report', 'click', function(e) {
	  
	  e.preventDefault();
	  $('#match-report').trigger('close');
	});
	
});



