////////////////////////////////////////////
// PresentationBox
// author: Dominik Scholz <info@go4u.de>
// copyright: go4u.de Webdesign
////////////////////////////////////////////

var presentationbox = {

	_box: null,
	_items: null,
	_currentItem: 0,
	_scrolling: false,
	_scrollPos: 0,
	
	hover: function(obj)
	{
		obj.pDirection = -1;
		if (!obj.pImage) obj.pImage = this._getImage(obj);
		if (!obj.pPercent) obj.pPercent = 1;
		if (!obj._animated) this._animate.bind(obj)();
	},
	
	blur: function(obj)
	{
		obj.pDirection = 1;
		if (!obj.pImage) obj.pImage = this._getImage(obj);
		if (!obj.pPercent) obj.pPercent = 0;
		if (!obj._animated) this._animate.bind(obj)();
	},
	
	next: function()
	{
		this._initBox();
		this._currentItem++;
		if (this._currentItem >= this._items.length) this._currentItem = 0;
		if (!this._scrolling) this._scroll();
	},
	
	previous: function()
	{
		this._initBox();
		this._currentItem--;
		if (this._currentItem < 0) this._currentItem = this._items.length - 1;
		if (!this._scrolling) this._scroll();
	},
	
	_initBox: function()
	{
		if (this._box) return
		this._box = document.getElementById("project-items");
		this._items = this._getChildrenByClassName(this._box, "project-item");
	},
	
	_scroll: function()
	{
		var delta = this._currentItem * 400 - this._scrollPos;
		this._scrollPos += delta * 0.15;
		this._box.style.left = -this._scrollPos + "px";
		
		this._scrolling = Math.abs(delta) >= 0.6;
		
		if (this._scrolling) window.setTimeout(this._scroll.bind(this), 25);
		else this._box.style.left = -400 * this._currentItem + "px";
	},
	
	_animate: function()
	{
		this._animated = false;
		
		if (this.pDirection > 0) {
			if (this.pPercent >= 1) return;
			this.pPercent += .05;
		} else {
			if (this.pPercent <= 0) return;
			this.pPercent -= .05;
		}
		
		var width = presentationbox._magic(this.pPercent) * 135 + 255;
		this.pImage.style.width = width + "px";
		window.setTimeout(presentationbox._animate.bind(this), 25);
		this._animated = true;
	},
	
	_getImage: function(obj)
	{
		return this._getChildrenByClassName(obj, "project-image")[0];
	},
	
	_getText: function(obj)
	{
		return this._getChildrenByClassName(obj, "project-text")[0];
	},
	
	_getChildrenByClassName: function(obj, name)
	{
		var result = [];
		var children = obj.childNodes;

		for (var i = 0; i < children.length; i++)
		{
			var child = children[i];
			if (child.className == name) result.push(child);
		}
		
		return result;
	},
	
	_magic: function(pos)
	{
		return ((-Math.cos(pos * Math.PI) / 2) + 0.5);
	}

};
