/**
 * mark-story.com
 * 
 * @author Mark Story <mark@mark-story.com>
 */
var App = (function () {
	var callbacks = [];

	return {
		bootstrap: function () {
			window.addEvent('domready', this.startup);
		},

		register: function (callback) {
			callbacks.push(callback);
		},

		startup: function (e) {
			for (var i = 0; i < callbacks.length; i++) {
				callbacks[i](e);
			}
		}
	};
})();

App.ImageNav = new Class({
	Implements: Options,

	options : {
		selector: '#image-nav dt'
	},

	initialize: function (options){
		this.setOptions(options);

		$$(this.options.selector).each(function(button){
			var slideBox = button.getNext('dd').getElement('ul');
			var myFx = new Fx.Slide(slideBox, {duration : 300});
			if (!button.hasClass('open')) {
				 myFx.hide();
			}
			button.addEvent('click', function(e){
				e.stop();
				myFx.toggle();
			});
		});
	}
});

App.Comments = new Class({

	Implements: Options,

	options: {
		form: 'CommentAddForm',
		loadingText: 'Loading..',
		button: 'input[type=submit]',
		container: 'comment-block',
		list: 'comment-list'
	},

	buttonText: null,

	// Add Ajax to comment forms
	initialize: function() {
		this.setup();
	},

	setup: function () {
		this.makeAddForm();
		this.makePagination();
	},

	// Bind events for the comments form
	makeAddForm: function () {
		var commentForm = $(this.options.form);
		var _this = this;
		var button = commentForm.getElement(this.options.button);

		this.buttonText = button.get('value');

		commentForm.addEvent('submit', function(e) {
			e.stop();
			button.set('value', _this.options.loadingText);
			this.set('send', {onSuccess : _this.loadComments.bind(_this)});
			this.send();
		});
	},
	
	// bind the events for pagination.
	makePagination: function () {
		var commentBlock = $(this.options.container);

		var commentList = $(this.options.list);
		var windowScroll = new Fx.Scroll(window);
		var _this = this;

		commentBlock.getElements('.paging a').each(function (element) {
			element.addEvent('click', function (event) {
				event.stop();
				var commentTween = new Fx.Tween(commentList, {property: 'opacity'});

				commentTween.start(1, 0.1);

				var commentRequest = new Request({
					method: 'get',
					url: element.href,
					onSuccess: function (responseText, responseXml) {
						windowScroll.toElement('comment');
						commentTween.start(0.1, 0).chain(
							function () {
								commentList.set('html', responseText);
								this.callChain();
							},
							function () {
								this.start(0, 1);
								_this.makePagination();
							}
						);
					}
				}).send();
			});
		});
	},

	// load the comments from an xhr request.
	loadComments : function(responseText, responseXml) {
		var updateBlock = $(this.options.container);
		updateBlock.set('html', responseText);
		this.setup();
	}
});

App.CodeBlocks = new Class({
	Implements: Options,
	options : {
		selector: 'div.code',
		hiText: 'Show Highlighted Code',
		loText: 'Show Plain Code'
	},

	initialize: function(options) {
		this.setOptions(options);
		options = this.options;

		$$(this.options.selector).each(function(block){
			var switchButton = block.getPrevious();
			var blockText = block.get('text');
			var plainTextEl = new Element('pre', { 
				'text' : blockText,
				'class' : 'plain-text'
			});
			var htmlText = block.get('html');
			block.store('state', 'code');

			switchButton.addEvent('click', function(e) {
				e.stop();
				var state = block.retrieve('state');
				var _blockText = plainTextEl;
				var _htmlText = htmlText;
				if (state == 'code') {
					this.set('text', options.hiText);
					block.store('state', 'text');
					block.empty().adopt(_blockText);
				} else {
					this.set('text', options.loText);
					block.set('html', _htmlText);
					block.store('state', 'code');
				}
			});
		});
	}
});

App.Bars = new Class({
	Implements: Options,
	options: {
		selector: '.indicator-bars'
	},
	initialize: function (options) {
		this.setOptions(options);
		var indicators = $$(this.options.selector);

		for (var i = 0; i < indicators.length; i++) {
			var indicator = indicators[i];
			var total = 0;
			var bars = indicator.getElements('span');
			//find all the values and make the total
			for (var b = 0; b < bars.length; b++) {
				var barValue = 0;
				var bar = bars[b];
				barValue = parseInt(bar.firstChild.nodeValue, 10);
				total += barValue;
			}
			//adjust the length of each bar.
			for (b = 0; b < bars.length; b++) {
				var bar = bars[b];
				var value = Math.round( (parseInt(bar.firstChild.nodeValue, 10) / total) * 100);
				if (value) {
					bar.getParent().setStyle('width', value+'%');
				}
			}
		}
	}
});


App.Contact = new Class({
	Implements: Options,
	options: {
		selector: 'ContactForm',
		submit: 'input[type=submit]'
	},
	initialize: function (options) {
		this.setOptions(options);
		options = this.options;
		$(this.options.selector).addEvent('submit', function (event) {
			this.getElement(options.submit).set('disabled', 'disabled');
		});
	}
});

// Load the domready event
// Wait for register calls.
App.bootstrap();

