// JavaScript Document


            YUI().use("node", "anim", function(Y) { 
                YAHOO.log("Animcalled", "info"); 
                // Constants
                var WINDOW_WIDTH,
                    TOTAL_PAGES,

                    // Class names
                    
                    PAGINATION = "bump",

                    // YNode references
                    _carousel, 

                    // Animator
                    _anim,

                    // Current X position used in animation 
                    _currentXPosition,
                    _startXPosition,

                    _startIndex = 0,
                    _lastIndex,
					_rewind=0,

                    // Current pagination page shown; it contains a integer like 1, 2, etc.
                    _currentIndex = 0;
					

                // Event handlers
                function init() {
                    // Initialize the DOM references
                    _body = Y.get(document.body);
                    _carousel = _body.query(".carousel"),

                    // Animation stuff
                    _anim = new Y.Anim({ 
                        node: _carousel, 
                        duration: 0.25, 
                        easing: Y.Easing.easeOut 
                    });

                    // Get the offsetWidth of the list containing the photos
                    WINDOW_WIDTH = _carousel.get("children").item(0).get("offsetWidth");
                    TOTAL_PAGES = _carousel.get("children").size(); 

                    _lastIndex = TOTAL_PAGES - 1;
                    _currentXPosition = _carousel.getX();

                    // Add a custom attribute to track the current page that we're on 
                    _anim.addAtt("currentPage", { value : _currentIndex });

                    _anim.on("end", handleScrollEnd);

                }

                // When the scrolling animation ends, this event handler is called.
                function handleScrollEnd(e) {
                    _body.query(".selected").removeClass("selected");

                    // After we've removed the selected class from the old node,
                    // We get the new selected node and set the selected class 
                    _body.query(".page" + this.get("currentPage").value).addClass("selected"); 
                }

                // Detect the click and then scroll appropriately
                function scrollControl(e) {
YAHOO.log("scrollControl Called", "info"); 
                    var target = e.target,
                        gotoPage;
						YAHOO.log(gotoPage, "info");
                    

                        // Parse out class name "page" to get the integer
                        gotoPage = parseInt(target.get("className").substring(4), 10);
                        paginate(gotoPage);

                    

                   

                        _anim.set("to", { xy: [_currentXPosition, _carousel.getY()] });
                        _anim.set("currentPage", { value : _currentIndex });
                        _anim.run(); 
						if (_rewind!=1)
				{foo.timer0.cancel();
						foo.timer1.cancel();
						foo.timer2.cancel();
						foo.timer3.cancel();}
						

                    
                }
				

               

               
				
				

                // Handles pagination
                function paginate(gotoPage) {
				YAHOO.log("paginate Called", "info"); 
                    var multiple;

                    if (gotoPage > _currentIndex) {

                        // This is like clicking go right
                        multiple = gotoPage - _currentIndex;

                        for (var i = 0;i < multiple;i++) {
                            _currentXPosition -= WINDOW_WIDTH;
                        }

                    } else if (gotoPage < _currentIndex) {

                        // This is like clicking go left
                        multiple = _currentIndex - gotoPage;

                        for (var i = 0;i < multiple;i++) {
                            _currentXPosition += WINDOW_WIDTH;
                        }

                    }

                    _currentIndex = gotoPage;

                }
				
				var realtime0=time0*1000;
				var realtime1=(time1*1000) + realtime0;
				var realtime2=(time2*1000) + realtime1;
				var realtime3=(time3*1000) + realtime2;
				
				var foo = {

count: 0,

'someMethod0': function(data) {
	this.count++;
	
	
	//console.log(this.count);
	paginate(1);
	 _anim.set("to", { xy: [_currentXPosition, _carousel.getY()] });
                        _anim.set("currentPage", { value : _currentIndex });
                        _anim.run(); 
	
	_currentIndex = 1;
	},
'someMethod1': function(data) {
	this.count++;
	
	
	//console.log(this.count);
	paginate(2);
	 _anim.set("to", { xy: [_currentXPosition, _carousel.getY()] });
                        _anim.set("currentPage", { value : _currentIndex });
                        _anim.run(); 
	
	_currentIndex = 2;
	}
,
'someMethod2': function(data) {
	this.count++;
	
	
	//console.log(this.count);
	paginate(3);
	 _anim.set("to", { xy: [_currentXPosition, _carousel.getY()] });
                        _anim.set("currentPage", { value : _currentIndex });
                        _anim.run(); 
	
	_currentIndex = 3;
	},
'someMethod3': function(data) {
	this.count++;
	
	
	//console.log(this.count);
	paginate(0);
	 _anim.set("to", { xy: [_currentXPosition, _carousel.getY()] });
                        _anim.set("currentPage", { value : _currentIndex });
                        _anim.run(); 
	
	_currentIndex = 0;
	_rewind=1;
	
	

	}
};
				var autoScroll0 = function(e)
{
var booleanPeriodic = false;
foo.timer0 = YAHOO.lang.later(realtime0, foo, 'someMethod0', 
	[{data:'bar', data2:'zeta'}], booleanPeriodic);
}
var autoScroll1 = function(e)
{
var booleanPeriodic = false;
foo.timer1 = YAHOO.lang.later(realtime1, foo, 'someMethod1', 
	[{data:'bar', data2:'zeta'}], booleanPeriodic);
}
var autoScroll2 = function(e)
{
var booleanPeriodic = false;
foo.timer2 = YAHOO.lang.later(realtime2, foo, 'someMethod2', 
	[{data:'bar', data2:'zeta'}], booleanPeriodic);
}
var autoScroll3 = function(e)
{
var booleanPeriodic = false;
foo.timer3 = YAHOO.lang.later(realtime3, foo, 'someMethod3', 
	[{data:'bar', data2:'zeta'}], booleanPeriodic);
}
             // When the DOM is ready load the photos!
                Y.on("domready", init); 
                Y.on("click", scrollControl, "body .container");
				Y.on("domready", autoScroll0); 
				Y.on("domready", autoScroll1); 
				Y.on("domready", autoScroll2); 
				Y.on("domready", autoScroll3); 

            }); 
   
			



