/**
 * @author ianfarrell
 */

//value swap for search

$(document).ready(function(){  
	
	//***********************************
	// ADD A CLASS TO THE BODY IF JS ENABLED
	//***********************************
	$("body").addClass("jsEnabled");

	
	if ($(".entry-content p img").length > 0) {
		$(".entry-content p img").parent().css("width","398px");
	}
	
	$("#s").focus(function(){
		//$("#s").css("color","red");
		if ($("#s").attr("value") == "search") {
			$("#s").attr("value", "");
		}
	});
	
	$("#s").blur(function(){
		//$("#s").css("color","blue");
		if ($("#s").attr("value") == "") {
			$("#s").attr("value", "search");
		}
	});
	
	
	$(".swapInfo1").click(function(){
		$("#swapInfo1").css("display","block");
		
		if ($("#swapInfo2").css("display","block")) {
			$("#swapInfo2").css("display","none");
		}
		if ($("#swapInfo3").css("display","block")) {
			$("#swapInfo3").css("display","none");
		}
		return false;
	})
	
	$(".swapInfo2").click(function(){
		$("#swapInfo2").css("display","block");
		
		if ($("#swapInfo1").css("display","block")) {
			$("#swapInfo1").css("display","none");
		}
		if ($("#swapInfo3").css("display","block")) {
			$("#swapInfo3").css("display","none");
		}
		return false;
	})
	
	$(".swapInfo3").click(function(){
		$("#swapInfo3").css("display","block");
		
		if ($("#swapInfo1").css("display","block")) {
			$("#swapInfo1").css("display","none");
		}
		if ($("#swapInfo2").css("display","block")) {
			$("#swapInfo2").css("display","none");
		}
		return false;
	})
	
	
	
	
	
	//gallery script
	//tests for first image, hides all but displays our first one
	if ($("#large1").length > 0) {
		$("#galleryRight img").hide();
		$("#galleryRight img#large1").show();
	}
	
	
	$("#thumb1 a").click(function(){
		$("#galleryRight img").fadeOut('slow');
		$("#large1").fadeIn('slow');
		
		var title = $("#thumb1 a").attr("title");
		$("p#description").text(title);
		
		return false;
	})
	
	$("#thumb2 a").click(function(){
		$("#galleryRight img").fadeOut('slow');
		$("#large2").fadeIn('slow');
		
		var title = $("#thumb2 a").attr("title");
		$("p#description").text(title);
		
		
		return false;
	})
	
	$("#thumb3 a").click(function(){
		$("#galleryRight img").fadeOut('slow');
		$("#large3").fadeIn('slow');
		
		var title = $("#thumb3 a").attr("title");
		$("p#description").text(title);
		
		
		return false;
	})
	
	$("#thumb4 a").click(function(){
		$("#galleryRight img").fadeOut('slow');
		$("#large4").fadeIn('slow');
		
		var title = $("#thumb4 a").attr("title");
		$("p#description").text(title);
		
		
		return false;
	})
	
	$("#thumb5 a").click(function(){
		$("#galleryRight img").fadeOut('slow');
		$("#large5").fadeIn('slow');
		
		var title = $("#thumb5 a").attr("title");
		$("p#description").text(title);
		
		
		return false;
	})
	
	$("#thumb6 a").click(function(){
		$("#galleryRight img").fadeOut('slow');
		$("#large6").fadeIn('slow');
		
		var title = $("#thumb6 a").attr("title");
		$("p#description").text(title);
		
		
		return false;
	})
	
	$("#thumb7 a").click(function(){
		$("#galleryRight img").fadeOut('slow');
		$("#large7").fadeIn('slow');
		
		var title = $("#thumb7 a").attr("title");
		$("p#description").text(title);
		
		
		return false;
	})
	
	$("#thumb8 a").click(function(){
		$("#galleryRight img").fadeOut('slow');
		$("#large8").fadeIn('slow');
		
		var title = $("#thumb8 a").attr("title");
		$("p#description").text(title);
		
		
		return false;
	})
	
	$("#thumb9 a").click(function(){
		$("#galleryRight img").fadeOut('slow');
		$("#large9").fadeIn('slow');
		
		var title = $("#thumb9 a").attr("title");
		$("p#description").text(title);
		
		
		return false;
	})
	
	
	
	//////////////////////////////////
	// home page carousel
	//////////////////////////////////
	//$("div#carousel img:not(:first)").hide();
	if ($("div#carousel").length > 0) {
		$("div#carousel div:first").addClass("active");
		
		$("#carouselContainer").append('<div id="carouselDots"></div>');
		
		$("div#carousel div").each(function(addDots) {	
			$("#carouselDots").append('<a href="#" class="carouselDot"><!-- --></a>');
		});
		$("#carouselDots a:first").addClass("active");	
	};
	
	$("#carouselDots a").click(function(e){
		e.preventDefault();
		
		var position = $(this).index() + 1;
		
			//reset all opacity to 1 (otherwise the below line doesn't overwrite inline opacity with the class on 2nd + click
		$("div#carousel div").css({opacity: 1.0});
			//set all but the require image to 0
		$("div#carousel div").not( $("div#carousel div:nth-child(" + (position) + ")")).css({opacity: 0.0});
			//make the required image visible
		$("div#carousel div").removeClass("active");
		$("div#carousel div:nth-child(" + (position) + ") ").addClass("active");
		
			//swaps out active dot
		$("#carouselDots a").removeClass("active")
		$(this).addClass("active")
		
		//use this to stop the timer on click, so it doesnt automate
		clearInterval(timer);
	})	
	
});

	function slideSwitch(){
		var $active = $('div#carousel div.active');
		var $activeDot = $('#carouselDots a.active');
		
		if ($active.length == 0) 
			$active = $('div#carousel div:last');
		
		// use this to pull the divs in the order they appear in the markup
		var $next = $active.next().length ? $active.next() : $('div#carousel div:first');
		
		var $nextDot = $activeDot.next().length ? $activeDot.next() : $('#carouselDots a:first');
		
		$active.addClass('last-active');
		
		$next.css({
			opacity: 0.0
		}).addClass('active').animate({
			opacity: 1.0
		}, 800, function(){
			$active.removeClass('active last-active');
		});
		
		$nextDot.addClass('active')
		$activeDot.removeClass('active last-active');
		
	}
	
	var timer = setInterval("slideSwitch()", 7000);
	$(function(){
		timer;
	});
	



