/**
 *	Created by Daniel Robenek 2009
 *	Multimage Version 1.00 
 *	Date: 27.7.2009
 *	Všechna práva vyhrazena. Bez souhlasu autora je užívání, kopírování tohoto, nebo části kódu zakázáno. 
 **/

function Multimage(container, defaultTab)
{	
	/*
		tabs.nullY : relativní nulová pozice
		tabs.xLeft: xová pozice tabu pokud má být vlevo
		tabs.xRight: xová pozice tabu pokud má být vpravo
		tabs.xCenter: xová pozice pokud není žádný vybrán
	*/
	this.Proxy = function(self, pfunc) { return function() { pfunc.apply(self, arguments); } }

	this.defaultTab = (defaultTab == null) ? null : defaultTab;
	this.container = $("#" + container); this.container.width = parseInt(this.container.css("width"));
	this.tabs = $("#" + container + " div"); for( var i = 0; i < this.tabs.length; i++ ) { this.tabs[i] = $(this.tabs[i]); this.tabs[i].width = parseInt(this.tabs[i].css("width"));}

	this.minXDistance = Math.floor(( this.container.width - this.tabs[0].width) / (this.tabs.length - 1));
	this.centerXDistance = Math.floor(this.container.width / this.tabs.length);
	
	for( var i = 0; i < this.tabs.length; i++ ) {
		this.tabs[i].nullY = (i == 0) ? 0 : (this.tabs[i-1].nullY - parseInt(this.tabs[i-1].css("height")) );
		this.tabs[i].xLeft = (i != this.tabs.length - 1) ? (i * this.minXDistance) : (this.container.width - this.tabs[i].width);
		this.tabs[i].xRight = this.container.width - (this.minXDistance * (this.tabs.length - i) );
		this.tabs[i].xCenter = i * this.centerXDistance;
	}

	this.showPage = function(page) {
		for( var i = 0; i < this.tabs.length; i++ ) {
			this.tabs[i].stop(true);
			if( i > page )
				this.tabs[i].animate( {"left": this.tabs[i].xRight}, "fast" );
			else
				this.tabs[i].animate( {"left": this.tabs[i].xLeft}, "fast" );
		}
	}
	
	this.showDefault = function() {
		if( this.defaultTab == null ) {
			for( var i = 0; i < this.tabs.length; i++ ) {
				this.tabs[i].stop(true);
				this.tabs[i].animate( {"left": this.tabs[i].xCenter}, "fast" );
			}
		} else this.showPage(this.defaultTab);
	}
	
	
	this.addEvents = function() {
		this.tabs.each(this.Proxy(this, function(index, obj){
			obj.mouseover(this.Proxy(this, function(){
				this.showPage(index);
			}));
		}));
		this.container.mouseout(this.Proxy(this, function(){
			this.showDefault();
		}));
	}

	this.init = function() {
		for( var i = 0; i < this.tabs.length; i++ ) {
			this.tabs[i].css("top", this.tabs[i].nullY);
			this.tabs[i].css("left", this.tabs[i].xCenter);
		}
		this.addEvents();
		this.showDefault();
	}
	this.init();
}
