/**
* 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.topnavi().startStyle();
	$$(".dynamicRasterPage ul li, #resultList ul li").each(function(item){
		item.addClassName("hasJs");	
	});
	ppInit.makeFullLink($$(".dynamicRasterPage ul li"), function(){
		location.href = this.select("a")[0].href;
	});
	ppInit.makeFullLink($$("#resultList ul li"), function(){
		var form = this.select("form");
		if(form.length > 0) {
			form[0].submit();
		} else {
			location.href = this.select("a")[0].href;
		}
	});
}

/**
* Namespace fuer die Initialisierungen
* @author	Florian Weber (florian.weber@pixelpark.com)
*/
ppInit = {


	/**
	* Manipuliere die Hauptnavigation. Sobald die Eintraege zu breit werden stelle die letzten
	* Elemente in einer neuen Zeile dar.
	* @author	Florian Weber (florian.weber@pixelpark.com)
	*/
	topnavi: function() {
		if($('topnavi')) {
			this.$topnavi = $('topnavi');
			/**
			* Konfiguration der TopNavi
			*/
			this.config = function() {
				this.totalAvailWidth = $('topnavi').getStyle('width');
				return this;
			}
			this.getEntryCount = function(){
				var entryCount = $$('#topnavi li').length;
				return entryCount;
			}

			this.getEntriesWidth = function() {
				widthComplete = 0;
				var list2 = new Array();
				totalAvailWidth = ppInit.topnavi().config().totalAvailWidth;

				allListElements = $$('#topnavi li');
				allListElements.each(function(ele) {
					widthComplete += parseInt(ele.getStyle('width')) + 3;
					if(widthComplete > parseInt(ppInit.topnavi().config().totalAvailWidth)) {
						list2[list2.length] = ele;
					}
					
				})
				this.widthComplete = widthComplete;
				this.list2 = list2;
				return this;
			}
			/**
			* Wenn die breite der LIs breiter als der zur verfuegungstehende Platz ist, zaehle die extra-Elemente und
			* parse Sie in das Element #extraRow
			*/
			this.startStyle = function() {
				this.$topnavi.addClassName("hasJS");
				var w = this.$topnavi.getWidth() - this.$topnavi.select("ul")[0].getWidth() + $('lastItem').getWidth();

				//special MAC OS test
				if(navigator.oscpu != null && navigator.oscpu.indexOf("Mac") != -1)
				{
					w-=1;
				};

				//special ie9 test
				if(parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 9)
				{
					w-=1;
				};

				$('lastItem').setStyle({'width': w + "px"});
			}
			return this;
		}

	},

	makeFullLink: function(list, callback){
		list.each(function(item){
			item.observe("click", callback.bindAsEventListener(item));
		});
	},
	getHash: function(){
		var hash = location.hash;
		return (hash.indexOf('#') != -1) ?  hash.substr(1) : hash;
	},
	trimTexts: function(selectDescs){
//		return;
		$$(selectDescs).each(function(item){
			if(!item.firstChild) return;
			function getImgCurPosY(){
				return ce.cumulativeOffset()[1];
			}
			function trimText(){
//			var imgCurPosY = img.cumulativeOffset()[1];
				item.update(item.firstChild.data.substr(0, item.firstChild.data.lastIndexOf(" ")));
				if(getImgCurPosY() >= imgMinViewPortPosY){
					trimText();
				}
			}
			var siblingPrize =  item.siblings()[1];
			var img = new Element('img', {'src': '../../master/img/NOIMAGE.gif'});
			var ce = new Element('span', {'style': "background: #FF0"}).update(" ...");
			var outerDesc = new Element('div');
			outerDesc.addClassName("outerDesc");
//			var desc = item.select(".desc")[0];
			item.wrap(outerDesc);
			outerDesc.insert(ce);
			var imgMinViewPortPosY = siblingPrize.cumulativeOffset()[1];
			if(typeof item.firstChild.data != "undefined" && getImgCurPosY() > imgMinViewPortPosY){
			} else {
				var data = "";
				item.childElements().each(function(child){
					data+=child.firstChild.data;
				});
				item.update(data);
			}
			trimText();
			trimText();
//			if(getImgCurPosY() > imgMinViewPortPosY){
//				trimText();
//			}
//			console.debug(item, imgMinViewPortPosY, siblingPrize.cumulativeOffset(), ce.cumulativeOffset());
			item.update(item.firstChild.data+"...");
//			ce.remove();
			outerDesc.replace(item);

		});
	}
};

Event.observe(window, 'load', function(){
    initMandantEvents();
});


/**
* Kürzt den Beschreibungstext dynamisch auf die passende Höhe
* @author	Simon Harjes (simon.harjes@hmmh.de)
*/
function cutDescriptionText() {
	var products = document.getElementById('productlist').getElementsByTagName('li');
	var i_max = products.length;
	for (var i = 0; i < i_max; i++) {
		var divNodes = products[i].getElementsByTagName('span');
		var j_max = divNodes.length;
		for (var j = 0; j < j_max; j++) {
			if (divNodes[j].className === 'desc') {
				var descNode = divNodes[j];
				break;
			}
		}
		if (descNode) {
			var descWrapperHeight = descNode.parentNode.getHeight();
			descWrapperHeight -= descNode.parentNode.parentNode.getElementsByTagName('h3')[0].getElementsByTagName('a')[0].getHeight();
			if (descNode.parentNode.nextSibling.nextSibling && descNode.parentNode.nextSibling.nextSibling.getElementsByTagName('span')[0].innerHTML !== '') {
				descWrapperHeight -= 15;
			}
			while (descNode.getHeight() > descWrapperHeight) {
				var description = descNode.innerHTML;
				descNode.innerHTML = description.substring(0, description.lastIndexOf(' '));
			}
			var description = descNode.innerHTML;
			description = description.substring(0, description.lastIndexOf(' '));
			descNode.innerHTML = description.substring(0, description.lastIndexOf(' ')) + ' ...';
		}
	}
}

