var Wildbit = {
	
	init: function(){
		Wildbit.initFormFields();

		Wildbit.initFormValidation();

		if ($$('.latest-twitter')) Wildbit.fetchLatestTwitterPost();
		
		if ($$('.latest-blog')) Wildbit.fetchLatestBlogEntry();
		
		if ($('team')) Wildbit.initTeamMembers();
		
		if ($('client-work')) Wildbit.initClientProjects();
	},
	
	start: function(){
		if ($('who') || $('blog')) Wildbit.initSmoothScrolling();
	},
	
	initFormFields: function(){
		$$('input.empty, textarea.empty').each(function(el){
			var value = el.get('value');
			
			el.addEvent('focus', function(){
				if (el.get('value') == value) {
					el.set('value', '');
				}
			});
			
			el.addEvent('blur', function(){
				if (el.get('value') == '') {
					el.set('value', value);
				}
			});
		});
	},
	
	initFormValidation: function(){	
		$$('form.validate').each(function(el){
			el.addEvent('submit', function(e){
				var submit = true;
				
				el.getElements('input.required, textarea.required').each(function(el){
					if (el.get('value') == el.retrieve('default') || el.get('value') == '') {
						submit = false;
						
						if (!el.getParent('.form-row').hasClass('error')) el.getParent('.form-row').addClass('error');
					} else {
						if (el.getParent('.form-row').hasClass('error')) el.getParent('.form-row').removeClass('error');
					}
				});
				
				if (!submit) e.stop();
			});
			
			el.getElements('input.required, textarea.required').each(function(el){
				el.store('default', el.get('value'));
			});
		});	
	},
	
	initSmoothScrolling: function(){
		var scrollFx = new Fx.Scroll($$('body')[0], { fps: 60, duration: 500, transition: Fx.Transitions.Sine.easeOut });
		var selected = window.location.hash.split('#')[1];
		
		if (selected != undefined) scrollFx.start(0, $(selected).getPosition().y - 160);
		
		$$('#who a, #blog .meta a').each(function(el){
			el.addEvent('click', function(e){
				var anchor = el.get('href').split('#')[1];
				
				if ($(anchor)) {
					e.stop();
					
					scrollFx.start(0, $(anchor).getPosition().y - 160);
				}
			});
		});
	},
	
	fetchLatestTwitterPost: function(){
	  new Request.JSONP({
	    url: 'http://twitter.com/statuses/user_timeline/wildbit.json',
	    data: { count: 1 },
	    onComplete: function(twdata) {
				var text = twdata[0].text;
				
				if (text.length > 90) text = text.substr(0, 90) + '&hellip;';
				
				$$('.latest-twitter i').destroy();
				$$('.latest-twitter p').set('html', text);
	    }
	  }).send();
	},
	
	fetchLatestBlogEntry: function(){	
		new Request.JSONP({
	    url: 'http://wildbit.com/blog/wp-json.php',
	    onComplete: function(blogdata) {
				var entry = blogdata['item'][0];
				
				$$('.header .latest-blog p').set('html', '<a href="' + entry.link + '">' + entry.title + '</a> <span class="meta">&bull; ' + entry.date + ' by ' + entry.author + '</span> ' + entry.photo);
	    }
	  }).send();
	},
	
	initTeamMembers: function(){
		var interval;
		var previous 	= 0;
		var random 		= 0;
		var profiles 	= $$('#team .block-profile');
		var links			= $$('#team ul.team-members li a');
		
		function showProfile(index){
			links[index].getParent('li').addClass('active');
			profiles[index].setStyle('z-index', 1000).fade('in');

			previous = index;
		}
		
		profiles.setOpacity(0).show();
		showProfile(Math.floor(Math.random()*profiles.length));
		
		links.each(function(el, index){
			el.addEvent('click', function(e){
				e.stop();
				
				links[previous].getParent('li').removeClass('active');
				profiles[previous].fade('out');
				
				showProfile(index);
			});
		});
	},
	
	initClientProjects: function(){
		var width 		= $$('#client-work .block-project')[0].getSize().x;
		var count			= $$('#client-work .block-project').length;
		var position	= 1;
		var scrollFx 	= new Fx.Scroll('scroll-area', { transition: Fx.Transitions.Sine.easeOut });
		
		$$('#client-work .dynamic-width').setStyle('width', width*count);
		
		$$('#client-work .next a').addEvent('click', function(e){
			e.stop();
			
			if (position == count) {
				scrollFx.start(0, 0);
				position = 1;
			} else {
				scrollFx.start(position * width, 0);
				position++;
			}
		});
	}
	
}

window.addEvent('domready', Wildbit.init);
window.addEvent('load', Wildbit.start);



/* Extension to MooTools Core */

Element.implement({
	
	show: function(){
		if (this.hasClass('hidden')) this.removeClass('hidden');
	},
	
	hide: function(){
		if (!this.hasClass('hidden')) this.addClass('hidden');
	}
	
});