jQuery().ready(function(){
	$('.ui-accordion').accordion({
		/*event: 'mouseover',*/
		header: '.accordion-toogle',
		active: '.firstopen',
		selectedClass: 'open',
		navigation: true,
		autoHeight: false,
		animated: 'easeslide'
		/*, alwaysOpen: false*/
	});
	$('.accordion-toogle').css({
		"cursor": "pointer"
	});

	var map = null;
	$("#accordion").bind('accordionchange', function(event, ui) {
		if (ui.newContent.find("#gmap").attr('id') == 'gmap' && !map)
		{
			map = initialize();
		}
	});


	$('.clickgallery ul li a').click(function() {
		var imgBiggerPreview = $(this).attr('rel');
		var imgLightbox = $(this).attr('href');

		$('.clickgallery ul li').removeClass('open');
		$(this).parent().addClass('open');

		$('.clickgallery p a').attr('href', imgLightbox);
		$('.clickgallery p a img').attr('src', imgBiggerPreview);

		adjustPreview(imgBiggerPreview);

		return false;
	});

	$('.clickgallery ul li:first').addClass('open');
	adjustPreview($('.clickgallery ul li a').attr('rel'));
	
	$("#accordion").bind('accordionchange', function(event, ui) {
		if (ui.newContent.find("#gmap").attr('id') == 'gmap' && !map)
		{
			initialize();
		}
	});
});

function adjustPreview(imgBiggerPreview) {
	var img = new Image();
	img.onload = function() { 
		if(img.height > img.width) {
			$('.clickgallery p').addClass('highsize');
			$('.clickgallery p a').css('margin-top','0');
			$('.clickgallery p a').css('margin-left',((300-(225*img.width/img.height))/2)+'px');
		}
		else {
			$('.clickgallery p').removeClass('highsize');
			$('.clickgallery p a').css('margin-top',((225-(img.height/img.width*300))/2)+'px');
			$('.clickgallery p a').css('margin-left','0');
		}
	};
	img.src = imgBiggerPreview; /*always set src after onload because of caching*/
}
