$(function(){
	
	/* randomly show each quote */
	/****************************/
	
	if (($('ul#quotes').length)) {
		var quotes = $('ul#quotes').children().length;
		var randomQuote = Math.ceil(Math.random() * quotes);
		$('ul#quotes li.first').removeClass('first');
		$('ul#quotes').children(':nth-child(' + randomQuote + ')').show();
	}
	
	/* slideshow setup */
	/*******************/
	
	if (($('div#slideshow').length)) {
		
		// over-ride non-javascript show img.first
		$('div#slideshow img.first').removeClass('first');
		$('div#slideshow img').show();
		
		// pick random beginning slide
		var slides = $('div#slideshow').children().length;
		var randomQuote = Math.ceil(Math.random() * slides);
		$('div#slideshow').children(':nth-child(' + randomQuote + ')').addClass('active');
		
		// intial start of slidshow
		playSlideShow = setInterval('slideShow();', 5000);
		
		// append the controller for javascript friendly users
		$('div#slideshow-container').append("<div class='controller'><a href='#' class='prev'></a><a href='#' class='pause'></a><a href='#' class='next'></a></div>");
		
		// play button	
		$('div#slideshow-container div.controller a.play').click(function() {	
			playSlideShow = setInterval('slideShow();', 5000);
			$(this).removeClass('play').addClass('pause').bind('click');
			return false;
		}); 
		
		// pause/play button
		$('div#slideshow-container div.controller a.pause').click(function() {
			if ($(this).hasClass('play')) {
				$(this).removeClass('play');
				$('div#slideshow img').show();
				playSlideShow = setInterval('slideShow();', 5000);
			} else {
				if (typeof(playSlideShow) != 'undefined') {
					clearInterval(playSlideShow);
				} 
				$(this).addClass('play');
			}
			return false;
		});
			
		// next button
		$('div#slideshow-container div.controller a.next').click(function() {
			if (typeof(playSlideShow) != 'undefined') {
				clearInterval(playSlideShow);
			}
			if (!$('a.play').length) {
				$('a.pause').addClass('play');
			}
			var active = $('div#slideshow img.active');
    		var next =  active.next().length ? active.next() : $('div#slideshow img:first');
			$('div#slideshow img').hide();
			next.addClass('active').show().prev().removeClass('active');
			return false;
		});
		
		// prev button
		$('div#slideshow-container div.controller a.prev').click(function() {
			if (typeof(playSlideShow) != 'undefined') {
				clearInterval(playSlideShow);
			}
			if (!$('a.play').length) {
				$('a.pause').addClass('play');
			}
			var active = $('div#slideshow img.active');
    		var next =  active.prev().length ? active.prev() : $('div#slideshow img:last');
			$('div#slideshow img').hide();
			next.addClass('active').show().next().removeClass('active');
			return false;
		});
			
		
	}
	
	/* compact forms */
	/*****************/
		
		if ($('form').length) { // if a form exists, apply the the jquery
			
			// add compact styling
			$('form div.input-control').addClass('compact');
			
			// if the page loads with a value in a field after the label hide the label and also
			// make the label disappear when you click it & focus on the next element
			$('form div.input-control label').each(function(){
				var getValue = $(this).next().val();
				if (getValue.length > 1) {
					$(this).hide();
				}
				$(this).click(function(){
					$(this).hide();
					$(this).next().focus();
				});
			}); 
			
			// on focus hide the label always
			$('form div.input-control label').next().focus(function(){
				$(this).prev().hide();
			});
			
			// show label on blur unless we've entered something
			$('form div.input-control label').next().blur(function() {
				var getValue = $(this).val();
				if (getValue.length > 1) {
					$(this).prev().hide();
				} else {
					$(this).prev().show();
				}
			});
		
		}

	
});

/* slideshow function | http://jonraasch.com/blog/a-simple-jquery-slideshow */
/****************************************************************************/

function slideShow() { 
    
	var active = $('div#slideshow img.active');
    var next =  active.next().length ? active.next() : $('div#slideshow img:first');
  
  	// animate slides
	active.addClass('last-active');
    next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, function() {
            active.removeClass('active last-active');
        });
}
