﻿function format_listen(id, spalten) {

	var test = id + ' wurde nicht gefunden und soll auf ' + spalten + ' Spalten sortiert ...';
	// alert(test);
	if(!$ES(id)) { alert(test); }
	
		// Formatiere Produktliste Boxen
		var size_old = 0
		// var spalten = 3
		var zeilen = 3
		var least = 1
		var zaehler = 1
		var box_werte = new Array();
		var box_werte_new = new Array();
		
		$$(id).each(
		function(liste){
			$ES("li", liste).each(
				function(zeile,i){
				var sizes = zeile.getCoordinates();
				var size_new = sizes["height"];
				box_werte[i] = sizes["height"];
				if(size_old < size_new) { size_old = size_new; }
				if ( i % spalten == 0 ) { zeile.addClass( "odd" ); }
				// zeile.appendText(sizes["height"]);
				// vater.getElement('p.content').firstChild.nodeValue = vater.getLast().firstChild.nodeValue.substr(0,100) + " ...";
				if($('div.lupe')) {
					zeile.getElement('div.lupe a').setProperty('title', zeile.getElement('h3').firstChild.nodeValue.substr(0,50) + " ...");
				}
				zaehler++
				}
			);
		});
		for (var i = 0; i < zaehler; i=i+spalten) {
			for (var a = (i+0); a < (i+spalten); a++) { if (least < box_werte [a]) { least = box_werte [a] } }
			for (var b = (i+0); b < (i+spalten); b++) { box_werte_new[b] = least; }
			least = 1;
		}
		$$(id).each( function(liste){ $ES("li", liste).each( function(zeile,i){ zeile.setStyle('height', box_werte_new[i] + "px"); } ) })

}


// Fadeover Productimages
Fx.FadeHoverLink = Fx.Base.extend({
	initialize: function(el, options) {
		this.element = $(el);
		// this.setOptions({});

		/*
		this.fadeElement = new Fx.Style(this.element,'opacity',options).set(0.8);

		this.element.addEvent('mouseover', function() { this.fadeUp(); }.bind(this) );
		this.element.addEvent('mouseout', function() { this.fadeDown(); }.bind(this) );
		*/

		window.addEvent('unload', function() { delete this; }.bind(this) );
	},

	fadeUp: function(){
		this.fadeElement.clearTimer();
		this.fadeElement.custom(0.8,1);
	},

	fadeDown: function(){
		this.fadeElement.clearTimer();
		this.fadeElement.custom(1,0.7);
	}

});


// Headeranimation
var Slideshow = new Class({
	setOptions: function(options){
		this.options = {
			firstPeriod: 1000,
			otherPeriod: 7000,
			fxDuration: 1000,
			mediaBufferId: 'bu'
		}
		Object.extend(this.options, options || {});
	},
	initialize: function(updateId, parent, imagearray, options){
		this.updateId = updateId;
		this.imagearray = imagearray;
		this.parentObj = parent;
		this.setOptions(options);
		this.currentShow = (this.options.startingPlace) ? this.options.startingPlace : 1;
		this.fxMethod = (this.options.fxMethod) ? this.options.fxMethod : 'opacity';
		this.fxStart = (this.options.fxStart) ? this.options.fxStart : 1;
		this.fxEnd = (this.options.fxEnd) ? this.options.fxEnd : 0;
		this.preload(0);
	},
	createOverlay: function() {
		if ($(this.options.mediaBufferId)) $(this.options.mediaBufferId).remove();
		var secondImg = new Element('img');
		secondImg.id = this.options.mediaBufferId;
		this.parentObj.adopt(secondImg);
	},
	preload: function(count) {
		if (count < this.imagearray.length) {
			var temp = new Element('img');
			temp.onload = function() { this.preload((count+1)); }.bind(this);
			temp.src = this.imagearray[count];
		}
		else this.timerAction(true);
	},
	timerAction: function(first) {
		this.timer = $clear(this.timer);
		this.timer = (first) ? this.transition.delay(this.options.firstPeriod, this) : this.transition.delay(this.options.otherPeriod, this);
	},
	transition: function() {
		this.createOverlay();
		this.currentShow = ((this.currentShow+1) > this.imagearray.length) ? 1 : ++this.currentShow;
		$(this.options.mediaBufferId).src = this.updateId.src;
		this.updateId.src = this.imagearray[this.currentShow-1];
		var effect = new Fx.Style($(this.options.mediaBufferId), this.fxMethod, {duration: 1000});
		effect.custom(this.fxStart,this.fxEnd);
		this.timerAction(false);
	}
});



var prev_tabcontent = null;

function show_content(id) {
	prev_tabcontent = id;

	var dds_id = id + '_content';
	var dds = 'dd#' + dds_id;
	var li_id = 'li#' + id;
	
	if($('nav_tab')) {
		$$("ul#nav_tab").each(
			function(liste){
				$ES("li", liste).each(function(zeile,i){ zeile.removeClass('active'); });
		});
		$$(li_id).addClass('active');
	}

	if($(dds_id)) {
		$$('dl.tabcontent dd').setStyle('display', 'none');
		$$(dds).setStyle('display', 'inline');
	}

}

function showAllContent(){
	$$('dl.tabcontent dd').setStyle('display', 'inline');
} // function

function hideAllContent(){
	$$('dl.tabcontent dd').setStyle('display', 'none');
} // function

function printHolePage(){
	showAllContent();
	
	try{ document.getElementById('prnt-logo').style.display = 'block'; }catch(e){}

	try{ document.getElementById('topmenu').style.display = 'none'; }catch(e){}
	try{ document.getElementById('header').style.display = 'none'; }catch(e){}
	try{ document.getElementById('main_nav').style.display = 'none'; }catch(e){}
	try{ document.getElementById('zoom').style.display = 'none'; }catch(e){}
	try{ document.getElementById('product_switcher').style.display = 'none'; }catch(e){}
	try{ document.getElementById('filesystem').style.display = 'none'; }catch(e){}
	try{ document.getElementById('box_rec').style.display = 'none'; }catch(e){}

	window.print();
	setTimeout(restorePage, 2500);
} // function


function restorePage(){
	hideAllContent();

	try{ document.getElementById('prnt-logo').style.display = 'none'; }catch(e){}

	try{ document.getElementById('topmenu').style.display = 'block'; }catch(e){}
	try{ document.getElementById('header').style.display = 'block'; }catch(e){}
	try{ document.getElementById('main_nav').style.display = 'block'; }catch(e){}
	try{ document.getElementById('zoom').style.display = 'block'; }catch(e){}
	try{ document.getElementById('product_switcher').style.display = 'block'; }catch(e){}
	try{ document.getElementById('filesystem').style.display = 'block'; }catch(e){}
	try{ document.getElementById('box_rec').style.display = 'block'; }catch(e){}

	show_content(prev_tabcontent);
} // function



// Startfunktion
window.onDomReady(function(){

var location_hash = document.location.hash;
var section = document.location.hash.split('#')[1];

var fs = Cookie.get('fs');
if(fs == 'minus') { $E('body').setStyle('font-size', 'x-small'); }
else if(fs == 'neutral') { $E('body').setStyle('font-size', 'small'); }
else if(fs == 'plus') { $E('body').setStyle('font-size', 'medium'); }

if($('zoom')) {
	$$('li a.minus').addEvent( 'click', function() {
		$E('body').setStyle('font-size', 'x-small');
		Cookie.set("fs", "minus", {duration: false});
	})
	$$('li a.neutral').addEvent( 'click', function() {
		$E('body').setStyle('font-size', 'small');
		Cookie.set("fs", "neutral", {duration: false});
	})
	$$('li a.plus').addEvent( 'click', function() {
		$E('body').setStyle('font-size', 'medium');
		Cookie.set("fs", "plus", {duration: false});
	})
}

// Verschiebe topmenu
$('topmenu').injectBefore('header');
$('topmenu').setStyle('display', 'block');

// Verschiebe main_navi
$('main_nav').injectBefore('twocols');
$('main_nav').setStyle('display', 'block');

// Formatiere Pager
$$("div.pager table").each( function(zeile){		
	var spalte = $ES("td", zeile);
	var anzahl = spalte.length - 1;
	spalte[0].addClass( "first" );
	spalte[anzahl].addClass( "last" );
});

// Input Field von Suchmaschine füllen
$ES('input.cl').addEvents({
	'focus': function () {if(this.value==this.defaultValue) this.value=''; return false;},
	'blur': function () {if(this.value=='') this.value = this.defaultValue; return false;}
});

// Faderscript
$$('img.bv').each(
	function(el) {
		el.fl = new Fx.FadeHoverLink(el,{duration:500});
	}
);

// Formatiere die Listen
if($$('prodlist')) {
	format_listen('ul#prodlist', 3);
}

if($('letzte_produkte')) {
	format_listen('ul#letzte_produkte', 4);
}

if($('types_diese_produkte')) {
	format_listen('ul#types_diese_produkte', 3);
}

if($('types_spareparts')) {
	format_listen('ul#types_spareparts', 3);
}

if($('types_replacementparts')) {
	format_listen('ul#types_replacementparts', 3);
}

if($('types_other_blades')) {
	format_listen('ul#types_other_blades', 3);
}

if($('types_mounted_in')) {
	format_listen('ul#types_mounted_in', 3);
}

if($('types_variants')) {
	format_listen('ul#types_variants', 3);
}

if($('prodlist_empfehlung')) {
	format_listen('ul#prodlist_empfehlung', 3);
}

if($('clist')) {
	format_listen('ul#clist', 3);
}

if($('citems')) {
	format_listen('ul#citems', 3);
}

if($('latest_home')) { format_listen('ul#latest_home', 3); }

if($('kwick')) {

	// var szNormal = 208, szSmall = 152, szFull = 264;
	var szNormal = 205, szSmall = 152, szFull = 259;
	var kwicks = $$("#kwick .kwick");
	
	var fx = new Fx.Elements(kwicks, {wait: false, duration: 500, transition: Fx.Transitions.Back.easeOut});
	kwicks.each(function(kwick, i) {
		kwick.addEvent("mouseenter", function(event) {
			var o = {};
			o[i] = {width: [kwick.getStyle("width").toInt(), szFull]}
			kwicks.each(function(other, j) {
				if(i != j) {
					var w = other.getStyle("width").toInt();
					if(w != szSmall) o[j] = {width: [w, szSmall]};
				}
			});
			fx.start(o);
		});
	});
	 
	$("kwick").addEvent("mouseleave", function(event) {
		var o = {};
		kwicks.each(function(kwick, i) {
			o[i] = {width: [kwick.getStyle("width").toInt(), szNormal]}
		});
		fx.start(o);
	});

}


// Sind wir auf einer Produktseite - gibt es eine Tabnavigation
if($('nav_tab')) {
	
	if($('information')) {
		show_content('information');
	}

	// information anzeigen
	if($('information')) {
		$('information').addEvent( 'click', function() {
			show_content('information');
		});
	}

	// details anzeigen
	if($('details')) {
		$('details').addEvent( 'click', function() {
			show_content('details');
			$$('dd.csc-textpic-caption').setStyle('display', 'inline');
		});
	}

	// other_blades anzeigen
	if($('other_blades')) {
		$('other_blades').addEvent( 'click', function() {
			show_content('other_blades');
		});
	}

	// mounted_in anzeigen
	if($('mounted_in')) {
		$('mounted_in').addEvent( 'click', function() {
			show_content('mounted_in');
		});
	}

	// other_videos anzeigen
	if($('other_videos')) {
		$('other_videos').addEvent( 'click', function() {
			show_content('other_videos');
		});
	}

	// spareparts anzeigen
	if($('spareparts')) {
		$('spareparts').addEvent( 'click', function() {
			show_content('spareparts');
		});
	}

	// replacementparts anzeigen
	if($('replacementparts')) {
		$('replacementparts').addEvent( 'click', function() {
			show_content('replacementparts');
		});
	}

	// variants anzeigen
	if($('variants')) {
		$('variants').addEvent( 'click', function() {
			show_content('variants');
		});
	}

	// other_blades_content anzeigen
	if($('show_klingen')) {
		$('show_klingen').addEvent( 'click', function() {
			show_content('other_blades');
		});
		$('show_klingen_img').addEvent( 'click', function() {
			show_content('other_blades');
		});
	}
	
	// other_spare_parts anzeigen
	if($('show_spare_parts')) {
		$('show_spare_parts').addEvent( 'click', function() {
			show_content('spareparts');
		});
		$('show_spare_parts_img').addEvent( 'click', function() {
			show_content('spareparts');
		});
	}

	// other_blades_content anzeigen
	$('show_all').addEvent( 'click', function() {
		$$('dl.tabcontent dd').setStyle('display', 'none');
		$$('dl.tabcontent dd').setStyle('display', 'inline');
	});

}


// Imagegallery
if($('he_pro')) {
	// var imagearray = ['fileadmin/_he_img/logo_01.jpg','fileadmin/_he_img/logo.jpg','fileadmin/_he_img/logo_02.jpg'];
	var imagearray = ['fileadmin/_he_img/116001.jpg','fileadmin/_he_img/119001.jpg','fileadmin/_he_img/101899.jpg','fileadmin/_he_img/110000.jpg','fileadmin/_he_img/122001.jpg']; // 19. Jan 2010
	var mastheadslides = new Slideshow($('img_id'), $('he_pro'), imagearray, { fxMethod: 'opacity'} );
}


if($('pf')) {
	$$("dl#pf").each(
		function(liste){
			$ES("dt", liste).each(function(zeile,i){
				var style = 'l' + (i + 1);
				var text = (i + 1) + '.';
				zeile.addClass(style);
				zeile.setHTML(text);
			});
	});
}

if($('pfwahl')) {
    $ES("li.pfliste").each(
		function(zeile){
			var link = zeile.getElement('a.pfbild');
			var bild = zeile.getElement('a.pfbild img');
			var el = new Element('div').injectAfter(link);
			var name = new Element('a').setHTML(bild.alt).injectInside(el);
			name.setProperties({
				href: link.getProperty('href'),
				alt: link.getProperty('alt')
			});
			el.addClass('ftinto');
			el.setOpacity(0.7);
		}
	);
}



if ($('ShowKontaktNotepad')) {
	$('ShowKontaktNotepad').addEvent('click', function(e) {
		e = new Event(e).stop();
		var number = Math.floor(Math.random()*1000000);
		var url = '1946.html?no_cache=1&notepad=1&random=' + number;
		var notepadloader = $('ajaxLoader');
		new Ajax(url, {
			method: 'get',
			onRequest: function() {
				notepadloader.setHTML(' ".tslib_uebersetzungen::get_uebersetzung(26277)." ');
				notepadloader.style.display = 'inline';
			},
			onComplete: function() {
				notepadloader.setHTML('&nbsp;');
				notepadloader.style.display = 'none';
				// var sizes = $('notepad').getSize(); // getPosition(); // .getSize(); getCoordinates()

				var page_size = $('pagewidth').getPosition().x; // $('pagewidth').getStyle('width').toInt();
				// var notepad_height = $('notepad').getCoordinates().height;
				var notepad_width = $('notepad').getStyle('width').toInt();
				var window_width = Math.floor((getWidth()-notepad_width) / 2 - page_size);
				// var window_height = Math.floor((getHeight() - notepad_height) / 2);

				$('notepad').setStyle('left', window_width);
				// $('notepad').setStyle('top', window_height);
				$('notepad').setStyle('top', window.getScrollTop());
				
				// $('test').setProperty('left', ($('test').getCoordinates().left)+10);
				// $('link').setHTML($('test').getCoordinates().left);

				// alert(window_width);
				$('notepad').setStyle('display', 'inline');
			},
			update: $('notepad_content')
		}).request();
	});
}


if ($('manageNotepad')) {
	$('manageNotepad').addEvent('submit', function(e) {
		new Event(e).stop();
		var notepadStatus = $('notepad_status');
		this.send({
			onRequest: function() {
				notepadStatus.setHTML(' working ... ');
			},
			onComplete: function (reg) {
				notepadStatus.setHTML('&nbsp;');
				$('notepad_content').setHTML(reg);
			}
		});
	});
}


if ($('send_notepad_with_contactform')) {
	$('send_notepad_with_contactform').addEvent('submit', function(e) {
		new Event(e).stop();
		var notepadStatus = $('notepad_status');
		this.send({
			onRequest: function() {
				$('manageNotepad').send({
					onRequest: function() {
						notepadStatus.setHTML(' saving ... ');
					},
					onComplete: function (reg) {
						notepadStatus.setHTML(' relocating ... ');
					}
				});
			},
			onComplete: function (reg) {

				var Language = 0;

				$('send_notepad_with_contactform').getFormElements().each(function(el){
					var name = el.name;
					var value = el.getValue();

					if (name == 'language') {
						Language = value;
						return;
					}

				});

				$('notepad').setStyle('display', 'none');

				location.href="contact_form.html?no_cache=1&L=" + Language;

			}
		});

	});

}










	if ($('notepad')) {

		if ($('list_notepad_entries')) {
			$('send_notepad_with_contactform').setStyle('display', 'none');
		}

		$('notepadClose').addEvent('click', function() {

			$('notepad').setStyle('display', 'none');
			
			if ($('list_notepad_entries')) {
				// alert('list_notepad_entries ist vorhanden');
				var number = Math.floor(Math.random()*1000000);
				var url = '1946.html?no_cache=1&notepad=2&random=' + number;
				var notepadloader = $('ajaxLoader');
				new Ajax(url, {
					method: 'get',
					update: $('list_notepad_entries')
				}).request();
			}
			
		});

		var draggables = $$('div.wd');
		draggables.each(function(el){
			el.makeDraggable({handle: el.getElementsBySelector('.wd_hd')[0]});
		});
	}



}); // Ende window.onDomReady(function(){


// Suckerfisher ... for bloody IE
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			// alert('mo: ' + this);
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}

if (window.attachEvent) window.attachEvent("onload", sfHover);
