
var isOldIE = (true == $.browser.msie && Math.round($.browser.version) < 9);

function showImage(anchor) {
	if(!anchor.length) return false;
	
	var title = anchor.attr('title');
	var caption = anchor.attr('data-caption');
	
	anchor.addClass('loading');
	$('#image #title, #image #failed').hide();
	$('#image #loading').show();

	var img = new Image();
	img.src = anchor.find('img').attr('src').replace('-thumb', '');

	var imgSuccess = function() {
		$('#image #container').empty();
		$('#image #container').append(img);
		$('.gallery a').removeClass('selected');
		anchor.addClass('selected');
		
		$('#image #title').html(title).show();
		$('#image #failted, #image #caption, #image #loading').hide(); 
		
		$('#image #caption')
			.html( caption? caption : '' )
			.css('text-align', (caption && caption.length < 64? 'center':'left'))
			.show();
	};
	
	var imgFail = function() {
		anchor.removeClass('loading');
		$('#image #caption, #image #loading, #image #title, #image #caption').hide(); 
		$('#image #failed').show();
	};
		
	if(isOldIE) {
		imgSuccess();
	} else {
		img.onload = imgSuccess;
		img.onerror = imgFail;
	}
	
	return false;
}

$(function() {
	if(!isOldIE) {
		$('.gallery img').MyFadeOverImage({
			normalAlpha: 0.8,
			hoverAlpha: 1,
			normalToneColor: "#000",
			imageWidth: 55,
			imageHeight: 55
		});

		$('.group-block').hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		}).click(function() {
			window.location.href = $(this).find('a').attr('href');
		});
	}
	
	$('.slideshow').cycle({ fx: 'fade' });	
	$(".horizontal-gallery,.scroll").dragscrollable();
	showImage( $('.gallery a:first') );
});

