var photoData=new Object();
var orderedPhotoData=new Object();
var cache = [];
var currentCenterTarget=null;

$(window).resize(function(){resizeImage();resizeScrollers()});
$(document).ready(function(){
	$('.scriptonly').css('display','block');
	$('.scrollcontainer').css('overflow','hidden');
});	

function resetPhotoData()
{
	photoData=new Object();
	orderedPhotoData=new Object();
}

function getProjectId(photoid)
{
    return photoData[photoid]['projectid'];
}

function getProjectName(photoid)
{
    return photoData[photoid]['projectname'];
}

function getProjectURI(photoid)
{
    return "/projectgallery/" + photoData[photoid]['uri'];
}

function registerPhotos(morePhotos){
	for (order in morePhotos) { 
		orderedPhotoData[order] = morePhotos[order];
		for (photoid in morePhotos[order]) { 
		photoData[photoid] = morePhotos[order][photoid];
		}
	}
}

function setImage(photoId){
	currentPhoto = photoId.toString();
	newImage=$(document.createElement('img'));
	newImage.addClass("largephoto");
	newImage.attr("data-photoid",photoId);
	newImage.attr("data-ratio",photoData[photoId].ratio);
	newImage.attr("src","/images/large/"+photoId+".jpg");
	$("#mainimage").prepend(newImage.get(0));
	if(!(topImage===undefined))
		$(topImage).animate({opacity: "hide"},600,"linear", function(){$(this).remove()});
	topImage=newImage;
	resizeImage();
}

function resizeScrollers(){
	$(".scrollcontainer").each(function(){resizeScroller($(this));});
}

function resizeScroller(scrollcontainer){
	newheight=150;
	if(scrollcontainer.width() < 300)
	{
		newheight=Math.max(50,Math.floor(scrollcontainer.width()/2));
	}
	scrollcontainer.height(newheight);
	scrollcontainer.find(".thumbnail").each(function()
		{
			$(this).height(newheight);
			$(this).width(photoData[$(this).attr("data-photoid")].ratio * newheight);
		}
	)
	scrollcontainer.parent().parent().find(".scrollleft,.scrollright").height(newheight);
	scrollcontainer.parent().parent().parent().parent().parent().height(newheight);
	
	$("#comment").css('bottom',newheight+20);
	$("#comment").css('height',$("#comment").parent().height()-(newheight+50));
	
}

function resizeImage(){
	$('img.largephoto').each(function(){
		jthis=$(this);
		ratio = jthis.attr('data-ratio');
		size=Math.min(800,$("#mainimage").height());
		size=Math.min(size,Math.floor($("#mainimage").width()/ratio));
		borderleft=1+Math.floor(($("#mainimage").width()-size*ratio)/2);
		bordertop=1+Math.floor(($("#mainimage").height()-size)/2);
		jthis.css('height',size)
		.css('width',Math.floor(size*ratio))
		.css('border-left', borderleft+"px solid white")
		.css('border-right', borderleft+"px solid white")
		.css('border-top', bordertop+"px solid white")
		.css('border-bottom', bordertop+"px solid white");
		
		if(jthis.attr('data-photoid') == currentPhoto){
			$('#caption').css('border-left', borderleft+"px solid white");
		}
	});
}

function preloadImage(photoId){
	var cacheImage = document.createElement('img');
	cacheImage.src = "/images/large/"+photoId+".jpg";

	cache.push(cacheImage);
}

function scrollLeft(element){
	scrollcontainer=element.parent().parent().children('td').children('.scrollcontainer');
	label=scrollcontainer.children(".itemcontainer").children(".categorybox");
	distance=scrollcontainer.width()*0.8;
	if(label.length==1) {distance-=label.width();};	
	newleft=scrollcontainer.scrollLeft()-distance;
	scrollcontainer.animate({scrollLeft: newleft}, distance);
	makeLabelVisible(label,newleft);
}

function scrollRight(element){
	scrollcontainer=element.parent().parent().children('td').children('.scrollcontainer');
	label=scrollcontainer.children(".itemcontainer").children(".categorybox");
	distance=scrollcontainer.width()*0.8;
	if(label.length==1) {distance-=label.width();};		
	newleft=scrollcontainer.scrollLeft()+distance;
	scrollcontainer.animate({scrollLeft: newleft}, distance);
	makeLabelVisible(label,newleft);
}

function makeLabelVisible(label, newleft){
	label.prependTo(label.parent());
	while(label.next().length==1 && (label.position().left < newleft ))
	{
		label.insertAfter(label.next());
	}
}

function centerScrollItem(element,delay){
	if(delay!=0 && currentCenterTarget && currentCenterTarget.get(0) == element.get(0)){
		return;
	}
	currentCenterTarget=element;
	label=element.parent().children(".categorybox");
	pos=element.attr('offsetLeft') + element.width()/2 - element.parent().parent().width()/2;
	element.parent().parent().stop();
	if(delay===undefined)
	{	
		delay=200;
	}
		
	element.parent().parent().animate({scrollLeft: pos},delay);
	if(label.length==1){
		if(label.position().left < element.position().left)
		{
			label.insertBefore(element);
		}
		else
		{
			label.insertAfter(element);
		}
	}
}
