/**
* Mandantenspezifische Scripte
* @requires prototype1.6, core.js
* @author	Florian Weber (florian.weber@pixelpark.com)
*/

/**
* Ruft die initial Events und Pruefungen fuer die Seite auf
* @author	Florian Weber (florian.weber@pixelpark.com)
*/
function initMandantEvents(){
	ppInit.moreLink();
}

/**
* Namespace fuer die Initialisierungen
* @author	Florian Weber (florian.weber@pixelpark.com)
*/
ppInit = {

	/**
	* Auf der Detailseite steht ein mehr-Link - Bei klick vergrößert sich das div mit der Artikelbeschreibung
	 * @author	Florian Weber (florian.weber@pixelpark.com)
	 */
	moreLink: function() {
		if(ele = $$('.moreLink')[0]) {
			Event.observe(ele, 'click', function(){
				if($$('.artikeltext')) $$('.artikeltext span')[0].setStyle('height: auto;');
				ele.addClassName('inv');
			})
			XPLAIN.core.disableHref(ele, true);
		}
	}
}

function checkIKnow(thisObj){
	if(thisObj.checked){
		document.getElementById("submitForward").removeAttribute("disabled");
		document.getElementById("submitForward").style.display = "block";
	} else {
		document.getElementById("submitForward").setAttribute("disabled", "disabled");
		document.getElementById("submitForward").style.display = "none";

	}
}


var Treadmill = Class.create();
Treadmill.prototype = {
	container : null,
	viewPortItems : 6,
	itemWidth : 110,
	items: null,
	effMove : null,
	counter: 0,
	isStopped: false,
	direction: 1,
	initialize: function(container, content) {
		if (!$(container)) {
			throw(container + " doesn't exist!");
		} else {
			this.container = $(container);
		}
		this.items = content;
		this.contentBox = this.container.select(".treadmillContent")[0];
		this.container.observe('mouseover', this._stop.bindAsEventListener());
		this._fill(content);
		//this._start(1);
	},
	_fill: function(content) {
		var contentString = "";
		var $this = this;
		content.each(function(item){
			contentString+= '<li><a href="'+item.ahref+'"><img src="'+item.image+'" alt="" /><span class="details">'+item.name+'<br /><span class="price">'+item.price+'</span></span></a></li>';
		})
		this.contentBox.update(contentString);
//		this.contentBox.select('li').each(function(item){
//			item.observe('mouseover', $this._stop.bind(null, $this))
//			item.observe('mouseout', $this._mouseOut.bind(null, $this))
//		})
	},
	_start: function(direction) {
		this.isStopped = false;
		var $this = this;
		var dur = 1;
		var moveLength = -1*direction*this.itemWidth;
		this.effMove = new Effect.Move(this.contentBox,{x : moveLength,y : 0 , duration:dur, afterFinish:$this._next.bind(null,$this, direction)});
	},
	_stop: function() {
		var $this = arguments[0];
		$this._start($this.direction);
	},
	_mouseOut: function() {
		var $this = arguments[0];
		console.debug($this);
		$this._start();
	},
	_getMaxCount: function() {
		return this.items.length-this.viewPortItems;
	},
	_next: function() {
		var $this = arguments[0];
		$this.counter++;
		var direction = arguments[1];
		if($this.counter>=$this._getMaxCount()) {
			$this.counter = 0;
			direction = direction*-1;
		}
		$this._sleep(2000);
		if(!$this.isStopped) {
			$this.direction = direction;
			$this._start(direction);
		}
		//options.$this._start(options.direction);
	},
	_sleep: function(ms){
		var now=(new Date()).getTime();
		var end=now+ms;
		while((new Date()).getTime()<end){};
	}
}

hasCheckWarenkorbWAError = false;
function checkWarenkorbWA(thisObj, minAmount, url){
	var inp = $(thisObj).ancestors()[2].select('.anzahlCol input')[0];
	if(inp.value < minAmount) {
		var p = new Element("p", {"style": "font-weight:bold;color:red;padding-top: 10px;"}).update(locations.errorWAMinAmount.replace(/###value###/, minAmount));
		if(!hasCheckWarenkorbWAError) {
			$('warenkorbform').insert({before: p});
			hasCheckWarenkorbWAError = !hasCheckWarenkorbWAError;
		}
	}else {
		document.location = url;
	}
}

/**
 * Prototype onload-Event
 */
Event.observe(window, 'load', function(){
    initMandantEvents();
});