// filename: jbl_gen.js
// version 1.0 - 2006-10-19 - mkr / rm
//
// martin dot krause at gpm dot de
//

//config	
var objParentLayerId = 'parentlayer';
var objLeftId = 'left';
var objMiddleId = 'middle';
var objRightId = 'right';
var objFooterId = 'footer';
var objContentId = 'content';
var myHeight; // global windowheight-objekt

// set window name
window.name = 'fuk';
	
window.onresize = reAdjust;
	
// escape from framesets
if (parent.frames && parent.frames.length != 0) top.location.href = document.location.href;
else self.focus();

// pageInit
function pageInit() {

	// initialize reload
	reAdjust(true);

	//browsercheck
	is = new BrowserCheck();
	
	//create eventhandlers for link elements
	createNavFocus() ;
	
	//create odd/even structur of li / td
	createOddEven();

	//create height objects
	myHeight = new calculatedHeightObject();
	
	//create decoration: blue fade, blue bar
//	createDecoration();

	//fix browser bugs
	fixBrowser();
}


// fixing some display errors
function fixBrowser() {
	
	if (is.opera) {
		obj = document.getElementById('quicksitemap')
		obj.style.position = 'relative';
		obj.style.top = '5px';
	}
	
}


//creating the blue deko on the right 
function createDecoration(){

	if (myHeight.calculatedHeight) 
	{
		//calculate height
		var createHeight = (parseInt(myHeight.calculatedHeight) - 4);
		
		// create element: blue stripe
		dekoblue = document.createElement('div');
		dekoblue.id = 'dekoblueid';
		
		// append element: blue stripe
		document.body.appendChild(dekoblue);

		// setting styles: blue stripe
		obj = document.getElementById('dekoblueid');
		
		obj.style.height = createHeight +'px';
		obj.style.background = 'transparent url(/img/gen_deko_blue.png) 0 0 repeat-y';
		obj.style.position = 'absolute';
		obj.style.top = '0px';
		obj.style.right = '0px';
		obj.style.width = '8px';
		obj.style.zIndex = '50000';
		obj.style.display = 'block';
	}

}	


//creating odd/even tables and lists
function createOddEven() {

	//starting with odd?
	var odd = true;
	
	// grabbing odd/even object
	var objParent = document.getElementById(objContentId);
	
	for (var i = 0; i < objParent.getElementsByTagName('*').length; i++){
	
		var objOddEven = (objParent.getElementsByTagName('*')[i].className.indexOf('oddeven') != -1) ? objParent.getElementsByTagName('*')[i] : "";
	
			
		// dealing with lists
		if (objOddEven.tagName == 'ul' || objOddEven.tagName == 'UL') {

			//evaluating the oddEven element, add odd even to LI 
			for (var j = 0; j < objOddEven.childNodes.length; j++){

				// li 
				if ( (objOddEven.childNodes[j].tagName == 'LI' || objOddEven.childNodes[j].tagName == 'li') ) 
				{
		
					//setting them odd/even
					if (odd) { 
						objOddEven.childNodes[j].className = objOddEven.childNodes[j].className + ' odd' 
						odd = false;
					}
					else {
						objOddEven.childNodes[j].className = objOddEven.childNodes[j].className + ' even' 
						odd = true; 
					}	
					
					// adding .first to first and .last to last child
					objOddEven.childNodes[j].className = (j == '1') ? objOddEven.childNodes[j].className + ' first' : objOddEven.childNodes[j].className;
					objOddEven.childNodes[j].className = (j == (objOddEven.childNodes.length - 2 )) ? objOddEven.childNodes[j].className + ' last' : objOddEven.childNodes[j].className;
				}
			}
	//starting with odd?
	var odd = true;
		}
	}
		
}



//needed for BITV 9.3.1, cycles throught the document, adding eventhandlers *navFocus* to all the links
function createNavFocus() {
	
	// loading all links into an array, cycling	
	for (var i = 0; i < document.getElementsByTagName('a').length; i++){

		// grabbing the current link
		obj = document.getElementsByTagName('a')[i];
		
		//skip img links like <a><img /></a>
		if ((obj.firstChild && obj.firstChild.tagName == 'IMG') || (obj.firstChild && obj.firstChild.tagName == 'img')) { continue;  }
	
				//check if eventhandler is already set - if not, add it

				// a.class="readoncontent"
				if (obj.className.indexOf('readoncontent') != -1) {
					if (!obj.onblur) { obj.onblur = function(){ navFocus(this, 'onblur', 'readoncontentactive')}; }
					if (!obj.onfocus) { obj.onfocus = function(){ navFocus(this, 'onfocus', 'readoncontentactive')}; }
				}
				// a.class="readon"
				else if (obj.className.indexOf('readon') != -1) {
					if (!obj.onblur) { obj.onblur = function(){ navFocus(this, 'onblur', 'readonactive')}; }
					if (!obj.onfocus) { obj.onfocus = function(){ navFocus(this, 'onfocus', 'readonactive')}; }
				}
				// a.class="addtocart"
				else if (obj.className.indexOf('addtocart') != -1) {
					if (!obj.onblur) { obj.onblur = function(){ navFocus(this, 'onblur', 'addtocart active')}; }
					if (!obj.onfocus) { obj.onfocus = function(){ navFocus(this, 'onfocus', 'addtocart active')}; }
				}
				// a.class="pdf"
				else if (obj.className.indexOf('pdf') != -1) {
				
					if (!obj.onblur) { obj.onblur = function(){ navFocus(this, 'onblur', 'pdf active')}; }
					if (!obj.onfocus) { obj.onfocus = function(){ navFocus(this, 'onfocus', 'pdf active')}; }
				}				
				// a.class="smaller"
				else if (obj.className.indexOf('smaller') != -1) {
				
					if (!obj.onblur) { obj.onblur = function(){ navFocus(this, 'onblur', 'smaller active')}; }
					if (!obj.onfocus) { obj.onfocus = function(){ navFocus(this, 'onfocus', 'smaller active')}; }
				}				
				// a.class="bigger"
				else if (obj.className.indexOf('bigger') != -1) {
				
					if (!obj.onblur) { obj.onblur = function(){ navFocus(this, 'onblur', 'bigger active')}; }
					if (!obj.onfocus) { obj.onfocus = function(){ navFocus(this, 'onfocus', 'bigger active')}; }
				}				
				// default
				if (!obj.onblur) { obj.onblur = function(){ navFocus(this, 'onblur', 'active')}; }
				if (!obj.onfocus) { obj.onfocus = function(){ navFocus(this, 'onfocus', 'active')}; }
				
	}
	
}



//needed for BITV 9.3.1, adds the active class - the "hover" effect - for tabbing
function navFocus(obj, eventhandler, addClass) {

	var defaultClass = '';
	
	//registering "e" as event-type
	if (!e) var e = window.event;	

	//dealing with .class links
	switch(addClass) {
		
		case 'readoncontentactive' :
			defaultClass = 'readoncontent';
		break; 
		case 'readonactive' :
			defaultClass = 'readon';
		break; 
		
		case 'addtocart active' :
			defaultClass = 'addtocart';
		break; 
		
		case 'pdf active' :
			defaultClass = 'pdf';
		break; 
		
		case 'smaller active' :
			defaultClass = 'smaller';
		break; 
		
		case 'bigger active' :
			defaultClass = 'bigger';
		break; 
	}

	switch(eventhandler) {
		
		//onfocus	
		case 'onfocus':
			if (obj.className.indexOf('active') != -1) {obj.className = obj.className + ' tabbed ';} //keep active links
			else {obj.className = obj.className + " " + addClass;}
		break;
		
		//onblur
		case 'onblur':
			if (obj.className.indexOf('tabbed') == -1 && obj.className.indexOf('first') == -1) {obj.className = defaultClass;} //keep active links && .first (navigation)
			else if (obj.className.indexOf('tabbed') == -1 && obj.className.indexOf('first') != -1) {obj.className = 'first';}
		break;
	
	}

}

function repositionLayers() {
	if (myHeight.calculatedHeight){
		// blue stripe
		var repositionHeight = (parseInt(myHeight.calculatedHeight) - 4);
//		alert(myHeight.calculatedHeight +':'+ repositionHeight);
		obj = document.getElementById('dekoblueid');		
		obj.style.height = repositionHeight +'px';	
	}
}

function reAdjust(init) {
		newHeight = new calculatedHeightObject();

	// initialize function on first call
	if (init == true) {
		// set resize event
		window.onresize = reAdjust
		adjustSmallScreen();
	}
	
	// do something if window size has changed
/*
	else if (myHeight.calculatedHeight != newHeight.calculatedHeight) {
		myHeight.calculatedHeight = newHeight.calculatedHeight;
		repositionLayers();
*/
	else {
		adjustSmallScreen();
	}
}


// misc functions 
//-----------------------------------------------------------------------------

function adjustSmallScreen (){ 
	if (is.ie) {return true;}
	var vpx;
	// get viewport width
	// all except Explorer
	if (self.innerHeight) { vpx = self.innerWidth;}
	// Explorer 6 Strict Mode
	else if (document.documentElement && document.documentElement.clientHeight){vpx = document.documentElement.clientWidth;}
	// other Explorers
	else if (document.body) {vpx = document.body.clientWidth;}
	// adjust size
	if (vpx < 650) 
	{ 
		document.getElementById('textmarkeImg').src = 'http://www.fuk.de/img/gen_visual_textmarke_small.gif'; 
	}
	else {
		document.getElementById('textmarkeImg').src = 'http://www.fuk.de/img/gen_visual_textmarke.gif'; 
	}
}

function calculatedHeightObject () {


	// get elements by id
	objParentLayer = document.getElementById(objParentLayerId);		
	objFooter = document.getElementById(objFooterId);

	//calculating visible height
	var visibleHeight = (window.innerHeight) ? window.innerHeight : document.documentElement.clientHeight;
	var visibleHeight = (visibleHeight) ? visibleHeight : document.body.clientHeight;

	// the border stuff is a wired workaround for firefox
//	objParentLayer.style.border = "1px solid transparent";
	var parentlayerHeight = document.getElementById('parentlayer').offsetHeight;
//	objParentLayer.style.border = "none";

	//footer height
	//var footerHeight = (objFooter.offsetHeight) ? objFooter.offsetHeight : "0";
		
	//if the content is longer than the window, add contentheight, margin and //footer height
	this.calculatedHeight = (visibleHeight < parentlayerHeight) ? (parentlayerHeight + 25 ) : visibleHeight;
}

// removes/restore default text, using obj.name if no defaultText is given
function removeDefault(obj, eventhandler, defaultText) {

	switch (eventhandler) {
		
		case 'onfocus' :
			if ( obj.value == firstLetterUpperCase(obj.name) ) { obj.value = '';}
			else if ( (typeof(defaultText) != 'undefined') && obj.value == defaultText ) { obj.value = '';}
		break;
		
		case 'onblur' :
			if ( obj.value == '' && (typeof(defaultText) == 'undefined') ) { obj.value = firstLetterUpperCase(obj.name) ;}
			else if ( obj.value == '' && (typeof(defaultText) != 'undefined') ) { obj.value = defaultText;}
		break; 
	
	}

}


// returns obj as Obj
function firstLetterUpperCase(obj) {
	formatedString = obj.slice(0,1).toUpperCase() + obj.slice(1) ;
	return formatedString;
}

// disables Button to prevent more than one click and enhance usabillity
function disableButton(objId){
	objButton = document.getElementById(objId);
	objButton.disabled = true;
	objButton.value = 'Bitte warten';
	objButton.style.cursor = 'wait'
}


