﻿$(document).ready(function () {

    var slidecount = $(".slide").length;
    var i = 0;
    var max = slidecount;
    var current = 0;
    var timeout;
    var clicked = false;
    $('#slides_container').css('width', slidecount * 387);
    $('#slides_container').fadeIn('6000', function () {

        // setTimeout(movenext(i + 1), 2000);
        timeout = setTimeout(function () { movenext(i + 1) }, 3000);
    });

    $('#next').click(function () { clearTimeout(timeout); clicked = true; movenext(current + 1); });
    $('#prev').click(function () { clearTimeout(timeout); clicked = true; movenext(current - 1); });

    $('#0').click(function () { clearTimeout(timeout); clicked = true; movenext(0); });
    $('#1').click(function () { clearTimeout(timeout); clicked = true; movenext(1); });

    function movenext(no) {

        if (no >= max)
            no = 0;
        else if (no < 0)
            no = 0;
        current = no;


        var pos1 = (no * 37) - 20;
        if (pos1 < -20)
            pos1 = -20;
        var pos = -no * 387;
        if (pos > 0)
            pos = 0;
        var prev = no - 1;
        var next = no + 1;
        if (prev < 0)
            prev = max - 1;
        $('#button' + (prev)).attr('src', 'images/indicator.png');
        $('#button' + no).attr('src', 'images/indicator-active.png');
        $('#button' + next).attr('src', 'images/indicator.png');
        $('#slidebtn').animate({ "margin-left": pos1 }, '6000', function () { });
        $('#slides_container').animate({ "margin-left": pos }, 'slow', function () {




            var next = no + 1;

            //$('#button' + next).attr('src', 'images/indicator-active.png');
            if (!clicked)
                timeout = setTimeout(function () { movenext(no + 1) }, 3000);
        });

    }


    /* addiing Controls */





}); 
