var $jq = jQuery.noConflict();
$jq(document).ready(function($){

    Cufon.set('fontFamily', 'EFN Adamas').replace('#longtitle', {
        color: '-linear-gradient(0.3=#eaeaea, #5f5f5f)',
        textShadow: '#ffffff 1px 1px'
    });
    
    Cufon.set('fontFamily', 'EFN Adamas').replace('h6', {
        color: '-linear-gradient(0.4=#eaeaea, #afafaf)',
        textShadow: '#000 1px 1px'
    })('div.smbox-wrapper p.title1', {
        color: '-linear-gradient(0.4=#eaeaea, #afafaf)',
        textShadow: '#000 1px 1px'
    })('div.smbox-wrapper p.title2', {
        color: '-linear-gradient(0.4=#eaeaea, #afafaf)',
        textShadow: '#000 1px 1px'
    });
    
    function bgSlideUp(el, pos, duration){
        $(el).stop().animate({
            backgroundPosition: pos
        }, {
            duration: duration
        });
    };
    
    function bgSlideDown(el, pos, duration, callback){
        $(el).css({
            backgroundPosition: "0px -70px"
        });
        $(el).stop().animate({
            backgroundPosition: pos
        }, {
            duration: duration,
            complete: function(){
                $(el).css({
                    backgroundPosition: pos
                });
                if (callback) {
                    callback(this);
                }
            }
        });
    };
    
    $('ul.navi li').mouseover(function(){
        if ($(this).is('.current')) 
            return false;
        bgSlideUp($(this), "0px -70px", 500);
        
    }).mouseout(function(){
        if ($(this).is('.current')) 
            return false;
        bgSlideDown($(this), "0px 0px", 500);
    });
    
    $("ul.navi li:not(.current)").stop().animate({
        backgroundPosition: "(0px 0px)"
    }, {
        duration: 500,
        complete: function(){
            $(this).css({
                backgroundPosition: "0px 0px"
            });
        }
    });
/*  
 * 
 *   
    $('ul.navi li a').click(function(){
        var t = this;
        if ($(t).parent().is('.current')) 
            return false;
        var oldEl = $('ul.navi li.current');
        var docid = new String($(this).attr('class').split(' ').slice(-1));
        $(t).parent().addClass('current');
        $('#content-mid').slideUp(1000, function(){
            $.ajax({
                url: "ajax.html",
                data: "action=getpage&docid=" + docid.substr(1, docid.length),
                type: "POST",
                cache: false,
                ifModified: false,
                async: true,
                dataType: "json",
                success: function(data){
                    data = eval(data);
                    document.title = data.pagetitle;
                    $('#text-wrapper').html(data.content);
                    $('#longtitle').html(data.longtitle);
                    Cufon.refresh('#longtitle');
                    Cufon.refresh('fontFamily', 'EFN Adamas').replace('h6', {
                        color: '-linear-gradient(0.4=#eaeaea, #afafaf)',
                        textShadow: '#000 1px 1px'
                    })('div.smbox-wrapper p.title1', {
                        color: '-linear-gradient(0.4=#eaeaea, #afafaf)',
                        textShadow: '#000 1px 1px'
                    })('div.smbox-wrapper p.title2', {
                        color: '-linear-gradient(0.4=#eaeaea, #afafaf)',
                        textShadow: '#000 1px 1px'
                    });
                    $(oldEl).removeClass('current');
                    initFeedBackForm();
                    if ($('a.fancybox').length) {
                        $('a.fancybox').fancybox({
                            'hideOnContentClick': true,
                            'overlayShow': true,
                            'overlayOpacity': 0.5,
                            'overlayColor': '#000'
                        });
                    };
                    bgSlideDown($(oldEl), "0px 0px", 500, function(){
                        var animationTime = $('#text-wrapper').height();
                        $('#content-mid').slideDown(parseInt(data.animationTime), function(){
                            return false;
                        });
                    });
                },
                error: function(XMLHttpRequest, textStatus, errorThrown){
                    $(t).parent().removeClass('current');
                    bgSlideDown($(t).parent(), "0px 0px", 500, function(){
                        var animationTime = $('#text-wrapper').height();
                        $('#content-mid').slideDown(800, function(){
                            return false;
                        });
                    });
                }
            });
        });
        return false;
    });
*/

    function initFeedBackForm(){
        if ($('#feedBackWrapper').length) {
            var fb = $('#feedbackForm .button').click(function(){
                var form = $('#feedbackForm').formSerialize();
                var panelheight = $('#feedBackWrapper').height();
                $('#feedBackWrapper').fadeOut(800, function(){
                    $.ajax({
                        url: "ajax.html",
                        data: "action=sendmail&" + form,
                        type: "POST",
                        cache: false,
                        async: true,
                        dataType: "json",
                        success: function(data){
                            data = eval(data);
                            $('#feedBackWrapper').html(data.html);
                            initFeedBackForm();
                            var newheight = $('#feedBackWrapper').height();
                            if (newheight != panelheight) {
                                $('.box-mid').animate({
                                    'height': newheight
                                }, {
                                    queue: false,
                                    duration: 500,
                                    complete: function(){
                                    }
                                });
                            }
                            if ($('.errors').length) {
                                $('.errors').show();
                            }
                            $('#feedBackWrapper').fadeIn(800);
                            return false;
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown){
                            $('.errors').html('<p>Nie udało się przesłać formularza. Proszę spróbowac ponownie.</p>');
                            $('.errors').show();
                            $('#feedBackWrapper').fadeIn(800);
                            initFeedBackForm();
                            return false;
                        }
                    });
                }, 600);
                return false;
            });
        }
        return false;
    };
    
    if ($('a.fancybox').length) {
        $('a.fancybox').fancybox({
            'hideOnContentClick': true,
            'overlayShow': true,
            'overlayOpacity': 0.5,
            'overlayColor': '#000'
        });
    };
    
    initFeedBackForm();
    
    $("a.blank").attr("target", "_blank");
    
});
