$(document).ready(function()
{
	setTimeout("$('#scroller').jScrollPane({scrollbarOnLeft: true, scrollbarMargin: 20, topCapHeight: 5, bottomCapHeight: 5, reinitialiseOnImageLoad: true})", 100);
    initMenu();
    initRolloverMenus();
    initExternalLinks();
    initSlimbox();
    replaceEmails();
});

function initMenu()
{
    if ($("#menu_top").css('background-image') != 'none')
    {
        // ustawiamy podswietlony element
        $("#menu_top a.selected").each(function()
        {
            $(this).css('background-image', 'url("' + menu_top_preload_img.src + '")').css('background-position', '-' + $(this).position().left + 'px 0px');
        });

        // dodajemy podswietlenie tla dla gornego menu, dla elementow nie nie wybranych
        $("#menu_top :not(a.selected)").bind(
        {
            mouseenter: function()
            {
                $(this).css('background-image', 'url("' + menu_top_preload_img.src + '")').css('background-position', '-' + $(this).position().left + 'px 0px');
            },
            mouseleave: function()
            {
                $(this).css('background-image', 'none');
            }
        });

        // rozciagamy ostatni element gornego menu, zeby wypenil cala przestrzen przeznaczona dla menu
        menu_elements_width = 0;
        $("#menu_top a").each(function()
        {
            menu_elements_width += $(this).outerWidth();
        });

        if (menu_elements_width < $("#menu_top").outerWidth())
        {
            last_el_old = $("#menu_top a").last().width();
            $("#menu_top a").last().width(last_el_old + $("#menu_top").outerWidth() - menu_elements_width - 2);
        }
    }
}

function replaceEmails()
{
    $("a.replace").each(function()
    {
        $(this).attr('href', $(this).attr('href') + '@grabczewscy.com.pl');
        $(this).text($(this).text() + '@grabczewscy.com.pl');
    });
}

function initRolloverMenus()
{
    $("a.rollover").each(function()
    {
        $(this).click(function()
        {
            if ($(this).hasClass("selected"))
            {
                $(this).blur();
                return false;
            }
            
            $("a.rollover").removeClass("selected");
            $(this).addClass("selected").blur();

            $("ul.rollover").filter(function(){ return $(this).css("display") == "block" ? true : false; }).slideUp();
            $("ul.rollover", $(this).parent()).first().slideDown();
            return false;
        });
    });
}

function initExternalLinks()
{
    $("a[rel=external]").attr('target', '_blank');
}

function initSlimbox()
{
    if (!/android|iphone|ipod|series60|symbian|windows ce|blackberry/i.test(navigator.userAgent))
    {
        jQuery(function($)
        {
            $("img.enlargeable").slimbox({loop: true, counterText: slimbox_counter_text}, function(el)
            {
                return [el.src.replace(/\.(jpg|gif|png)$/i, "_mq.$1"), el.title.replace(/\[(.*)\]/i, "<br /><span style=\"font-weight: normal; font-style: italic;\">$1</span>")];
            });
        });
    }
}