Element.extend({
	getElementInParents: function(selector) {
		if ($chk(this.getParent())) {
			var test = this.getParent().getElement(selector);
			if ($chk(test) && test != false) return(test);
			else return(this.getParent().getElementInParents(selector));
		}
		else return (false);
	},
	getParentByClass: function(selector) {
		if ($type(this.getParent()) == 'element') {
			if (this.getParent().hasClass(selector)) return(this.getParent());
			else return(this.getParent().getParentByClass(selector));
		}
		else return(false);
	},
	getClass: function() {
		return this.getProperty('class');
	},
	setClass: function(str) {
		this.setProperty('class', str);
		return this;
	},
	setValue: function(str) {
		this.setProperty('value', str);
		return this;
	},
	getPropertiesArray: function(pArr) {
		// Takes an array of properties and returns an array of values in the same order
		var rArr = [];
		for (var i = 0; p = pArr[i]; i++) {
			rArr[i] = this.getProperty(pArr[i]);
		}
		return rArr;
	},
	isUnder: function(client) {
		var c = this.getCoordinates();
		if ((client.x > c.left) && (client.x < c.left + c.width) && (client.y > c.top) && (client.x < c.left + c.width)) return true;
		else return false;
	}
});

var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

var Page = {

	init: function() {
		this.Feeds.init();
	},

	start: function() {
		Page.Format.start();
		Page.Form.start($E('form'));
		Page.Feeds.start();
	},
	
	Feeds: {
		init: function() {
			this.blogFeed = new google.feeds.Feed("http://heybuddycanada.blogspot.com/feeds/posts/default?alt=rss");
		},
		start: function() {
			if ($chk($('blogFeedDiv'))) { this.show($('blogFeedDiv')); }
		},
		show: function(el) {
			this.blogFeed.load(function(result) {
				if (!result.error) {
					var post, postDate;
					for (var i = 0; post = result.feed.entries[i]; i++) {
						postDate = new Date(post.publishedDate);
						el.adopt(
							new Element('div', {'class': 'post'}).adopt(
								new Element('a', {
									'href': post.link
								}).setText(post.title)
							).adopt(
								new Element('span', {
									'class': 'date'
								}).setText(
									months[postDate.getMonth()] + ' ' + postDate.getDate()
								)
							)
						);
					}
				}
			});	
		},
	},

	Format: {
		start: function() {
			$$('table.autoColor').each(Page.Format.alternateRows);
		},
		alternateRows: function(el) {
			el.getChildren()[0].getChildren().each(function(el, i){
				if (!el.hasClass('gridHeader') && !el.hasClass('gridFooter') && !el.hasClass('gridPager')) {
					if (i % 2) el.setClass('gi');
					else el.setClass('ga');
				}
			});
		}
	},

	Form: {
		elForm: null,
		// Only works with 1 form on page
		start: function(elForm) {
			if ($chk(elForm) && $chk($('submit'))) {
				this.elForm = elForm;
			
				// Set click
				$('submit').addEvent('click', Page.Form.click);
				new Element('span', {'id': 'submitWait', 'class': 'wait'}).setStyle('display', 'none').setText('Please wait').injectAfter($('submit'));

				// Sytle required fields
				this.elForm.getElements('input.required').each(function (el) {
					new Element('span', { 'class': 'required' }).setText('Required').injectAfter(el);
				});

				// Build request
				this.request = new XHR({
					onSuccess: this.result.bind(this),
					onFailure: this.error.bind(this),
					method: 'get'
				});
			}
		},
		validate: function() {
			this.elForm.getElements('span.msg').each(function (el) {
				el.remove();
			});
			var pass = true;
			this.elForm.getElements('input.required').each(function (el) {
				if (el.getValue() == '') pass = false;
			});
			if (!pass) {
				new Element('span', { 'class': 'msg error' }).setText('Required field(s) missing or invalid').injectTop(this.elForm);
			}
			return pass;
		},
		send: function() {
			this.request.send('forms/' + this.elForm.getProperty('action'), this.elForm.toQueryString());
		},
		result: function() {
			if (this.request.response.text == "OK") {
				new Element('span', { 'class': 'msg ok' }).setText('Form sent').injectTop(this.elForm);
			}
			else {
				new Element('span', { 'class': 'msg error' }).setText('Required field(s) missing or invalid').injectTop(this.elForm);
				$('submit').setStyle('display', 'inline');
				$('submitWait').setStyle('display', 'none');
			}
		},
		error: function() {
			new Element('span', { 'class': 'msg error' }).setText('Unknown error').injectTop(this.elForm);
			$('submit').setStyle('display', 'inline');
			$('submitWait').setStyle('display', 'none');
		},
		click: function(event) {
			if (Page.Form.validate()) {
				Page.Form.send();
				$('submit').setStyle('display', 'none');
				$('submitWait').setStyle('display', 'inline');
			}
		}
	}
}

Page.init();
window.addEvent('domready', Page.start);