var PINavigation; 
if (!PINavigation) {
	PINavigation = {};
}

PINavigation.i = 1;
PINavigation.iLevel = 0;
PINavigation.iCount = 0;
PINavigation.aULWidth = new Array();
PINavigation.aSubNavigationObserver = new Array();
PINavigation.aInitStatus = new Array();
PINavigation.aTrack = new Array();
PINavigation.aULWidth = new Array();
PINavigation.aSubNavigationObserver = new Array();
PINavigation.aInitStatus = new Array();
PINavigation.sCurrentButton = '';
PINavigation.oCloseObserver;

/**
 * Blendet alle Kind Elemente aus und danach ein
 * @param {Object} mParent
 */
PINavigation.init = function(mParent)
{
		// Object des Elemetes
		var oParent = $(mParent);

		if(!oParent)
		{
			return;
		}

		// Kind Elemente
		var oLevel1 = oParent.childElements();
		// geht alle Kind Elemente durch
		oLevel1.each(
			function(oItem){
				// geht die N�chste Ebene durch und schaltet diese auf inactive
				PINavigation.NextLevel(oItem, 0);
		});
		// Kind Elemente
		oLevel1 = oParent.childElements();
		// geht alle Kind Elemente durch
		oLevel1.each(
			function(oItem){
				// geht die N�chste Ebene durch und schaltet diese auf active
				PINavigation.NextLevel(oItem, 1);
		});
	
		
};
/**
 * Geht die n�chste Ebene durch
 * @param {Object} oItem
 * @param {Object} bDisplay
 */
PINavigation.NextLevel = function(oItem, bDisplay){
	// Object mit den Kind Elementen
	var oNextItem = oItem.childElements();
	// Durchlaufe die Kind Elemente
	oNextItem.each(function(oItem){
		// Wenn Display (true) ist
		if(bDisplay) {
			// und es ein LI oder UL Element ist
			if (oItem.tagName == 'LI' || oItem.tagName == 'UL') {
				// wenn es ein UL ist
				if (oItem.tagName == 'UL') {
					// Ausblenden
					oItem.style.display = "none";
					PINavigation.iLevel--;
				}
			}
		// Wenn Display (false)
		} else {
			// und es ein LI oder UL Element ist
			if(oItem.tagName == 'LI' || oItem.tagName == 'UL'){
				// wenn es ein UL ist
				if(oItem.tagName == 'UL'){
					PINavigation.iLevel++;
					// Wenn es bereits eine Breite gibt
					if (PINavigation.aULWidth[PINavigation.iLevel-1]) {
						oItem.style.top = '-1px';
						oItem.style.left = PINavigation.aULWidth[PINavigation.iLevel-1];
					}
					// Breite definieren
					PINavigation.aULWidth[PINavigation.iLevel] = oItem.getWidth();
				
					var aOffset = oItem.positionedOffset();
					var aDimensions = oItem.getDimensions();

					var oBGDiv = oItem.previous('DIV.divNavigationBG');
					if(oBGDiv) {
						oBGDiv.style.width = aDimensions.width+'px';
						oBGDiv.style.height = aDimensions.height+'px';
						oBGDiv.style.left = (aOffset[0]+1)+'px';
						oBGDiv.style.top = (aOffset[1])+'px';
						oBGDiv.hide();
					}
				// wenn es ein LI ist
				} else {
					oItem.style.position = "relative";
					if (PINavigation.aULWidth[PINavigation.iLevel]) {
						oItem.style.width = PINavigation.aULWidth[PINavigation.iLevel];
					}
				}
	
				PINavigation.NextLevel(oItem);
	
				if(oItem.tagName == 'UL') {
					PINavigation.iLevel--;
				}
				oItem.id = 'item_'+PINavigation.iCount;
				PINavigation.iCount++;
			}
		}
	});

}

/**
 * 
 * @param {Object} oEvent
 * @param {Object} oItem
 * @param {Object} bActive
 */
PINavigation.changeHorizontalNavigationButton = function(oEvent, oItem, bActive){
	
	PINavigation.closeNavigation(1);
	
	oEvent = Event.element(oEvent);

	if(oEvent.tagName == 'A') {
		
		var oLi = oEvent.up('LI');
		if(
			bActive &&
			oLi.className == 'liHorizontalNavigation'
		) {
			
			if(PINavigation.sCurrentButton) {
				switchButtonStyle($(PINavigation.sCurrentButton), 0);
			}
			var oButton = oLi.down("DIV");
			if (oButton && !oButton.id) {
				oButton.id = 'item_' + PINavigation.iCount;
				PINavigation.iCount++;
			}
			PINavigation.sCurrentButton = oButton.id;
			switchButtonStyle(oButton, bActive);
		}
		var oUl = oLi.down('UL');

		// hide inactive elements
		PINavigation.aTrack.each(function(sId) {
			
			if(
				sId && 
				!oEvent.descendantOf($(sId)) &&
				oUl.id != sId
			) {
				
				PINavigation.switchSubNavigation($(sId), 0);
				
			}
			
		});
		
		if(bActive) {
			
			if(oUl) {
				
				PINavigation.switchSubNavigation(oUl, bActive);

				var sClass = oUl.className;
				var sMatch = sClass.match(/level_[0-9]+/);
				if(sMatch) {
					var iLevel = sMatch[0].replace(/level_/, '');
					if(iLevel) {
						iLevel = parseInt(iLevel);
						PINavigation.aTrack[iLevel] = oUl.id;
					}
				}
			}
		}
		
	}
	
	if(!bActive) {
		PINavigation.closeNavigation(0);
	}
	
}
/**
 * 
 * @param {Object} bActive
 */
PINavigation.closeNavigation = function(bActive) {

	if(bActive) {
		clearTimeout(PINavigation.oCloseObserver);
	} else {
		PINavigation.oCloseObserver = setTimeout("PINavigation.executeCloseNavigation();", 200);
	}

}
/**
 * 
 */
PINavigation.executeCloseNavigation = function() {

	PINavigation.aTrack.each(function(sId) {

		PINavigation.switchSubNavigation($(sId), 0);
	
	});
	
	if(PINavigation.sCurrentButton) {
		switchButtonStyle($(PINavigation.sCurrentButton), 0);
	}

}
/**
 * 
 * @param {Object} oItem
 * @param {Object} bActive
 */
PINavigation.switchSubNavigation = function(oItem, bActive) {
	
	if(oItem && oItem.id) {
	
		if(
			bActive
		) {
			PINavigation.switchSubNavigationCallback(oItem, bActive);

			clearTimeout(PINavigation.aSubNavigationObserver[oItem.id]);
		} else {
			PINavigation.aSubNavigationObserver[oItem.id] = setTimeout("PINavigation.switchSubNavigationCallback('"+oItem.id+"', 0);", 200);
		}
	}
}
/**
 * 
 * @param {Object} mItem
 * @param {Object} bActive
 */
PINavigation.switchSubNavigationCallback = function(mItem, bActive) {

	var oItem = $(mItem);
	var oBGDiv = oItem.previous('DIV.divNavigationBG');
	
	if(oItem && oItem.style) {
		if(bActive) {
			oBGDiv.show();
			oItem.show();

			if (!PINavigation.aInitStatus[oItem.id]) {
				var oLevel1 = oItem.childElements();
				oLevel1.each(function(oItem){
					PINavigation.NextLevel(oItem, 1);
				});
				PINavigation.aInitStatus[oItem.id] = 1;
			}

			var aDimensions = oItem.getDimensions();

			oBGDiv.style.width = (aDimensions.width-1)+'px';
			oBGDiv.style.height = aDimensions.height+'px';
			
			var oLi = oItem.up();
			
			if(!oLi.hasClassName('liHorizontalNavigation')) {
				oLi.style.height = '18px';
			}			

		} else {	
			oBGDiv.hide();
			oItem.hide();
		}
	}
}

