
//globals
var scrollable_api;

jQuery(function($){

    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');
        }
    });

    
    //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');

        //zmiana bordera aktywnego pola
       $('.form').find('input, textarea').click(function(){
            $('.form').find('input, textarea').removeClass('active');
            $(this).addClass('active');
        });
    }

    //GALERIA
    $(".scrollable").scrollable();

    //colorbox
    $("a[rel='gallery']").colorbox({
        photo: true
    });

    $(".items img").click(function() {

            // see if same thumb is being clicked
            if ($(this).hasClass("active")) { return; }

            // calculate large image's URL based on the thumbnail URL (flickr specific)
            var url = $(this).attr("src").replace("85x75", "405x304");
            var lightbox_href = $(this).attr("src").replace("/85x75", "/1000x1000/crop/0");
            var lightbox_title = $(this).attr("alt");

            // get handle to element that wraps the image and make it semi-transparent
            var wrap = $("#image_wrap").fadeTo("fast", 0.3);

            // the large image from www.flickr.com
            var img = new Image();

            // call this function after it's loaded
            img.onload = function() {

                    // make wrapper fully visible
                    wrap.fadeTo("slow", 1);

                    // change the image
                    wrap.find("img").attr("src", url);

            };

            // begin loading the image from www.flickr.com
            img.src = url;
            // switch href in anchor for lightbox link and title
            wrap.find("a").attr("href", lightbox_href).attr("title", lightbox_title);

            // activate item
            $(".items img").removeClass("active");
            $(this).addClass("active");

            //usuwamy rel="gallery" z #lightbox_wrap dla zdjecia ktore jest aktualnie wyswietlone
            $("#lightbox_wrap a").attr("rel", "gallery");
            $("#lightbox_wrap a[href='"+lightbox_href+"']").attr("rel", "");

            //change photo desc
            var photo_desc = $(this).attr('alt');
            if( photo_desc.length > 0 ){
                $("#photo_desc p").html( photo_desc );
                $("#photo_desc").show();
            }
            else {
                $("#photo_desc").hide();
            }

    // when page loads simulate a "click" on the first image
    }).filter(":first").click();

    //launch colorbox
    $('a.zoom').click(function(){
        $("#image_wrap a").trigger('click');
    });

});

