var my_current_id = '';

var loadzoomlibrary = function() {
	var initLayout = function() {
		$('div#galleryElement a.open').zoomimage({
			border: 10,
			centered: true,
			hideSource: true,
			opacity: 0.8,
			duration: 450,
			prevent: 100,
			controls: true,
			controlsTrigger: "mouseover",
			preload: "load"
		});
	};
	EYE.register(initLayout, 'init');
	EYE.init();
}

var new_gallery = function(hlink) {

	$('.loader').css('background-image','url(../images/preloader.gif)');
	$('div#galleryElement').hide();

	$.ajax({
		type: "GET",
		url: hlink + "?pid=" + Math.random() * 1000,
		dataType: "html",
		async: true,
		success: function(msg) {
			$('div#galleryElement').html(msg);
			$('div#galleryElement a.open').each(function(index){
				$(this).append("<p>"+ $(this).attr('title') +"</p>");
			});

			loadzoomlibrary();
			$('div#galleryElement').slideDown("slow");
			$('div.loader').css('background-image','none');
		}
	});
};

$(document).ready(function(){

	$('div.galleryMenu a').click(function(e){
		e.preventDefault();
		if($(this).attr('id') != my_current_link) {
			new_gallery($(this).attr('href'));
			
			$('div.galleryMenu a#' + my_current_id).removeClass('active');
			my_current_id = $(this).attr('id');
			$(this).addClass('active');
		}
	});

	var my_current_link = $('div.galleryMenu a#' + my_current_id).attr('href');
	$('div.galleryMenu a#' + my_current_id).addClass('active');

	new_gallery(my_current_link);
});