function showContent(elts, settings, callback) {
    calculateSize(elts, settings);
    elts.loading
			.css({
				marginTop: settings.marginTopLoading+'px',
				marginLeft: settings.marginLeftLoading+'px'
			})
			.show()
			.animate({
				width: settings.width+'px',
				height: settings.height+'px',
				marginTop: settings.marginTop+'px',
				marginLeft: settings.marginLeft+'px'
			}, {duration: 350, complete: function() {
				elts.contentWrapper
					.css({
						width: settings.width+'px',
						height: settings.height+'px',
						marginTop: settings.marginTop+'px',
						marginLeft: settings.marginLeft+'px'
					})
					.show();
					elts.loading.fadeOut(200, callback);
				}
			});
}

function resize(elts, settings, callback) {
    calculateSize(elts, settings);
    elts.contentWrapper
        .animate({
            width: settings.width+'px',
            height: settings.height+'px',
            marginLeft: settings.marginLeft+'px',
            marginTop: settings.marginTop+'px'
        }, {complete: callback, duration: 400});
}

function calculateSize(elts, settings) {
    var image = $('.big_image img');
    var iH = settings.height - settings.borderH;
    var iW = settings.width - settings.borderW - 200;
    var nH = image[0].height
    var nW = image[0].width
    if (nH > iH || nW > iW) {
        var h = nH/iH;
        var w = nW/iW;
        if (h >= w) {
            h = iH;
            w = Math.round(nW*h/nH);
        } else {
            w = iW;
            h = Math.round(nH*w/nW);
        }
        image.attr({height: h, width: w});
        settings.width = w + settings.borderW + 200;
        settings.height = h + settings.borderH;
        elts.content.css({height: settings.height, width: settings.width});
        elts.wrapper.css({height: settings.height, width: settings.width});
    }
    settings.marginLeft = -(settings.width+settings.borderW)/2;
	settings.marginTop = -(settings.height+settings.borderH)/2;
}

$(function() {
    $('#dropdown').css('top', -(5 + $('#dropdown').height()) + 'px');
    $('#tagslink').mouseenter(function(){
        $('#dropdown').animate({top: 0}, 200, 'linear', function(){
            $('#tagslink').text($('#tagslink').text().replace(/↓/g, '↑'));
        });
    });
    $('#dropdown').mouseleave(function(){
        $('#dropdown').animate({top: -(5 + $('#dropdown').height()) + 'px'}, 200, 'linear', function(){
            $('#tagslink').text($('#tagslink').text().replace(/↑/g, '↓'));
        });
    });

    $('a.nyro').nyroModal();
    $('.images a').nyroModal({closeButton: '', showContent: showContent, resize: resize});
    $(".images_wrapper").carousel();
});

var loader_image = new Image();
loader_image.src = '/images/ajaxLoader.gif'

function loadImages(url) {
    $(".images_wrapper").css({background: '50% 50% url(/images/ajaxLoader.gif) no-repeat'});
    $('.images').load(url, null, function(){
        $('.images a').nyroModal({closeButton: '', showContent: showContent, resize: resize});
        $(".images_wrapper").carousel();
        $(".images_wrapper").css({background: 'none'});
    });
    return false;
}

