jQuery.noConflict();



jQuery(document).ready(function($){

    var root = $('a#url').text();



    // info o dodaniu produktu do koszyka

    $('div.ui-widget-overlay').bgiframe();

    $('div.ui-widget-overlay').height( jQuery(document).height() );

    $('div#kosz_msg').css('top', 200);

    $('div#kosz_msg').css('left', parseInt(jQuery(document).width() / 2 - 150) );

    if( $('div#kosz_msg').is('div') ) {

        $('div.ui-widget-overlay').show();

        $('div#kosz_msg').show().delay(2000).hide(1);

        $('div.ui-widget-overlay').delay(2000).hide(1);

    }



    // custom selectbox

    $('select.custom').selectbox();



    // delete dialog

    var watch = 0;

    $("input[name='delete_dialog']").click(function() {

        $('#dialog').dialog('open');

        watch = $(this).parent();

        return false;

    });

    $('#dialog').dialog({

        bgiframe: true,

        resizable: false,

        modal: true,

        autoOpen: false,

        overlay: {

            backgroundColor: '#000',

            opacity: 0.5

        },

        buttons: {

            'Usuń': function() {

                $(this).dialog('close');

                watch.submit();

                watch=0;

            },

            'Anuluj': function() {

                $(this).dialog('close');

                watch=0;

            }

        }

    });



    // koszyk - wybór adresu

    $('tr.adresy_list').hide();

    $('input[name="ch_adres_d"]').click(function(){

        $('tr#adresy_list_d').toggle();

    });

    $('input[name="ch_adres_f"]').click(function(){

        $('tr#adresy_list_f').toggle();

    });

    // koszyk - dane klienta niezalogowanego

    $('table.kosz_dane_d input').change(function(){

        $.post(root+'koszyk/kosz_adres_d', $(this).serializeArray() );

    });

    $('table.kosz_dane_f input').change(function(){

        $.post(root+'koszyk/kosz_adres_f', $(this).serializeArray() );

    });

    // koszyk - uwagi

    $('textarea[name="uwagi"]').change(function(){

        $.post(root+'koszyk/kosz_ajax', {
            'type': 1,
            'uwagi': $(this).val()
            } );

    })



    // menu boczne - kategorie

    // poziom 1

    $('div.menu_lvl1 div.left_item a.left_menu_link').click(function(){

        if($(this).parent().parent().children('div.menu_lvl2').length > 0) {

            $(this).parent().parent().children('div.menu_lvl2').toggle();

            return false;

        }

    });

    // poziom 2

    /*$('div.menu_lvl2 div.left_light_item a.left_menu_link_lvl2').click(function(){

        if($(this).parent().parent().children('div.menu_lvl3').length > 0) {

            $(this).parent().parent().children('div.menu_lvl3').toggle();

            return false;

        }

    });

    // poziom 3

    $('div.menu_lvl3 div.left_light_item a.left_menu_link_lvl3').click(function(){

        if($(this).parent().parent().children('div.menu_lvl4').length > 0) {

            $(this).parent().parent().children('div.menu_lvl4').toggle();

            return false;

        }

    });*/



    // wyszukiwarka zaawansowana

    $('input#model_d').autocomplete({

        source: root+"modele.html",

        minLength: 3,

        delay: 750

    });

    $('input#symbol_k').autocomplete({

        source: root+"symbole.html",

        minLength: 3,

        delay: 750

    });



    // powiększenie zdjęcia produktu

    $('div.fotomid').bgiframe();

    $('img.fotomin').hover(function(){

        $(this).parent('a').parent('div.foto').children('div.fotomid').show();

    }, function(){

        $(this).parent('a').parent('div.foto').children('div.fotomid').hide();

    });



    flipSlides = function(current, flip_to) {

        $('div#baner_' + flip_to).show();

        $('div#baner_' + current).hide();

        $('a#baner_anchor_' + flip_to).addClass('baner_anchor_selected');

        $('a#baner_anchor_' + current).removeClass('baner_anchor_selected');

        return false;

    }

    animateSlides = function() {

        if($('.baner_anchor_pause').attr('id') == 'start' || $('.baner_anchor_pause').attr('id') == '') {
            var current = $('div.baner:visible').attr('id').replace('baner_','');

            var last = $('div.baner').last().attr('id').replace('baner_','');

            if(current == last) {

                var number = $('div.baner').first().attr('id').replace('baner_','');

            } else {

                var number = parseInt(parseInt(current)+1);

            }

            flipSlides(current, number);
	    
            $.doTimeout( 8000, function(){
                animateSlides();
            });
       
        
        }
    }
        
        

    



    if ($('div.baner').length > 1) {

        var height = 0;

        $('div.baner').each(function(){

            if ($(this).height() > height) {

                height = $(this).height();

            }

        });

        $('div.baner').each(function(){

            $(this).height(height);

        });

        $.doTimeout( 8000, function() {

            animateSlides();

        });

    }





    $('a.baner_anchor').click(function(event){

        event.preventDefault();

        var current = $('div.baner:visible').attr('id').replace('baner_','');

        var number = $(this).attr('id').replace('baner_anchor_', '');

        if(current != number) {
          flipSlides(current, number);
        }
        

    });

    $('a.baner_anchor_pause').click(function(event){
        event.preventDefault();
        if($(this).attr('id') == '' || $(this).attr('id') == 'start') {
            $(this).attr('id','stop');
            $(this).html('||');
            animateSlides();
        }else if($(this).attr('id') == 'stop') {
            $(this).attr('id','start');
            $(this).html('>');
            animateSlides();
        }
    });


    //facebook

    $("#wysuwany_div").mouseover(function() {
        $(this).css('z-index', 11);
        $("#wysuwany_div").stop();

        $("#wysuwany_div").animate({

            left: "0"

        },256

        );

    }).mouseout(

        function() {
            $(this).css('z-index', 10);
            $("#wysuwany_div").stop();

            $("#wysuwany_div").animate({

                left: "-256"

            }, 256

            );

        });
    $("#wysuwany_div2").mouseover(function() {

        $("#wysuwany_div2").stop();

        $("#wysuwany_div2").animate({

            left: "0"

        },256

        );

    }).mouseout(

        function() {

            $("#wysuwany_div2").stop();

            $("#wysuwany_div2").animate({

                left: "-223"

            }, 256

            );

        });



});
