﻿var ss = 1;
function rotateHeader(rr) {

    if ( ss < rr) {
        $(".content").animate({ 'left': -(ss * 980) + 'px' });
        $(".control span").removeClass("clicked");
        $(".control span:eq("+ss+")").addClass("clicked");
        ss++;
    } else {
        $(".content").animate({ 'left': 0 + 'px' });
        $(".control span").removeClass("clicked");
        $(".control span:eq(" + 0 + ")").addClass("clicked");
        ss = 1;
    }
}

function gotoSlide(x) {
    $(".rotator .content").stop().animate({ 'left': -(x * 970) + 'px' });
}

function clientLeft(x, y) {
    if (y == 0) {
        $(".clients .center ul").stop().animate({ 'margin-left': -((y + 1) * 211) + 'px' });
        return (y + 1);
    } else if( y < ( x - 4 )) {
        $(".clients .center ul").stop().animate({ 'margin-left': -((y + 1) * 211) + 'px' });
        return (y + 1);
    } else {
        return(y);
    }
}

function clientRight(x, y) {
    if (y == 0) {
        return(y);
    } else if (y >= 1) {
        $(".clients .center ul").stop().animate({ 'margin-left': -((y -1 ) * 211) + 'px' });
        return (y - 1);
    } else {
        $(".clients .center ul").stop().animate({ 'margin-left': -((y -1 ) * 211) + 'px' });
        return (y - 1);
    }
}

$().ready(function () {

    $("ul").each(function () {
        $(this).children("li:last").addClass("last");
    });

    var contentSize = $(".rotator .content li").size();
    var clientsSize = $(".clients .rotator .center li").size();

    for (i = 0; i < contentSize; i++) {
        $(".rotator .control").append('<span></span>');
    }
    $(".rotator .control span:eq(0)").addClass("clicked");

    $(".rotator .content").css({ 'width': (contentSize * 970) + 'px' });

    $(".rotator .control span").click(function () {
        $(".rotator .control span").removeClass();
        $(this).addClass("clicked");
        var x = $(".rotator .control span").index(this);
        clearInterval(t);
        gotoSlide(x);
    });

    var currentClient = 0;
    $("#goToRight").click(function () { currentClient = clientLeft(clientsSize, currentClient); });
    $("#goToLeft").click(function () { currentClient = clientRight(clientsSize, currentClient); });


    var eee = $(".content li").size();
    var t = setInterval("rotateHeader(" + eee + ")", 5000);

});
