

		   YUI().use("node", "anim", function(Y) {

                // Constants
                var CAROUSEL_WIDTH,
				    TOTAL_PAGES,

                    // Class names
                    RIGHT_BUTTON = "right-button",
                    LEFT_BUTTON = "left-button",

                    SCROLL_DIRECTION,

                    // YNode references
                    _body,
                    _carousel,

                    // Animator
                    _anim,

                    // Current X position used in animation
                    _currentXPosition,
                    _startXPosition,

                    _startIndex = 0,
                    _lastIndex,

                    //Widths of constituency elements
                    constituency_widths = Array(),
                    _carousel_width,


                    // Current pagination page shown; it contains a integer like 1, 2, etc.
                    _currentIndex = 0;

                // Event handlers
                function init() {

                    var _carousel_width = 0;
                    // Initialize the DOM references
                    _body = Y.get(document.body);
                    _carousel = _body.query(".carousel"),

                    // Animation stuff
                    _anim = new Y.Anim({
                        node: _carousel,
                        duration: 0.5,
                        easing: Y.Easing.easeOut
                    });

                    // Get the offsetWidth of the list containing the photos
                    TOTAL_PAGES = _carousel.get("children").size();
                    constituency_widths = Array();
                    for(i=0;i<TOTAL_PAGES;i++) {
                         constituency_widths[i] = _carousel.get("children").item(i).get("offsetWidth") + 20;
                         _carousel_width += constituency_widths[i];
                    }

                    CAROUSEL_WIDTH = _carousel_width;

                    _lastIndex = TOTAL_PAGES - 1;
                    _currentXPosition = _carousel.getX();

					_anim.on("start", handleScrollStart);
                    _anim.on("end", handleScrollEnd);

                }

                function handleScrollStart(e){

                     if(SCROLL_DIRECTION == "left") {

						var node = _anim.get('node');
					    var childNode = _anim.get('node').get("children").item(TOTAL_PAGES - 1).cloneNode(true);

						node.removeChild(node.get("children").item(TOTAL_PAGES - 1));
						node.prepend(childNode);


                        for(i=0;i<TOTAL_PAGES;i++) {
                           constituency_widths[i] = _carousel.get("children").item(i).get("offsetWidth") + 20;
                           _carousel_width += constituency_widths[i];
                        }

                        _carousel.setX(_carousel.getX() - constituency_widths[0]);


                     }
                }


                // When the scrolling animation ends, this event handler is called.
                function handleScrollEnd(e) {

                    if(SCROLL_DIRECTION == "right") {
					  var node = _anim.get('node');
					  var childNode = _anim.get('node').get("children").item(_currentIndex - 1).cloneNode(true);
					  node.appendChild(childNode);
					  _carousel.setX(_carousel.getX() + constituency_widths[_currentIndex - 1]);
					  _anim.get('node').removeChild(_anim.get('node').get("children").item(_currentIndex - 1));

					  _lastIndex = TOTAL_PAGES - 1;
					  _carousel_width =  0;

                      for(i=0;i<TOTAL_PAGES;i++) {
                         constituency_widths[i] = _carousel.get("children").item(i).get("offsetWidth") + 20;
                         _carousel_width += constituency_widths[i];
                      }

                       _currentIndex = 0;

                      CAROUSEL_WIDTH = _carousel_width;
                    }

                }//handleScrollEnd(e)



                // Detect the click and then scroll appropriately
                function scrollControl(e) {

                    var target = e.target,
                        hasLeftButton = target.hasClass(LEFT_BUTTON),
                        hasRightButton = target.hasClass(RIGHT_BUTTON);

                    if (hasLeftButton) {
                        scrollLeft();
                    } else if (hasRightButton) {
                        scrollRight();
                    }

                    if (hasLeftButton || hasRightButton) {

                        _anim.set("to", { xy: [_currentXPosition, _carousel.getY()] });
                        _anim.run();

                    }


                }

                // Handles scrolling left
                function scrollLeft() {
                        _currentXPosition = _carousel.getX();
                        SCROLL_DIRECTION = "left";

                }

                // Handles scrolling right
                function scrollRight() {


                        _currentXPosition = _carousel.getX() - constituency_widths[_currentIndex];
                        ++_currentIndex;
                        SCROLL_DIRECTION = "right";

				}


                // When the DOM is ready load the photos!
                Y.on("domready", init);
                Y.on("click", scrollControl, "body #carouselWrap");

            });
