/**************************************
Created by EXP Team
http://www.feellove.eu
Copyright (C) 2007. all rights reserved.
**************************************/
var ThumbTable = new Class({

	getOptions: function(){
		return {
			thumbnailColumns: '2',
			thumbnailRows: '2',
			buttonNextLocation: 'none',
			buttonPrevLocation: 'none'
		};
	},
	
	initialize: function(element, hrefval, srcval, titleval, widthval, heightval, descval, bigimg, obj) {
		this.setOptions(this.getOptions(), obj);

		this.lastThumbImageIndex=0;
		this.lastThumbsOnCurrentPage=0;
		this.indexFrom=0;
		this.currPage=0;
		this.thumbsPerPage = this.options.thumbnailRows * this.options.thumbnailColumns;
		this.indexTo = (this.thumbsPerPage).toInt();
	
		this.obj = obj;
		this.hrefval = new Array();
		this.hrefval = hrefval;
		
		var firstRIndex = this.hrefval.length / this.thumbsPerPage;		
		this.pages = Math.ceil(firstRIndex);
		this.arr_length = this.hrefval.length;
		
		this.srcval = new Array();
		this.srcval = srcval;

		this.titleval = new Array();
		this.titleval = titleval;

		this.widthval = new Array();
		this.widthval = widthval;

		this.heightval = new Array();
		this.heightval = heightval;

		this.descval = new Array();
		this.descval = descval;
		
		this.bigimg = new Array();
		this.bigimg = bigimg
		
		this.menuDiv = $(element);				
		this.menuDiv.appendChild(this.defineNextButton('imagoNavNextLink'));
		this.menuDiv.appendChild(this.definePrevButton('imagoNavPrevLink'));
		this.firstLoad();
	},
	
	firstLoad: function() {
		if(this.indexFrom == 0 && this.indexTo < this.arr_length) {
			ElementHelper.hide(this.navPrevLink);
			ElementHelper.show(this.navNextLink);
		}
		else if(this.arr_length == this.indexTo && this.indexFrom > 0) {
			ElementHelper.show(this.navPrevLink);
			ElementHelper.hide(this.navNextLink);			
		}
		else {
			ElementHelper.hide(this.navPrevLink);
			ElementHelper.hide(this.navNextLink);		
		}

		var hrefval = this.getArrayRange(this.indexFrom, this.indexTo, this.hrefval);
		var srcval = this.getArrayRange(this.indexFrom, this.indexTo, this.srcval);
		var titleval = this.getArrayRange(this.indexFrom, this.indexTo, this.titleval);
		var widthval = this.getArrayRange(this.indexFrom, this.indexTo, this.widthval);
		var heightval = this.getArrayRange(this.indexFrom, this.indexTo, this.heightval);
		var descval = this.getArrayRange(this.indexFrom, this.indexTo, this.descval);		
		var bigimg = this.getArrayRange(this.indexFrom, this.indexTo, this.bigimg);

		this.menuDiv.appendChild(this.getCurrenThumbTable(hrefval, titleval, widthval, heightval, descval, srcval, bigimg));
		this.slideShowClass = new SlideShow('slideshow','slideshowThumbnail', this.obj);
		this.slideShowClass.play();
	},
	
	shiftNext: function() {
		this.indexFrom = this.indexTo;
		this.indexTo = this.indexFrom + this.thumbsPerPage;
		if(this.arr_length < this.indexTo) {
			this.indexTo = this.arr_length;
		}
		var hrefval = this.getArrayRange(this.indexFrom, this.indexTo, this.hrefval);
		var srcval = this.getArrayRange(this.indexFrom, this.indexTo, this.srcval);
		var titleval = this.getArrayRange(this.indexFrom, this.indexTo, this.titleval);
		var widthval = this.getArrayRange(this.indexFrom, this.indexTo, this.widthval);
		var heightval = this.getArrayRange(this.indexFrom, this.indexTo, this.heightval);
		var descval = this.getArrayRange(this.indexFrom, this.indexTo, this.descval);		
		var bigimg = this.getArrayRange(this.indexFrom, this.indexTo, this.bigimg);
		if(this.indexFrom == 0 && this.indexTo < this.arr_length) {
			ElementHelper.hide(this.navPrevLink);
			ElementHelper.show(this.navNextLink);
		}
		else if(this.arr_length > this.indexTo && this.indexFrom > 0) {
			ElementHelper.show(this.navPrevLink);
			ElementHelper.show(this.navNextLink);			
		}
		else if(this.indexTo >= this.arr_length && this.indexFrom > 0)  {
			ElementHelper.show(this.navPrevLink);
			ElementHelper.hide(this.navNextLink);		
		} 
		else {
			ElementHelper.hide(this.navPrevLink);
			ElementHelper.hide(this.navNextLink);				
		}
		this.slideShowClass.stop();
		$('imagoCurrentThumbTable').remove();
		this.menuDiv.appendChild(this.getCurrenThumbTable(hrefval, titleval, widthval, heightval, descval, srcval, bigimg));
		this.slideShowClass.reset();
		this.slideShowClass = new SlideShow('slideshow','slideshowThumbnail', this.obj);
		this.slideShowClass.play();
	},
	
	shiftPrev: function() {
		this.indexTo = this.indexFrom;
		this.indexFrom = this.indexTo - this.thumbsPerPage;
		if(this.indexFrom < 0) {
			this.indexFrom = 0;
		}
		var hrefval = this.getArrayRange(this.indexFrom, this.indexTo, this.hrefval);
		var srcval = this.getArrayRange(this.indexFrom, this.indexTo, this.srcval);
		var titleval = this.getArrayRange(this.indexFrom, this.indexTo, this.titleval);
		var widthval = this.getArrayRange(this.indexFrom, this.indexTo, this.widthval);
		var heightval = this.getArrayRange(this.indexFrom, this.indexTo, this.heightval);
		var descval = this.getArrayRange(this.indexFrom, this.indexTo, this.descval);
		var bigimg = this.getArrayRange(this.indexFrom, this.indexTo, this.bigimg);		
		
		if(this.indexFrom == 0 && this.indexTo < this.arr_length) {
			ElementHelper.hide(this.navPrevLink);
			ElementHelper.show(this.navNextLink);
		}
		else if(this.arr_length > this.indexTo && this.indexFrom > 0) {
			ElementHelper.show(this.navPrevLink);
			ElementHelper.show(this.navNextLink);			
		}
		else if(this.indexTo >= this.arr_length && this.indexFrom > 0)  {
			ElementHelper.show(this.navPrevLink);
			ElementHelper.hide(this.navNextLink);		
		} 
		else {
			ElementHelper.hide(this.navPrevLink);
			ElementHelper.hide(this.navNextLink);				
		}
		this.slideShowClass.stop();
		$('imagoCurrentThumbTable').remove();
		this.menuDiv.appendChild(this.getCurrenThumbTable(hrefval, titleval, widthval, heightval, descval, srcval, bigimg));
		this.slideShowClass.reset();
		this.slideShowClass = new SlideShow('slideshow','slideshowThumbnail', this.obj);
		this.slideShowClass.play();
	},

	getArrayRange: function(indexFrom, indexTo, fullArr) {
		var rezult = new Array();
		for(var i=indexFrom; i<indexTo; i++) {
			if(fullArr[i] != null) {
				rezult.push(fullArr[i]);
			}
		}
		return rezult;
	},
		
	defineNextButton: function(element) {
		var img = new Element('img');
		img.setProperty('src', this.options.buttonNextLocation);
		img.setStyle('border', '0');
		this.navNextLink = new Element('a');
		this.navNextLink.className = 'imago_navNext';
		this.navNextLink.setProperty('id', element);
		this.navNextLink.onclick = this.shiftNext.bind(this);
		this.navNextLink.appendChild(img);
		return this.navNextLink;
	},
	
	definePrevButton: function(element) {
		var img = new Element('img');
		img.setProperty('src', this.options.buttonPrevLocation);
		img.setStyle('border', '0');
		
		this.navPrevLink = new Element('a');
		this.navPrevLink.className = 'imago_navPrev';
		this.navPrevLink.setProperty('id', element);
		this.navPrevLink.onclick = this.shiftPrev.bind(this);
		this.navPrevLink.appendChild(img);
		return this.navPrevLink;
	},
	
	
	getCurrenThumbTable: function(hrefval, titleval, widthval, heightval, descval, srcval, bigimg) {

	var thumbTable = new Element("table");
		var thumbTableBody = new Element("TBODY");
		thumbTable.setProperty('border', '0');
		thumbTable.setProperty('id', 'imagoCurrentThumbTable');
		thumbTable.setProperty('class', 'imago_currentThumbTable');
		var counter = 0;
		for(i=0;i<this.options.thumbnailRows;i++) {
			if(hrefval[counter] == null) {
				break;
			}
			var tr = new Element('tr');
			for(j=0;j<this.options.thumbnailColumns;j++) {
				if(hrefval[counter] == null) {
					break;
				}			
				var td = new Element('td');
				if(this.hrefval[counter] != null) {
					var a = new Element('a');
					a.addClass('slideshowThumbnail');
					a.setProperty('href', hrefval[counter]);
					a.setProperty('title', titleval[counter]);
//					a.setProperty('rel', 'lightbox[atomium]');
					var input_hidden = new Element('input');
					input_hidden.setProperty('type', 'hidden');
					input_hidden.setProperty('style', 'width: ' + widthval[counter] + 'px; height: ' + heightval[counter] + 'px;');
					input_hidden.setProperty('value', descval[counter]);
					var img = new Element('img');
					img.setProperty('src', srcval[counter]);
					img.setProperty('alt', bigimg[counter]);
					a.appendChild(img);
					a.appendChild(input_hidden);
					td.appendChild(a);
					counter++;
				}
				tr.appendChild(td);
			}
			thumbTableBody.appendChild(tr);
		}
		thumbTable.appendChild(thumbTableBody);
		return thumbTable;
	}
});

ThumbTable.implement(new Options);
ThumbTable.implement(new Events);

//inspired by mooshow
var ImagoElement = new Class({
    initialize: function(){
    
    },
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth; 
	},
	getHeight: function(element) {
	   	element = $(element);
	   	return element.offsetHeight; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	},	
	hide: function(element) {
      	//element = $(element);
      	element.style.display = 'none';
  	},
  	show: function(element) {
      	//element = $(element);
      	element.style.display = 'inline';
  	},
	setOpacity: function(element,opacity) {
    	element = $(element);
    	element.style.opacity = opacity; 
	}
});

var ElementHelper = new ImagoElement();

