/*  Program: siteclasses.js
 *     Date: 9 Sep 2009
 *   Author: Steven James Tierney
 * Comments: Classes for the Site
 */

// Menu Items
var Menu = Class.create({
	initialize: function(ipID, ipName, ipFile, ipDesc, ipType) {
	this._ID   = ipID;
	this._Name = ipName;
	this._File = ipFile;
	this._Date = ipDesc;
	this._Type = ipType;
}
}); // Menu

function f_CreateMenu(ipJson) {

	// Variables
	var vMenuArray = [];
	var vMenuItem;

	// Go through the JSON and create Fixture objects
	ipJson.each( function ( vMenu ) {
		vMenuItem = new Menu ( vMenu.id, vMenu.name, vMenu.file, 
					vMenu.desc, vMenu.type );
		vMenuArray.push(vMenuItem);	
	}); // each
	return vMenuArray;
} // function f_CreateMenu

//About Article
var About = Class.create({
	initialize: function(ipID, ipText, ipNote, ipDate) {
	this._ID       = ipID;
	this._Text     = ipText;
	this._Note     = ipNote;
	this._Date     = ipDate;
}
}); // About

function f_CreateAbout(ipJson) {

	// Variables
	var vAboutArray = [];
	var vAboutItem;

	// Go through the JSON and create News objects
	ipJson.each( function ( vAbout ) {
		vAboutItem = new About ( vAbout.id, vAbout.text, vAbout.note, vAbout.date );
		vAboutArray.push(vAboutItem);	
	}); // each
	return vAboutArray;
} // function f_CreateAbout


//News Articles
var News = Class.create({
	initialize: function(ipID, ipType, ipHeadline, ipDate, ipTime, ipText) {
	this._ID       = ipID;
	this._Type     = ipType;
	this._Headline = ipHeadline;
	this._Date     = ipDate;
	this._Time     = ipTime;
	this._Text     = ipText;
}
}); // News

function f_CreateNews(ipJson) {

	// Variables
	var vNewsArray = [];
	var vNewsItem;

	// Go through the JSON and create News objects
	ipJson.each( function ( vNews ) {
		vNewsItem = new News ( vNews.id, vNews.type, vNews.headline, 
				               vNews.date, vNews.time, vNews.text );
		vNewsArray.push(vNewsItem);	
	}); // each
	return vNewsArray;
} // function f_CreateNews


// Fixtures
var Fixture = Class.create({
	initialize: function(ipID, ipSeason, ipCompetition, ipHomeTeam, ipAwayTeam, ipHomeGoals, ipAwayGoals,
			ipDate, ipTime, ipStadium, ipText, ipPrice, ipHomeGoals_aet, ipAwayGoals_aet,
			ipHomeGoals_pen, ipAwayGoals_pen, ipResultType, ipSeasonDesc, 
			ipHomeTeamName, ipAwayTeamName, ipStadiumName, ipCompetitionName) {
	this._ID              = ipID;
	this._Season          = ipSeason;
	this._Competition     = ipCompetition;
	this._HomeTeam        =	ipHomeTeam;
	this._AwayTeam        = ipAwayTeam;
	this._HomeGoals       = ipHomeGoals;
	this._AwayGoals       = ipAwayGoals;
	this._Date            = ipDate;
	this._Time            = ipTime;
	this._Stadium         = ipStadium;
	this._Text            = ipText;
	this._Price           = ipPrice;
	this._HomeGoals_aet   = ipHomeGoals_aet;
	this._AwayGoals_aet   = ipAwayGoals_aet;
	this._HomeGoals_pen   = ipHomeGoals_pen;
	this._AwayGoals_pen   = ipAwayGoals_pen;
	this._ResultType      = ipResultType;
	this._SeasonDesc      = ipSeasonDesc;
	this._HomeTeamName    = ipHomeTeamName;
	this._AwayTeamName    = ipAwayTeamName;
	this._StadiumName     = ipStadiumName;
	this._CompetitionName = ipCompetitionName;
}
}); // Fixture

function f_CreateFixture(ipJson) {

	// Variables
	var vFixtureArray = [];
	var vFixture;

	// Go through the JSON and create Fixture objects
	ipJson.each( function ( vFixItem ) {
		vFixture = new Fixture ( vFixItem.id, vFixItem.seaid, vFixItem.comid, vFixItem.hometeam, vFixItem.awayteam, 
				vFixItem.homegoals, vFixItem.awaygoals, vFixItem.fixdate, vFixItem.fixtime, vFixItem.stmid, 
				vFixItem.fixtext, vFixItem.price, vFixItem.homegoals_aet, vFixItem.awaygoals_aet, 
				vFixItem.homegoals_pen, vFixItem.awaygoals_pen, vFixItem.resulttype,
				vFixItem.seasondesc, vFixItem.hometeamname, vFixItem.awayteamname, vFixItem.stadiumname, 
				vFixItem.competitionname );
		vFixtureArray.push(vFixture);
	}); // each
	return vFixtureArray;
} // function f_CreateFixtures


// Stadium
var Stadium = Class.create({
	initialize: function(ipID, ipName, ipDesc, ipDirections, ipLongitude, ipLatitude, ipUseMap) {
	this._ID         = ipID;
	this._Name       = ipName;
	this._Desc       = ipDesc;
	this._Directions = ipDirections;
	this._Longitude  = ipLongitude;
	this._Latitude   = ipLatitude;
	this._UseMap     = ipUseMap;
}
}); // Stadium

function f_CreateStadium(ipJson) {

	// Variables
	var vStadiumArray = [];
	var vStadiumItem;

	// Go through the JSON and create Stadium objects
	ipJson.each( function ( vStadium ) {
		vStadiumItem = new Stadium ( vStadium.id, vStadium.name, vStadium.desc, 
				vStadium.directions, vStadium.longitude, vStadium.latitude, vStadium.usemap );
		vStadiumArray.push(vStadiumItem);	
	}); // each
	return vStadiumArray;
} // function f_CreateStadium


//Photos
var Photo = Class.create({
	initialize: function(ipID, ipType, ipTypeID, ipSeq, ipName, ipCaption ) {
	this._ID       = ipID;
	this._Type     = ipType;
	this._TypeID   = ipTypeID;
	this._Seq      = ipSeq;
	this._Name     = ipName;
	this._Caption  = ipCaption;
}
}); // Photo

function f_CreatePhoto(ipJson) {

	// Variables
	var vPhotoArray = [];
	var vPhotoItem;

	// Go through the JSON and create Photo objects
	ipJson.each( function ( vPhoto ) {
		vPhotoItem = new Photo ( vPhoto.id, vPhoto.type, vPhoto.typeid, 
				                 vPhoto.seq, vPhoto.name, vPhoto.caption );
		vPhotoArray.push(vPhotoItem);	
	}); // each
	return vPhotoArray;
} // function f_CreatePhoto

function f_GetPhoto( ipContainerObj, ipType, ipTypeID, ipQty, ipStart ) {

	var vSuccess = false;
	
	// Get the first news article photo
	new Ajax.Request(f_GetBaseUrl() + '/src/inc/ajax.php?action=fetch&type=photo&photype='+ipType+'&photypeid='+ipTypeID+'&phoqty='+ipQty+'&phostart='+ipStart, {
		evalJS: false,
		onCreate: function() { 
			// Create a spinner in the Div
			var vImg = document.createElement('img');
			vImg.id = "photo_" + ipTypeID;
			vImg.src = f_GetBaseUrl() + '/img/ajax/spinner01.gif';
			vImg.className = "";
			ipContainerObj.appendChild(vImg);
		},
		onSuccess: function() {
			vSuccess = true;
		},
		onComplete: function (transport) {
		var vMsgArray = [];
		var vPhotoArray = [];
		if (transport.responseText.isJSON()) {
			// alert("is json");
			// alert("transport.responseText\n" + transport.responseText);
			var vJSON = transport.responseText.evalJSON();
			// Use the JSON to create the Photo Objects & Message Objects
			vPhotoArray = vJSON.Response[0].Photo;
			vMsgArray = vJSON.Response[1].Message;

			if (vMsgArray[0].msgtype == "Success") {
				// Create & show the Photo objects
				vPhotoArray = f_CreatePhoto( vPhotoArray );

				// Display the Photo(s)
				f_DispPhoto( ipContainerObj, vPhotoArray );
			} // if success
			else {
				f_DispErrors( vMsgArray );
			}
		}  // if Is JSON
		else {
			// alert('Error: Photo NOT JSON\n' + transport.responseText);
		}
	} // onComplete
	}); // new Ajax.Request
} // f_GetPhoto

// Guestbook Articles
var Guest = Class.create({
	initialize: function(ipID, ipText, ipDate, ipForename, ipSurname, ipLocation ) {
	this._ID       = ipID;
	this._Text     = ipText;
	this._Date     = ipDate;
	this._Forename = ipForename;
	this._Surname  = ipSurname;
	this._Location = ipLocation;
}
}); // Guest

function f_CreateGuest(ipJson) {

	// Variables
	var vGuestArray = [];
	var vGuestItem;

	// Go through the JSON and create Photo objects
	ipJson.each( function ( vGuest ) {
		vGuestItem = new Guest ( vGuest.id, vGuest.text, vGuest.subdate, 
				                 vGuest.forename, vGuest.surname, vGuest.location );
		vGuestArray.push(vGuestItem);	
	}); // each
	return vGuestArray;
} // function f_CreateGuest


// Players
var Player = Class.create({
	initialize: function(ipID, ipCombname, ipForename, ipSurname, ipTeamname, 
			             ipPosID, ipTeamID, ipDOB, ipText, ipPosDesc) {
	this._ID       = ipID;
	this._Combname = ipCombname;
	this._Forename = ipForename;
	this._Surname  = ipSurname;
	this._TeamName = ipTeamname;
	this._PosID    = ipPosID;
	this._TeamID   = ipTeamID;
	this._DOB      = ipDOB;
	this._Text     = ipText;
	this._PosDesc  = ipPosDesc;
}
}); // Player

function f_CreatePlayer(ipJson) {

	// Variables
	var vPlayerArray = [];
	var vPlayerItem;

	// Go through the JSON and create News objects
	ipJson.each( function ( vPlayer ) {
		vPlayerItem = new Player ( vPlayer.id, vPlayer.combname, vPlayer.forename, vPlayer.surname, 
				vPlayer.teamdesc, vPlayer.ppoid, vPlayer.teamid, vPlayer.dob, vPlayer.text, vPlayer.playerpos );
		vPlayerArray.push(vPlayerItem);	
	}); // each
	return vPlayerArray;
} // function f_CreatePlayer
