// Javascript document

$(document).ready(function () {



// Background positioning - required for firefox :( - stupid firefox bug!
    $('body').css ({
        "background-position":"center " + ($(document).height() - 551) + "px"
    });
    $(window).resize(function() {
        $('body').css ({
            "background-position":"center " + ($(document).height() - 551) + "px"
        });
    });
    
    
// Aparitie buton actualizare Cos ----------------------
    $('.formCos input[type="text"]').focus(function () {
        $('#comanda').hide();
        $('#actualizareCos').show();
    }); 


// Slider galerie lucrari ------------------------------    
    $(".smallViewport").each(function () {
        $this = $(this);
        myWidth = $(this).width();
        mySlots = $(this).find(".sliderSlot");
        smallViewport_ID = setInterval ("moveSlider($this, mySlots,myWidth, -1)", 2000);
        $this.parent().parent().find(".larr").click(function () {
            clearInterval (smallViewport_ID);
            moveSlider($this, mySlots, myWidth, 1);
            smallViewport_ID = setInterval ("moveSlider($this, mySlots,myWidth, -1)", 2000);
            return false;
        });
        $this.parent().parent().find(".rarr").click(function () {
            clearInterval (smallViewport_ID);
            moveSlider($this, mySlots, myWidth, -1);
            smallViewport_ID = setInterval ("moveSlider($this, mySlots,myWidth, -1)", 5000);
            return false;
        });
    }); 

// Slider Noutati Produse ------------------------------
    scrollStep = 250;
    rArr = $(".noutatiSlider .rarr");
    lArr = $(".noutatiSlider .larr");
    slideWidth = $(".noutatiSlider .noutatiCounter").width();
    viewportWidth  = $(".noutatiSlider .noutatiViewport").width();
    slidePos = parseInt($('.noutatiContainer').css("left"));
    rArr.click(function () {
        slidePos = parseInt($('.noutatiContainer').css("left"));
        if ((slideWidth+slidePos - viewportWidth) > scrollStep) {
            destination = slidePos - scrollStep;
        } else {
            destination = -slideWidth + viewportWidth;
        }
        $('.noutatiContainer').animate({
           "left":destination+"px"
        });
        return false;
    });
    lArr.click(function () {
        slidePos = parseInt($('.noutatiContainer').css("left"));
        if (slidePos < 0) {
            if (slidePos < -scrollStep) {
                destination = slidePos + scrollStep;
            } else {
                destination = 0;
            }
             $('.noutatiContainer').animate({
                "left":destination+"px"
            });
        }
        return false;
    });
    
    
});

function moveSlider(Slider, mySlots, myWidth, direction) {
    var currentIndex = Math.abs(Math.ceil(parseInt(Slider.find(".sliderContainer").css("left"))/myWidth))+1;
    if (direction == -1) {
        destination = (mySlots.length == currentIndex) ? 0 : myWidth * currentIndex * direction;
        Slider.find(".sliderContainer").animate({"left":destination});
    }
    if (direction == 1) {
        destination = (currentIndex==1 ) ? -(mySlots.length-1)*myWidth : - myWidth * (currentIndex - 2) * direction;
        Slider.find(".sliderContainer").animate({"left":destination});
    }
}

