/* -------------------------------------------------------------- 

	Lewis Creek Association 
	Version 0.1
	
	Developed by MasterMade <http://mastermade.us>

-------------------------------------------------------------- */

/* Initiator */
$(document).ready(function() {
	inlabel.init();
	pseudoizer.init();
	images.init();
	slideshow.init();	
});

/* ==slideshow */
var slideshow = {
	images: null,
	active: null,
	time: 5000,
	timer: null,
	init: function() {
		slideshow.images= $('#slideshow ul li');
		
		slideshow.images.hide();
		
		slideshow.switchTo( slideshow.images.eq(0), false );
	},
	setTimer: function() {
		clearTimeout ( slideshow.timer );
		slideshow.timer= setTimeout ( 'slideshow.switchNext()', slideshow.time );
	},
	switchNext: function() {		
		target= slideshow.active.next();
		
		if(target.length == 0) {
			// check for last
			target= slideshow.images.eq(0);
		}
		
		slideshow.switchTo(target, true);
	},
	switchTo: function(target, animate) {
		
		if( slideshow.active != null )
		{
			if( animate )
			{
				slideshow.active.fadeOut();
			}
			else
			{
				slideshow.active.hide();
			}
		}
		
		slideshow.active= target;
		
		i= slideshow.images.index(target);
		
		if( animate )
		{
			slideshow.active.fadeIn();
		}
		else
		{
			slideshow.active.show();
		}
				
		slideshow.setTimer();
	}
}

/* images */
images = {
	init: function() {
		columned = $('body #content #primary .image.column');
		
		columned.each( function() {
			image = $(this);
			section = image.parents('.section').eq(0);
			
			if( image.hasClass('square') ) {
				section.addClass('square-columned');
			}
			
			if( image.hasClass('thumbnail') ) {
				section.addClass('thumbnail-columned');
			}
			
			if( image.hasClass('small') ) {
				section.addClass('small-columned');
			}
			
		});
		
	}
}


/* pseudoizer */
pseudoizer = {
	init: function() {
		
		// Input classes
		$('input').each( function() {
			$(this).addClass($(this).attr('type'));
		});
		
		// child selectors
		$('*:first-child').addClass('first-child');
		$('*:last-child').addClass('last-child');
		
	}
}

/* labels within inputs */
inlabel = {
	init: function() {
		inputs = $('input.inlabel');
				
		inputs.each( function() {
			inlabel.test( $(this), false );
			$('label[for=' + $(this).attr('id') + ']').addClass('inlabel');
		});
		
		inputs.focus( function() {
			inlabel.test( $(this), true );
		});
		
		inputs.blur( function() {
			inlabel.test( $(this), false );
		});
		
	},
	test: function( input, active ) {
		label = $('label[for=' + input.attr('id') + ']');
		
		empty = false;
		
		if( input.val() == '') {
			empty = true;
		}
		
		if( input.val() == label.text() ) {
			empty = true;
		}
		
		if( empty == true && active == true ) {
			inlabel.unlabel( input );
			return;
		}
		
		if( empty == true && active == false ) {
			inlabel.label( input );
			return;
		}
	},
	unlabel: function( input ) {
		label = $('label[for=' + input.attr('id') + ']');
		
		input.removeClass( 'labelled' );
		label.removeClass( 'labelled' );
		
		input.val('');
	},
	label: function( input ) {
		label = $('label[for=' + input.attr('id') + ']');
		
		input.addClass( 'labelled' );
		label.addClass( 'labelled' );
		
		input.val( label.text() );
		
	}
}
