// Tomasz Konieczny 2011
// All rights reserved :)
// Saturday's night banner
// v. 1.20
//
// Istrukcja wdrażania skryptu:
// W pliku html należy dowolnie wypełnić pole bannera zawierając jego treść pomiędzy divami o sturkturze:
// <div id='slider'>
//      <div id='go'>
//          <div class="slide">
//              Treść pojedynczego slajdu
//          </div>
//      </div>
// </div>
// 
// Treść każdego ze slajdów umieszczamy w divach o klasie 'slide'.
// W divie o ID 'go' ostatnim divem powienien być pusty div o klasie 'change_buttons'.
// Poprawne utworzenie struktury HTML zapewnia popawne wygenrowanie całego slajdera.
// Dodatkowo możemy skonfigurować właściwości animacji bannera modyfikując wartości zmiennych delay, tempo i defletion.
// Opis możliwości konfiguracji zmiennych w skrypcie opisana jest w komentarzach przy samych zmiennych.


jQuery(function(){
    var delay = 5000;       // czas zmiany slajdów (w milisekundach)
    var tempo = 500;        // tempo animacji slajdu (w milisekundach)
    var animation_type = "slide"; // do wyboru sposób animacji: "slide", "fade" lub "slideUp"
    var deflection = 0;   // odległość/wielkość zamachnięcie przy zmianie slajdu (w pikselach i przy opcji "slide")
    

    var width = $('#banner #slider').width();
    var deflection_tempo;
    if(deflection==0){
        deflection_tempo = 0;
    }
    else {
        deflection_tempo = tempo/4;
    }

    var clicked_id = 1;
    var zindex  = 1;
    var changeId = 0;
    var animation = width+deflection;
    var items_amount = $('#banner #go .slide').size();

    $('#banner #change_buttons').append("<a class='prev' title='"+items_amount+"'></a>");
    $('#banner #change_buttons').append("<a class='next' title='2'></a>");

    animation = animation.toString()+'px';
    deflection = deflection.toString()+'px';
    $('#banner .button1').css({
        'opacity':'1'
    });

   var element_position = 0;
   var actual = 1;
   $('#banner #change_buttons .prev').click(function(){
       element_position = $("#banner .slide").width()*$(this).attr('title')-$("#banner .slide").width();
       clearInterval(changeId);
       actual = $(this).attr('title');
       if(actual=="1") {
           $(this).attr('title',items_amount);
       }
       else {
           $(this).attr('title', parseInt($(this).attr('title'))-1);
       }
       if(actual==items_amount){
           $('#banner #change_buttons .next').attr('title', '1');
       }
       else {
           $('#banner #change_buttons .next').attr('title', parseInt(actual)+1);
       }
       $("#banner #go").stop(true,true);
       $("#banner #go").animate({
                    left: "-"+element_position
                }, tempo, runIt(actual));
   });

   $('#banner #change_buttons .next').click(function(){
       element_position = $("#banner .slide").width()*$(this).attr('title')-$("#banner .slide").width();
       clearInterval(changeId);
       actual = $(this).attr('title');
       if(actual==items_amount) {
           $(this).attr('title',"1");
       }
       else {
           $(this).attr('title', parseInt($(this).attr('title'))+1);
       }
       if(actual=="1"){
           $('#banner #change_buttons .prev').attr('title', items_amount);
       }
       else {
           $('#banner #change_buttons .prev').attr('title', parseInt(actual)-1);
       }
       $("#banner #go").stop(true,true);
       $("#banner #go").animate({
                    left: "-"+element_position
                }, tempo, runIt(actual));
   });

    function runIt(item) {
        changeId = setInterval(function(){
            if(item==items_amount){
                item=1;
                $('#banner #change_buttons .prev').attr('title', items_amount);
                $('#banner #change_buttons .next').attr('title', item+1);
                $("#banner #go").animate({
                    left:'-='+deflection
                }, deflection_tempo, activeButton(item));
                $("#banner #go").animate({
                    left:'0px'
                }, tempo);
            }
            else {
                item++;
                if(item==items_amount) {
                    $('#banner #change_buttons .prev').attr('title', item-1);
                    $('#banner #change_buttons .next').attr('title','1');
                }
                else {
                    $('#banner #change_buttons .prev').attr('title', item-1);
                    $('#banner #change_buttons .next').attr('title', item+1);
                }
                $("#banner #go").animate({
                    left:'+='+deflection
                }, deflection_tempo, activeButton(item));
                $("#banner #go").animate({
                    left:'-='+animation
                }, tempo);
            }
        }, delay+tempo+deflection_tempo);
    }

    function toggleIt(item) {
        changeId = setInterval(function(){
            zindex = (items_amount+1) - item;
            $('#banner #go .slide:nth-child('+item+')').css({
                'z-index':zindex
            });
            if(item!=items_amount){
                $('#banner #go .slide:nth-child('+item+')').fadeOut(tempo);
                item++;
                activeButton(item);
                if(item==2){
                    $('#banner #go .slide:last-child').css({
                        "display":"block",
                        "z-index":'1'
                    }).show();
                }
                is_clicked=0;
            }
            else {
                item=1;
                activeButton(item);
                $('#banner #go .slide:last-child').css({
                    "display":"block",
                    "z-index":items_amount+1
                }).fadeOut(tempo);
                for(j=1; j < items_amount; j++){
                    zindex = items_amount-j+1;
                    $('#banner #go .slide:nth-child('+j+')').css({
                        "display":"block",
                        "z-index":zindex
                    });
                    $('#banner #go .slide:nth-child('+j+')').show();
                }
                activeButton(item);

            }
        }, delay+tempo+deflection_tempo);
    }

    function slideUpIt(item) {
        changeId = setInterval(function(){
            zindex = (items_amount+1) - item;
            $('#banner #go .slide:nth-child('+item+')').css({
                'z-index':zindex
            });
            if(item!=items_amount){
                $('#banner #go .slide:nth-child('+item+')').slideUp(tempo);
                item++;
                activeButton(item);
                if(item==2){
                    $('#banner #go .slide:last-child').css({
                        "display":"block",
                        "z-index":'1'
                    }).show();
                }
                is_clicked=0;
            }
            else {
                item=1;
                activeButton(item);
                $('#banner #go .slide:last-child').css({
                    "display":"block",
                    "z-index":items_amount+1
                }).slideToggle(tempo);
                for(j=1; j < items_amount; j++){
                    zindex = items_amount-j+1;
                    $('#banner #go .slide:nth-child('+j+')').css({
                        "display":"block",
                        "z-index":zindex
                    });
                    $('#go .slide:nth-child('+j+')').show();
                }
                activeButton(item);

            }
        }, delay+tempo+deflection_tempo);
    }

    function activeButton(id){
        for(i=1; i<=items_amount; i++){
            if(i==id){
                $('#banner .button'+i).css({
                    'opacity':'1'
                });
            }
            else {
                $('#banner .button'+i).css({
                    'opacity':'0.6'
                });
            }
        }
    }

    var banner_width;
    var index = 1;
    if(animation_type=="fade") {
        $('#banner #go .slide').css({
            'position':'absolute',
            'left':'0',
            'top':'0'
        });
        for(j=0; j < items_amount; j++){
            zindex = items_amount-j;
            index = j+1;
            $('#banner #go .slide:nth-child('+index+')').css({
                "display":"block",
                "z-index":zindex
            });
        }
        $('#banner #change_buttons').css({
            "z-index":"99"
        });
        toggleIt(1,0);
    }
    else if(animation_type=="slide"){
        banner_width = width*items_amount;
        banner_width = banner_width.toString();
        $('#banner #go').css({
            width: banner_width
        });
        runIt(1);
    }
    else if(animation_type=="slideUp"){
        $('#banner #go .slide').css({
            'position':'absolute',
            'left':'0',
            'top':'0'
        });
        for(j=0; j < items_amount; j++){
            zindex = items_amount-j;
            index = j+1;
            $('#banner #go .slide:nth-child('+index+')').css({
                "display":"block",
                "z-index":zindex
            });
        }
        $('#banner #change_buttons').css({
            "z-index":"99"
        });
        slideUpIt(1);
    }
})
