YUI.add('slideshow', function(Y) {
	
	// Transitions:	
	Y.Transitions = {

		none: {},
		fadeIn: { from:{opacity:0}, to:{opacity:1} },

                fadeOut: { from:{opacity:1}, to:{opacity:0} },

                flyIn: { from: {xy: function(node){return [node.get('parentNode').getX(), node.getY()];}, opacity: 0},to: {xy: function(node){return node.getXY();}, opacity: 1} },

		flyOut: { from: {opacity: 1},to: {opacity: 0} }
	}; // Y.Transitions
	
	Y.Slideshow = function(pTarget, pConfig){		
                //Begin initialization................
                //////////////////////////////////////
		// Target:
		this.id = (pTarget.indexOf('#') === 0) ? pTarget : '#' + pTarget;
		this.container = Y.one(this.id);
		
		// Properties:
                this.slides = Y.all('#banner_default ul li');

	this.zIndex = { container: 1, slides: 2, nextSlide: 3, currentSlide: 4 };
		
		this.easingIn = Y.Easing.easeOut;
		this.easingOut = Y.Easing.easeOut;
		
                //Default initial values if not provided
		this.currentSlide = 0;
		this.interval = 4000;
		this.duration = 0.5;
		
		this.autoplay = true;
		this.debug = false;
		this.loop = false;
		this.stopOnUser = true;

  
               // Config:
		if(Y.Lang.isObject(pConfig)){
			for(var i in pConfig){
				if(pConfig.hasOwnProperty(i)){
					this[i] = pConfig[i];

				}
			}
		}

                //Animation Array Objects
                this.arrayOutImgAnm   = new Array;
                this.arrayOutTagAnm   = new Array;
                this.arrayOutTeaseAnm = new Array;
                this.arrayInImgAnm    = new Array;
                this.arrayInTagAnm    = new Array;
                this.arrayInTeaseAnm  = new Array;

                for ( i=0; i < this.slides.size(); i++) {
                    this.arrayOutImgAnm[i] = new Y.Anim({
                        node: this.slides.item(i).one('IMG'), 
                        from: {opacity:1}, 
                        to: {opacity:0}, 
                        duration: this.duration, 
                        easing: this.easing
                    });
                    this.arrayOutTagAnm[i] = new Y.Anim({
                        node: this.slides.item(i).one('div.banner_tag'), 
                        from: {opacity: 1}, 
                        to: {opacity: 0}, duration: this.duration, easing: this.easing
                    });
                    this.arrayOutTeaseAnm[i] = new Y.Anim({
                        node: this.slides.item(i).one('div.banner_tease'), 
                        from: {opacity: 1}, 
                        to: {opacity: 0}, duration: this.duration, easing: this.easing
                    });
                    this.arrayInImgAnm[i] = new Y.Anim({
                        node: this.slides.item(i).one('IMG'), 
                        from: {opacity:0}, 
                        to: {opacity:1}, 
                        duration: this.duration, 
                        easing: this.easing
                    });
                    this.arrayInTagAnm[i] = new Y.Anim({
                        node: this.slides.item(i).one('div.banner_tag'), 
                        from: {xy: function(node){return [node.get('parentNode').getX(), node.getY()];}, opacity: 0}, 
                        to: {xy: function (node){return node.getXY();}, opacity: 1}, 
                        duration: this.duration, 
                        easing: this.easing
                    });
                    this.arrayInTeaseAnm[i] = new Y.Anim({
                        node: this.slides.item(i).one('div.banner_tease'), 
                        from: {opacity: 0}, 
                        to: {opacity: 1}, 
                        duration: this.duration, 
                        easing: this.easing});
                }

		var that = this;

                this.container.setStyles({
			position: 'relative',
			zIndex: this.zIndex.container
		});

	
                      this.slides.setStyles({
			position: 'absolute',
			zIndex: this.zIndex.slides 
		});
                this.slides.item(this.currentSlide).setStyle('zIndex', this.zIndex.currentSlide);
                this.slides.item(this.currentSlide + 1).setStyle('zIndex', this.zIndex.nextSlide);
         
                for ( kk = 1; kk < this.slides.size(); kk++ ) {
                    this.slides.item(kk).one('IMG').setStyle('opacity', 0);
                    this.slides.item(kk).one('div.banner_tag').setStyle('opacity', 0);
                    this.slides.item(kk).one('div.banner_tease').setStyle('opacity', 0);
                }
                /////////////////////////////////////////
				
		this.slideChange = function(pWhich){
                    lPrevSlide = this.currentSlide;
		    this.switchSlide(pWhich);
                    this.slides.item(this.currentSlide).setStyle('zIndex', this.zIndex.nextSlide);
                    this.arrayOutImgAnm[lPrevSlide].on('end', function(){this.slides.item(lPrevSlide).setStyle('zIndex', this.zIndex.slides);}, this);

                    this.arrayOutImgAnm[lPrevSlide].run();
                    this.arrayOutTagAnm[lPrevSlide].run();
                    this.arrayOutTeaseAnm[lPrevSlide].run();
		    this.arrayInImgAnm[this.currentSlide].run();
                    this.arrayInImgAnm[this.currentSlide].on('end', function(){
                        this.slides.item(this.currentSlide).setStyle('zIndex', this.zIndex.currentSlide);
                        this.arrayInTagAnm[this.currentSlide].run();
                    }, this);
                    this.arrayInTagAnm[this.currentSlide].on('end', function(){this.arrayInTeaseAnm[this.currentSlide].run();}, this);     
		
		}; // slideChange()


                this.switchSlide = function(pWhich){
			switch(pWhich){
				case 'next':
					if(this.currentSlide + 1 < this.slides.size()){
						this.currentSlide++;
					} else {
						this.currentSlide = 0;
					}
					break;
				case 'previous':
					if(this.currentSlide - 1 > -1){
						this.currentSlide--;
					} else {
						this.currentSlide = this.slides.size() - 1;
					}
					break;
				default:
					this.currentSlide = parseInt(pWhich, 10);
			}

                }; //switchSlide()

		
		this.startLoop = function(){
			if(this.autoplay === true){
				if(this.debug === false){
					this.loop = setInterval(function(){ that.slideChange('next'); }, this.interval);
				} else if(this.debug === true) {
					this.loop = setTimeout(function(){ that.slideChange('next'); }, this.interval);
				}
			}
		}; // startLoop()
		
                this.pause = function() {
                    clearInterval(this.loop);
                    this.loop = false;
                };//pause

                this.previous = function() {
                    clearInterval(this.loop);
                    this.loop = false;
                    this.slideChange('previous');
                }; //previous
		
                this.next = function() {
                    clearInterval(this.loop);
                    this.loop = false;
                    this.slideChange('next');
                }; //next

                this.noOfSlide = function(currentSlide) {
                    clearInterval(this.loop);
                    this.loop = false;
                    this.slideChange(currentSlide);
                }; //noOfSlide


                this.play = function(currentSlide) {
                    this.currentSlide = currentSlide;
                    this.slideChange(next);
                }; //noOfSlide
this.startLoop();
		
	}; // Y.Slideshow()
	
}, '1.0.0', { requires: ['anim'] });


