/**************************************
@Clase jsEPG 
***************************************
@Autor: Sergio Cerrillo
@Fecha: 2007/08/23
@Dependencia: xmlParser.js 
@
*/

var jsEPG = Class.create();

jsEPG.prototype = {

	/**********************************************************************************************
	* Constructor
	**********************************************************************************************/
	initialize: function(pdate, idChannel, idEvent, Title, HTMLTitle, startTime, endTime, startHour, endHour, diffHoras, groupId, programId, Genre, Cathegory, shortSynopsis, longSynopsis, chapterNumber, Logo, Relevant){

		/*==========================================================================
		== Variables EPG
		==========================================================================*/
		this._Date=pdate;
		this._idChannel=idChannel;
		this._idEvent=idEvent;
		this._Title=Title;
		this._HTMLTitle=HTMLTitle;
		this._startTime=startTime;
		this._endTime=endTime;
		this._startHour=startHour;
		this._endHour=endHour;
		this._diffHoras=diffHoras;
		this._groupId=groupId;
		this._programId=programId;
		this._Genre=Genre;
		this._Cathegory=Cathegory;
		this._shortSynopsis=shortSynopsis;
		this._longSynopsis=longSynopsis;
		this._chapterNumber=chapterNumber;
		this._Logo=Logo;
		this._Relevant=Relevant;

		/*==========================================================================
		== Variables obligatorias
		==========================================================================*/
		this._objOwner = null;
		this._error=false;
		this._errorTxt="";

	},

	/**********************************************************************************************
	* whoIam
	**********************************************************************************************/
	whoIam: function(){
		alert("jsEPG.js");
	},
	
	/**********************************************************************************************
	* parserEPG
	**********************************************************************************************/
	parserEPG: function(xmlsrc){
		var xotree = new XML.ObjTree();
		tree = xotree.parseXML( xmlsrc );
		var contador=0;
		var cont=0;
		
		var retorno = Array();
		if (tree.epg.day.length!=undefined)
			contador=tree.epg.day.length;
		else if (tree.epg.day!=undefined) contador=1;
			
		debug = "**EPG** \num: "+contador+"\n";
		if (contador){
			if (contador>1){
				for (var i=0; i < contador; i++){
					this._Date=tree.epg.day[i]["-date"];
					debug+=this._Date;
					for (var j=0; j < tree.epg.day[i].Channel.length; j++){
						this._idChannel=tree.epg.day[i].Channel[j]["-id"];
						debug+=" [Channel:"+this._idChannel+"] ";
						for (var x=0; x < tree.epg.day[i].Channel[j].event.length; x++){
							this._idEvent=tree.epg.day[i].Channel[j].event[x]["-id"];
							this._Title=tree.epg.day[i].Channel[j].event[x].title;
							this._startTime=tree.epg.day[i].Channel[j].event[x].startTime;
							this._endTime=tree.epg.day[i].Channel[j].event[x].endTime;
							if (tree.epg.day[i].Channel[j].event[x].groups!=undefined){
								if (tree.epg.day[i].Channel[j].event[x].groups.groupId.length!=undefined){
									for (var y=0; y < tree.epg.day[i].Channel[j].event[x].groups.groupId.length; y++){
										this._groupId[y]=tree.epg.day[i].Channel[j].event[x].groups.groupId[y];
									}
								}else{
										this._groupId[0]=tree.epg.day[i].Channel[j].event[x].groups.groupId;
								}
							}
							this._programId=tree.epg.day[i].Channel[j].event[x].programId;
							this._Genre=tree.epg.day[i].Channel[j].event[x].genre;
							this._Cathegory=tree.epg.day[i].Channel[j].event[x].cathegory;
							this._shortSynopsis=tree.epg.day[i].Channel[j].event[x].shortSynopsis;
							this._longSynopsis=tree.epg.day[i].Channel[j].event[x].longSynopsis;
							this._chapterNumber=tree.epg.day[i].Channel[j].event[x].chapterNumber;
							this._Logo=tree.epg.day[i].Channel[j].event[x].logo;
							debug+= " -"+this._idEvent+"- "+this._Title;
							retorno[cont++] = new jsEPG(this._Date, this._idChannel, this._idEvent, this._Title, this._startTime, this._endTime, this._groupId, this._programId, this._Genre, this._Cathegory, this._shortSynopsis, this._longSynopsis, this._chapterNumber, this._Logo, '');
						}
					}
				}
			}else if (contador==1){
					this._Date=tree.epg.day["-date"];
					debug+=this._Date+" canales: "+tree.epg.day.Channel.length+"\n";
					if (tree.epg.day.Channel.length!=undefined){
						for (var j=0; j < tree.epg.day.Channel.length; j++){
							this._idChannel=tree.epg.day.Channel[j]["-id"];
							debug+=" [Channel:"+this._idChannel+"] ";
							for (var x=0; x < tree.epg.day.Channel[j].event.length; x++){
								this._idEvent=tree.epg.day.Channel[j].event[x]["-id"];
								this._Title=tree.epg.day.Channel[j].event[x].title;
								this._startTime=tree.epg.day.Channel[j].event[x].startTime;
								this._endTime=tree.epg.day.Channel[j].event[x].endTime;
								if (tree.epg.day.Channel[j].event[x].groups!=undefined){
									if (tree.epg.day.Channel[j].event[x].groups.groupId.length!=undefined){
										for (var y=0; y < tree.epg.day.Channel[j].event[x].groups.groupId.length; y++){
											this._groupId[y]=tree.epg.day.Channel[j].event[x].groups.groupId[y];
										}
									}else{
											this._groupId[0]=tree.epg.day.Channel[j].event[x].groups.groupId;
									}
								}
								this._programId=tree.epg.day.Channel[j].event[x].programId;
								this._Genre=tree.epg.day.Channel[j].event[x].genre;
								this._Cathegory=tree.epg.day.Channel[j].event[x].cathegory;
								this._shortSynopsis=tree.epg.day.Channel[j].event[x].shortSynopsis;
								this._longSynopsis=tree.epg.day.Channel[j].event[x].longSynopsis;
								this._chapterNumber=tree.epg.day.Channel[j].event[x].chapterNumber;
								this._Logo=tree.epg.day.Channel[j].event[x].logo;
								debug+= " -"+this._idEvent+"- "+this._Title;
								retorno[cont++] = new jsEPG(this._Date, this._idChannel, this._idEvent, this._Title, this._startTime, this._endTime, this._groupId, this._programId, this._Genre, this._Cathegory, this._shortSynopsis, this._longSynopsis, this._chapterNumber, this._Logo, '');
							}
						}
					}else{
						this._idChannel=tree.epg.day.Channel["-id"];
						debug+=" [Channel:"+this._idChannel+"] ";
						for (var x=0; x < tree.epg.day.Channel.event.length; x++){
							this._idEvent=tree.epg.day.Channel.event[x]["-id"];
							this._Title=tree.epg.day.Channel.event[x].title;
							this._startTime=tree.epg.day.Channel.event[x].startTime;
							this._endTime=tree.epg.day.Channel.event[x].endTime;
							if (tree.epg.day.Channel.event[x].groups!=undefined){
								if (tree.epg.day.Channel.event[x].groups.groupId.length!=undefined){
									for (var y=0; y < tree.epg.day.Channel.event[x].groups.groupId.length; y++){
										this._groupId[y]=tree.epg.day.Channel.event[x].groups.groupId[y];
									}
								}else{
										this._groupId[0]=tree.epg.day.Channel.event[x].groups.groupId;
								}
							}
							this._programId=tree.epg.day.Channel.event[x].programId;
							this._Genre=tree.epg.day.Channel.event[x].genre;
							this._Cathegory=tree.epg.day.Channel.event[x].cathegory;
							this._shortSynopsis=tree.epg.day.Channel.event[x].shortSynopsis;
							this._longSynopsis=tree.epg.day.Channel.event[x].longSynopsis;
							this._chapterNumber=tree.epg.day.Channel.event[x].chapterNumber;
							this._Logo=tree.epg.day.Channel.event[x].logo;
							debug+= " -"+this._idEvent+"- "+this._Title;
							retorno[cont++] = new jsEPG(this._Date, this._idChannel, this._idEvent, this._Title, this._startTime, this._endTime, this._groupId, this._programId, this._Genre, this._Cathegory, this._shortSynopsis, this._longSynopsis, this._chapterNumber, this._Logo,'');
						}

					}
			}
			return retorno;
		}
			
	}
};
