jQuery(document).ready(function($){
	//functions
	var decoratePostParagraph = function() {
		$('.post-content').before('<div class="top"><div></div></div>').after('<div class="bottom"><div></div></div>');	
	}
	
	var matchPostColumns = function() {
		if ( $('body').hasClass('single') === false ) {
			$('.post').each(function(){
				var left = $('.left',$(this));
				var right = $('.right',$(this));
				left.height(right.height());
			});
		}
	}
	
	var serviceAccordian = function() {
		var toplevel = $('#services ul:first');
		$('ul', toplevel).hide();
		$('li',toplevel).each(function(){
			$(this).css({
				'cursor':'pointer'
			});
			$(this).toggle(function(){
				$('ul',$(this)).show('slow');
			},function(){
				$('ul',$(this)).hide('slow');
			});
		});
		
	}
	
	var addCommentCorner = function() {
		var imageRoot = '/wp-content/uploads/2009/12/';
		$('.single .comment p').each(function(){
			$(this).css({
				'position':'relative'
			})
			var img = $('<img src="' + imageRoot + 'comment-corner.gif" width="12" height="11" />');
			img.css({
				'position':'absolute',
				'top':'-1px',
				'left':'-12px',
				'z-index':'99'
			});
			if ( $('em',$(this).parent()).length > 0 ) {
				img.css({'top':'1px'});
			}
			$(this).append(img);
		});
	}
	
	var vanillaClouds = function() {
		var imageRoot = '/wp-content/uploads/2009/11/';
		var stageHeight = $('#outer-wrapper').height();
		var winWidth = $(window).width();
		
		$(window).resize(function(){
			winWidth = $(window).width();
		});
		
		var sections = Math.floor((stageHeight/200));
		var topOffset = 205;
		var clouds = [
			{
				'image_name':'clouds_01',
				'width':200,
				'height':65
			},
			{
				'image_name':'clouds_02',
				'width':148,
				'height':49
			},
			{
				'image_name':'clouds_03',
				'width':173,
				'height':62
			},
			{
				'image_name':'clouds_04',
				'width':122,
				'height':41
			},
			{
				'image_name':'clouds_05',
				'width':134,
				'height':46
			}
		];
		
		var getRandomXOffset = function() {
			return Math.floor(Math.random() * winWidth) + 1;
		};
		
		var animateClouds = function() {
			
			var randomSpeed = function() {
				var speed = Math.floor(Math.random() * 40000) + 35000; //randomize the speed between 25000 and 30000
				return speed;
			};
			
			var randomLeftOffset = function() {
				var offset = Math.floor(Math.random() * 150) + 50;
				return (offset * -1);
			};
			
			var animateCloud = function(somecloud) {
				somecloud.show();
				somecloud.animate({
					left:winWidth
				}, randomSpeed(),function(){
					somecloud.fadeOut('slow',function(){
						$(this).css({
							'left': randomLeftOffset()
						});
					});
					animateCloud(somecloud);
				});
			};
			
			$('.vanilla-cloud').each(function(){
				animateCloud($(this));
			});
		}
		
		var getRandomCloud = function() {
			var numClouds = clouds.length;
			var index = Math.floor(Math.random() * numClouds) //get a random index number
			return clouds[index];
		};
		
		var getCloudImage = function(img_name,img_width,img_height) {
			var src = imageRoot + img_name;
			var imghtml = '<img class="vanilla-cloud" src="' + src + '" width="' + img_width + '" height="' + img_height + '" />';
			return $(imghtml);
		};
		
		for ( var i = 0; i < sections; i++ ) {
			var cloud = getRandomCloud();
			var img = getCloudImage(cloud['image_name'] + '.gif',cloud['width'],cloud['height']);
			if ( (stageHeight - topOffset) > 70 ) {
				img.css({
					'position':'absolute',
					'top': topOffset + 'px',
					'left': getRandomXOffset() + 'px',
					'z-index':'-1'
				});
				topOffset = topOffset + (cloud['height'] + 150);
				img.appendTo('#outer-wrapper');	
			}
		}
		
		animateClouds();
		
	}
	
	var searchForm = function() {
		$('label[for=s]').hide();
		$('#s').click(function(){
			$(this).val('');
		});
	}
	
	var commentForm = function() {
		var authorLabel = $('label[for=author]');
		var emailLabel = $('label[for=email]');
		var urlLabel = $('label[for=url]');
		
		if ( !$('#author').val() ) {
			$('#author').val(authorLabel.text());
			$('#author').click(function(){
				$(this).val('');
			});
		}
		if ( !$('#email').val() ) {
			$('#email').val(emailLabel.text());
			$('#email').click(function(){
				$(this).val('');
			});
		}
		if ( !$('#url').val() ) {
			$('#url').val(urlLabel.text());
			$('#url').click(function(){
				$(this).val('');
			});
		}
		
		authorLabel.hide();
		emailLabel.hide();
		urlLabel.hide();
		$('label[for=comment]').hide();
	}
	
	var serviceCallToAction = function() {
		$('#services li').each(function(){
			if ( $('ul',$(this)).length > 0 ) {
				$(this).css({
					'position':'relative'
				});
				$(this).append('<span class="call-to-action">click to read more</span>');
			}
		});
		$('.call-to-action').hide();
		$('.call-to-action:first').fadeIn(1500);
		$('#services li').each(function(){
			$(this).hover(function(){
				var cta = $('.call-to-action',$(this));
				if ( cta.length > 0 ) {
					if ( cta.is(':hidden') ) {
						$('.call-to-action:visible').fadeOut(500);
						cta.fadeIn(500);
					}
				}
			});
			var cta = $('.call-to-action',$(this));
			$(this).click(function(){
				if ( cta.length > 0 ) {
					var txt = cta.text();
					if ( /more/.test(txt) ) {
						cta.text(txt.replace(/more/,'less'));
					} else if ( /less/.test(txt) ) {
						cta.text(txt.replace(/less/,'more'));
					}
				}
			});
		});
	}
	
	//execute
	decoratePostParagraph();
	matchPostColumns();
	serviceAccordian();
	vanillaClouds();
	searchForm();
	addCommentCorner();
	commentForm();
	serviceCallToAction();
});