$(document).ready(function() {
    $("#prev").click(function() {
        prev();
    });
    $("#next").click(function() {
        next();
    });
    $("#next").everyTime("9s", function() { next(); });
});
var index = 1;
var curr = 1;
var block = false;

function prev() {
    if (!block) {
        $("#next").stopTime();
        block = true;
        curr = index;
        var margin = $("#promo" + curr + " img").width() / 2;
        var width = $("#promo" + curr + " img").width();
        var height = $("#promo" + curr + " img").height();
        index = index - 1;
        if (index < 1)
            index = size;
        $("#promo" + index + " img").stop().css({ width: '0px', height: '' + height + 'px', marginLeft: '' + margin + 'px', opacity: '0.5' });
        $("#promo" + curr + " div").stop().fadeOut(250);
        $("#promo" + index + " img").stop().css({ width: '0px', height: '' + height + 'px', marginLeft: '' + margin + 'px', opacity: '0.5' });
        $("#promo" + curr + " img").stop().animate({ width: '0px', height: '' + height + 'px', marginLeft: '' + margin + 'px', opacity: '0.5' }, 500,
          function() {
            $("#promo" + index + " img").stop().animate({ width: '' + width + 'px', height: '' + height + 'px', marginLeft: '0px', opacity: '1' }, 500, function() {
              $("#promo" + curr + " div").stop().fadeIn(250);
              unblock(); });
          }
        );
        curr = index;
    }
}

function next() {
    if (!block) {
        $("#next").stopTime();
        block = true;
        curr = index;
        var margin = $("#promo" + curr + " img").width() / 2;
        var width = 465; //$("#promo" + curr + " img").width();
        var height = 284;// $("#promo" + curr + " img").height();
        //alert(width + " " + height + " " + margin);
        index = index + 1;
        if (index > size)
            index = 1;
        $("#promo" + index + " img").stop().css({ width: '0px', height: '' + height + 'px', marginLeft: '' + margin + 'px', opacity: '0.5' });
        $("#promo" + curr + " div").stop().fadeOut(250);
        $("#promo" + index + " img").stop().css({ width: '0px', height: '' + height + 'px', marginLeft: '' + margin + 'px', opacity: '0.5' });
        $("#promo" + curr + " img").stop().animate({ width: '0px', height: '' + height + 'px', marginLeft: '' + margin + 'px', opacity: '0.5' }, 500,
          function() {
              $("#promo" + index + " img").stop().animate({ width: '' + width + 'px', height: '' + height + 'px', marginLeft: '0px', opacity: '1' }, 500, function() {
                  $("#promo" + curr + " div").stop().fadeIn(250);
                  unblock();
              });
          }
        );
        curr = index;
    }
}
function unblock() {
    block = false;
    $("#next").everyTime("9s", function() { next(); });
}


