$(document).ready(function() {
	var noOfThumbs = $("#thumbsreel img").size();
	var widthOfThumbs = $("#thumbsreel img").width() + 16;
	var thumbsReelWidth = noOfThumbs * widthOfThumbs;
	$("#thumbsreel").css({'width' : thumbsReelWidth});
});	

function setActiveThumb(id){
	
	var activeThumb = "#thumb-" + id;
	oldID = activeThumb;
	$(activeThumb).css('opacity',0.5);
	
}
var scrolled = 0;
var scrolling = 0; // hack
function right(){
	if (scrolling===1){
		return;
	}
	

	if(scrolled > -($("#thumbsreel").width()-384)){
		scrolling = 1;
  	$('#thumbsreel').animate({
	    left: '-=384',
	  }, 700, function() {
		scrolled -= 384;
		// DONE ! ok to press right again
		scrolling = 0;
	  });
	}
}
function left(){
	if (scrolling===1){
		return;
	}

	
	if(scrolled < 0){
		scrolling = 1;
  	$('#thumbsreel').animate({
	    left: '+=384',
	  }, 700, function() {
		scrolled += 384;
		scrolling = 0;
	  });
	}
}
var oldID = null;
function getSweInfo(id){
	var url = "http://ateljetorvaldsson.se/?attachment_id=" + id;
	var newID = "#thumb-" + id;
	if(oldID!=null){
		$(oldID).removeAttr("style");
	}
	$(document).find(newID).css('opacity',0.5);
	oldID = newID;
	$(document).find("#loader").css('display', 'block');
	$(document).find("#image img").css('opacity',0.1);
	//alert($(document).find("#loader").html());
	$.get(url, function(responseData){
	// anrop av callback
		responseData = $('<div>' + responseData + '</div>');
		var image = $(responseData).find('#image').html();
		var desc = $(responseData).find('#swedish_desc').html();
		var title = $(responseData).find('#swedish_title').html();
		
		$('#image').html(image);
		$('#desc').html(desc);
		$('#title').html(title);
		
		var imageHeight = $('#image img').height();
		//alert(imageHeight);
		if(imageHeight != 0){
			if(imageHeight < 320 ){
				var remainder = (320-imageHeight)/2;
				
				var quotient = ( (320-imageHeight) - remainder ) / 2;
				if(quotient > 1){
				var margin = quotient;
				$('#image img').css('margin-top',margin);
				}
			}
		}
		$(document).find("#loader").css('display', 'none');
	});
	
}
function getEngInfo(id){
	var url = "http://ateljetorvaldsson.se/?attachment_id=" + id;
	var newID = "#thumb-" + id;
	if(oldID!=null){
		$(document).find(oldID).removeAttr("style");	
	}
	$(document).find(newID).css('opacity',0.5);
	oldID = newID;
	$(document).find("#loader").css('display', 'block');
	$(document).find("#image img").css('opacity',0.1);
	$.get(url, function(responseData){
	// anrop av callback
		responseData = $('<div>' + responseData + '</div>');
		var image = $(responseData).find('#image').html();
		var desc = $(responseData).find('#english_desc').html();
		var title = $(responseData).find('#english_title').html();
		
		$('#image').html(image);
		$('#desc').html(desc);
		$('#title').html(title);
		
		var imageHeight = $('#image img').height();
		//alert(imageHeight);
		if(imageHeight != 0){
		if(imageHeight < 320 ){	
			var remainder = (320-imageHeight)/2;
			var quotient = ( (320-imageHeight) - remainder ) / 2;
			if(quotient > 1){
				var margin = quotient;
			$('#image img').css('margin-top',margin);
			}
		}
	}	
		$(document).find("#loader").css('display', 'none');
	});
}
function rollSlidesForward(){
	$("#thumbs").animate({
        left: 80
    }, 500 );
}
