var sFirstImage = null;
var sCurrentImage = null;
var sLastImage = null;
var oScrollable = null;
var bViewInfo = false;
var certificateExists = false;
$(document).ready(function(){
    if (sFirstImage == sCurrentImage) {
        $('#galleryButtonPrev').hide();
    }
    if (sLastImage == sCurrentImage) {
        $('#galleryButtonNext').hide();
    }

    $('#galleryButtonShowThumbs a').click(function(){$('#galleryButtonShowThumbs').slideUp(0, function(){$('div.portfolio-bottom').slideDown();});return false;});
    $('#galleryButtonHideThumbs a').click(function(){$('div.portfolio-bottom').slideUp(function(){$('#galleryButtonShowThumbs').slideDown(0);});return false;});
    $('#galleryButtonInfo').click(function(){return false;});
    $('#galleryButtonInfo')
        .mouseenter(function(){
            if (!bViewInfo) {$('#galleryBlockInfo').fadeIn();}
            return false;
        }).mouseleave(function(){
            setTimeout(function(){if (!bViewInfo) {$('#galleryBlockInfo').fadeOut();}}, 2000);
        });
    $('#galleryBlockInfo')
        .mouseenter(function(e){
            bViewInfo = true;return false;
        }).mouseleave(function(e){
            bViewInfo = false;$(this).fadeOut();return false;
        });
    $('#galleryButtonPrev a').click(function(){
        var oPrev = $('div.scrollable div.items img.active').prev();
        if (oPrev.length) {viewGalleryImage(oPrev[0], true);}
        oScrollable.data('scrollable').prev();
        return false;
    });
    $('#galleryButtonNext a').click(function(){
        var oNext = $('div.scrollable div.items img.active').next();
        if (oNext.length) {viewGalleryImage(oNext[0], true);}
        oScrollable.data('scrollable').next();
        return false;
    });
	$('div.portfolio-bottom a').click(function(){return false;});
	oScrollable = $('div.scrollable').scrollable({size:10, speed: 200});
});

function viewGalleryImage(oImage, setActive)
{
	if($(oImage).hasClass('active')) return true;
	var oOptions = $.parseJSON($(oImage).attr('rel'));
    sCurrentImage = $(oImage).attr('id');
    if (setActive) {
        $(oImage).parent().children('.active').removeClass('active');
        $(oImage).addClass('active');
    }

    if (sCurrentImage == sFirstImage) {
        $('#galleryButtonPrev').hide();
    } else if ($('#galleryButtonPrev').css('display') == 'none') {
        $('#galleryButtonPrev').show();
    }
    if (sCurrentImage == sLastImage) {
        $('#galleryButtonNext').hide();
    } else if ($('#galleryButtonNext').css('display') == 'none') {
        $('#galleryButtonNext').show();
    }
	$('.portfolio-overlay').show();
	$('div.big-image-block img').load(function(){
		
/*		$('#galleryBlockInfo div.portfolio-image-title div.content').html($(oImage).attr('title')); */

		if (oOptions.imageAuthor) {
			$('#galleryBlockInfo div.portfolio-image-author div.content').html(oOptions.imageAuthor).parent().show();
                        if($('#galleryButtonInfo').css('display') == 'none'){$('#galleryButtonInfo').show();}
		} else if (!certificateExists) {
                        $('#galleryButtonInfo').hide();
                } else {
			$('#galleryBlockInfo div.portfolio-image-author').hide();
		}
/*
		if (oOptions.imageDescription) {
			$('#galleryBlockInfo div.portfolio-image-description div.content').html(oOptions.imageDescription).parent().show();
		} else {
			$('#galleryBlockInfo div.portfolio-image-description').hide();
		}

		if (oOptions.imageEquipment) {
			$('#galleryBlockInfo div.portfolio-image-equipment div.content').html(oOptions.imageEquipment).parent().show();
		} else {
			$('#galleryBlockInfo div.portfolio-image-equipment').hide();
		}
*/
		$('.portfolio-overlay').hide();
		$(this).attr({
			width: oOptions.imageWidth,
			height: oOptions.imageHeight,
			alt: $(oImage).attr('title'),
			title: $(oImage).attr('title')
		}).fadeIn(300);
	}).fadeOut(300, function(){
		$(this).attr('src', oOptions.imageSrc);
	});
}

