var fastSpeed = 100;
var showSpeed = 300;
var defaultOpa = .5;
var theSelected = 0;
var thisTimer;
var bubTimer;
var firstLoad;
$(document).ready(function(){
	//Que the slideshow
	itemSelected(theSelected, "init");
	//Start the Timer
	thisTimer = setInterval ("itemSelected(theSelected, 'fromTimer')", 3000 );
	bubTimer = setInterval ("bubbleRandomizer()", 1000 );
	//=====PRELOADER=====
	$(document).append("<div style='width:0; height:0; display:none; position:absolute;'><img class='star' src='images/star.png' width='24' height='22' /></div>");
	//=====NAVIGATION=====
	$('#navigation ul li a').each(function(){	
		var sPath = window.location.pathname;
		var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
		var blowlumps = $(this).attr('href');
		var blowlumps2 = $(this).text();
		if ((blowlumps.substring(blowlumps.lastIndexOf('/') + 1)) == (sPage) && (sPage) != "index.php") {
			$(this).parent('li').addClass('active');
			document.title = blowlumps2 + " - " + document.title;
		}
		if (sPage == null || sPage == "" || sPage == " " || sPage == "index.php"){
			$('#home').parent('li').addClass('active');
		}
		//ACTIVE STATES
		if (($(this).parent('li').hasClass('active'))) {
			$(this).parent('li').prepend("<img class='star' src='images/star.png' width='24' height='22' />");
		}//end if
		//HOVER STATES
		else {
			$(this).hover(function(){		
					$(this).parent('li').prepend("<img class='star' src='images/star.png' width='24' height='22' />");
			}, function() {
				$(this).parent('li').find('.star').remove();
			}); //end hover
		}//end else
	}); //end each
	//=====EQUAL-HEIGHTS=====
	var highestCol = Math.max($('#navigation').outerHeight(),$('#content').outerHeight());
	$('#content').height(highestCol);
	//=====SLIDESHOW=====
	//Arrow Navigation
	$('#arrow_left').css('opacity',defaultOpa);
	$('#arrow_right').css('opacity',defaultOpa);
	//-------Arrow-Left-------
	$('#arrow_left').hover(function(){
		$(this).stop(false,true).animate({opacity: 1},fastSpeed);
		},function(){
			$(this).stop(false,true).animate({opacity: defaultOpa},fastSpeed);
		}).click(function(){
			itemSelected(theSelected, "fromLeft");
		});//end hover click;
	//-------Arrow-Right-------
	$('#arrow_right').hover(function(){
		$(this).stop(false,true).animate({opacity:1},fastSpeed);
		},function(){
			$(this).stop(false,true).animate({opacity: defaultOpa},fastSpeed);
		}).click(function(){
			itemSelected(theSelected, "fromRight");
		});//end hover click;
	//-----Thumbnail-Navigation-----
	$('#slide_wrapper').find('.slide').each(function(){
		$(this).click(function(){
			itemSelected($(this).index(), "fromSlide");
		}).hover(function(){
			$(this).stop(false,true).animate({opacity:1},showSpeed);
		},function(){
		if ( $(this).index() != theSelected ) {
			$(this).stop(false,true).animate({opacity:defaultOpa},showSpeed);
		}
		});//end click hover
	});//end each
	//=====BUBBLES=====
	bubbler($('#b_cow'),$('#cow_bubble'));
	bubbler($('#b_pig'),$('#pig_bubble'));
	bubbler($('#s_talkers'),$('#chick_bubble'));
	bubbler($('#s_talkers'),$('#s_cow_bubble'));
	
}); //end ready
function bubbler(theTalker,theBubble){
	var cow_bubble_ps = $(theBubble).children('p').size();
	//clear time on click
	$(theBubble).children('p').hide();
	$(theTalker).click(function(){
			clearInterval(bubTimer);
			//Chicken Convo
			if (firstLoad == true){
				var theRandom = 0;
			}
			else{
				var theRandom = Math.floor(Math.random()*(cow_bubble_ps));
			}
			if ((theTalker.attr('id') == "s_talkers")){
				$('#chick_bubble').children('p').stop(false,true).fadeOut(0);
				$('#s_cow_bubble').children('p').stop(false,true).fadeOut(0);
				$('#chick_bubble').children('p:eq('+theRandom+')').stop(false,true).fadeIn('fast');
				$('#s_cow_bubble').children('p:eq('+theRandom+')').stop(false,true).fadeIn(1500);
			}
			else {
				$(theBubble).children('p').stop(false,true).fadeOut('fast');
				$(theBubble).children('p:eq('+(Math.floor(Math.random()*(cow_bubble_ps)))+')').stop(false,true).fadeIn('fast');	
			}
	});//end Click
}//end bubbler
//-----Old-Bubble-Function-----
/*function bubbler(theTalker,theBubble){
	var cow_bubble_ps = $(theBubble).children('p').size();
	$(theBubble).hide();
	//clear time on click
	
	$(theTalker).toggle(function(){
			clearInterval(bubTimer);
			//Chicken Convo
			if ((theTalker.attr('id') == "s_talkers")){
				$('#chick_bubble').fadeIn('fast');
				$('#chick_bubble').children('p:eq('+(Math.floor(Math.random()*(cow_bubble_ps)))+')').fadeIn('fast');
				$('#s_cow_bubble').stop(false,true).delay(300).fadeIn('fast');
				$('#s_cow_bubble').children('p:eq('+(Math.floor(Math.random()*(cow_bubble_ps)))+')').stop(false,true).delay(300).fadeIn('fast');
			}
			else {
				$(theBubble).stop(false,true).fadeIn('fast');
				$(theBubble).children('p:eq('+(Math.floor(Math.random()*(cow_bubble_ps)))+')').fadeIn('fast');	
			}
	},function(){
			if ((theTalker.attr('id') == "s_talkers")){
				$('#chick_bubble').fadeOut('fast');
				$('#chick_bubble').children('p:visible').fadeOut('fast');
				$('#s_cow_bubble').fadeOut('fast');
				$('#s_cow_bubble').children('p:visible').fadeOut('fast');
			}
			else {
				$(theBubble).stop(false,true).fadeOut('fast');
				$(theBubble).children('p:visible').each(function(){
					$(this).fadeOut('fast');
				});
			}
	});//end hover
}//end bubbler */
function bubbleRandomizer(){
	//var randomGuy = Math.round(Math.random()*2);
	var randomGuy = "all";
	if (randomGuy == 0) {
		$('#b_cow').click();
	}
	if (randomGuy == 2) {
		$('#b_pig').click();
	}
	if (randomGuy == 1) {
		$('#s_talkers').click();
	}
	if (randomGuy == "all") {
		firstLoad = true;
		$('#s_talkers').click();
		$('#b_pig').click();
		$('#b_cow').click();
		firstLoad = false;
	}
}
//-----Counts-the-number-of-slides-----
function numberSlides() {
	var numSlides = 0;
	$('#slide_wrapper').find('.slide').each(function(){
		numSlides++;
	});//end each
	return numSlides
}
//-----Moves-the-slideshow-navigation-----
function itemSelected(theElement, theSource){
	//Select the Element
	if (theSource == "init"){
		letsGo();
	}
	if (theSource == "fromSlide"){
		if (theSelected != theElement){
			theSelected = theElement;
			letsGo();
		}
	}
	if (theSource == "fromLeft"){
		//Select the proper thumbnail
		if(theSelected > 0) {
			theSelected--;
			letsGo();
		}
	}
	if (theSource == "fromRight"){
		//Select the proper thumbnail
		if(theSelected < (numberSlides()-1)) {
			theSelected++;
			letsGo();
		}
	}
	if (theSource == "fromTimer"){
		if(theSelected < (numberSlides()-1)) {
			theSelected++;
			letsGo();
		}
		else{
			//Stop timer at end of slides
			clearInterval ( thisTimer );
			//Return to first slide
			theSelected = 0;
			letsGo();
		}
	}
	else {
		//Stop timer on click
		clearInterval ( thisTimer );
	}
	function letsGo(){
		//Remove Highlights, Count the slides
		$('#slide_wrapper').find('.slide').each(function(){
			//Opacity
			if ( $(this).index() != theSelected ) {
				$(this).css('opacity',defaultOpa);
			}
			$(this).find('.slide_overlay').remove();
		});//end each
		//Fade all other slides out
		$('#images_box').each(function(){
			$(this).find('img:visible').stop(false,true).fadeOut(showSpeed);
		});//end each
		$('#slide_wrapper .slide:eq('+theSelected+')').append("<div class='slide_overlay'></div>")
		$('#slide_wrapper .slide:eq('+theSelected+')').stop(false,true).animate({opacity:1},100);
		$('#images_box img:eq('+theSelected+')').stop(false,true).fadeIn(showSpeed);
	}
}
