/*
 *resize
 */
$( function() {

    if( $('#programForm').get().length > 0 ) {
        $('#programForm input[name=form[od]], #programForm input[name=form[do]]').datepicker({
            dateFormat: 'dd/mm/yy',
            firstDay : 1,
            monthNames: [
                'Styczeń','Luty', 'Marzec', 'Kwiecień', 'Maj', 'Czerwiec', 'Lipiec', 'Sierpień', 'Wrzesień', 'Październik', 'Listopad', 'Grudzień'
            ],
            dayNamesMin: [
                'N', 'Pon', 'Wt', 'Śr', 'Cz', 'Pt', 'Sob'
            ]
        });
    }
} );


/*lightbox */
$( function() {
    $('[rel=lightbox], [rel=lightbox[gallery]]').prettyPhoto( {
        theme: 'dark_square',
        showTitle: false
    } );
} );


/*news */
$(function() {
    $('.newsBox .more').click( function() {
        if($('#homeNews').get().length>0) {
            return true;
        }
        $(this).parent().parent().find('.full').slideDown();
        $(this).css('display', 'none');
        $('.newsBox .back').css('display', 'block');
        return false;
    } );
    $('.newsBox .img a').click(function() {
        $(this).parent().parent().find('.more').click();
        return false;
    });
    $('.newsBox .back').each(function() {
        if($(this).parent().parent().hasClass('fullNews')) {
            return;
        }
        $(this).click( function() {
            $(this).hide();
            $(this).parent().parent().find('.more').css('display', 'block');
            $(this).parent().slideUp();
            return false;
        }  ) }  );
});

/*
 * Contact Form
 */
$( function() {
    $('.txtinput, #searchForm .text').each( function() {
        $(this).attr('defaultText', ( $(this).hasClass('mess') ? $(this).text() : $(this).attr('value') )).focus( txtinputFocus ).blur( txtinputBlur );
    } );
} );
function txtinputFocus() {
    if($(this).hasClass('mess')) {
        if($(this).attr('defaultText') == $(this).text())
            $(this).text('');
    } else {
        if($(this).attr('defaultText') == $(this).attr('value'))
            $(this).attr('value','');

    }
}

function txtinputBlur() {
    if($(this).hasClass('mess')) {
        if($(this).attr('value') == '')
            $(this).text($(this).attr('defaultText'));
    } else {
        if($(this).attr('value') == '')
            $(this).attr('value', $(this).attr('defaultText') );

    }
}

$(  function() {
    $('#top .logo, #top .text1, #top .text2').css( 'opacity', 0 );
    setTimeout( function() {
        $( '#top .logo' ).animate({opacity: 1}, 1000);
        $( '#top .text1' ).animate({opacity: 1}, 1200, function() {
            $('#top .text2').animate( {opacity: 1}, 1200 );
        });
    }, 600 );
    

} );

$(function() {
    $('.polecamy .title').toggle( function() {
        var up = $(this).parent();
        up.find('.all').slideDown();
        up.stop().animate( { height: '193px' } ).addClass('polecamyActive');
        return false;
    }, function() {
        var up = $(this).parent();
        up.find('.all').slideUp();
        up.stop().animate( { height: '25px' }, 'normal', function() {
            $(this).removeClass('polecamyActive');
        } );
        return false;
    } );
});




//partners
var lastNum = 0;
var allPartners;
$( function() {
    $('#partners a').attr('target', 'bla').each( function(i) {
        $(this).attr('num', i).hide();
    } );
    allPartners = $('#partners a').get().length;
    showNextPartners();
} );

function showNextPartners() {
    if( allPartners > lastNum ) {
        $( '#partners a[num=' + lastNum + ']' ).fadeIn(1500, showNextPartners);
        lastNum++;
    } else {
        startPartners();
    }
}

function startPartners() {
    setInterval( partners, 5000 );
    $('#partners').wrapInner('<div></div>');
    
};
var newTop = 0;
function partners() {
    var box = $('#partners > div');
    if( box.height() > 61) {
        var top = parseInt( box.css('top') ) * (-1);
        if(  (top+61) >=box.height() ) {
           box.fadeOut( 'fast', function() {
                $(this).css('top', '0px').fadeIn();
            } );
        } else {
            newTop = (-1)*top - 61 + 'px';
            box.fadeOut( 'normal', function() {

                $(this).css({
                    top: newTop
                }).fadeIn();
            } );
 
        }
    }
}

//ajax home
var activePage = 1;
var activePageOver = false;
$( function() {
    $('#ajax .text').each( function(i) {
        $(this).attr('num', i);
    } );
    $( '#ajax .menu a' ).each( function(i) {
        $(this).attr('num', i);
        $(this).click( changePage );
    } );

    $('#ajax').hover( function() {
        activePageOver = true;
    }, function() {
        activePageOver = false;
    } );

    setInterval( function() {
        if(activePageOver)
            return;

        if(activePage == '5')
            activePage = '1';
        else
            activePage++;

        $( '#ajax .menu [num='+activePage+']' ).click();
    }, 20000 );
} );

function changePage() {
    if($(this).hasClass('activePage')) {
        return false;
    }
    var num = $(this).attr('num');
    $('#ajax .menu .active').removeClass('active');
    $('#ajax .activePage').css('display', 'block').removeClass('activePage').fadeOut();
    activePage = num;
    $(this).addClass('active');
    $('#ajax .text[num='+num+']').hide().addClass('activePage').fadeIn();
    return false;
}

var siteLoaded = false;
/*
 * drop menu
 */
$( function() {
    $( '#menu > ul li' ).hover( hoverMenu, overMenu );
    //$( '#menu > ul > li li' ).hover( hoverMenu, overMenu );
    //$( '#menu ul ul' ).hover( null, cleanMenu );
    $( '#menu ul ul' ).css('display', 'none');
    
} );
var toHover;
var toOver;
var el;
var el2;

function hoverMenu() {
    el = this;
    if(el == el2)
        clearTimeout(toOver);
    if( $(this).find('> a').hasClass('sel') ) {
        return;
    } else {
        clearTimeout( toHover );
        toHover = setTimeout( function() {
            $( el ).find('> a').addClass('sel');
            $( el ).find('> ul').slideDown('fast');
            $( el ).parent().find('> li').each(function() {
                if( el != this ) {
                    $( this ).find('a').removeClass('sel');
                    $( this ).find('ul').slideUp('fast');
                }
            });
            el = null;
        }, 300 );
    }
   
    
}

function overMenu() {
    el2 = this;
    if(el == el2)
        clearTimeout(toHover);
    if( $(this).find('> a').hasClass('sel') ) {
        clearTimeout( toOver );
        toOver = setTimeout( function() {
            $( el2 ).find('a').removeClass('sel');
            $( el2 ).find('ul').slideUp('fast');
            
            el2 = null;
        }, 500 );
    }
    
}

function hoverSubMenu() {

}
function overSubMenu() {
    
}
$( function() {
    if( $('#banner > img').get().length > 0 ) {
        $('#banner > img').hide();
        setTimeout( function() {
            $('#banner img ').fadeIn( 'slow' );
        }, 300 );
    }
} );

