// JavaScript Document

var slider = {
	initialize: function(element, options) {
		this.setOptions({
			element : element,
			elementSelector: "img",	
			width :"600",
			height :"320",
			imgclass : "thumbnail",
			numItem : 1
		}, options);
		
		this.currentItem = 0;  					  //trenutna pozicija
		this.numItem = this.options.numItem;      // število elementov za prikaz
		this.maxItem = 0;      					  //število vseh elementov
		this.IndextData = this.ProjectData = [];
		
		this.initReloder();		
	},
	
	initReloder: function () {
		this.ProjectData = this.populateProject( this.options.element );
		
		if( this.numItem > this.maxItem) {
			this.numItem = this.maxItem;
		}
	
		if( this.maxItem > 1 )
		   this.arrow();
		   
		this.goTo(0);
	},
	
	
	
	arrow: function() {
		element = this.options.element;
				
			var leftArrow = new Element('a').addClass('left').addEvents({
				'click': function () {
					this.previtem();	
				}.pass($('prev'), this),
			
				'mouseover': function () {
					this.addClass('left_active');
				},
				
				'mouseout': function () {
					this.removeClass('left_active');
				}
				
			}).injectTop(element);
			
			var rightArrow = new Element('a').addClass('right').addEvents({
				'click': function () {
					this.nextitem();	
				}.pass($('next'), this),
			
				'mouseover': function () {
					this.addClass('right_active');
				},
				
				'mouseout': function () {
					this.removeClass('right_active');
				}
			
			}).injectInside(element);
			
			
	},
	
	populateProject: function( element ) {
		var data = [];
		options = this.options;
		element.getElements(options.elementSelector).each(function(el) {
																   
			elementDict = { 
				thumbnail: el
			};
			data.extend([elementDict]);
			el.remove();
		});

		this.maxItem = data.length;
		return data;
	},
	
	
	fillProject: function() {

		options = this.options;
		element = options.element; 
    	var currentImg;

		for(i=0; i<this.IndexData.length;i++)
		{		
				this.ProjectData[ this.IndexData[i] -1].thumbnail.injectTop(element);
		}
		
	},
	
	clearItem: function() {
		element = this.options.element; 
	
		element.getElements(this.options.elementSelector).each(function(el) {
			el.remove();
		});
			
		this.fillProject();
	},
	
	
	nextitem: function() {
		
		this.currentItem = this.currentItem + 1;
	
		if (this.currentItem > (Math.ceil(this.maxItem/this.numItem )) - 1) {
			this.currentItem = 0;
		}
		
		this.nextItem = this.currentItem;
		this.goTo(this.nextItem);
	},
	
	
	previtem: function() {

		this.currentItem = this.currentItem - 1;
		
		if (this.currentItem < 0) 
			this.currentItem = Math.ceil(this.maxItem/this.numItem ) -1;
			
		this.nextItem = this.currentItem;
	
		this.goTo(this.nextItem);
	},
	
	
	goTo: function(num) {
		var arrayIndex = [];
		
		for(i=1;  i <= this.numItem;i++) {
			
			if(this.maxItem >= (i + (num * this.numItem))) {
				arrayIndex[i-1] = i + (num * this.numItem);
			}
		}	
		
		this.IndexData = arrayIndex;
		this.clearItem();	
	}
	
	
}

slider = new Class(slider);
slider.implement(new Events);
slider.implement(new Options);