/**
 * Renders the event list for the specified user on the gridmob site
 */

gm.widget.eventList = {
	
	/**
	 * Holds the URL's used to get the list of events
	 */
	_URL : {
		localEvents : 'events.xml',
		events : "http://" + location.hostname + '/api/events.php',
		eventsPicPage : 'event_pics.html'
	},

	/**
	 * Holds the html strings used in the picture blog
	 */
	_HTML : {
		loading : '<div class="loadingDiv">Loading ...</div>',
		item :    '<a href="${link}">${name} = ${date}</a>'
	},
	
	/**
	 * Holds the xml data
	 * @type XMLDocument
	 */
	_x : null,
	
	/**
	 * holds the data
	 * @type Array
	 */
	_d : [],
	
	
	/**
	 * ref to the artist id
	 * @type int
	 */
	_artistid : gm.artistid || 3,
	
	/**
	 * State trackers
	 * @type boolean
	 */
	isInit : false,
	
	
	/**
	 * Tracks the total number of events
	 * @type int
	 */
	totalRecords : 0,
	
	/**
	 * 
	 */
	showMoreLink : 0,
	
	
	/**
	 * function initializes the object / html
	 */
	init : function() {
		if (gm.isDebug) {
			this._URL.events = this._URL.localEvents;
		}
		this.render();
		this.isInit = true;
	},
	
	/**
	 * Gets the data
	 */
	getEventData : function() {
		pel = $('eventList');
		pel.innerHTML = this._HTML.loading;
		this._x = gm.util.AJAX.syncRequest(this._URL.events + "?program_ids=" + this._artistid);
		this._d = this._x.getElementsByTagName("item");
		this.totalRecords = this._x.firstChild.getAttribute("totalRecords");
	},

	/**
	 * Renders the event data
	 */
	render : function() {
		if (!this._x) {
			this.getEventData();
		}

		var i=0,el,u,t,a,m,s=this.showMoreLink - 1,renderedItems = 0;
		el = $('eventList');
		el.innerHTML = "";
		
		for (i = 0;i<this._d.length;i++) {
			// create the element
			t = document.createElement("div");
			t.className = "item";

			// set the display to add a special class to items over the cutoff
			if (s > 0 && i >= s) {
				t.className += " over";
			}

			// add the more link if we have the appropriate number or rendered items
			if (s > 0 && i == s && this._d.length != i) {
				m = document.createElement("a");
				m.innerHTML = "more ..."
				m.className = "moreLink"
				YAHOO.util.Event.addListener(m, "click", function() {gm.util.addClass(this.parentNode,"showAll");}); 
				el.appendChild(m);
			}
			
			// create the event link
			a = document.createElement("a");
			if (this._d[i].getAttribute('messagecount') == "0") {
				a.innerHTML = this._d[i].getAttribute('eventname');;
				a.className = "disabled";
			} else {
				u = this._URL.eventsPicPage + "?eventid=" + this._d[i].getAttribute('eventid') + "&program_ids=" + this._artistid;
				u +=  (gm.isDebug) ? "&gmdebug=true" : "";
				a.innerHTML = this._d[i].getAttribute('eventname');
				a.href = u;
			}

			el.appendChild(t);
			t.appendChild(a);
		}
	},

	/**
	 * Unloads the content and variables in this object
	 */
	unload : function(){
		this._x = null;
		this._d = null;
		this._HTML = null;
		this._URL = null;
	}
};


