$(document).ready(function() {
    // description animation and close button
    $("#photo-description:not(.no-anim)").animate({
        width: "364px"
    }, '1500').children('p').append("<a href='#' class='close'>x</a>").find(".close").click(function(){
        $(this).parent().parent("#photo-description").hide('fast');
        return false;
    });
    
    // images in news
    if ($("#news-content img").length > 0) {
        $("#news-content img").each(function(){
            var element = $(this);
            var width = element.width();
            var height = element.height();
            
            if (width > 590) {
                element.attr('width', '590');
                element.attr('height', Math.floor(590 * height / width));
            }
        });
    }
    
    // flashplayer
    if (fp = $("object")[0]) {
        $(fp).removeAttr('style');
        var width = fp.width;
        var height = fp.height;
        $(fp).wrap("<div style='width: " + width + "px; height: " + height + "px;'></div>");
    }
    
    
    var photo_cont;
    // if in photo show layout
    if (photo_cont = $("#photo-container")[0]) {
        var difference = ($(photo_cont).height() + 20 + $("#page-content").height()) - $("#sidebar").height() + 28;
        
        if (difference > 0) {
            $("#sidebar").height($("#sidebar").height() + difference);
        } else {
            $("#page-content").height($("#page-content").height() - difference);
        }
    } else {
        var difference = $("#page-content").height() - $("#sidebar").height() + 29;
        
        if($("#main-photo")[0]){
            difference += $("#main-photo").height() + 51;
        }
        
        if (difference > 0) {
            $("#sidebar").height($("#sidebar").height() + difference);
        } else {
            $("#page-content").height($("#page-content").height() - difference);
        }
    }
    if ($(".overlay-gallery")[0]) {
        var overlay_obj = $("<div />");
        overlay_obj.css("display", "none")
                .attr('id', "overlay-gallery")
                .attr('class', 'simple-overlay')
                .css('background', '#fff')
                .css('padding', '10px')
                .css("width", "620px")
                .css("height", "400px")
                .css('border', '1px solid #000')
                .css('border-radius', '5px')
                .css('-moz-border-radius', '5px')
                .css('-webkit-border-radius', '5px')
                .html('<div id="photos-nav"><a class="prev">&laquo; poprzednie</a><a class="next">następne &raquo;</a></a>')
                .append('<img class="progress" src="/public/images/ajax-loader.gif" />')
                .appendTo('body');
        overlay_obj.find('.progress')
                .css("position", "absolute")
                .css("top", "45%")
                .css("left", "40%")
                .css("display", "none");
        overlay_obj.find('a')
                .css('cursor', 'pointer');
        
        $(".overlay-gallery li a").overlay({
            target: '#overlay-gallery',
            expose: '#000000'
        }).gallery({
            speed : 800,
            opacity : 1,
            next : '.prev',
            prev : '.next'
        });
    }
    
    
    if ($("#dyno")[0]) {
        var overlay_obj = $("<div />");
        overlay_obj.css("display", "none")
                .attr('id', "overlay-dyno")
                .attr('class', 'simple-overlay')
                .css('background', '#fff')
                .css('padding', '10px')
                .css('border', '1px solid #000')
                .css('border-radius', '5px')
                .css("width", "620px")
                .css("height", "400px")
                .css('-moz-border-radius', '5px')
                .css('-webkit-border-radius', '5px')
                .appendTo('body');
    
        $("#dyno a").overlay({
            target: '#overlay-dyno',
            expose: '#000000'
        }).gallery();
    }
});
