(function($){
	
	
	
	
	
	
	
	
	
	
	
	if (document.getElementById("preview_inner")) {
	
		var outer = $("#preview_inner");
		var imageSize = 640;
		var thumbs	= $(".locations li");
		
		$(".locations li:first-child").addClass("selected")
		
		outer.click(function() {
			var nextsrc = $(".locations ul li.selected").next();
			
			if(nextsrc.length == 0) {
				nextsrc = $(".locations ul li:first-child");
			}
			
			 $(".locations ul li.selected").removeClass("selected");
			 nextsrc.addClass("selected")
			 
			fadeImage(nextsrc.children('a').attr('href'));
		});
		
		thumbs.click(function() {
			
			var image_src = $(this).children('a').attr('href');
			var oldimage_src = $('#preview_inner').children('img').attr('src');
			$(".locations ul li").removeClass("selected");
			$(this).addClass("selected")
			
			fadeImage(image_src);
			$('html, body').animate({scrollTop:0}, 'slow');
			return false;	
		});
	}
	
	function fadeImage(scr) {
		
		var img = new Image();
		
		$(img).load(function () {
				$(this).hide();
				$('#preview_inner').append($(this));
				$('#preview_inner').removeClass("loading")
				$(this).fadeIn();
			})
			
			$('#preview_inner').addClass("loading")
			$('#preview_inner').children('img').fadeOut();
			
			$(img).attr('src', scr);
	}
	
	if (document.getElementById("searchForm")) {
		$("#searchForm button").hide();
		var thumbs	= $("select");
		thumbs.change(function() {
			$("#searchForm").submit();
		});
	}
	

	/*
	var count = 1;
	var cycle;
	var slideshowMode = false;
	
	$('li.parent').hover(
			function() { $('ul', this).css('display', 'block'); },
			function() { $('ul', this).css('display', 'none'); 
	});


	if (document.getElementById("slideshowControls")) {
		var slideNums = $("#slideshowControls li");
		slideNums.click(function() {
			
			var image_src = $(this).children('a').attr('href');
			var oldimage_src = $('.slideshow').children('img').attr('src');
			$("#slideshowControls li").removeClass("selected");
			$(this).addClass("selected")
			
			
			if (slideshowMode === true) {
				count = $(this).index() + 1;
				clearInterval(cycle);
				var temp = setInterval(function(){
					setUp();
					clearInterval(temp);
				}, 
      			3000);
			}
			
			slideImage(image_src);
			
			return false;	
		});
	}

	// Check on right page!
	if (document.getElementById("preview_inner")) {
	
		var outer = $("#preview_inner");
		var imageSize = 640;
		var thumbs	= $(".locations li");
		
		$(".locations li:first-child").addClass("selected")
		
		outer.click(function() {
			var nextsrc = $(".locations ul li.selected").next();
			
			if(nextsrc.length == 0) {
				nextsrc = $(".locations ul li:first-child");
			}
			
			 $(".locations ul li.selected").removeClass("selected");
			 nextsrc.addClass("selected")
			 
			fadeImage(nextsrc.children('a').attr('href'));
		});
		
		thumbs.click(function() {
			
			var image_src = $(this).children('a').attr('href');
			var oldimage_src = $('#preview_inner').children('img').attr('src');
			$(".locations ul li").removeClass("selected");
			$(this).addClass("selected")
			
			fadeImage(image_src);
			
			return false;	
		});
	}
	
	function slideImage(scr) {
		
		var img = new Image();
		
		$(img).load(function () {
				$(this).hide();
				$('#df_preview_inner').append($(this));
				$('#df_preview_inner').removeClass("loading")
				$(this).fadeIn();
			})
			
			$('#df_preview_inner').addClass("loading")
			$('#df_preview_inner').children('img').fadeOut();
			
			$(img).attr('src', scr);
	}
	
	function fadeImage(scr) {
		
		var img = new Image();
		
		$(img).load(function () {
				$(this).hide();
				$('#preview_inner').append($(this));
				$('#preview_inner').removeClass("loading")
				$(this).fadeIn();
			})
			
			$('#preview_inner').addClass("loading")
			$('#preview_inner').children('img').fadeOut();
			
			$(img).attr('src', scr);
	}
	
	if (document.getElementById("searchForm")) {
		$("#searchForm button").hide();
		var thumbs	= $("select");
		thumbs.change(function() {
			$("#searchForm").submit();
		});
	}
	
	if(document.getElementById("df_preview_inner")) {
		slideshowMode = true;
		
		var total = $("#slideshowControls li").length;
		
		if($("#slideshowControls").hasClass('nocontrols')) {
			$("#slideshowControls").css('display', 'none');
		}
		
		setUp();
	}
	
	function setUp() {
		cycle = setInterval(function(){
			if(count == total) {
				count = 1;
			} else {
				count ++;
			}
			
			
			
			var image = $("#slideshowControls li:nth-child(" + count + ") a").attr("href");
			//console.log(image);
			slideImage(image);
			
			 $("#slideshowControls li.selected").removeClass("selected");
			 $("#slideshowControls li:nth-child(" + count + ")").addClass("selected")

         }, 
      	3000);
	}*/


})(window.jQuery);



