if(typeof(MA)=="undefined"){MA={}}

MA.Slider = new Class({

	orientation:"horizontal",
	container:null,
	maskDiv:null,
	items:null,
	currentPage:1,
	itemsPerPage:null,
	totalPages:null,
	maskInnerDimension:null,
	isAnimating:false,
	_isCarousel:true,
			
	initialize: function(a, c, b) {
		this.items = new Array();
		this.itemsPerPage = b;
		this.container = $(a);
		this.populate(c)
	},

	setCarousel: function(a) {
		this._isCarousel = a;
	},

	populate:function() {
		this.container.innerHTML = "The slider requires population with data. You need to call this.render(itemsPerPage) from your custom this.populate() function."
	},
	
	render: function(b) {
		this.container.innerHTML = "";
		if(!b){
			b=this.itemsPerPage
		}
		this.itemsPerPage=b;
		var listItems = this.renderItems();

		this.list = new Element("ul");

		for (var a=0; a<listItems.length;a++)
		{
			if (!listItems[a].length)
			{
				listItems[a].inject(this.list);
			}
			else
			{
				for (var b=0; b<listItems[a].length;b++)
				{
					listItems[a][b].inject(this.list);
				}
			}
		}

		var a= new Element("div",{"class":"MASliderMaskDiv"});
		this.list.inject(a);
		this.container.appendChild(a);
		this.maskDiv = a;
		this.totalPages=Math.ceil(this.items.length/this.itemsPerPage);

		if(this.items.length>this.itemsPerPage){
			this.positionWithinMask(a);
			this.createArrows();
			this.createDots()
		}
	},

	positionWithinMask: function(a) {
		//console.log('positionWithinMask');
		if(this.orientation=="horizontal"){
			//console.log('a.getSize().x: ' + a.getSize().x);
			this.maskInnerDimension=a.getSize().x;
			
			this.list.position({
				relativeTo: this.maskDiv,
				position: 'topLeft',
				edge: 'topLeft',
				offset: {x: -this.maskInnerDimension*this.currentPage, y: 0}
			});
		} else {
			this.maskInnerDimension=a.getSize().y;
			this.list.position({
				relativeTo: this.maskDiv,
				position: 'topLeft',
				edge: 'topLeft',
				offset: {x: 0, y: -this.maskInnerDimension*this.currentPage}
			});
		}
	},
		
	renderItems: function(){
		//console.log('renderItems');
		var f=[];
		var b=this.items.length-this.items.length%this.itemsPerPage;
		if(b==this.items.length){
			b=this.items.length-this.itemsPerPage
		}
		var d=this.itemsPerPage-this.items.length%this.itemsPerPage;
		
		if(d==this.itemsPerPage){
			d=0
		}
		var e=this.items.length>this.itemsPerPage;
		if(e){
			f.push(this.renderPlaceholderItems(b,this.items.length));
			for(var a=0;a<d;a++){
				var c = new Element('li', {'class': 'empty'});
				f.push(c)
			}
		}
		for(var a=0; a<this.items.length;a++){
			var c=this.items[a].render().clone(true);
			f.push(c);
		}
		if(e){
			for(var a=0;a<d;a++){
				var c= new Element("li",{"class":"empty"});
				f.push(c)
			}
			f.push(this.renderPlaceholderItems(0,this.itemsPerPage))
		}
		return f
	},
			
	renderPlaceholderItems: function(g,c){
		//console.log('renderPlaceholderItems');
		var e=[];
		for(var b=g;b<c;b++){
			var d=this.items[b].render().clone(true);
			if(d.id){
				d.removeProperty("id");
			}
			
			for(var a=0,f;f=d.childNodes[a];a++){
				if(f.getProperty("id")){
					f.removeProprety("id")
				}
			}
			$(d).addClass("cloned");
			e.push(d)
		}
		return e
	},
			
	createArrows: function(){
		//console.log('createArrows');
		this.prevArrow=new Element("a",{"class":"MASliderPreviousArrow", "html": "&lt;"});

		this.prevArrow.addEvent('click', function(event) {
			event = new Event(event).stop();
			if(this._isCarousel===true||(this._isCarousel===false&&this.currentPage!=1)){
				this.getPrevious()
			}
		}.bind(this));
		
		this.container.appendChild(this.prevArrow);
		this.nextArrow=new Element("a",{"class":"MASliderNextArrow", "html": "&gt;"});

		this.nextArrow.addEvent('click', function(event) {
			event = new Event(event).stop();
			if(this._isCarousel===true||(this._isCarousel===false&&this.currentPage!=this.totalPages)){
				this.getNext()
			}
		}.bind(this));
		
		this.container.appendChild(this.nextArrow)
	},
			
	createDots:function(){
		//console.log('createDots');
		this.pageNav=new Element("ul",{"class":"MASliderPageNav"});
		for(var b=1;b<=this.totalPages;b++){
			var a=(b==this.currentPage) ? new Element("a",{"class":"active", "html": b}) : new Element("a",{"html": b});

			a.addEvent("click",function(event, d){
				event = new Event(event).stop();
				this.scrolltoPageNumber(d)
			}.bindWithEvent(this, b));
			
			var c=new Element("li");
			c.grab(a);
			this.pageNav.grab(c);
			
		}
			
		this.container.appendChild(this.pageNav)
	},
			
	getPrevious:function(){
		var a=this.currentPage-1;
		this.scrolltoPageNumber(a,1)
	},
			
	getNext:function(){
		var a=this.currentPage+1;
		this.scrolltoPageNumber(a,-1)
	},
			
	scrolltoPageNumber:function(f,e){
		//console.log('scrolltoPageNumber');
		//console.log('f:' + f);
		//console.log('e:' + e);

		if(this.currentPage!=f&&!this.isAnimating){
			if(!e){
				var e=this.currentPage-f
			}
			this.isAnimating=true;
			this.currentPage=f;
			this.resetPages();
			
			var d=this.pageNav.getElements("a");
			
			for(var c=0,b;b=d[c];c++){
				if(b.hasClass("active")){
					b.removeClass("active");
				}
				if(b.get("html")==this.currentPage){
					b.addClass("active");
				}
			}


			//console.log("this.maskInnerDimension: " + this.maskInnerDimension);
			//console.log("offset: " + this.maskInnerDimension*e);

					
			if(!this.maskInnerDimension){
				this.positionWithinMask(this.container)
			}

			var moveTransition = new Fx.Transition(Fx.Transitions.Cubic, 3);

			var eff = new Fx.Move(this.list, {
				relativeTo: this.maskDiv,
				position: 'topLeft',
					edge: 'topLeft',
					duration: '1000',
					transition: moveTransition.easeInOut
				});
			
			eff.addEvent('onComplete',function(){
  				this.afterScroll();
			}.bind(this));

			if(this.orientation=="horizontal"){
				eff.start({
					offset: {x: -this.maskInnerDimension*this.currentPage, y: 0}
				});
			}
			else{
				eff.start({
					offset: {x: 0, y: -this.maskInnerDimension*this.currentPage}
				});
			}
		}
	},
		
	jumptoPageNumber:function(f){
		//console.log('jumptoPageNumber');
		if(this.currentPage!=f&&!this.isAnimating){
			var e=this.currentPage-f;
			this.currentPage=f;
			this.resetPages();
			if(this.pageNav){
				var d=this.pageNav.getElements("a");
				d.each(function(item, index){
					if(item.hasClass("active")){
						item.removeClass("active");
					}
					if(item.get('html').innerHTML==this.currentPage){
						item.addClass("active");
					}
				});
			}
			var eff = new Fx.Move(this.list, {
					relativeTo: this.maskDiv,
					position: 'topLeft',
					edge: 'topLeft',
					duration: '0'
			});
			if(this.orientation=="horizontal"){
				eff.start({
					offset: {x: -this.maskInnerDimension*f, y: 0}
				});
			}
			else{
				eff.start({
					offset: {x: 0, y: -this.maskInnerDimension*f}
				});
			}
		}
	},
		
	resetArrows:function(){
		//console.log('resetArrows');
		if(this.prevArrow){
			this.prevArrow.removeClass("inactive");
		}
			
		if(this.nextArrow){
			this.nextArrow.removeClass("inactive");
		}
			
		if(this.currentPage==1){
			this.prevArrow.addClass("inactive");
		} else {
			if(this.currentPage==this.totalPages){
				this.nextArrow.addClass("inactive");
			}
		}
	},
		
	resetPages:function(){
		this.resetArrows();
		if(this.currentPage==0){
			this.currentPage=this.totalPages;
			this.positionOffset=-this.maskInnerDimension*this.totalPages;
			if(this.prevArrow){
				this.prevArrow.addClass("inactive");
			}
		} else {
			if(this.currentPage==this.totalPages+1){
				this.currentPage=1;
				this.positionOffset=-this.maskInnerDimension;
				if(this.nextArrow){
					this.nextArrow.addClass("inactive");
				}
			} else {
				this.positionOffset=false;
			}
		}
	},
		
	afterScroll:function(){
		//console.log('afterScroll');
		if(this.positionOffset){
			if(this.orientation=="horizontal"){
				this.list.position({
					relativeTo: this.maskDiv,
					position: 'topLeft',
					edge: 'topLeft',
					offset: {x: this.positionOffset, y: 0}
				});
			}else{
				this.list.position({
					relativeTo: this.maskDiv,
					position: 'topLeft',
					edge: 'topLeft',
					offset: {x: 0, y: this.positionOffset}
				});
			}
		}
		this.isAnimating=false
	},
			
	jumpToPage:function(a){}

});

MA.SliderItem = new Class({

	html:"List items must be populated with data; it can be an HTML object or HTML as a string.",
		
	initialize:function(a){
		if(a){
			this.html=a;
		}
	},
		
	render:function(){
		if(typeof(this.html)=="string"){
			var a=new Element("li");
			a.innerHTML=this.html;
		} else { 
			var a=new Element("li");
			a.grab(this.html);
		}
			
		return a
	}
});