/*  Program: fixturebrowse.js
 *     Date: 16 Sep 2009
 *   Author: Steven James Tierney
 * Comments: JS for fixture browse page
 */
function f_GetFixture( ipStart ) {
	// Get a batch of fixtures
	var vTeamID = $('teamselect').options[$('teamselect').selectedIndex].value;
	var vTargetDiv = $('browse_article');
	var vQtyToFetch = parseFloat(vNumToFetch+1);
	
	new Ajax.Request(f_GetBaseUrl() + '/src/inc/ajax.php?action=fetch&type=fixture&teamid='+vTeamID+'&qty='+vQtyToFetch+'&start='+ipStart , {
		evalJS: false,
		onCreate: function () {
			  f_DispSpinner( vTargetDiv );
		      }, // onCreate
		onComplete: function (transport) {
		var vFixtureArray = [];
		var vMsgArray = [];
		if (transport.responseText.isJSON()) {
			// alert("is json");
			// alert("transport.responseText\n" + transport.responseText);
			var vJSON = transport.responseText.evalJSON();
			// Use the JSON to create the News Objects & Message Objects
			vFixtureArray = vJSON.Response[0].Fixture;
			vMsgArray = vJSON.Response[1].Message;
			
			if (vMsgArray[0].msgtype == "Success") {
				if (vFixtureArray.length > 0) {
					// Create & show the Fixture objects
					vFixtureArray = f_CreateFixture( vFixtureArray );
	
					// Display the News Articles
					f_DispFixture( vTargetDiv, vFixtureArray );

					// Set the Prev and Next links
					f_SetLinks( ipStart, vFixtureArray.length==vQtyToFetch );
				} else {
					vTargetDiv.innerHTML = "No Fixtures were found";

					// Set the Prev and Next links
					f_SetLinks( ipStart, false );
				} // zero-length array
			}
			else {
				f_DispErrors( vMsgArray );
			}
		}  // if Is JSON
		else {
			// alert('Error: NOT JSON' + transport.responseText);
		}												 
	} // onComplete
	}); // new Ajax.Request
}

function f_GetTeams() {
	// Get the Teams to the Select
	new Ajax.Request(f_GetBaseUrl() + '/src/inc/ajax.php?action=fetch&type=teamselect', {
		evalJS: false,
		onComplete: function (transport) {
		var vItemArray = [];
		var vMsgArray = [];
		var vOption;
		var vCount = 0;
		var vSelect = $('teamselect');
		if (transport.responseText.isJSON()) {
			// alert("is json");
			// alert("transport.responseText\n" + transport.responseText);
			var vJSON = transport.responseText.evalJSON();
			// Use the JSON to create the News Objects & Message Objects
			vItemArray = vJSON.Response[0].Team;
			vMsgArray = vJSON.Response[1].Message;
			
			if (vMsgArray[0].msgtype == "Success") {
				// Empty the Select
				f_DropSelectOptions ( vSelect );
				if (vItemArray.length > 0) {
					// Add the Teams to the Select
					vItemArray.each( function (vItem) {
					  vOption = document.createElement('option');
					  vOption.value = vItem.teamid;
				      vOption.text  = vItem.teamname;
				      if (vCount == 0) {
				    	  vOption.defaultSelected = true;
				      }
				      
				      // Add the option to the Select
                      try {
                    	  vSelect.add( vOption, null );  // stds
                      } catch (e) {
                    	  vSelect.add( vOption );  // ie
                      }
					  vCount += 1;
					}); // each

					// Get the fixtures
					f_GetFixture( 0 );
				}
			} // success
			else {
				f_DispErrors( vMsgArray );
			}
		}  // if Is JSON
		else {
			// alert('Error: NOT JSON' + transport.responseText);
		}												 
	} // onComplete
	}); // new Ajax.Request
}

function f_SetLinks( ipStart, ipShowPrev ) {
	// Set the Prev (Older) and Next (Newer) links
	var vNext = parseFloat(ipStart) - parseFloat(vNumToFetch);
	var vPrev = parseFloat(ipStart) + parseFloat(vNumToFetch);
	var vLinkParam = "";

	// Set the Next link (Newer articles)
	if (vNext < 0) { 
		vNext = 0; 
		$('nextlink').hide();
	}
	else {
		$('nextlink').show();
	}
	
	if (ipShowPrev==true){ $('prevlink').show(); }
	else { $('prevlink').hide(); }

	vLinkParam = "'"+vNext+"'";
	$('nextlink').href = "javascript: f_GetFixture("+vLinkParam+");"
    vLinkParam = "'"+vPrev+"'";
	$('prevlink').href = "javascript: f_GetFixture("+vLinkParam+");"
} // f_SetLinks

Event.observe(window,'load',function(){
	f_GetTeams();
	f_WatchForEmails();
}); // observe load 



