﻿// Felhasználói Beállítások
var configRotaltElemekSzama = 6;
var cfgElsoElemIdoszelet = 10000;
var cfgMasoidkElemIdoszelet = 10000;
var cfgHarmadikElemIdoszelet = 10000;
var cfgNegyedikElemIdoszelet = 10000;
var cfgOtodikElemIdoszelet = 10000;
var cfgHatodikElemIdoszelet = 10000;





jQuery.timer2 = function(time, func, callback) {
    var a = { timer2: setTimeout(func, time), callback: null }
    if (typeof (callback) == 'function') { a.callback = callback; }
    return a;
};

jQuery.clearTimer = function(a) {
    clearTimeout(a.timer2);
    if (typeof (a.callback) == 'function') { a.callback(); };
    return this;
};




jQuery.fn.fadeSliderToggle = function(settings) {
    /* Damn you jQuery opacity:'toggle' that dosen't work!~!!!*/
    settings = jQuery.extend({
        speed: 500,
        easing: "swing"
    }, settings)

    caller = this
    if ($(caller).css("display") == "none") {
        $(caller).animate({
            opacity: 1,
            height: 'toggle'
        }, settings.speed, settings.easing);
    } else {
        $(caller).animate({
            opacity: 0,
            height: 'toggle'
        }, settings.speed, settings.easing);
    }
};






// Code
var state = 0;
var myTimerA = {};
var myTimerFirstRefresh = {};

function startBannerRotate() {
    if (configRotaltElemekSzama == 1) {
        $('#bRotatorA').show("slow");
        $('#controlerLine').hide("slow");
    }
    if (configRotaltElemekSzama == 2) {
        $('#ctrA').show("slow");
        $('#ctrB').show("slow");
        myTimerFirstRefresh = $.timer2(500, function() {
        refresh();
        });
    }
    if (configRotaltElemekSzama == 3) {
        $('#controlerLine').show("slow");
        $('#ctrA').show("slow");
        $('#ctrB').show("slow");
        $('#ctrC').show("slow");
        myTimerFirstRefresh = $.timer2(500, function() {
        refresh();
        });
    }
    if (configRotaltElemekSzama == 4) {
        $('#controlerLine').show("slow");
        $('#ctrA').show("slow");
        $('#ctrB').show("slow");
        $('#ctrC').show("slow");
        $('#ctrD').show("slow");
        myTimerFirstRefresh = $.timer2(500, function() {
        refresh();
        });
    }
    if (configRotaltElemekSzama == 5) {
        $('#controlerLine').show("slow");
        $('#ctrA').show("slow");
        $('#ctrB').show("slow");
        $('#ctrC').show("slow");
        $('#ctrD').show("slow");
        $('#ctrE').show("slow");
        myTimerFirstRefresh = $.timer2(500, function() {
        refresh();
        });
    }
    if (configRotaltElemekSzama == 6) {
        $('#controlerLine').show("slow");
        $('#ctrA').show("slow");
        $('#ctrB').show("slow");
        $('#ctrC').show("slow");
        $('#ctrD').show("slow");
        $('#ctrE').show("slow");
        $('#ctrF').show("slow");
        myTimerFirstRefresh = $.timer2(500, function() {
        refresh();
        });
    }
}
function refresh() {
    if (state == 0) {
        $('#ctrA').addClass("controlBTN_A");
        $('#bRotatorA').fadeSliderToggle({ speed: 300, easing: "swing" });

        $.clearTimer(myTimerA);
        myTimerA = $.timer2(cfgElsoElemIdoszelet, function() {

            // alert('show A');
            $('#ctrA').attr("class", "controlBTN");
            $('#bRotatorA').hide("slow");
            if (configRotaltElemekSzama > 1) { state = 1; } else { state = 0; }
            refresh();
            // Optional function to call when timer is canceled
        });
    }
    if (state == 1) {
        $('#ctrB').addClass("controlBTN_A");
        $('#bRotatorB').fadeSliderToggle({ speed: 300, easing: "swing" });

        $.clearTimer(myTimerA);
        myTimerA = $.timer2(cfgMasoidkElemIdoszelet, function() {
            //alert('show B');

            $('#ctrB').attr("class", "controlBTN");
            $('#bRotatorB').hide("slow");
            if (configRotaltElemekSzama > 2) { state = 2; } else { state = 0; }
            refresh();
            // Optional function to call when timer is canceled
        });
    }
    if (state == 2) {
        $('#ctrC').addClass("controlBTN_A");
        $('#bRotatorC').fadeSliderToggle({ speed: 300, easing: "swing" });

        $.clearTimer(myTimerA);
        myTimerA = $.timer2(cfgHarmadikElemIdoszelet, function() {

            // alert('show C');
            $('#ctrC').attr("class", "controlBTN");
            $('#bRotatorC').hide("slow");
            if (configRotaltElemekSzama > 3) { state = 3; } else { state = 0; }
            refresh();
        });
    }
    if (state == 3) {
        $('#ctrD').addClass("controlBTN_A");
        $('#bRotatorD').fadeSliderToggle({ speed: 300, easing: "swing" });

        $.clearTimer(myTimerA);
        myTimerA = $.timer2(cfgNegyedikElemIdoszelet, function() {

            // alert('show D');
            $('#ctrD').attr("class", "controlBTN");
            $('#bRotatorD').hide("slow");
            if (configRotaltElemekSzama > 4) { state = 4; } else { state = 0; }
            refresh();
        });
    }
    if (state == 4) {
        $('#ctrE').addClass("controlBTN_A");
        $('#bRotatorE').fadeSliderToggle({ speed: 300, easing: "swing" });

        $.clearTimer(myTimerA);
        myTimerA = $.timer2(cfgOtodikElemIdoszelet, function() {

            // alert('show E');
            $('#ctrE').attr("class", "controlBTN");
            $('#bRotatorE').hide("slow");
            if (configRotaltElemekSzama > 5) { state = 5; } else { state = 0; }
            refresh();
        });
    }
    if (state == 5) {
        $('#ctrF').addClass("controlBTN_A");
        $('#bRotatorF').fadeSliderToggle({ speed: 300, easing: "swing" });

        $.clearTimer(myTimerA);
        myTimerA = $.timer2(cfgHatodikElemIdoszelet, function() {

            // alert('show F');
            $('#ctrF').attr("class", "controlBTN");
            $('#bRotatorF').hide("slow");
            state = 0;
            refresh();
        });
    }
}

function clearFn() {
    if (state == 5) {
        $('#bRotatorF').hide("slow");
        $('#ctrF').attr("class", "controlBTN");
    }
    if (state == 4) {
        $('#bRotatorE').hide("slow");
        $('#ctrE').attr("class", "controlBTN");
    }
    if (state == 3) {
        $('#bRotatorD').hide("slow");
        $('#ctrD').attr("class", "controlBTN");
    }
    if (state == 2) {
        $('#bRotatorC').hide("slow");
        $('#ctrC').attr("class", "controlBTN");
    }
    if (state == 1) {
        $('#bRotatorB').hide("slow");
        $('#ctrB').attr("class", "controlBTN");
    }
    if (state == 0) {
        $('#bRotatorA').hide("slow");
        $('#ctrA').attr("class", "controlBTN");
    }
}
$(document).ready(function() {
    $("#btn_1").click(function() {
        if (state != 0) {
            clearFn();
            state = 0;
            refresh();
        }
    });
    $("#btn_2").click(function() {
        if (state != 1) {
            clearFn();
            state = 1;
            refresh();
        }
    });
    $("#btn_3").click(function() {
        if (state != 2) {
            clearFn();
            state = 2;
            refresh();
        }
    });

    $("#btn_4").click(function() {
        if (state != 3) {
            clearFn();
            state = 3;
            refresh();
        }
    });
    $("#btn_5").click(function() {
        if (state != 4) {
            clearFn();
            state = 4;
            refresh();
        }
    });
    $("#btn_6").click(function() {
        if (state != 5) {
            clearFn();
            state = 5;
            refresh();
        }
    });
});
