var timer_is_on = 0;
var start_opacity_down = 1;
var second_opacity = 0;
var start_opacity_up_ie = 0;
var start_opacity_down_ie = 100;
var second_opacity_ie = 0;
var start_opacity_up = 0;
var length = 5;
var helper = 0;
var helper2 = 0;
var start1 = 1;
var start2 = 2;
var t;
var busy=false;
var trans = 0;
var n = 0;
var next;
var prev;
function initLightSlider(nr)
{
    length = nr;
    //alert(length);
}
function timedCount()
{
    if (helper == 50)
    {
        helper = 0;
        if (trans == 1)
        {
            trans = 0;
            start1 = start2;
            start2 = start1+1;
        }
        else
        {
            if (start1 != length)
            {
                start1++;
            }
            else
            {
                start1 = 1;
            }
            if (start2 != length)
            {
                start2++;
            }
            else
            {
                start2 = 1;
            }
        }
        second_opacity = 0;
    }
    // alert(document.getElementById("sliderka1").style.filter);
    var obj1 = "sliderka"+start1;
    var obj2 = "sliderka"+start2
    //document.getElementById("counter_images").innerHTML = '<img src="/images/buttonok/kep_pottyok_'+start2+'.png" alt="one" />';
    second_opacity += 0.1;
    start_opacity_down_ie += 10;
    var objToFade1 = document.getElementById(obj1);
    var objToFade2 = document.getElementById(obj2);
    var first = start_opacity_down-second_opacity;
    if (first<0)
        first = 0;
    if (first > 1)
        first = 1;
    var seq = start_opacity_up+second_opacity;
    if (seq<0)
        seq = 0;
    if (seq > 1)
        seq = 1;
    objToFade1.style.opacity = first;
    objToFade2.style.opacity = seq;
    objToFade1.style.filter = 'alpha(opacity = '+((start_opacity_down-second_opacity)*40) +')';
    objToFade2.style.filter = 'alpha(opacity = '+((start_opacity_down+second_opacity)*40)+' )'
    helper++;
    helper2++;
    t=setTimeout("timedCount()",100);
}
function doTimer()
{
    if (!timer_is_on)
    {
        timer_is_on=1;
        helper = 0;
        timedCount();
    }
}
function closeTimer()
{
    timer_is_on = 0;
    clearTimeout(t);
}
function next_image()
{
    if (trans == 1)
    {
        trans = 0;
        start1 = start2;
        start2 = start1+1;
        start1 -=1;
        start2 = start1-1;
    }
    //alert(start1+" "+start2);
    if (busy == false)
    {
        busy = true;
        start1 += 1;
        if (start1 >length)
            start1 = 1;
        if (start1<=0)
            start1 = length;
        start2 = start1+1;
        if (start2>length)
            start2 = 1;
        if (start2<=0)
            start2 = length;
        //alert(start1+" "+start2);
        second_opacity = 0;
        closeTimer();
        nextTrans();
    }
}
function prev_image()
{
    // alert(busy);
    if (trans == 1)
    {
        trans = 0;
        start1 = start2;
        start2 = start1+1;
        start1 -=1;
        start2 = start1-1;
    }
    //alert(start1+" "+start2);
    if (busy == false)
    {
        busy = true;
        start1 += 1;
        if (start1 >length)
            start1 = 1;
        if (start1<=0)
            start1 = length;
        start2 = start1-1;
        if (start2>length)
            start2 = 1;
        if (start2<=0)
            start2 = length;
        //alert(start1+" "+start2)
        // alert(start1+" "+start2)
       // alert(start1+" "+start2);
        second_opacity = 0;
        closeTimer();
        prevTrans();
    }
}
function nextTrans()
{
    //alert(document.getElementById("sliderka"+start1).style.opacity);
    if (document.getElementById("sliderka"+start1).style.opacity == 30)
    {
        var obj1 = "sliderka"+start1;
        var obj2 = "sliderka"+start2
        second_opacity += 0.1;
        start_opacity_down_ie += 10;
        var objToFade1 = document.getElementById(obj1);
        var objToFade2 = document.getElementById(obj2);
        objToFade1.style.opacity = start_opacity_down-second_opacity;
        objToFade2.style.opacity = start_opacity_up+second_opacity;
        objToFade1.style.filter = 'alpha(opacity = '+((start_opacity_down-second_opacity)*40) +')';
        objToFade2.style.filter = 'alpha(opacity = '+((start_opacity_down+second_opacity)*40)+' )';
        next=setTimeout("nextTrans()",100);
    }
    else
    {
        busy = false;
        clearTimeout(next);
        doTimer();
    }
}
function prevTrans()
{
    // alert(document.getElementById("sliderka"+start2).style.opacity);
    if (document.getElementById("sliderka"+start2).style.opacity <= 1)
    {
        //alert(document.getElementById("sliderka"+start2).style.opacity);
        var obj1 = "sliderka"+start1;
        var obj2 = "sliderka"+start2;
        second_opacity += 0.1;
        start_opacity_down_ie += 10;
        var objToFade1 = document.getElementById(obj1);
        var objToFade2 = document.getElementById(obj2);
        objToFade1.style.opacity = start_opacity_down-second_opacity;
        objToFade2.style.opacity = start_opacity_up+second_opacity;
        objToFade1.style.filter = 'alpha(opacity = '+((start_opacity_down-second_opacity)*40) +')';
        objToFade2.style.filter = 'alpha(opacity = '+((start_opacity_down+second_opacity)*40)+' )'
        prev=setTimeout("prevTrans()",100);
    }
    else
    {
        //alert(document.getElementById("sliderka"+start2).style.opacity);
        /*start1 = start2;
        start2 = start1+1;*/
        // alert(start1+" "+start2)
        trans = 1;
        busy = false;
        clearTimeout(prev);
        doTimer();
    }
}
