jQuery.noConflict();

jQuery(document).ready(function($){
    var root = $('a#url').text();

    $('div.blackshadow').dropShadow({
        left: 1,
        top: 1,
        opacity: 1,
        blur: 0,
        color: '#000000'
    });
    $('div.darkshadow').dropShadow({
        left: 1,
        top: 1,
        opacity: 1,
        blur: 0,
        color: '#409595'
    });
    $('div.lightshadow').dropShadow({
        left: 1,
        top: 1,
        opacity: 1,
        blur: 0,
        color: '#F9F6F0'
    });

    // jQueryUI datepicker
    $('.datepicker').datepicker({
        dateFormat: 'yy-mm-dd',
        monthNames: ['Styczeń','Luty','Marzec','Kwiecień','Maj','Czerwiec','Lipiec','Sierpień','Wrzesień','Październik','Listopad','Grudzień'],
        dayNamesMin: ['Nd', 'Pn', 'Wt', 'Śr', 'Cz', 'Pt', 'So'],
        changeYear: true,
        firstDay: 1,
        showAnim: 'slideDown',
        duration: 150
    });

    $('input[name="pesel_u"]').live('keyup', function(){
        if(!isNaN($(this).val()) && ($(this).val().length)>5) {
            var rok = $(this).val().substr(0,2);
            var mies = parseInt($(this).val().substr(2,2), 10);
            var dzien = $(this).val().substr(4,2);
            if(mies >= 1 && mies <=12) {rok = '19'+rok;}
            if(mies >= 21 && mies <=32) {rok = '20'+rok;mies-=20;}
            if(mies >= 41 && mies <=52) {rok = '21'+rok;mies-=40;}
            if(mies >= 61 && mies <=72) {rok = '22'+rok;mies-=60;}
            if(mies >= 81 && mies <=92) {rok = '18'+rok;mies-=80;}
            if(mies < 10) mies = '0'+mies;
            $('input[name="data_u"]').val(rok+'-'+mies+'-'+dzien);
        }
    });

    $('input[name="adres_jw"]').change(function(){
        if($(this).is(':checked')) {
            $('table.adres_koresp').hide();
        } else {
            $('table.adres_koresp').show();
        }
    });


    $('select[name="id_turnus"]').change(function() {
        recount_all();
    });
    $('input.camp_addon').change(function(){
        recount_all();
    });
    $('select[name="doplata_m"]').change(function() {
        recount_all();
    });
    $('select[name="doplata_m2"]').change(function() {
        recount_all();
    });

    recount_all();

    function recount_all() {
        var reg = /\[([0-9\.]*)\szł\]$/;
        var cena = $('select[name="id_turnus"] option:selected').html();
        reg.exec(cena);
        cena = RegExp.$1;
        if(isNaN(cena)) cena = 0.00;
        var tmp = $('select[name="doplata_m"] option:selected').html();
        reg.exec(tmp);
        tmp = RegExp.$1;
        if(isNaN(tmp)) tmp = 0.00;
        tmp = parseFloat(tmp);
        cena = parseFloat(parseFloat(cena)+parseFloat(tmp));
        var tmp = $('select[name="doplata_m2"] option:selected').html();
        reg.exec(tmp);
        tmp = RegExp.$1;
        if(isNaN(tmp)) tmp = 0.00;
        tmp = parseFloat(tmp);
        cena = parseFloat(parseFloat(cena) + parseFloat(tmp));
        $('input.camp_addon').each(function(){
            if($(this).is(':checked')) {
                var replace = '';
                var tmp = $(this).siblings('span').html();
                reg.exec(tmp);
                tmp = RegExp.$1;
                if(isNaN(tmp)) tmp = 0.00;
                cena = parseFloat(parseFloat(tmp) + parseFloat(cena));
            }
        });
        $('b#camp-reserve-price').html(cena.toFixed(2));
        if($('select[name="faktura"]').val()==0) {
            $('tr#tr-faktura-dane').hide();
        }
        if($('input[name="adres_jw"]').is(':checked')) {
            $('table.adres_koresp').hide();
        } else {
            $('table.adres_koresp').show();
        }
    }

    $('select[name="faktura"]').change(function(){
        if($(this).val()==0) {
            $('tr#tr-faktura-dane').hide();
        } else {
            $('tr#tr-faktura-dane').show();
        }
    });

    function flip_divs(current, flip_to, element, class_name) {
        $(element + flip_to).fadeIn(500).addClass(class_name);
        $(element + current).hide().removeClass(class_name);
        return false;
    }

    function animate_gallery() {
        var current = $('div.oferta-details-gallery-img-selected').attr('id').replace('oferta-details-gallery-img-','');
        var last = $('div.oferta-details-gallery-img').last().attr('id').replace('oferta-details-gallery-img-','');
        if(current == last) {
            var number = $('div.oferta-details-gallery-img').first().attr('id').replace('oferta-details-gallery-img-','');
        } else {
            var number = parseInt(parseInt(current)+1);
        }
        if (current != number) {
            flip_divs(current, number, 'div#oferta-details-gallery-img-', 'oferta-details-gallery-img-selected');
            $.doTimeout( 4000, function(){
                animate_gallery();
            });
        }
    }

    $.doTimeout( 4000, function() {
        animate_gallery();
    });
$('div.jak-rezerwowac-vt ul li a').hover(function(){
        $(this).siblings('div.more-info').show();
    }, function(){
        $(this).siblings('div.more-info').hide();
    });

    //facebook
    $("#wysuwany_div").mouseover(function() {
        $("#wysuwany_div").stop();
        $("#wysuwany_div").animate({
            right: "0"
        },256
        );
    }).mouseout(
        function() {
            $("#wysuwany_div").stop();
            $("#wysuwany_div").animate({
                right: "-256"
            }, 256
            );
    });

    // zakładki w szczegółach oferty
    $('div.camp-tabs a').click(function(event){
        event.preventDefault();
        var tab = $(this).attr('id').replace('camp-tab-', '');
        $('div.camp-tabs a').removeClass('selected');
        $(this).addClass('selected');
        $('div.camp-tab-description').hide();
        $('div#camp-tab-description-' + tab).show();
    });

});
