/**
 * mark-story.com
 * 
 * @author Mark Story <mark@mark-story.com>
 */
var MS = {};

//Start every other 'module'
MS.init = function(){
	for(prop in this){
		if(typeof this[prop].init == 'function'){
			this[prop].init();
		}
	}
}

/**
 * Illustration side nav area.
 */
MS.imageNav = {
	/**
	 * Add event handlers
	 */
	init: function(){
		var imageNav = $('image-nav');
		if (!imageNav) return;
		
		$$('#image-nav dt').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){
				myFx.toggle();
				e = new Event(e).stop();
			});
		});
	}
};

MS.comments = {
	// Add Ajax to comment forms
	init: function() {
		this.makeAddForm();
		this.makePagination();
	},

	// Bind events for the comments form
	makeAddForm: function () {
		var commentForm = $('CommentAddForm');
		if (!commentForm) return false; 
		commentForm.addEvent('submit', function(e) {
			e.stop();
			var loading = new Element('span', {'text' : 'Loading..', 'class' : 'loading-spinner'})
			this.adopt(loading);
			this.set('send', {onSuccess : MS.comments.loadComments})
			this.send();
		});
	},
	
	// bind the events for pagination.
	makePagination: function () {
		var commentBlock = $('comment-block');

		if (!commentBlock) return false;
		var commentList = $('comment-list');
		var windowScroll = new Fx.Scroll(window);

		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);
								MS.comments.makePagination();
							}
						);
					}
				}).send();
			});
		});
	},

	// load the comments from an xhr request.
	loadComments : function(responseText, responseXml) {
		var updateBlock = $('comment-block');
		updateBlock.set('html', responseText);
		MS.comments.init();
	}
};

/**
 *  Make Dynamically Sized Indicator Bars
 *
 */
MS.indicatorBars = {
	init : function() {
		var indicators = $$('.indicator-bars');
		if (!indicators) return null;

		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 (var 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+'%');
				}
			}
		}
	}
};

MS.preBlocks = {
	init: function() {
		var codeBlocks = $$('div.code');
		codeBlocks.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', 'Show Highlighted Code');
					block.store('state', 'text');
					block.empty().adopt(_blockText);
				} else {
					this.set('text', 'Show Plain Text');
					block.set('html', _htmlText);
					block.store('state', 'code');
				}
			});
		}, this);
	},
	
	getText : function(block) {
		var textLines = [];
		block.getElements('li').each(function(line) {
			var text = line.get('text');
			textLines.push(text)
		});
		return textLines.join("\n");
	}
}

MS.contactForm = {
	init: function () {
		var form = $('ContactForm');
		if (!form) return;
		form.addEvent('submit', function (event) {
			this.getElement('input[type=submit]').set('disabled', 'disabled');
		});
	}
}

window.addEvent('domready', function() {
	MS.init();
});