/**************************************
@Clase jsProgram 
***************************************
@Autor: Sergio Cerrillo
@Fecha: 2007/09/04
@Dependencia: jsProgram.js 
@
*/

var jsProgram = Class.create();

jsProgram.prototype = {

	/**********************************************************************************************
	* Constructor
	**********************************************************************************************/
	initialize: function(programId, title, groupId, genre, cathegory, shortSynopsis, longSynopsis, chapterNumber, moralCalification, artCritic, comercialCritic, year, persons, productors,newsassociated){

		/*==========================================================================
		== Variables Program
		==========================================================================*/
		this._programId= programId;
		this._title= title;
		this._groupId= groupId;
		this._genre= genre;
		this._cathegory= cathegory;
		this._shortSynopsis= shortSynopsis;
		this._longSynopsis= longSynopsis;
		this._chapterNumber= chapterNumber;
		this._moralCalification= moralCalification;
		this._artCritic= artCritic;
		this._comercialCritic= comercialCritic;
		this._year= year;
		this._persons= persons;
		this._productors= productors;
		this._newsassociated= newsassociated;

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

	},

	/**********************************************************************************************
	* whoIam
	**********************************************************************************************/
	whoIam: function(){
		alert("jsProgram.js");
	},
	
	/**********************************************************************************************
	* parser
	**********************************************************************************************/
	parser: function(xmlsrc){
		var xotree = new XML.ObjTree();
		tree = xotree.parseXML( xmlsrc );
		var contador=0;
		var cont=0;
		
		var retorno = Array();
		if (xmlsrc!="" && tree && tree.programs)
		{
			if (tree.programs.program.length!=undefined)
				contador=tree.programs.program.length;
			else if (tree.programs.program!=undefined) contador=1;
				
			debug = "**PROGRAM** \num: "+contador+"\n";
			if (contador){
				this._persons = Array();
				this._productors = Array();
				if (contador>1){
					for (var i=0; i < contador; i++){
						this._programId= tree.programs.program[i]["-id"];
						this._title= tree.programs.program[i].title;
						this._groupId= tree.programs.program[i].groupId;
						//this._genre= tree.programs.program[i].genre;
						this._cathegory= tree.programs.program[i].cathegory;
						this._shortSynopsis= tree.programs.program[i].shortSynopsis;
						this._longSynopsis= tree.programs.program[i].longSynopsis;
						this._chapterNumber= tree.programs.program[i].chapterNumber;
						this._moralCalification= tree.programs.program[i].moralCalification;
						this._artCritic= tree.programs.program[i].artCritic;
						this._comercialCritic= tree.programs.program[i].comercialCritic;
						this._year= tree.programs.program[i].year;
						this._newsassociated= tree.programs.program[i].NewsAssociated;
						if (tree.programs.program[i].persons!=undefined){
							if (tree.programs.program[i].persons.person.length!=undefined){
								for (var j=0; j < tree.programs.program[i].persons.person.length; j++){
									this._persons[j] = {
										"id":tree.programs.program[i].persons.person[j]["-id"],
										"name":tree.programs.program[i].persons.person[j],
										"idrol":tree.programs.program[i].persons.person[j]["-idrol"]
									};
								}
							}else{
								this._persons[0] = {
									"id":tree.programs.program[i].persons.person[j]["-id"],
									"name":tree.programs.program[i].persons.person[j],
									"idrol":tree.programs.program[i].persons.person[j]["-idrol"]
								};
							}
						}else this._persons=Array();
						if (tree.programs.program[i].productors!=undefined){
							if (tree.programs.program[i].productors.productor.length!=undefined){
								for (var j=0; j < tree.programs.program[i].productors.productor.length; j++){
									this._productors[j]= tree.programs.program[i].productors.productor[j];
								}
							}else{
								this._productors[0] = tree.programs.program[i].persons.person;
							}
						}else this._productors=Array();
						retorno[cont++] = new jsProgram(this._programId, this._title, this._groupId, "", this._cathegory, this._shortSynopsis, this._longSynopsis, this._chapterNumber, this._moralCalification, this._artCritic, this._comercialCritic, this._year, this._persons, this._productors,this._newsassociated);
					}
				}else if (contador==1){
					this._programId= tree.programs.program["-id"];
					this._title= tree.programs.program.title;
					this._groupId= tree.programs.program.groupId;
					//this._genre= tree.programs.program.genre;
					this._cathegory= tree.programs.program.cathegory;
					this._shortSynopsis= tree.programs.program.shortSynopsis;
					this._longSynopsis= tree.programs.program.longSynopsis;
					this._chapterNumber= tree.programs.program.chapterNumber;
					this._moralCalification= tree.programs.program.moralCalification;
					this._artCritic= tree.programs.program.artCritic;
					this._comercialCritic= tree.programs.program.comercialCritic;
					this._year= tree.programs.program.year;
					this._newsassociated= tree.programs.program.NewsAssociated;
					if (tree.programs.program.persons!=undefined && tree.programs.program.persons.person){
						if (tree.programs.program.persons.person.length!=undefined){
							for (var j=0; j < tree.programs.program.persons.person.length; j++){
								this._persons[j] = {
									"id":tree.programs.program.persons.person[j]["-id"],
									"name":tree.programs.program.persons.person[j],
									"idrol":tree.programs.program.persons.person[j]["-idrol"]
								};
							}
						}else{
							this._persons[0] = {
								"id":tree.programs.program.persons.person["-id"],
								"name":tree.programs.program.persons.person,
								"idrol":tree.programs.program.persons.person["-idrol"]
							};
						}
					}else this._persons=Array();
					if (tree.programs.program.productors!=undefined && tree.programs.program.productors.productor){
						if (tree.programs.program.productors.productor.length!=undefined){
							for (var j=0; j < tree.programs.program.productors.productor.length; j++){
								this._productors[j]= tree.programs.program.productors.productor[j];
							}
						}else{
							this._productors[0] = tree.programs.program.productors.productor;
						}
					}else this._productors=Array();
					retorno[cont++] = new jsProgram(this._programId, this._title, this._groupId, "", this._cathegory, this._shortSynopsis, this._longSynopsis, this._chapterNumber, this._moralCalification, this._artCritic, this._comercialCritic, this._year, this._persons, this._productors,this._newsassociated);
				}
			}
		}
	    
	    return retorno;
	}
};
