var img_width = 32;
var img_height = 32;
var img_current_width = 32;
var img_current_height = 32;
var img_x_padding = 20;
var img_y_padding = 20;
var img_x_margin = 20;
var img_y_margin = 20;

var gallery;
var gallery_pos;
var gallery_labels;


var click_help = 'Zavři kliknutím';
var next_help = 'Zobrazit další obrázek';
var prev_help = 'Zobrazit předchozí obrázek';

function lightbox(img) {
	img_width = img_height = img_current_width = img_current_height = 32;
    img_x_padding = img_y_padding = 20;

	$('body').append('<div title="' + click_help + '" id="ltbg"></div><div title="' + click_help + '" id="ltcontent"><div id="ltloading" /><img id="ltimg" src="" /></div>');
	updateContent(0);
	$('#ltbg').height($(document).height());
	$('#ltcontent,#ltbg').click(lightboxHide);

	$('#ltimg').load(function() {
		$('#ltloading').hide();

		img_width = $('#ltimg').width();
		img_height = $('#ltimg').height();

		$('#ltimg').css({
			visibility: 'visible',
			width: '100%',
			height: '100%'
		});

		updateContent(500);
	}).attr('src', img);

	$(window).bind('scroll resize',function () {updateContent(1000);});
}

function lightbox_gal(img, label) {
	img_width = img_height = img_current_width = img_current_height = 32;
    img_y_padding = 60;
    img_x_padding = 80;

    var c = '';
    var l = '';
    if (label != null) {
    	c = 'class="label"';
    	l = '<div id="ltlabel">' + label + '</div>';
    }
	$('body').append('<div title="' + click_help + '" id="ltbg"></div>'+
					 '<div title="' + click_help + '" id="ltgalcontent" ' + c + '>'+
					 '<a id="ltprev" title="' + prev_help + '"></a>' + 
					 '<div id="ltloading"></div><img id="ltimg" src="" />' + 
					 '<a id="ltnext" title="' + next_help + '"></a>' + l +
					 '</div>');
					 
	updateContent(0);
	$('#ltbg').height($(document).height());
	$('#ltgalcontent,#ltbg').click(lightboxHide);

	$('#ltimg').load(function() {
		$('#ltloading').hide();

		$('#ltimg').height('auto').width('auto');
		img_width = $('#ltimg').width();
		img_height = $('#ltimg').height();

		$('#ltimg').css({
			visibility: 'visible',
			width: '100%',
			height: '100%'
		});
		$('#ltlabel').css({visibility: 'visible'});
		
		updateContent(500);
	}).attr('src', img);

	$(window).bind('scroll resize',function () {updateContent(1000);});
	$('#ltnext').click(function (e) {
		gallery_pos = (gallery_pos + 1) % gallery.length;
		$('#ltloading').show();
		$('#ltimg').css({visibility: 'hidden'}).attr('src',gallery[gallery_pos]);

		if (gallery_labels) {
			$('#ltlabel').text(gallery_labels[gallery_pos]);
		}
		
		return false;
	});
	$('#ltprev').click(function (e) {
		gallery_pos = (gallery_pos + gallery.length - 1) % gallery.length;
		$('#ltloading').show();
		$('#ltimg').css({visibility: 'hidden'}).attr('src',gallery[gallery_pos]);

		if (gallery_labels) {
			$('#ltlabel').text(gallery_labels[gallery_pos]);
		}
		return false;
	});
	if (!window.XMLHttpRequest) $('#ltnext,#ltprev').hover(function(){$(this).addClass('hover');}, function() {$(this).removeClass('hover');});
	$('#ltgalcontent').disableTextSelect();
}

function lightboxHide() {
	$('#ltbg,#ltcontent,#ltgalcontent').remove();
}

function updateContent(time) {
	var content_width = img_width;
	var content_height = img_height;

	var window_width = $(window).width();
	var window_height = $(window).height();

	var max_width = window_width-img_x_margin-img_x_padding;
	var max_height = window_height-img_y_margin-img_y_padding;

	var top = $(window).scrollTop();
	var left = $(window).scrollLeft();

	if (content_width > max_width) {
		content_height *= max_width/content_width;
		content_width = max_width;
	}
	if (content_height > max_height) {
		content_width *= max_height/content_height;
		content_height = max_height;
	}

	content_height = Math.floor(content_height);
	content_width = Math.floor(content_width);

	top = ((window_height-content_height-img_y_padding)/2 + top) + 'px';
	left = ((window_width-content_width-img_x_padding)/2 + left) + 'px';

	$('#ltcontent,#ltgalcontent').stop().animate({
		width: content_width + 'px',
		height: content_height + 'px',
		top: top,
		left: left
	}, time);
}

$().ready(function () {
	$('[rel="lightbox"]').click(function() {
		lightbox($(this).attr('href'));
		return false;
	});
});


function lightbox_gallery(images, pos, labels) {
	gallery = images;
	gallery_pos = 0;
	gallery_labels = labels;
	if (pos) gallery_pos = pos;
	
	if (gallery_labels) {
		lightbox_gal(gallery[gallery_pos], gallery_labels[gallery_pos]);
	} else {
		lightbox_gal(gallery[gallery_pos]);
	}
}



(function($) {
    if ($.browser.mozilla) {
        $.fn.disableTextSelect = function() {
            return this.each(function() {
                $(this).css({
                    'MozUserSelect' : 'none'
                });
            });
        };
    } else if ($.browser.msie) {
        $.fn.disableTextSelect = function() {
            return this.each(function() {
                $(this).bind('selectstart', function() {
                    return false;
                });
            });
        };
    } else {
        $.fn.disableTextSelect = function() {
            return this.each(function() {
                $(this).mousedown(function() {
                    return false;
                });
            });
        };
    }
})(jQuery);
