var baseUrl = "http://klanten.edubits.nl/robineggenkamp.nl/";

$(document).ready(function() {
	//Fix png transparency in IE6 and below
	$('img[@src$=.png]').ifixpng();
	
	
	//Empty menu links
	$('#menu a').empty();
	
	$('a.prev').empty().click(function() {
		$('a.prev').blur();

		$('#photo').fadeOut('slow', function() {
			refreshCurrent(prev);
	
			$('#photo').fadeIn('slow');
	
			//Refresh next and previous
			$.getJSON(baseurl+'photo/ajax/album/1/id/'+prev.id, refreshPhotos);
		});
	});
	
	$('a.next').empty().click(function() {
		$('a.prev').blur();
		$('#photo').fadeOut('slow', function() {
			refreshCurrent(next);
			
			$('#photo').fadeIn('slow');
			
			//Refresh next and previous
			$.getJSON(baseurl+'photo/ajax/album/1/id/'+next.id, refreshPhotos);
		});
	});
});

function refreshCurrent(photo) {
	$('img.current').attr('src', photo.url());
	$('img.current').attr('alt', photo.title);
	$('img.current').width(photo.ratio*480);
	$('#photo').width(photo.ratio*480+6);
	if(photo.title != null) {
		$('#title').text(photo.title+', ');
	} else {
		$('#title').text('');
	}
	$('#date').text(photo.date);
	$('#link').attr('href', photo.url());
}

function refreshPhotos(data) {
	current.update(data.current);
	prev.update(data.prev);
	next.update(data.next);
	
	if(!isNaN(next.id)) {
		$('img.next').attr('src', next.url());
		$('a.next').show();
	}
	else {
		$('a.next').hide();
	}
	
	if(!isNaN(prev.id)) {
		$('img.prev').attr('src', prev.url());
		$('a.prev').show();
	}
	else {
		$('a.prev').hide();
	}
}

function Photo(id, title, date, ratio) {
	this.id = id;
	this.title = title;
	this.date = date;
	this.ratio = ratio;
	this.url = function() {
		if(!isNaN(this.id)) {
			return baseurl+'photo/img/id/'+this.id+'/size/normal';
		} else {
			return baseurl+'photo/img/id/'+current.id+'/size/normal';
		}
	}
	
	this.update = function(data) {
		this.id = data.id;
		this.title = data.title;
		this.date = data.date;
		this.ratio = parseFloat(data.ratio);
	}
}