/*
	Testimonial Module

	Testimonial slide

	Dependency:
*/
Core.register("testimonials", function(sandbox){
	// Variables privadas
    var $slidesContainer = $('#testimonialSlideContainer');
    var WIDTH = 310;
    var IntervalId = null;
    var Interval = 5000;

	// Método privados
	var _init = function () {

        $('#slideshowright').bind('click', function () {clearInterval(IntervalId); _next();});
        $('#slideshowleft').bind('click', function () {clearInterval(IntervalId); _prev();});
        
        setAutoforward();

	}

    var setAutoforward = function () {
        IntervalId = setInterval(_next, Interval);  
    }

    var setAutoprevious = function () {
        IntervalId = setInterval(_prev, Interval);  
    }
 

	var _next = function () {
        var len = $('#testimonialSlideContainer .slideshowimg').length;
        var limit = (WIDTH * len * -1) + WIDTH;
        
        if ($slidesContainer.position().left > limit) {
            $slidesContainer.animate({
                "left": "-="+WIDTH+"px"
            });
        } else {
            clearInterval(IntervalId);
            setAutoprevious();
        }

	}

	var _prev = function () {
        if ($slidesContainer.position().left <= (WIDTH * -1)) {
            $slidesContainer.animate({
                "left": "+="+WIDTH+"px"
            });
        } else {
            clearInterval(IntervalId);
            setAutoforward();
        }

	}


	return {
		init: function(){
			try{
                _init();

			} catch(ex) {
				sandbox.alert("Error Testimonial Module.\nDesc.:"+ex.message);
			}
		},
		destroy: function(){
			// destructor
		}
	};
});

