$(function () {


	var fadeSpeed = 220;
	var drawerFadeSpeed = 150;
	
	
	
	//--Gallery hover states
	$('.tn a img').not('.selected').hover(function (event) {
   		$(this).not('.selected').animate({ opacity: 1.0 }, fadeSpeed);
  	},function (event) {
    	$(this).not('.selected').animate({ opacity: 0.3 }, fadeSpeed);
  	});
  	
  	
  	$('#prev').hover(function () {
   		$("#prev_image").animate({ opacity: 1.0 }, fadeSpeed);
   		$('.tn').stop().animate({ marginTop: "0px" }, fadeSpeed);
  	},function () {
    	$("#prev_image").animate({ opacity: 0.01 }, fadeSpeed);

  	});
  	
  	$('#next').hover(function () {
   		$("#next_image").animate({ opacity: 1.0 }, fadeSpeed);
   		$('.tn').stop().animate({ marginTop: "0px" }, fadeSpeed);
  	},function () {
    	$("#next_image").animate({ opacity: 0.01 }, fadeSpeed);
  	});
  	
  	
  	$('.main').hover(function (event) {
  		
  	},function (event) {
    	$('.tn').stop().animate({ marginTop: "0px" }, drawerFadeSpeed);
  	});
  
  	var config = {    
     over: showTn,   
     timeout: 100,     
     sensitivity: 7,
     interval: 80,
     out: hideTn_nil    
	};

	$("#show_tn").hoverIntent(config);
  	
  	function showTn() {
  		$('.tn').stop().animate({ marginTop: "-71px" }, drawerFadeSpeed);
  	}
  	function hideTn_nil(){ return false; }

  	
  	//--Actions
  	
  	$('.tn a').click(function(e){
	  	//cancels scrolling to top
  		e.preventDefault();
  		return false;
  	});
  
  	
  	$('.tn a img').click(function(){
	  	//thumb click
  		if($(this).hasClass('selected')) { return false; }
  		
		$('.tn a img.selected').removeClass('selected');
		$(this).addClass('selected');
		$('.tn a img').not('.selected').animate({ opacity: 0.3 }, fadeSpeed);
		
		var imageId = "#photo" + $(this).next().val();
		
		var selected_image = $(imageId);
		
		$('.holder').remove(imageId);
		var $holder_selected = $('.holder img.selected');
		$(selected_image).insertBefore($holder_selected);
		//$(selected_image).show();
		$(selected_image).fadeTo(0,1.0);
		$holder_selected.removeClass('selected');
		$(selected_image).addClass("selected");

		$holder_selected.fadeTo(300,0,function(){
				$("#fadeContainer").children().last().remove();
			});
		
	});
  	
  	
  	$('#next').click(function(){
	  	//next click
  		var selected_index = $('.tn a img.selected').next().val();
  		selected_index++;
  		if(selected_index > 15) { selected_index = 1; }
  		var tnId = "#photo_tn" + selected_index;
		$(tnId).animate({ opacity: 1.0 }, 120);
		$(tnId).click();
  	});
  	
  	
  	$('#prev').click(function(){
	  	//prev click
  	  	var selected_index = $('.tn a img.selected').next().val();
  		selected_index--;
  		if(selected_index <= 0) { selected_index = 15; }
  		var tnId = "#photo_tn" + selected_index;
		$(tnId).animate({ opacity: 1.0 }, 120);
		$(tnId).click();
  	});
  	
  	
  	//INITIAL STATE
  	//animate drawer down
   	setTimeout(function() { $('.tn').stop().animate({ marginTop: "0px" }, fadeSpeed); }, 2000); 
	
 	//--Select first image
	$("#photo1").addClass("selected");
	$("#.tn a img").first().addClass("selected");
	
	//--Initialize states
	$('.tn a img').not('.selected').animate({ opacity: 0.3 }, 1);
	
	$('#prev_image').animate({ opacity: 0.01 }, 1);
	$('#next_image').animate({ opacity: 0.01 }, 1);
	
	$('.holder img').not('img.selected').hide();
  	
});
