// 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 = "fade"; // do wyboru sposób animacji: "slide", "fade" lub "slideUp"
    var deflection = 100;   // odległość/wielkość zamachnięcie przy zmianie slajdu (w pikselach i przy opcji "slide")
    

    var width = $('#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 = $('#go > .slide').size();

    //$('#change_buttons_l').append("<a style='cursor:pointer;background:none;' class='change navis button3' title='3'><img src='templates/wtc/images/banner_left.png' /></a>");
    for(i=1; i<=items_amount; i++) {
        $('#change_buttons').append("<a style='cursor:pointer' class='change button"+i+"' title='"+i+"'>"+i+"</a>");
    }
    //$('#change_buttons_r').append("<a style='cursor:pointer;background:none;' class='change navis button2' title='2'><img src='templates/wtc/images/banner_right.png' /></a>");

    animation = animation.toString()+'px';
    deflection = deflection.toString()+'px';
    $('.button1').css({
        'background-image':'url(templates/wtc/images/banner_123_a.jpg)'
    });

    $('.change').click(function(){
        if(animation_type=="slide"){
            var position = ($(this).attr("title")*width)-width;
            position = position.toString()+'px';
            clearInterval(changeId);
            activeButton($(this).attr("title"));
            $("#go").queue("fx",[]);
            $("#go").stop(true,true).animate({
                left: '-'+position
            },tempo, runIt($(this).attr("title")));
        }
        else if(animation_type=="fade"){
            clearInterval(changeId);
            activeButton($(this).attr("title"));
            clicked_id = $(this).attr("title");
            for(j=1; j < clicked_id; j++){
                $('#go .slide:nth-child('+j+')').css({
                    "display":"block",
                    "z-index":'1'
                });
            }
            for(j=clicked_id; j <= items_amount; j++){
                zindex = items_amount-j+1;
                $('#go .slide:nth-child('+j+')').css({
                    "display":"block",
                    "z-index":zindex
                });
                $('#go .slide:nth-child('+j+')').fadeIn();
            }        
            toggleIt(clicked_id);      
        }
        else if(animation_type=="slideUp"){
            clearInterval(changeId);
            activeButton($(this).attr("title"));
            clicked_id = $(this).attr("title");
            for(j=1; j < clicked_id; j++){
                $('#go .slide:nth-child('+j+')').css({
                    "display":"block",
                    "z-index":'1'
                });
            }
            for(j=clicked_id; j <= items_amount; j++){
                zindex = items_amount-j+1;
                $('#go .slide:nth-child('+j+')').css({
                    "display":"block",
                    "z-index":zindex
                });
                $('#go .slide:nth-child('+j+')').fadeIn();
            }
            slideUpIt(clicked_id);
        }
    });

    function runIt(item) {
        changeId = setInterval(function(){
            if(item==items_amount){
                item=1;
                $("#go").animate({
                    left:'-='+deflection
                }, deflection_tempo, activeButton(item));
                $("#go").animate({
                    left:'0px'
                }, tempo);
            }
            else {
                item++;
                $("#go").animate({
                    left:'+='+deflection
                }, deflection_tempo, activeButton(item));
                $("#go").animate({
                    left:'-='+animation
                }, tempo);
            }
        }, delay+tempo+deflection_tempo);
    }

    function toggleIt(item) {
        changeId = setInterval(function(){
            zindex = (items_amount+1) - item;
            $('#go .slide:nth-child('+item+')').css({
                'z-index':zindex
            });
            if(item!=items_amount){
                $('#go .slide:nth-child('+item+')').fadeOut(tempo);
                item++;
                activeButton(item);
                if(item==2){
                    $('#go .slide:last-child').css({
                        "display":"block",
                        "z-index":'1'
                    }).show();
                }
                is_clicked=0;
            }
            else {
                item=1;
                activeButton(item);
                $('#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;
                    $('#go .slide:nth-child('+j+')').css({
                        "display":"block",
                        "z-index":zindex
                    });
                    $('#go .slide:nth-child('+j+')').show();
                }
                activeButton(item);

            }
        }, delay+tempo+deflection_tempo);
    }

    function slideUpIt(item) {
        changeId = setInterval(function(){
            zindex = (items_amount+1) - item;
            $('#go .slide:nth-child('+item+')').css({
                'z-index':zindex
            });
            if(item!=items_amount){
                $('#go .slide:nth-child('+item+')').slideUp(tempo);
                item++;
                activeButton(item);
                if(item==2){
                    $('#go .slide:last-child').css({
                        "display":"block",
                        "z-index":'1'
                    }).show();
                }
                is_clicked=0;
            }
            else {
                item=1;
                activeButton(item);
                $('#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;
                    $('#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){
                $('.button'+i).css({
                    'background-image':'url(templates/wtc/images/banner_123_a.jpg)'
                });
				$('.navisl').css({
                    'background-image':'url(templates/wtc/images/banner_left.png)'
                });
				$('.navisr').css({
                    'background-image':'url(templates/wtc/images/banner_right.png)'
                });
            }
            else {
                $('.button'+i).css({
                    'background-image':'url(templates/wtc/images/banner_123_c.jpg)'
                });
				$('.navisl').css({
                    'background-image':'url(templates/wtc/images/banner_left.png)'
                });
				$('.navisr').css({
                    'background-image':'url(templates/wtc/images/banner_right.png)'
                });
            }
        }
    }

    var banner_width;
    var index = 1;
    if(animation_type=="fade") {
        $('#go .slide').css({
            'position':'absolute',
            'left':'0',
            'top':'0'
        });
        for(j=0; j < items_amount; j++){
            zindex = items_amount-j;
            index = j+1;
            $('#go .slide:nth-child('+index+')').css({
                "display":"block",
                "z-index":zindex
            });
        }
        $('#change_buttons').css({
            "z-index":"99"
        });
        toggleIt(1,0);
    }
    else if(animation_type=="slide"){
        banner_width = width*items_amount;
        banner_width = banner_width.toString();
        $('#go').css({
            width: banner_width
        });
        runIt(1);
    }
    else if(animation_type=="slideUp"){
        $('#go .slide').css({
            'position':'absolute',
            'left':'0',
            'top':'0'
        });
        for(j=0; j < items_amount; j++){
            zindex = items_amount-j;
            index = j+1;
            $('#go .slide:nth-child('+index+')').css({
                "display":"block",
                "z-index":zindex
            });
        }
        $('#change_buttons').css({
            "z-index":"99"
        });
        slideUpIt(1);
    }
})
