var API_KEY = "ABQIAAAA1OZ1Lj4aMqFtK37NNB64XRQNeZCG1UwhJWaTrEb7AYOAkPG3ZxQLzfz7uIKJfRfLYrKvOx5pHQAsRw";

function getPosition(obj) {
    var pos = { x:0, y:0 };

    do {
        pos.x += obj.offsetLeft;
        pos.y += obj.offsetTop;
    } while (obj = obj.offsetParent);

    return pos;

}

function setNewsticker(strUserVal, strColor)
{
	// linke Position des rechten Logo + Breite des Logos - manueller Abstand rechts = TickerPos
	newPos = parseInt(getPosition($('posLogo')).x) + $('posLogo').getWidth() - 17;
	// Position - Tickerbreite
	newPos = newPos - 610;
	// Zusammensetzen des Javascript strings
	return "<script language=\"JavaScript\" src=\"http://www.wcm-center.com/lauftext.php?username=" + strUserVal + "&poben=74&plinks="
		+ newPos + "&wbreite=610&whoehe=20&fsize=8&fcolor=" + strColor + "\" "
		+ "type=\"text/javascript\" class=\"ticker\"></script>";
}

function getSubMenu(pID)
{
	var strGetBack = "";

	var myAjax = new Ajax.Request(
    "tb/inc/getSubMenu.inc.php",
    { method: 'get', parameters: 'pID=' + pID, asynchronous: false,

        onSuccess:
            function(req)
            {
                values = req.responseText;
                strGetBack = values;
            },
        onFailure:
            function(req)
            {
                alert("geht net");
            }
    }
  );
  return strGetBack;
}

function openPage(obj, subMenuID)
{
	location.href="index.php?id=" + subMenuID
}

// Layer für die Hauptnavigation erstellen
function createSubMenu(pID, strValues)
{
	menPos = getPosition($(pID));
	// alert(menPos.x);
  var oDiv = document.createElement("DIV");
  oDiv.id = "subMen" + pID;
  oDiv.className = "menDiv";

  posY = menPos.y + 28;
  posX = menPos.x;
  oDiv.style.top = posY;
  oDiv.style.left = posX;
  oDiv.style.clear = "both";
  oDiv.style.visibility = "hidden";
  // width: valWidth, height: '60px'
  $('menTop').appendChild(oDiv);
  $('menTop').setStyle
	subMenuSplit = strValues.split("|");
	// alert(subMenuSplit.length);

	for (k=0; k<subMenuSplit.length; k++)
	{
		if (subMenuSplit[k] != "")
		{
			subMenuID = subMenuSplit[k].split(';')[0];
			subMenuName = subMenuSplit[k].split(';')[1];
		  var nDiv = document.createElement("DIV");
		  nDiv.id = "subNav" + pID;
		  nDiv.className = "subNav";
		  nDiv.style.cursor = "pointer";
		  nDiv.innerHTML = "&nbsp; &nbsp; " + subMenuName;
		  Event.observe(nDiv, 'click', openPage.bindAsEventListener(nDiv, subMenuID));
		  $('subMen' + pID).appendChild(nDiv);
		}

	}

}

function hideAllSubMenues(obj, menuID)
{
	topMenues = document.getElementsByClassName('mainlevel');
	for(y=0; y<topMenues.length; y++)
		$(topMenues[y].id).style.backgroundColor = '#646464';

	allMenues = document.getElementsByClassName('menDiv');
	for(y=0; y<allMenues.length; y++)
	{
		$(allMenues[y].id).style.visibility = "hidden";
		$(allMenues[y].id).style.backgroundColor = '#646464';
	}
	$(menuID).style.backgroundColor = '#505050';
}

function showHideSubMenu(obj, subMenuID, menuID, intVisible)
{
	hideAllSubMenues(obj, menuID);
	$(subMenuID).style.visibility = "visible";


}

function setIDFromURL()
{
	// get elements by id
	var topNavElements = $('menTop').select('[id="special.value"]');
	for (i=0; i<topNavElements.length; i++)
	{
		// split href to get page ID
		slittetHref = topNavElements[i].href.split('=');
		// set page ID to element
		$(topNavElements[i]).id = slittetHref[1];

		strReturnVal = getSubMenu(slittetHref[1]);
		if (strReturnVal != "")
		{
			createSubMenu(slittetHref[1], strReturnVal);
			Event.observe($(topNavElements[i]), 'mouseover', showHideSubMenu.bindAsEventListener($(topNavElements[i]), 'subMen' + slittetHref[1], slittetHref[1], 1));
		}
		else
		{
			Event.observe($(topNavElements[i]), 'mouseover', hideAllSubMenues.bindAsEventListener($(topNavElements[i]), slittetHref[1]));
		}

	}

}
