
//globals
var scrollable_api;

jQuery(function($){

    //superfish category menu
    $('ul.sf-menu').superfish({
        autoArrows:  false,
        dropShadows: false,
        animation: {
            height: ['toggle']
        },
        speed: 'normal'
    });

    //wysuwanie shopping cart
    $('#shopping_cart').hover(
    //rozwijanie
    function(){
        $('#shopping_cart_expand').css('marginTop', '-2px').slideDown(200, function() {}).show();
    },
    //zwijanie
    function(){
        $('#shopping_cart_expand').hide();
    }
);

    //hover dla search
    $('#search_wrapper').mouseover(function(){
        $(this).find('p').addClass('hover');
        $(this).find('input[type="submit"]').addClass('submit_hover');
    });
    $('#search_wrapper').mouseout(function(){
        $(this).find('p').removeClass('hover');
        $(this).find('input[type="submit"]').removeClass('submit_hover');
    });
    
    //cursor dla search submit
    $('#search_wrapper input[type="submit"]').mouseover(function(){
        $(this).css('cursor', 'pointer');
    });

    if($('.slide').length){
        //scrollable, assigning to global!
        scrollable_api = $("#slider_root").scrollable({
            vertical: true,
            circular: true,
            easing: 'easeInOutExpo',
            speed: 1200,

            //przed przewinieciem
            onBeforeSeek: function(event, slideIndex) {
                //console.log("BeforeSeek: "+slideIndex);
                //ostatni slajd
                if(slideIndex == this.getSize()){
                    this.begin();
                    //zapobiega podwojnej animacji na koncu wywolanej przez autoscroll
                    //bez autoscroll nie jest potrzebne
                    return false;
                }
                //pierwszy slajd
                else if(slideIndex == -1){
                    this.end();
                    return false;
                }
            }
        }).autoscroll({
            interval: 4000,
            autopause: false,
            autoplay: true,
            api: true
        });

        //slider nav show pause/play
        $('#slider').hover(
        function(){
            if($.browser.msie){
                $(this).find('.autoslide').show();
            }
            else {
                $(this).find('.autoslide').fadeIn(300);
            }
        },
        function(){
            if($.browser.msie){
                $(this).find('.autoslide').hide();
            }
            else {
                $(this).find('.autoslide').fadeOut(600);
            }
        }
    );

        //klik na nast / poprzedni ukrycie paska
        //$('#slider_nav a.prev, #slider_nav a.next').click(function(){
        //});

        //obsluga play / pause
        $('#slider .autoslide a').click(function(){
            if($(this).hasClass('pause')){
                //console.log('klik pauza');
                $(this).hide();
                $('#slider .autoslide a.play').css('display', 'block');
                scrollable_api.pause();
            }
            else if($(this).hasClass('play')){
                //console.log('klik play');
                $(this).hide();
                $('#slider .autoslide a.pause').css('display', 'block');
                scrollable_api.play();
            }
        });
    }
    //hover dla produktow
    $('#products .box').live('mouseover mouseout', function(event) {
        if (event.type == 'mouseover') {
            $(this).addClass('highlight');
        } else {
            $(this).removeClass('highlight');
        }
    });

    //wysuwanie panelu logowania
    $('#login_btn').click(function(){
        $li = $(this).parent();
        //pokazanie
        if($('#loginpanel_wrapper').css('display') == 'none'){
            $('#loginpanel_wrapper').show();
            $li.addClass('opened');
            $('body').animate({
                paddingTop: $('#loginpanel_wrapper').outerHeight()+'px'
            }, 500, function() {
                //
            });
        }
        //ukrycie
        else{
            $('body').animate({
                paddingTop: '0'
            }, 500, function() {
                $('#loginpanel_wrapper').hide();
                $li.removeClass('opened');
            });
        }
    });

    //formualarz validacja css
    if($('.form').length){
        //zaznaczenie obramowania na czerwony gdy wystąpi błąd
        $(this).find('.error_list').parent().find('input, textarea').addClass('error');
    }

    //ajax dla produktow
    $('#products .pagination a, #products .sort_types a').live('click', function(){
        var content = $('#content-holder');
        content.empty().addClass('ajax_loader_big');
        $.ajax({
            type: 'GET',
            url: $(this).attr('href'),
            success: function(data){
                content.removeClass('ajax_loader_big').html(data);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                content.removeClass('ajax_loader_big');
                alert('Error: '+textStatus+'. '+errorThrown);
            }
        });

        return false;
    });

});

