/*

	filename:	/js/base.js
	version:	1.4 - 2007-026-27 - mkr

	author:		martin (dot) krause (at) gpm (dot) de 

	depends on : 
				evt.js 
				prototype.js 
				fontsize.js 
				styleTables.js

	1.4 - mkr: added function gotoProductPage() from 2003
	1.3 - mkr: added decorate(), setting h2.deko to uppercase
	1.2 - mkr: added getElementsByClassName(), created prepareDropdowns() from scratch, replacing addNavEvents()
	1.1 - mkr: introduced eventmanager, evt.js
*/

if (merz) { throw new Error(0,"merz already defined");}
else 
{
	var merz = {

		// config 
		// id 
		idNavMain: 'navMain',
		idContentMain: 'contentMain',
		// class
		cnDropDownShow: 'navDropDownShow',
		cnDropDownHide: 'navDropDownHidden',
		cnHasDropDown: 'dropdown',
		//misc 
		dropdownDisplayMs: 200,

		
		/*!
		@function merz.prepareDropdowns()
		@abstract prepares dropdowns by adding eventhandlers on navigaiton elements
		depends on : evt.js 
						evt.add(el, type, fn )
		@result nice dropdowns, features: delayed hide
		*/
		prepareDropdowns: function() {
			// add eenthandlers toggling dorpdowns on all elements (a class=cnHasDropDown) on main navigaiton (id=idNaMain)
			var _col = merz.getElementsByClassName('a',merz.cnHasDropDown,merz.idNavMain)
			var _n = _col.length;
			while(_n--)
			{
				evt.add(_col[_n],'mouseover',merz.foldOut)
				evt.add(_col[_n],'mouseout',merz.foldIn)
			}
			// add eventhandlers controlling timer on every dropdown-element (ul > a ) inside mainav, reason: evt gets fired
			var _col = merz.getElementsByClassName('ul',merz.cnDropDownHide,merz.idNavMain)
			var _n = _col.length;
			while(_n--)
			{
				var _kol = _col[_n].getElementsByTagName('a');
				var _m = _kol.length;
				while(_m--) {
					evt.add(_kol[_m],'mouseover',merz.clearDropdownTimer);
					evt.add(_kol[_m],'mouseout',merz.setDropdownTimer);
				}
			/*
			evt.add(_col[_n],'mouseover',merz.clearDropdownTimer);
			evt.add(_col[_n],'mouseout',merz.setDropdownTimer);
			*/
			}
		},

		
		/*!
		@function merz.foldOut()
		@abstract prepare dropdown show by clearing timer, calling merz.dropdown
		*/
		foldOut: function() { 
			merz.clearDropdownTimer();
			merz.dropdown(this);
			return false; 
		},
		
		/*!
		@function merz.foldIn()
		@abstract prepare dropdown hide by setting timer
		*/
		foldIn: function() { 
			merz.setDropdownTimer(); 
		},
	
		/*!
		@function merz.setDropdownTimer()
		@abstract setting timer, delay calling merz.dropdownHide
		*/
		setDropdownTimer: function(){
			merz.setDropdownTimer.timer = window.setTimeout(function() {
				merz.dropdownHide();
				},merz.dropdownDisplayMs);
		},
		
		/*!
		@function merz.clearDropdownTimer()
		@abstract if present clears timer 
		*/
		clearDropdownTimer: function(){
			if (merz.setDropdownTimer.timer) 
			{
				window.clearTimeout(merz.setDropdownTimer.timer);
			}
		},

		/*!
		@function merz.dropdown()
		@abstract hides all dropdowns (calling merz.dropdownHide()), hides selectboxes (calling toggleSelects.hide()) if necessary, shows desired dropdown by replacing classNames
		*/
		dropdown: function(me) {
			if(!me) { return false };
			merz.dropdownHide();
			// get el parent
			var myParent = me.parentNode;
			// replace selectboxes if we're dealing with ie
			if (toggleSelects.hide)
			{
				toggleSelects.hide();
			}
			// show the one we'd like to see
			merz.replaceNavClassName(this.cnDropDownHide,this.cnDropDownShow,myParent);
		},

		/*!
		@function merz.dropdownHide()
		@abstract hides all dropdowns, shows selectboxes (calling toggleSelects.show()) if necessary,
		*/
		dropdownHide: function() {
			// get main navigation container
			var elNavMain = document.getElementById(merz.idNavMain);
			// hide every dropdown
			merz.replaceNavClassName(this.cnDropDownShow,this.cnDropDownHide, elNavMain);
			// ie ? show selectboxes ...
			if (toggleSelects.show)
			{
				toggleSelects.show();
			}
		},

		/*!
		@function merz.replaceNavClassName(_cn,_cnNew,_navParent) 
		@param 	_cn: className being replaced
				_cnNew: new className
				_navParent: parent element
		@abstract replaces classNames inside navigation parent element, 
		*/
		replaceNavClassName: function(_cn,_cnNew,_navParent) {
			// set parent elemente, if empty use document body
			var _navParent = _navParent || document.body ;
			// grab elements, cycle 'em and replace all classnames instances
			var _col = _navParent.getElementsByTagName('ul');
			var _n = _col.length;
			while(_n--)
			{
				var _elcn = _col[_n].className;
				_elcn = _elcn.replace(_cn,_cnNew);
				_col[_n].className = _elcn;
			}
		},
		

		/*!
		@function merz.decorate()
		@abstract grabs h2.deko, transformingtext to uppercase
		*/
		decorate: function() {
			var _col = merz.getElementsByClassName('h2','deko',merz.idContentMain)
			var _n = _col.length;
			while(_n--) {
				var _txt = _col[_n].firstChild.nodeValue;
				_col[_n].firstChild.nodeValue = _txt.toUpperCase();
			}
		},
		
		/*!
		@function merz.getElementsByClassName(_tag,_cn,_parent)
		@param 	_tag: html-element
				_cn: className we're loking for 
				_parent: parent element, either id or element
		@abstract returns an array with all elements (_tag) with className (_cn) inside parent elmenet (_parent)
		*/
		getElementsByClassName: function(_tag,_cn,_parent) {
			if (!_cn) { return false;}
			var _tag = _tag || "*";
			var _parent = ( (typeof(_parent) == 'string') ? document.getElementById(_parent) : _parent ) || document;
			var _els = [];
			var _col = ( _tag == '*' && document.all ) ? document.all :_parent.getElementsByTagName(_tag);
			var _n = _col.length;
			while(_n--)
			{
				if(_col[_n].className != ''){
					if(_col[_n].className.strstr(_cn)) {_els.push(_col[_n]);}
				}
			}
			return _els;
		},

		/*!
		@function merz.enableToolbox(_parentId,[id,...])
		@param 	_parentId: parent Element, aka toolbox
				array[id,..] array condainint ids of multiple elements being altered
		@abstract adds eventhandlers for fontsize and print elements
		*/
		enableToolbox: function() {
			var _el = document.getElementById(arguments[1][0]);
			_el.onclick=function(){fontsize.change(['contentMain'],'-1'); return false;}
			_el = document.getElementById(arguments[1][1]);
			_el.onclick=function(){fontsize.change(['contentMain'],'0'); return false;}
			_el = document.getElementById(arguments[1][2]);
			_el.onclick=function(){fontsize.change(['contentMain'],'+1'); return false;}
			_el = document.getElementById(arguments[1][3]);
			_el.onclick=function(){merz.printWindow(); return false;}
			_el = document.getElementById(arguments[0]);
			_el.style.display = "block"
		},
	
		/*!
		@function merz.printWindow()
		@abstract window.print
		*/
		printWindow: function () {
			if (window.print) window.print();
		}
	}
}


// open pop-up window, deprecated
function windowOpener(winUrl,winWidth,winHeight,winName,winX,winY,winScrollbars,winLocation,winStatus,winPersonalbar,winResizable,winToolbar,winMenubar,winDependent,winDirectories) {
	var windowName = ""
	// set defaults
	if (!winWidth) winWidth = 770
	if (!winHeight) winHeight = 550
	if (!winName) winName = 'popupWindow'
	if (!winX) winX = 10
	if (!winY) winY = 10
	if (!winScrollbars) winScrollbars = "yes"
	if (!winLocation) winLocation = "no"
	if (!winStatus) winStatus = "no"
	if (!winPersonalbar) winPersonalbar = "no"
	if (!winResizable) winResizable = "no"
	if (!winToolbar) winToolbar = "no"
	if (!winMenubar) winMenubar = "yes"
	if (!winDependent) winDependent = "no"
	if (!winDirectories) winDirectories = "no"
	// open pop-up window
	eval(winName +  '= window.open(winUrl,winName,"width=' + winWidth + ",height=" + winHeight + ",left=" + winX + ",top=" + winY + ",screenX=" + winX + ",screenY=" + winY + ",scrollbars=" + winScrollbars + ",location=" + winLocation + ",status=" + winStatus + ",personalbar=" + winPersonalbar + ",resizable=" + winResizable + ",toolbar=" + winToolbar + ",menubar=" + winMenubar + ",dependent=" + winDependent + ",directories=" + winDirectories + '")')
	eval(winName).focus()
}

function showlayerbyid(derlayer) {
	if (document.getElementById) {
		document.getElementById(derlayer).style.display = "block";
	}
}

function hidelayerbyid(derlayer) {
	if (document.getElementById) {
		document.getElementById(derlayer).style.display = "none";
	}
}

// inititalize toggelSelects, adding methods later
var toggleSelects = {};

// add onload events 
evt.add(window,'load',function(){merz.enableToolbox('toolbox',['fontSizeDecreaseA','fontSizeResetA','fontSizeIncreaseA','printA'])});
evt.add(window,'load',merz.prepareDropdowns);
evt.add(window,'load',function(){fontsize.init(['contentMain'])});
evt.add(window,'load',function(){styleTables.createOddEven('tCom')});
evt.add(window,'load',function(){merz.decorate()});
//evt.add(window,'load',printReferences.create);

// hide subnavigation when clicking somewhere
evt.add(document,'mouseup',merz.dropdownHide)

// seite drucken
function printWindow() {
	if (window.print) window.print();
}


function gotoProductpage(){
top.location.href = document.getElementById("productlistselect").options[document.getElementById("productlistselect").selectedIndex].value;
//alert(document.getElementById("productlistselect").options[document.getElementById("productlistselect").selectedIndex].value)

}
