jQuery(document).ready(function(){
	
	/* --- populate search box --- */
	
	if(jQuery('#s').val()==''){
		jQuery('#s').val('Site wide search');
	}
	
	jQuery('input[type=text],textarea').click(function(){
		var element = jQuery(this);
		element.data('orig',element.val());
		element.val('');
	});
	
	jQuery('input[type=text],textarea').blur(function(){
		if(jQuery(this).val()==''){
			jQuery(this).val(jQuery(this).data('orig'));
		}
	});
	
	/* --- image slider --- */
	
	if(jQuery('#slider').length > 0){
		
		jQuery('#slider ul li').first().show();
		
		var divs = jQuery('#slider ul li').first().children('div');
		
		jQuery('#sliderPanel').html('');
		jQuery('#sliderInfo').html('');
		
		divs.eq(0).clone(true).appendTo('#sliderInfo').show();
		divs.eq(1).clone(true).appendTo('#sliderPanel').show();
		
		var int = setInterval("slide()",8000);
	}
	
	/* --- testimonials slider --- */
	
	if(jQuery('#testimonialStorage').length > 0){
		var storage = jQuery('#testimonialStorage').html();
		
		storage = storage.replace(/\]/g,'>')
		storage = storage.replace(/\[/g,'<')
		
		jQuery('#testimonialStorage').html(storage);
		
		var images = jQuery('#testimonialStorage img');
		var captions = jQuery('#testimonialStorage caption');
		
		jQuery('#viewTestimonial').click(function(){
			
			var counter = Math.floor(Math.random()*images.length);
			
			var image = images.eq(counter);
			
			var caption = captions.eq(counter).attr('caption');
			
			jQuery('#testimonialImage').fadeOut('slow', function(){
				jQuery(this).html(image).fadeIn('slow')
			});
			
			jQuery('#whatOurClientsSay span').fadeOut('slow', function(){
				jQuery(this).html(caption).fadeIn('slow')
			});
			
		});
		
		jQuery('#viewTestimonial').click();
	}
	
});

function slide(){
	
	jQuery('#slider ul li').first().fadeOut('slow',function(){

		var nextElement = jQuery('#slider ul li').eq(1);
		var divs = nextElement.children('div');
		nextElement.fadeIn();
		
		jQuery('#sliderPanel').slideUp('slow', function(){
			jQuery(this).html('').append(divs.eq(1).clone(true).show()).slideDown('slow');
		});
		jQuery('#sliderInfo').fadeOut('slow', function(){
			jQuery(this).html('').append(divs.eq(0).clone(true).show()).fadeIn();
		});
		
		jQuery(this).appendTo('#slider ul');
	});
	
}
