$(function () {


	var fadeSpeed = 220;
	var total = $(".tn a").length;
  	
	$("#.tn a").hover(function () {
	   	$(this).animate({ opacity: 0.85 }, 200);
	  	},function () {
			if($(this).hasClass('selected')){
			
			}else{
				$(this).animate({ opacity: 1.0 }, 200);
			}
	  	});

	$("#photo_thumbnails_btn").click(function(e){
		e.preventDefault();
		$('#.tn a').css("opacity", 1.0);
		$('#.tn a.selected').css("opacity", 0.85);
		
		//$('#.tn a.selected').animate({ opacity: 0.85 }, 0.1);
		
		$(".tn").fadeToggle("fast");
	});

  	$('#prev').hover(function () {
   		$("#prev_image").animate({ opacity: 1.0 }, fadeSpeed);
  	},function () {
    	$("#prev_image").animate({ opacity: 0.01 }, fadeSpeed);
  	});
  	
  	$('#next').hover(function () {
   		$("#next_image").animate({ opacity: 1.0 }, fadeSpeed);
  	},function () {
    	$("#next_image").animate({ opacity: 0.01 }, fadeSpeed);
  	});
  	


	$('.tn a').click(function(e){
		
		e.preventDefault();
		
  		if($(this).hasClass('selected')) { return false; }
  		
		$('.tn a.selected').removeClass('selected');
		$(this).addClass('selected');
		
		var cnt =  $(this).next().val()
		var imageId = "#photo" + cnt;
		
		updateLocation(cnt);
		//update counter
		
		$("#tn_counter").html(cnt);
		
		var selected_image = $(imageId);
		
		$('.holder').remove(imageId);
		var $holder_selected = $('.holder img.selected');
		$(selected_image).insertBefore($holder_selected);
		$(selected_image).fadeTo(0,1.0);
		$(selected_image).show();
		$holder_selected.removeClass('selected');
		$(selected_image).addClass("selected");
		
		if( $('.tn').is(':visible') ) {
			var fadeDelay  = 0.1;
		} else {
			var fadeDelay = 300;
		}
		$holder_selected.fadeTo(fadeDelay,0,function(){
			//	$("#fadeContainer").children().last().remove();
			$holder_selected.hide();
			});
			
		//$("#fadeContainer").children().last().remove();
			
			
		//set description
		var imageDescription = $(this).next().next().val();
		$("#photo_info_btn").html("" + imageDescription + "");
		
		if( $('.tn').is(':visible') ) {
			$(".tn").fadeOut("fast");
		}
		
		
		return false;
		
	});
  	
  	
  	$('#next').click(function(){
	  	//next click
  		var selected_index = $('.tn a.selected').next().val();
  		selected_index++;
  		if(selected_index > total) { selected_index = 1; }
  		var tnId = "#photo_tn" + selected_index;
		$(tnId).click();
  	});
  	
  	
  	$('#prev').click(function(){
	  	//prev click
  	  	var selected_index = $('.tn a.selected').next().val();
  		selected_index--;
  		if(selected_index <= 0) { selected_index = total; }
  		var tnId = "#photo_tn" + selected_index;
		$(tnId).click();
  	});
  	
  	$("#photo_info_prev").click(function(e){
		e.preventDefault();
		$('#prev').click();
	});
	
	$("#photo_info_next").click(function(e){
		e.preventDefault();
		$('#next').click();
	});
  
 	//--Select first image
//	$("#photo1").addClass("selected");
//	$("#.tn a").first().addClass("selected");
	
	//--Initialize states
	$('#prev_image').animate({ opacity: 0.01 }, 1);
	$('#next_image').animate({ opacity: 0.01 }, 1);
	
	$('.holder img').not('img.selected').hide();
  	
	var intialText = $(".tn a.selected").next().next().val();
	$("#photo_info_btn").html("" + intialText + "");
	$("#tn_total").html(total);
	
	var hash =  gethash();
	if(hash) {
		if(hash <= total && hash > 0){
	  		var tnId = "#photo_tn" + hash;
			$(tnId).click();
		} else {
			alert("no");
		}
	}
	
});

function updateLocation(type){
	window.location = window.location.pathname + "#" + type;
}

function gethash(){
	var hash=window.location.hash;
	hash=hash.replace(/#/,'');
	//if(hash == settings.currenthash) return;
	return hash;
}
