/* represent a Song from MBL

*/

function SongsDatabase(xml){
	// ==============
	// = Attributes =
	// ==============
	this.raw_xml = xml || 'blank';
	
	// ===========
	// = Methods =
	// ===========
	this.database = function(){return $(this.raw_xml);};
	this.first = function(){return new Song(this.database.find('song:first').html())}
	this.find = function(id){
		return new Song(this.database.find('song[id=' + id + ']:first').html());
	}
	
}

function Song (xml) {
    if(xml){}else{xml = $('')}
    //any attributes you just add them here
    this.xml = xml;	
    this.id = xml.attr('id');
    this.tracks = new Array();
    this.cdnum = xml.find('cdnum:first').text();
    this.cdtitle = xml.find('cdtitle:first').text();
    this.cdtitlelong = this.cdnum + ": " + xml.find('cdtitle:first').text();
    this.title = xml.find('title:first').text();			//Song Title
    this.stitle = xml.find('stitle:first').text();
    this.desc = xml.find('desc:first').text();				//description
    this.composers = xml.find('composers:first').text();	//composers
    this.publishers = xml.find('pub:first').text();			//publishers
    this.tempo = xml.find('tempo:first').text();			//tempo
    this.instrumentation = xml.find('inst:first').text();	//Instrument
    this.release_date = xml.find('releasedate:first').text();
    this.theme = xml.find('songtheme:first').text();
    this.duration = xml.find('tracks t:first').attr('d');
    this.library = xml.find('library:first').text();
    this.tracks_xml = xml.find('tracks');
    this.mood = xml.find('mood:first').text();				//Mood
    this.genre = xml.find('genre:first').text();			//Genre



    


    var me = this;
    var track_numbers = new Array();
    xml.find('tracks t').each(function(){
      var track = new Track($(this));
      if(track_numbers.includes(track.trknum)){}else{
        me.tracks.push(track);
        track_numbers.push(track.trknum);
      }
    });
    
    
}


function Track (xml) {
	
	//any attributes you just add them here
    this.id = xml.attr('id');
    this.trknum = xml.attr('trknum');
    this.title = xml.find('title:first').text();
    this.stitle = xml.find('stitle:first').text();
    this.desc = xml.find('desc:first').text();   
    this.sdesc =  xml.find('sdesc:first').text();
    
    
    //write a function to generate a row for the main listing 
    this.rowInTrackList = function(){
    	return "<tr sid=\""+  this.id +"\"><td><nobr class='col_cd'>" + this.title+ "</nobr></td><td><nobr class='col_song'>" + 
								this.stitle + "</nobr></td><td><nobr class='col_desc'>" + 
								this.desc + 
							"</nobr></td></tr>";
    }
    
    this.getInfo = function() {
        return this.color + ' ' + this.type + ' apple';
    };
    
    
}

