var breadcrumbs = new Array();
function makeBreadCrumbs()
{
	var urlRegex = /(\w+):\/\/([\w.-]+)((\/[\S]+)\.\w{3,4})/i;
	var resultRegex = window.location.href.match(urlRegex);
	try{
		var pageId = resultRegex[3];
	}catch(e){return}

//	var pageId = document.body.getAttribute('id');

	// search the node inside the global_navi
	var globalNav = document.getElementsByClassName('global_nav_level_0')[0];

	getNextBreadCrumb(pageId, globalNav);
	//if no breadcrumbs found put breadcrumbs for the last visit
	if(breadcrumbs.length == 0){
		if(getCookie('pageId')){
			getNextBreadCrumb(getCookie('pageId'), globalNav);
		}
	}else{
		//set the cookie to this last found breadcrumb in case the next level subpage is not in the globalnav
		setCookie('pageId', pageId);
	}

	if(!$('breadcrumbs')) return;
	var breadCrubmsUlEl = $('breadcrumbs');
	$A(breadcrumbs).each(function(value, index) {
							var newLiEl = document.createElement('li');
							if(index == 0){
								newLiEl.setAttribute("id", 'breadcrumbs_first');
							}
							var newLiElA = value.cloneNode(true);
							newLiEl.appendChild(newLiElA);
							breadCrubmsUlEl.appendChild(newLiEl);
							if(index == 0){
								Element.addClassName($('breadcrumbs_first'), 'first');
							}
						});
}

function getNextBreadCrumb(inTagValue, inCurrentNode)
{
	try{
		if(inCurrentNode.getAttribute('href').indexOf(inTagValue) > -1){
			breadcrumbs.push(inCurrentNode);
			return;
		}
	}catch(e){}

	var nextNode = $A(inCurrentNode.childNodes).detect(function(value, index){
						if(value.nodeType !=1) return false;
						linkIsChild = ( document.getAnchorsByHrefValue(inTagValue, value).length > 0 ) ? (true) : (false);
						return linkIsChild;
					});
	if(nextNode){
		if(nextNode.tagName == 'LI'){
			breadcrumbs.push(nextNode.getElementsByTagName('a')[0]);
		}
		getNextBreadCrumb(inTagValue, nextNode);
	}else{
		return;
	}

}

function makeImageReplacement()
{
	var elements = new Array();
	elements = elements.concat( document.getElementsByClassPrefix('jsir_') );
	elements = elements.concat( document.getElementsByIdPrefix('jsir_') );
	var l = elements.length;
	var replacingImg = document.createElement('img');
	for(var i=0;i<l;i++){
		var el = elements[i];
		var newReplacingImg = replacingImg.cloneNode(true);
		try{
			newReplacingImg.src = Element.getStyle( el, 'background-image' ).match(/\("(.*)"\)/)[1];
		}catch(e){
			try{
				newReplacingImg.src = Element.getStyle( el, 'background-image' ).match(/\((.*)\)/)[1];
			}catch(e){};
		}

		Element.setStyle( el, {backgroundImage: 'none'} );
		newReplacingImg.alt = el.firstChild.nodeValue;
		var w = 0;
		var h = 0;
		try{ var w = Element.getStyle( el, 'width' ).match(/\d*/) }catch(e){}
		try{ var h = Element.getStyle( el, 'height' ).match(/\d*/) }catch(e){}
//		if(Number(w) > 0) newReplacingImg.width = w;
//		if(Number(h) > 0) newReplacingImg.height = h;
		el.replaceChild( newReplacingImg, el.firstChild );
	}
}

function makeSelectReplacement()
{
	var elements = new Array();
	elements = elements.concat( document.getElementsByClassPrefix('jssr_') );
	elements = elements.concat( document.getElementsByIdPrefix('jssr_') );

	var l = elements.length;
	var replacingSel = document.createElement('select');
	for(var i=0;i<l;i++){
		var el = elements[i];
		var newReplacingSel = replacingSel.cloneNode(true);
		var elClassNames = Element.classNames(el);
		var suffixedClassName = elClassNames.find( function(val){
			return (val.indexOf('jssr_') == 0);
		});
		var selId = suffixedClassName.split('jssr_')[1];
		newReplacingSel.setAttribute("id", selId);
		var newOptTag = document.createElement('option');
		newOptTag.setAttribute("value", '#');
		newOptTag.setAttribute("selected", el.getAttribute('selected'));
		newOptTag.innerHTML = el.getAttribute('title');
		newReplacingSel.appendChild(newOptTag);

		var nodesList = el.getElementsByTagName('li');
		var nodes = $A(nodesList);
		nodes.each(function(node){
			optStyle = new Array();
			if( nodes.indexOf(node) % 2 == 0 ) optStyle.push('gray');
			if( node.getElementsByTagName('a')[0].getAttribute('target') == '_blank' ) optStyle.push('openblank');
			optStyle = optStyle.join(' ');
			var newOptTag = document.createElement('option');
			newOptTag.setAttribute("value", node.getElementsByTagName('a')[0].getAttribute('href'));
			newOptTag.setAttribute("class", optStyle);
			newOptTag.innerHTML = node.getElementsByTagName('a')[0].innerHTML;
			newReplacingSel.appendChild(newOptTag);
		});
		var p = el.parentNode;
		p.removeChild(el);
		p.appendChild(newReplacingSel);

		Event.observe($(selId), 'change', onQuicknavChange, false);
	}
}

function makePopReplacement()
{
	var elements = new Array();
	elements = elements.concat( document.getElementsByClassPrefix('jspop_') );
	elements = $A(elements);
	elements.each(function(el, index){
		if(el.tagName == 'A'){
			el.onclick = onPopupOpenRequest.bindAsEventListener(el);
		}
	});
}

function onPopupOpenRequest(inEvt)
{
	var w = screen.width/2;
	var h = screen.height/2;

	var el = Event.findElement(inEvt, 'A');
	var pUrl = el.getAttribute('href');
	var elements = new Array();
	elements = document.getElementsByClassPrefix('jspop_size_');
	if( elements.indexOf( el ) > -1 ){
		var matches = el.className.match(new RegExp("(^|\\s)jspop_size_(\\d*)_(\\d*)(\\s|$)"));
		w = (matches[2] != null) ? matches[2] : w;
		h = (matches[3] != null) ? matches[3] : h;
	}
	centerPopup(pUrl, 'jspop', w, h );
	return false;
}

function centerPopup(popup_url,popup_name,popup_with,popup_height,reopen,myScrollbar,myLeftPos,myTopPos) {
  if(!myLeftPos)  {myLeftPos    = 5;}
  if(!myTopPos)   {myTopPos     = 15;}
  if(!myScrollbar){myScrollbar  = 1;}

  var popup_left                = (window.screen.width/2)  - (popup_with/2 + myLeftPos);
  var popup_top                 = (window.screen.height/2) - (popup_height/2 + myTopPos);
  if ((typeof popup_window != 'object') || (typeof popup_window == 'object' && popup_window.closed)) {
    if (document.all) {
      var xyPos                 = 'left=' + popup_left + ',top=' + popup_top;
    } else {
      var xyPos                 = 'screenX=' + popup_left + ',screenY=' + popup_top;
    }
    popup_window                = window.open(popup_url, popup_name, "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=" + myScrollbar + ",resizable=yes,width=" + popup_with + ",height=" + popup_height + ",copyhistory=no," + xyPos + "");
    popup_window.opener         = self;
    popup_window.focus();
    winUrl                      = popup_url;
    windowWidth                 = popup_with;
    windowHeight                = popup_height;
  } else {
    if ((winUrl != popup_url) || reopen) {
      popup_window.location.href = popup_url;
    }
    if ((windowWidth + windowHeight > 0) && (popup_with != windowWidth || popup_height != windowHeight || myLeftPos != diffWidth || myTopPos != diffHeight)) {
      var newWidth              = popup_with  - windowWidth;
      var newHeight             = popup_height - windowHeight;
      popup_window.resizeBy(newWidth,newHeight);
      popup_window.moveTo(popup_left,popup_top);
    }
    popup_window.focus();
    winUrl                      = popup_url;
    windowWidth                 = popup_with;
    windowHeight                = popup_height;
  }
  diffWidth                     = myLeftPos;
  diffHeight                    = myTopPos;
}

function simpleCenter(w, wWidth, wHeight)
{
	w.moveTo((window.screen.width/2 - wWidth/2), (window.screen.height/2 - wHeight/2));
}

function popupFitWidth()
{
	//has the body a class of popup?
	var elsWithClassPopup = document.getElementsByClassName('popup');
	bodyIsPopup = elsWithClassPopup.find( function(val, index){
		return (val == document.body);
	});
	if(bodyIsPopup){
		var layoutDims = Element.getDimensions('layout');
		Element.setStyle('layout', {width: '100%'});
		window.self.resizeTo(layoutDims.width, window.screen.height/2);
		simpleCenter(window.self, layoutDims.width, window.screen.height/2);
	}
}

function onQuicknavChange(inEvent)
{
	var selEl = Event.element(inEvent);
	var option = selEl.options[selEl.selectedIndex];
	var elClassNames = Element.classNames(option);
	if(option.value == '#') return;
	var openBlank = elClassNames.find( function(val){
			return (val == 'openblank');
		});
	if(openBlank){
		window.open(option.value, '_blank');
	}else{
		window.location = option.value;
	}
}

function collapseNavigation()
{
	var urlRegex = /(\w+):\/\/([\w.]+)((\/[\S]+)\.\w{3,4})/i;
	var resultRegex = window.location.href.match(urlRegex);
	try{
		var pageId = resultRegex[3];
	}catch(e){}
	var globalNav = document.getElementsByClassName('global_nav_level_0')[0];
	if(!globalNav) return;

	if(breadcrumbs.length == 0){
		var currentPageLink = document.getAnchorsByHrefValue (pageId, globalNav);
	}else{
		var l = breadcrumbs.length;
		var currentPageLink = breadcrumbs[l-1];
	}

	//hide all the UL s in the children tree ov NAVI level 0
	var childULs = $A(globalNav.getElementsByTagName('ul'));
	childULs.each(function(childUL, index) {
		Element.hide(childUL);
	})
	//traverse the tree downwards from the final link and make ancestors UL s visible
	breadcrumbParent = currentPageLink.parentNode;
	while(breadcrumbParent){
		if(breadcrumbParent.style){
			Element.show(breadcrumbParent);
		}
		//if parent has an UN in childnodes show it also
		$A(breadcrumbParent.childNodes).each(function(child, index){
			if(child.nodeType == 1 ){
				if(child.tagName == 'UL'){
					Element.show(child);
				}
			}
		})
        breadcrumbParent = breadcrumbParent.parentNode;
    }
}

/* get, set, and delete cookies */
function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+"="+escape( value ) +
		( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
