/* Author: Ludwig Wendzich

*/

function advanceShow() {
	if($(".selected").next("li").length>0){
		$(".selected").removeClass("selected").next().addClass('selected');
	} else {
		$(".selected").removeClass("selected").parent().find("li:first").addClass("selected");
	}
		ref = $(".selected a").attr("href");
		$(".full li:visible").hide();
		$(ref).fadeIn(300, function(){
			$("section[role=main]").removeClass("vertical horizontal").addClass($(ref+" img").checkRatio());
			//$(".full").css("height",$(ref+" img").height());
			});
	}

function backShow() {
	if($(".selected").prev("li").length>0){
		$(".selected").removeClass("selected").prev().addClass('selected');
	} else {
		$(".selected").removeClass("selected").parent().find("li:last").addClass("selected");
	}
		ref = $(".selected a").attr("href");
		$(".full li:visible").hide();
		$(ref).fadeIn(300, function(){
			$("section[role=main]").removeClass("vertical horizontal").addClass($(ref+" img").checkRatio());
			//$(".full").css("height",$(ref+" img").height());
			});
	}
	
(function( $ ){

  $.fn.checkRatio = function() {
        
    	if(this.width()>this.height()){
			return "horizontal"
		} else {
			return "horizontal" //vertical
		}
  };

})( jQuery );

function preloader() 
{
prevImg = new Image(); 
prevImg.src = "/assets/img/arrows.png";
}

preloader();

//SCROLLPANE SETUP
$(".thumbs").wrap("<div class='thumb-slider' />");
$(".thumb-slider").append('<span class="next"></span><span class="prev"></span>');

$(document).ready(function(){
	
	
	//SCROLLPANE
	
	$('.photo .content-box').jScrollPane({autoReinitialise: true});
	
	// THUMBNAIL SCROLLING
	

	$(".thumb-slider").jCarouselLite({
					scroll: 3,
					visible: 4,
					rows: 2,
			        btnNext: '.next',
			        btnPrev: '.prev',
					circular: false
				});
	
	$(".full").after('<span class="next"></span><span class="prev"></span>');
	
	//SETUP CAROUSEL
	$("#carousel img").addClass("cloudcarousel");
	$("#carousel").CloudCarousel(		
			{	
				autoRotate: "left",
				autoRotateDelay: 2000,
				xPos: 450,
				yPos: 190,
				altBox: $("#carousel h3"),
				bringToFront: true,
				mouseWheel: true,
				reflHeight: 60,
				reflOpacity: 0.25
			}
		);
	$("#carousel a").click(function(){
		if($(this).find("img").css("zIndex")<=96){
			event.preventDefault();
		}
	});
	
	$("#carousel").append("<span class='highlight' />");
	
	// NAVIGATION
	$("nav ul ul ul").remove();
	$("nav li.parent_here ul").show();
	$("nav>ul>li>a").click(function(){
		$(this).parent().find("ul").slideToggle();
		if($(this).parent().find("ul").length>0){
			return false;
		}
	});
	
	//PHOTO PAGE
	//$(".full").css("height",$(".full img:visible").height());
	
	if($("section[role=main]").hasClass('photo')){
		//$(".full").css("height",$(".full img:visible").height());
		$(".full").after($(".thumb-slider"));
		$(".full li:not(:first)").hide();
		$(".thumbs li:first").addClass("selected");
		$("section[role=main]").removeClass("vertical horizontal").addClass($(".full li:first img").checkRatio());

		$(".thumbs a").click(function(){
				clearInterval(autoShow);
				$(".thumbs li").removeClass("selected");
				$(this).parent("li").addClass("selected");
				ref = $(this).attr("href");
				$(".full li:visible").hide();
				$(ref).fadeIn(300, function(){
					$("section[role=main]").removeClass("vertical horizontal").addClass($(ref+" img").checkRatio());
					//$(".full").css("height",$(ref+" img").height());
				});
				event.preventDefault;
				return false;
		});
		
		$("section>.next, .full a").click(function(){
				clearInterval(autoShow);
				advanceShow();
				event.preventDefault;
				return false;
		});
		$("section>.prev").click(function(){
				clearInterval(autoShow);
				backShow();
				event.preventDefault;
		});
		
		$(document).keydown(function (e) {
		  var keyCode = e.keyCode || e.which,
		      arrow = {left: 37, up: 38, right: 39, down: 40 };

		  switch (keyCode) {
		    case arrow.left:
		      	clearInterval(autoShow);
				backShow();
				event.preventDefault;
		    break;
		    case arrow.right:
		      	clearInterval(autoShow);
				advanceShow();
				event.preventDefault;
		    break;
		  }
		});

		autoShow = setInterval(function(){advanceShow()},3000);
	}//end photo page
	
	if($("section[role=main]").hasClass("blog")){
		$("nav li:contains(Blog) ul").replaceWith($("#blog-navigation"));
	} else {
		$("nav li:contains(Blog) ul").remove();
	}//end blog
	
	/*Add Inline Gallery*/
	$('.inline-gallery').each(function(){
		$(this).find("img").each(function(){
			largeImage = $(this).attr('src');
			$(this).wrap('<a href="'+largeImage+'" />');
		});
	});
	
	$('.inline-gallery a').fancybox({ 'overlayOpacity': 0 });
	
	/*Contact Us*/
	$(".content-box").after($(".contact-details"));
	
	});
