var trim = function(s) {
	while(s.substring(0,1)==' ') {
		s=s.substring(1,s.length);
	}
	while(s.substring(s.length-1,s.length)==' ') {
		s=s.substring(0,s.length-1);
	}
	return s;
};

/*
Class: CommentForm
	Description:	Setup Comment Form Elements
*/
var CommentForm = new Class({ 
	
	initialize: function(cform) {
		this.cform = cform;
		var thisObj = this;
		
		this.textarea = this.cform.getElement('textarea');
		this.post = this.cform.getElement('button.post').addClass('hide');
		
		this.inputName = this.cform.getElement('input.name');
		this.inputEmail = this.cform.getElement('input.email');
		this.inputURL = this.cform.getElement('input.url');

		if (this.inputName.value == "") {
			this.inputName.value = "Name"; this.inputName.addClass('default'); }
		if (this.inputEmail.value == "") {
			this.inputEmail.value = "Email"; this.inputEmail.addClass('default'); }
		if (this.inputURL.value == "http://") {
			this.inputURL.value = "http://"; this.inputURL.addClass('default'); }


		this.cform.getElements('input.textfield').each(function(input, i) {
			var defaultText = input.value;
			input.addEvents({
				'focus':	function(e) {
						// Clear Form Value
						if (input.hasClass('default')) { input.value = ""; }
						input.removeClass('default').addClass('focus');
					},
				'blur':		function(e) {
						input.removeClass('focus');
						if (trim(input.value)=='') {
							input.addClass('default');
							input.value = defaultText;
						}
					}
			});
		});
		this.textarea.addEvents({
			'focus':		function(e) {
					this.addClass('focus'); thisObj.togglePostButton();
				},
			'blur':			function(e) {
					thisObj.togglePostButton(); this.removeClass('focus');
				},
			'mousedown':	function(e) { thisObj.togglePostButton(); },
			'mouseup':		function(e) { thisObj.togglePostButton(); },
			'keydown':		function(e) { thisObj.togglePostButton(); },
			'keyup':		function(e) { thisObj.togglePostButton(); },
			'keypress':		function(e) { thisObj.togglePostButton(); }
		});
	},

	togglePostButton: function() {
		if (trim(this.textarea.value)!='') {
			this.post.removeClass('hide');
		} else {
			this.post.addClass('hide');
		}
	}

});
CommentForm.implement(new Options, new Events);




/*
Class: AutoExpandTextArea
	Integrated from autoexpand.js

	Author: Scrivna
	WWW:	http://scrivna.com
	Description:	Will automagically resize any textarea... that's amazing
					So far it works on IE7, Firefox 2, Safari 3 and Opera 

	Usage:
	Simply use a classname of "autoExpand" on your textarea and include this file in your html
	If you are doing something a bit more fancy ie ajax, you can simply call autoExpand(document.getElementById('yourtextarea')) to auto expand that area
*/
var AutoExpandTextArea = new Class({

	initialize: function(){
		this.ae_debug = false;
		var thisObj = this;
			
		$$('body textarea.autoExpand').each(function(textarea, i) {
			thisObj.autoExpand(textarea);
		});
	},
	
	getStyle: function(el, style) {
		if(!document.getElementById) return;
		var value = el.style[this.toCamelCase(style)];
		if(!value) {
			if(document.defaultView) {
				value = document.defaultView.getComputedStyle(el, "").getPropertyValue(style);
			} else if(el.currentStyle) {
				value = el.currentStyle[this.toCamelCase(style)];
			}
		}
		if(value.length>0) {
			value = ((value.charAt(value.length-1,1) == "x") ? parseInt(value.substring(0,value.length-2)) : value);
		}
		return value;
	},
	
	toCamelCase: function(sInput) {
		var oStringList = sInput.split('-');
		if(oStringList.length == 1)   
			return oStringList[0];
		var ret = sInput.indexOf("-") == 0 ?
		   oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1) : oStringList[0];
		for(var i = 1, len = oStringList.length; i < len; i++){
			var s = oStringList[i];
			ret += s.charAt(0).toUpperCase() + s.substring(1)
		}
		return ret;
	},

	autoExpand: function(t) {
		var textarea = t;
		var __minHeight = textarea.offsetHeight;
		var a = parseInt(textarea.getStyle('min-height'));
		if (a>0) __minHeight = a;
	
		// adjust the textarea
		textarea.setStyles({
			'overflow': 'hidden',
			'overflowX': 'auto',		
			// set the width and height to the correct values
			'width': this.getStyle(textarea, 'width')+'px',
			'height': this.getStyle(textarea, 'height')+'px'
		});
		
		// create a new element that will be used to track the dimensions
		var dummy_id = Math.floor(Math.random()*99999) + '_dummy';
		
		// match the new elements style to the textarea
		var dummy = new Element('div').setProperty('id', dummy_id).injectInside(document.body);

		var copiedStyles = textarea.getStyles('font-family', 'font-weight', 'font-size', 'width', 'padding', 'margin');
		dummy.setStyles(copiedStyles);
		dummy.setStyles({
			'overflowX': 'auto',
			// hide the created div away
			'position': 'absolute',
			'top': '0px',
			'left': '-9999px'
		}).setHTML('&nbsp;42');
				
		var __lineHeight = dummy.offsetHeight;
		
		var checkExpand = function(){
			var html = textarea.value;
			html = html.replace(/\n/g, '<br>new');
			if (dummy.innerHTML != html)
			{
				dummy.innerHTML = html;
				var __dummyHeight = dummy.offsetHeight;
				var __textareaHeight = textarea.offsetHeight;
				ae_debug('Textarea: '+ __textareaHeight);
				ae_debug('Dummy: '+ __dummyHeight);
				if (__textareaHeight != __dummyHeight)
				{
					if (__dummyHeight > __minHeight)
					{
						textarea.style.height = (__dummyHeight+__lineHeight) + 'px';
					}
					else 
					{
						textarea.style.height = __minHeight+'px';
					}
				}
			}
		}
		
		var ae_startExpand = function()
		{
			interval = window.setInterval(function() {checkExpand()}, 500);
		}
		var ae_stopExpand = function()
		{
			clearInterval(interval);
		}
		
		var ae_debug = function(str)
		{
			if(ae_debug==true) document.getElementById('debug').innerHTML += '<br />'+str;
		}
		
		textarea.addEvents({
			'focus': ae_startExpand,
			'blur': ae_stopExpand
		});
		checkExpand();
	}


});
AutoExpandTextArea.implement(new Options, new Events);








/*
Class: ImagesBrowser
	Description:	Setup Image Browsing
*/
var ImageBrowser = new Class({

	initialize: function(form) {
		var thisObj = this;
	}

});
ImageBrowser.implement(new Options, new Events);
















window.addEvent('domready', function() {





// Load Top Image
var topImg = $('topImg');
var numOfImgs = topImg.getProperty('class');
var topImgTrigger = new Element('div').addClass('trigger').injectInside(topImg);
var topImgLoading = new Element('div').addClass('loading').injectAfter(topImgTrigger);
var loadingFx = new Fx.Style(topImgLoading, 'opacity', {
		wait: false, duration: 300
	});	

var iniImg = topImg.getElement('img');
var imgPath = iniImg.getProperty('src');
new Asset.image(imgPath, {
	onload: function() {
		iniImg.setOpacity(0).removeClass('hide').effect('opacity', {
			wait: false, duration: 300
		}).start(0, 1);
		loadingFx.start(1, 0);
	}
});
var originalImg;
topImgTrigger.addEvent('click', function(e) {
	e = new Event(e).stop();
	originalImg = topImg.getElement('img');
	originalImg.effect('opacity', {
		wait: false, duration: 300, transition: Fx.Transitions.linear
	}).start(1, 0.4);
	loadingFx.start(0, 1);
	
	var randomImg = Math.ceil(numOfImgs*Math.random());
	var newPath = imgPath.substring(0,imgPath.indexOf('top/top')+7) + randomImg + '.jpg';

	new Asset.image(newPath, {
		onload: function() {
			originalImg.effect('opacity', {
				wait: false, duration: 300
			}).start(0.4, 0).chain(function() { originalImg.remove(); });
			this.effect('opacity', {
				wait: false, duration: 300
			}).start(0, 1);
			loadingFx.start(1, 0);
		}
	}).setOpacity(0).injectBefore(topImgTrigger);
});



// Scroll to Content if Applicable
var doScroll;
if ($('doScroll')) {
	var scroll = new Fx.Scroll(window, {
		wait: false, duration: 1500, transition: Fx.Transitions.Quad.easeInOut
	});
	doScroll = $('doScroll').getValue();
	if (doScroll == 'true') scroll.toElement($('body'));
}



var commentForms;
if ($$('#layout div.commentsBlock form')) {
	commentForms = $$('#layout div.commentsBlock form');
	commentForms.each(function(cform, i) {
		new CommentForm(cform); 
		new AutoExpandTextArea(cform); 
	});
}



});