/*Class:
	Meets
	The meets class will create a component with all the information related to meets.
	It will be an array of Meet elements.
		
*/

var Meets = new Class({
        
    length: 0,
    
    initialize: function(xmlFile){
        // Open xmlFile and read it
        var xmlDoc = null;
        
        if (window.ActiveXObject) {// code for IE
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");         
        }
        else if (document.implementation.createDocument) {// code for Mozilla, Firefox, Opera, etc.
            xmlDoc = document.implementation.createDocument("", "", null);
        }

        if (xmlDoc == null) {        
            return;        
        }          
        xmlDoc.async = false;
        xmlDoc.load(xmlFile);   

        var x=xmlDoc.getElementsByTagName("meet");    
        for (var i = 0; i < x.length; i++) { 
            this[i] = new Meet();
            
            // Meets Date            
            if (x[i].getElementsByTagName("date")[0] != null) { 
                this[i].date.day = x[i].getElementsByTagName("date")[0].getElementsByTagName("day")[0].childNodes[0].nodeValue;
                this[i].date.month = x[i].getElementsByTagName("date")[0].getElementsByTagName("month")[0].childNodes[0].nodeValue;
                this[i].date.year = x[i].getElementsByTagName("date")[0].getElementsByTagName("year")[0].childNodes[0].nodeValue;
                this[i].date.longFormat = x[i].getElementsByTagName("date")[0].getElementsByTagName("display_format")[0].childNodes[0].nodeValue;
                this[i].date.shortFormat = this[i].date.month + '-' + this[i].date.day + '-' + this[i].date.year;
                if (x[i].getElementsByTagName("date")[0].getElementsByTagName("days")[0] !=null) {
                    this[i].date.days = x[i].getElementsByTagName("date")[0].getElementsByTagName("days")[0].childNodes[0].nodeValue;
                }
            }           
           
            // Meets Joint Walk
            if (x[i].getElementsByTagName("joint_walk")[0] != null) {                             
                this[i].joint = Boolean(x[i].getElementsByTagName("joint_walk")[0].childNodes[0].nodeValue);
            }            
            // Meets Title
            if (x[i].getElementsByTagName("title")[0] != null) { 
                this[i].title = x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
            }
            
            // Meets Type
            if (x[i].getElementsByTagName("type")[0] != null) { 
                this[i].type = x[i].getElementsByTagName("type")[0].childNodes[0].nodeValue;
            }
            
            // Meets Time
            if (x[i].getElementsByTagName("time")[0] != null) { 
                this[i].time = x[i].getElementsByTagName("time")[0].childNodes[0].nodeValue;
            }
            
            // Meets Destination
            if (x[i].getElementsByTagName("destination")[0] != null) { 
                this[i].destination = x[i].getElementsByTagName("destination")[0].childNodes[0].nodeValue.replace("'","\\'");
            }            
                                    
            // Meet Directions
            if (x[i].getElementsByTagName("directions")[0] != null) { 
                var directions = x[i].getElementsByTagName("directions")[0].getElementsByTagName("direction");            
                for (var j = 0; j < directions.length; j++) {                    
                    this[i].directions[j] = directions[j].childNodes[0].nodeValue;
                }       
            }
            
            // Meet Via
            if (x[i].getElementsByTagName("via")[0] != null) { 
                this[i].via = x[i].getElementsByTagName("via")[0].childNodes[0].nodeValue;
            }
                        
            // Meet Stops 
            if (x[i].getElementsByTagName("stops")[0] != null) {
                var stops = x[i].getElementsByTagName("stops")[0].getElementsByTagName("stop");            
                for (var j = 0; j < stops.length; j++) {
                    this[i].stops[j] = stops[j].childNodes[0].nodeValue ;            
                }
            }
            
            // Meet Notes
            if (x[i].getElementsByTagName("notes")[0] != null) {
                var notes = x[i].getElementsByTagName("notes")[0].getElementsByTagName("note");            
                for (var j = 0; j < notes.length; j++) {
                    this[i].notes[j] = notes[j].childNodes[0].nodeValue ;            
                }
            }            
            
            // Meets Special Event            
            if (x[i].getElementsByTagName("special_event")[0] != null) {
                this[i].specialEvent.name = x[i].getElementsByTagName("special_event")[0].getElementsByTagName("name")[0].childNodes[0].nodeValue;
                this[i].specialEvent.icon = x[i].getElementsByTagName("special_event")[0].getElementsByTagName("icon")[0].childNodes[0].nodeValue;
                this[i].specialEvent.info = x[i].getElementsByTagName("special_event")[0].getElementsByTagName("info")[0].childNodes[0].nodeValue;    
            }           
        }
        this.length = x.length;
    },
    
    getNearestMeet: function(){
		var nearestMeet = this[0];
		// CHANGE
		var today = new Date();
		//var today = new Date(2009,0,1);		
		for (i = 0; i < this.length; i++) {     		    
            if ( ((this[i].date.month == today.getMonth() + 1) && (this[i].date.day >= today.getDate())) || (this[i].date.month > today.getMonth() + 1)   ) {
                nearestMeet = this[i];
                return nearestMeet;
            }                    
        }   
        return nearestMeet;         
	},	
	
	getLastMeetDate: function(){
		return this[this.length - 1].date;        
	},	
	
    getNearestDate: function(){
        return this.getNearestMeet().date.shortFormat;		
	},	
	
	getNearestMeetType: function(){
	    return this.getNearestMeet().type;		
	}

});


var Meet = new Class({
		
	joint:false,
	title:'',
	type:'',
	destination:'',	
	date:{
	    day:1,
	    month:0,
	    year:1900,
	    shortFormat:'',
	    longFormat:'',
	    days:1
	    },	
	time:'7:30',
	directions:[],
	via:'',
	stops:[],
	notes:[],
	specialEvent: {
	    name:'',
        icon:'',
        info:''
	    },
	
	/*
		Function:
			isPtarmiganMeet
		
		Returns:
			Boolean. True if is a Ptarmigan Meet
			
	*/
	isPtarmiganMeet: function (){
		var type = this.type.toLowerCase();
		return (type == "ptarmiganmeet");
	}
});
