$(document).ready(function(){

    $('.tab-container').tabs();

    $('#nav li a').colorTo();

    // szukajka
    $('input.placeholder').placeholder();
    

    $menu_parents = $('#nav .has-children');
    $menu_parents.each(function(){
        
        $(this).click(function(){
            //            $menu_parents.removeClass('hover');
            //            $(this).addClass('hover');
            });
    });
    
    if($('#od, #do').length)
        $.datepicker.setDefaults( $.datepicker.regional[ "pl" ] );
    $('#od, #do').datepicker({
        monthNames: ['Styczeń','Luty','Marzec','Kwiecień','Maj','Czerwiec','Lipiec','Sierpień','Wrzesień','Październik','Listopad','Grudzień'],
        dayNamesMin: ['Pn', 'Wt', 'Sr', 'Cz', 'Pt', 'So', 'Nd'],
        showOn: 'both',
        buttonImage: "images/calendar25.png",
        buttonImageOnly: true,
        dateFormat: 'yy-mm-dd'
            
    }
    );

    // kij go wie
    $('.right-column .default-box').not('.half-right-column *').not('.news .default-box').not('.no-autohide').not('.no-autohide .default-box').toggleBox({
        autohide: true
    });
    $('.zrown-model, .dynamic-model').toggleBox({
        autohide: true
    });
    
    $('.roll-ikze .ikze-summary, .ikze-compare table, .ikze-efect table').hide();
    
    $('.roll-ikze h3').click(function(){
        $('.roll-ikze .ikze-summary').fadeToggle('slow');
    });
    $('.ikze-compare h3').click(function(){
        $('.ikze-compare table').fadeToggle('slow');
    });
    $('.ikze-efect h3').click(function(){
        $('.ikze-efect table').fadeToggle('slow');
    });
    
    
    $('.nowe-strategie-body').hide();
    $('.nowe-strategie a').click(function(){
        $('.nowe-strategie-body').fadeToggle('slow');
        return false;
    });
    
    //$('.news .right-column .default-box').toggleBox();

    
    
    if($('#poll label span').length > 0){
       
        $('#poll label .tip').hover(function(){
            $(this).parent().children('.tip-body').show();
            $(this).children('img').attr('src', '/images/q2.png');
        }, function(){
            $(this).parent().children('.tip-body').hide();
            $(this).children('img').attr('src', '/images/q1.png');
        });

    }
    
    
    
    if($('.adjust-en').length > 0){
        $('.adjust-size').append('<span>Change font size</span><a href="#" class="adjust-plus">+</a><a href="#" class="adjust-minus">-</a><a class="print-all" href="#">Print</a>');
    }else if($('.adjust-size').length > 0){
        $('.adjust-size').append('<span>Zmień rozmiar czcionki</span><a href="#" class="adjust-plus">+</a><a href="#" class="adjust-minus">-</a><a class="print-all" href="#">Drukuj</a>');
    }
    

    // font +
    if($('.adjust-plus').length > 0){
        $('.adjust-plus').click(function(){
            $.changeFontSize([
                $('td'),
                $('th'),
                $('h3'),
                $('.default-box p'),
                $('#content li')
                ]);
            return false;
        });
    }

    // font -
    if($('.adjust-minus').length > 0){
        $('.adjust-minus').click(function(){
            $.changeFontSize([
                $('td'),
                $('th'),
                $('h3'),
                $('.default-box p'),
                $('#content li')
                ], {
                    mode: 'decrace'
                });
            return false;
        });
    }
    
    
    $('.print-all').click(function(){
        window.print();
        return false;
    });

    // toplayer
    $('#TopLayerM').each(function() {
        $(this).show();
        $(this).click(function(){
            $('#TopLayerM').remove();
        })

        var topMargin = parseInt( ( $(window).height( ) - 350 ) / 2 );
        $('#Obrazek').css( 'margin-top', topMargin + 'px' );
        $(this).css( 'width', $(window).width( ) );
    });

    $('#ObrazekExit').click(function() {
        $('#TopLayerM').remove();
    });

    $('INPUT[name="obrazekCheck"]').click(function() {
        if ($(this).attr('checked') == true) {
            createCookie(this.id, 1, 365);
        }
        else {
            createCookie(this.id, 0, 365);
        }
    });

    // rotator bannerów
    $('#slider').slider();
});

var timerFunc;
jQuery.fn.slider = function(){
    var banners = $('#slider div.banner');
    var current = 0;
    var timerStart = 3000;
    var timer = 0;

    for (var i = banners.length - 1; i >= 0; i--) {
        $('#bannerlist').append( '<a href="#" id="bn-' + i + '">'+(i+1)+'</a>' );
        $('#bn-' + i).click(function(){
            var id = parseInt(this.id.replace(/bn-/, ''));
            setBanner(id);
            return false;
        });
    }

    var setBanner = function( i ) {
        $(banners).addClass( 'hidden' );
        $(banners[i]).removeClass( 'hidden' );
        current = i;
        if(i==5){
            timer = 9000;
        }else if(i==3 ){
            timer = 8000;
        }else if(i==4 ){
            timer = 9000;
        }else if(i==2 ){
            timer = 10000;
        }else if(i==1 ){
            timer = 9000;
        } else if(i==0 ){
            timer = 11500;
        }else{
            timer = timerStart;
        }
        

        $('#bannerlist a').removeClass( 'active' );
        $('#bn-' + i).addClass( 'active' );
    }

    timerFunc = function() {
        timer -= 1000;
        if (timer <= 0) {
            timer = timerStart;
            current++;
            if (current >= banners.length) {
                current = 0;
            }
            setBanner( current );
        }

        setTimeout( 'timerFunc()', 1000 );
    }

    setBanner( current );
    setTimeout( 'timerFunc()', 1000 );
}

jQuery.fn.placeholder = function(){
    $(this).each(function(index, element){
        $(element).data('initial', $(element).val());
    })
    $(this).focus(function(){
        var $this = $(this);
        if($this.val() == $this.data('initial')) {
            $this.val('').removeClass('placeholder');

        }
    });
    $(this).blur(function(){
        var $this = $(this);
        if($this.val() == '') {
            $this.val($this.data('initial')).addClass('placeholder');
        }
    });
}

jQuery.fn.colorTo = function(options){
    $(this).each(function(){
        //$.log($(this).css('backgroundColor'));
        $(this).data('normalColor', $(this).css('backgroundColor'));
    });

    $(this).hover(function(){
        $(this).stop().animate({
            backgroundColor: '#dfdfa3'
        }, 500);
    },
    function(){
        $(this).stop().animate({
            backgroundColor: $(this).data('normalColor')
            }, 500);
    });
}

jQuery.fn.toggleBox = function(options){
    $this = $(this);

    var defaults = {
        caption: 'h3',
        autohide: false
    }
    var options = $.extend(defaults, options);

    $caption = $this.find(options.caption);

    $this.addClass('rolling-box');
    $this.each(function(index){
        $(this).find('p, ul, table').wrapAll('<div class="box-content" />');
    });
    $caption.wrapInner('<span />');

    if(options.autohide){
        $this.addClass('box-roll-up');
        $this.find('.box-content').hide();
    //$this.find('p').hide();
    //$this.find('ul').hide();
    }else{
        $this.addClass('box-roll-down');
    }

    rolled = options.autohide;

    $caption.click(function(){
        $(this).parent().toggleClass('box-roll-up', !rolled);
        $(this).parent().toggleClass('box-roll-down', rolled);
        /*$(this).parent().find('p, ul').slideToggle('slow', function(){
            rolled = !rolled;
        });*/
        $(this).parent().find('.box-content').slideToggle('slow', function(){
            rolled = !rolled;
        });
    //$(this).parent().find('ul').slideToggle('slow');
    }).css('cursor','pointer');
}


jQuery.changeFontSize = function(collection, options){
    $.each(collection, function(index, obj){
        obj.changeFontSize(options);
    });
}
jQuery.fn.changeFontSize = function(options){
    $this = $(this);

    var defaults = {
        max: 15,
        min: 7,
        mode: 'incrase',
        value: false
    }
    var options = $.extend(defaults, options);

    if(!options.value){
        currValue = parseFloat($this.css('font-size'));
        if(options.mode=='incrase' && (currValue+1 <= options.max))
            $this.css('font-size', currValue+1);
        else if(options.mode=='decrace' && (currValue-1 >= options.min))
            $this.css('font-size', currValue-1);

    }else{
        $this.css('font-size', options.value);
    }
    return this;
}

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

jQuery.fn.tabs = function (options){
    $this = $(this);

    defaults = {
        tabsList: '.tabs'
    };
    var options = $.extend(defaults, options);

    $tabList = $(options.tabsList);
    $tab = $(this);

    $this.find('.tab-content').not('.tab-content:first').hide();
    $tabList.find('li:first').addClass('active');

    $tabList.find('li').click(function() {
        $tabList.find('li').removeClass('active');
        $(this).addClass('active');
        $tab.find('.tab-content').hide();
        var activeTab = $(this).find("a").attr("href");
        if($.browser.msie == true && $.browser.version == "6.0"){
            $(activeTab).show();
        }else{
            $(activeTab).fadeIn();
        }
        
        return false;
    });


}

if(typeof console === "undefined") console={
    log:function(){}
};
jQuery.log = function(msg){
    console.log("%s", msg);
}
jQuery.fn.log = function(msg){
    console.log("%s: %o", msg, this);
    return this;
}
