/*

  MODIFICATION BY NATHAN SUDELL @ MARS COMPUTER SERVICES LTD
  
  03 June 2010
  
  Added "camcontinue" for infinite carousel.

*/

/*
Title 	: Simple Carousel with Paging Using Mootools
Author 	: Nikhil Kunder (nik1409@gmail.com)
Date 	: 2008/09/12
Version : 1.0
    moocarousel_v1.0.js  is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 	Lesser General Public License for more details.
*/

var MooCarousel = new Class({
		wrapper:'',
		items:'',
		moveleft:'',
		moveright:'',
		slides:2,
		offset:350,
		currentslide:1,
		pos:0,
		mL: '',
		mR: '',
		periodical: '',
		speed: '',
		repeat: '',
		ispaged:false,
		initialize: function(wrapper,items,ns,sss,ispaged,periodical,speed){
			this.wrapper = $(wrapper);
			this.items = $(items);
			this.slides = ns;
			this.offset = sss;
			this.ispaged = ispaged;
			this.parent = this.wrapper.getParent();
			this.scroll = new Fx.Scroll(this.wrapper, {offset:{'x':0, 'y':0}, wait: true, duration: 400 });
			this.dir = "right"; // direction of paging
			this.mR = '';
			this.mL = '';
			this.periodical = periodical;
			this.repeat = '';
			this.speed = speed;
			var that = this;
			

			if(this.ispaged){
				this.carousel_paging = new Element('div').addClass('carousel_paging');
				this.carousel_paging.id= this.wrapper.id + "_p";

				var mLSpan = new Element('span');
				mL = new Element('a').set('id', 'moveleft').set('text', '<').injectInside(mLSpan);
				mLSpan.injectInside(this.carousel_paging);

				for (  i = 1;  i <= parseInt(this.slides) ; i++){
					var aa = new Element('a').addClass('page');
					if(i==1) aa.addClass("current");
					aa.addClass("index");
					aa.href="javascript:void(0);";
					aa.addEvent('click', this.page.bind(this, [i, aa, this.carousel_paging]));
					aa.innerHTML = i;
					aa.injectInside(this.carousel_paging);
				}
				aa.addClass('last');
				
				var mRSpan = new Element('span');
				mR = new Element('a').set('id', 'moveright').set('text', '>').injectInside(mRSpan);
				mRSpan.injectInside(this.carousel_paging);
				
				this.carousel_paging.injectInside(this.parent);
				var carousel_fix = new Element('div').addClass('clearfix').injectBefore(this.carousel_paging);
				var carousel_fix = new Element('div').addClass('clearfix').injectInside(this.carousel_paging);
			}
			mL.addEvent('click', this.camoveleft.bind(this));
			mR.addEvent('click', this.camoveright.bind(this));
			
			mL.addEvent('click', this.removeperiodical.bind(this));
			mR.addEvent('click', this.removeperiodical.bind(this));

			if(this.periodical) {
				this.repeat = this.camcontinue.periodical(this.speed, this);
			}
		},
		removeperiodical: function() {
			$clear(this.repeat);
		},
		camoveleft: function(event){
			if(this.currentslide == 1) {
				this.currentslide = this.slides;
				this.pos = (this.offset*(this.slides-1));
			}
			else {
				this.currentslide--;
				this.pos += -(this.offset);
			}
			
			if(this.ispaged){																						
				this.setcss('left');
				this.dir = 'left';
			}
			this.scroll.start(this.pos);this.scroll.toLeft();
			
		},
		camoveright: function(event){
			if(this.currentslide >= this.slides) {
				this.currentslide = 1;
				this.pos = 0;
			}
			else {
				this.currentslide++;
				this.pos += this.offset;
			}
			if(this.ispaged){																						
				this.setcss('right');
				this.dir = 'right';
			}
				
			this.scroll.start(this.pos);this.scroll.toLeft();
		},
		camcontinue: function(event){    
                         
      if (this.items.getElements("div").length == this.slides) {
        firstSlide = this.items.getFirst();
  		  firstSlide.clone().inject(this.items.getLast(),"after");
  		  this.scroll.oldComplete = this.scroll.complete;
      }                         
                              
			if(this.currentslide >= this.slides) {
				this.currentslide = 1;        		    		  
        this.scroll.complete = function() {          
          this.oldComplete();
          this.complete = function() {
            this.oldComplete();
            this.complete = this.oldComplete;
            this.options.duration = this.tempOptions.duration;
            this.options.wait = this.tempOptions.wait;
          }
          
          this.tempOptions = {};
          this.tempOptions.duration = this.options.duration;
          this.tempOptions.wait = this.options.wait;
          
          this.options.duration = 0;
          this.options.wait = false;
          
          this.start(0);
          this.toLeft();        
        };
			}
			else {
				this.currentslide++;        				
			}			
			
			this.pos += this.offset;
			
			
			if(this.ispaged){																						
				this.setcss('right');
				this.dir = 'right';
			}
			
			this.scroll.start(this.pos);this.scroll.toLeft();
			
			if (this.currentslide == 1) {
        this.pos = 0;
      }
		},
		
		page: function(pagenum,o, p){
			this.removeperiodical();
			var sss = ((pagenum-1)*this.offset) ;
			if(pagenum > this.slides) return;
			if(pagenum == 1) sss = 0;
			this.currentslide = pagenum
			this.pos = sss
			this.scroll.start(this.pos);this.scroll.toLeft();
			this.resetcss(o, p);
		},
		setcss:function(dir){
			var x = parseInt(this.currentslide)-1; 		
			if(x < 0 ) x = 0; if( x >9) x =9;
			var o = this.carousel_paging.getElements('a.index')[x];
			this.resetcss(o,this.carousel_paging);
		},
		resetcss: function(o,p){
			var cpa = p.getElements('a.index');
			cpa.each(function(el,i){
				el.removeClass("current");
				el.addClass("page");
			});
			o.removeClass("page");
			o.addClass("current");
			cpa.getLast().addClass('last');
		}		
	});

	MooCarousel.implement(new Events);
	MooCarousel.implement(new Options);