/**
 * @author christophertossing
 */

poth = {};

var gNow = new Date();

poth.gInstructor = {
	name : 'Gaia Tossing',
	credentials : ['FCCI', 'LMT', 'RCR', 'BCRS', 'LSH'],
	phNum : '(615) 496-3366'
};

setupPoth = function() {
	// setup poth.year
	var curYear = gNow.getYear()+1900;
	poth.year = curYear.toString() + ' - ' + (curYear+1).toString();
	
	// setup ...
};
setupPoth();

poth.location = function(config) {
	this.init = function() {
		this.name = null;
		this.street = null;
		this.city = null;
		this.state = null;
		this.zip = null;
		for (var key in config)
			this[key] = config[key];
	};
	this.init();
	this.getCityStateStr = function() {
		var retStr = (this.street) || '';
		retStr += ((retStr.length > 0 ? ', ' : '') + this.city) || '';
		retStr += ((retStr.length > 0 ? ', ' : '') + this.state) || '';
		retStr += (retStr.length > 0 ? ' ' : '') + ((this.zip) || '');
		return retStr;
	};
	this.getAddrStr = function() {
		var retStr = (this.street) || '';
		retStr += ((retStr.length > 0 ? ', ' : '') + this.city) || '';
		retStr += ((retStr.length > 0 ? ', ' : '') + this.state) || '';
		retStr += ((retStr.length > 0 ? ' ' : '') + this.zip) || '';
		return retStr;
	};
};

// datesRangeStr, eventTitle, locationStreet, locationCity, locationState
// endDate
poth.eventObj = function(config) {
	this.init = function() {
		for (var key in config)
			this[key] = config[key];
		if (!this.location) {
			this.location = new poth.location({
				name:this.location_name,
				street:this.location_street,
				city:this.location_city,
				state:this.location_state,
				zip:this.location_zip
			});
		}
	};
	this.init();
	this.getListItemHtml = function() {
		if (gNow.getTime() >= this.exp_date.getTime())
			return '';
		else {
			var tmpHtml = '';
			tmpHtml += '<li>';
			tmpHtml += this.dates_range_string + ' - "<i>' + this.title
					+ '</i>"<br />';
			tmpHtml += this.location.name + ', ';
			tmpHtml += (this.location.getCityStateStr() + '<br />') || '';
			tmpHtml += '</li>';
			return tmpHtml;
		}
	};
};

poth.gEventListHeaderHtml = 'Note: Classes taught in CARE Intensives may also be taken individually.'
		+ ' The individual classes are Vita Flex, Raindrop Technique, Emotional Release,'
		+ ' Essential Oil Chemistry, and Healing Oils of the Bible. A complete description'
		+ ' of the intensive format can be found at <a href="http://www.raindroptraining.com" target="_blank">www.raindroptraining.com</a>';

poth.insertHtmlBeforeBegin = function(insertText) {
	var thisEl = Ext.get(this);
	thisEl.insertHtml('afterBegin', insertText);
};

poth.loadSpans = function(elSearchName, insertHtml) {
	var tmpArr = Ext.select('.'+elSearchName).elements;
	for (var key in tmpArr) {
		if (typeof(tmpArr[key]) == 'object')
			poth.insertHtmlBeforeBegin
					.apply(Ext.get(tmpArr[key]), [insertHtml]);
	}
};

poth.load = function() {
	poth.loadSpans('gEventListHeaderText', poth.gEventListHeaderHtml);
	poth.loadSpans('poth_gInstructor_name', poth.gInstructor.name);
	poth.loadSpans('poth_gInstructor_credentials', '('
					+ poth.gInstructor.credentials.join(',') + ')');
	poth.loadSpans('poth_gInstructor_phNum', poth.gInstructor.phNum);
	poth.loadSpans('poth_year', poth.year);
	//poth.insertHtmlBeforeBegin.apply(Ext.get('poth.phone'),[poth.gContactNum]);
	//Ext.select('pothPhone').each(function(){poth.insertHtmlBeforeBegin.apply(this,[poth.gContactNum]);});

	poth.events = new Ext.data.JsonStore({
		url: 'get_events.php',
	    root: 'data',
	    fields: ['id', 'dates_range_string', 'title', 'location_name', 'location_street', 'location_city', 'location_state', 'location_zip', {name:'exp_date', type:'date', dateFormat: 'Y-m-d'}],
	    autoLoad: true,
	    listeners: {
		    load : {
		        fn: function() {
	    			poth.loadSpans('poth_eventListItems',poth.getEventListItemsHtml(poth.events));
		        },
		        scope: this,
		        delay: 100
	    	}
	    }
	});
};

poth.getEventListItemsHtml = function(eventsDataStore) {
	var tmpHtml = '<ul>';
	var items = eventsDataStore.data.items;
	for (var i=0; i<items.length; ++i) {
		tmpHtml += poth.getEventListItemHtml(items[i].data);
	}
	tmpHtml += '</ul>';
	return tmpHtml;
};

poth.getEventListItemHtml = function(eventData) {
	var eventObj = new poth.eventObj(eventData);
	return eventObj.getListItemHtml();
};

Ext.onReady(poth.load);
