var activeContentID = '';
var activeTitleID = '';
var menuHeights = new Array(0,0,0,0,0);
var stopHideContent = false;
/*
	Used to handle the start page
*/
function clearHideTimeout()
{
    stopHideContent = true;
}
function hideContent(pDivId, pDelay)
{
	if(pDelay > 0) 
	{
	    stopHideContent = false;
		window.setInterval("hideContent('" + pDivId+ "', 0)", pDelay);
		return;
	}
	
	if(!stopHideContent) {	
	    stopHideContent = false;
	    if(activeContentID != '' && activeContentID == pDivId) 
	    {
		    oldContent = document.getElementById(activeContentID);
		    if(oldContent != null) {
			    oldContent.style.display = 'none';
		    }
		    activeContentID = '';
	    }
	}
}

function getElementByClassName(obj,className){
    var tagName="div";
    var papa=obj||document;
    var el=papa.getElementsByTagName(tagName);
    for (var i=0;i<el.length;i++){
        if(el[i].className==className)return el[i];
    }
} 

function swapContent(pDivId, pTitleId, pDelay)
{
    stopHideContent = true;    
	if(pDelay > 0){
		window.setTimeout("swapContent('" + pDivId+ "','" + pTitleId+ "', 0)", pDelay);
		return;
	}
    var oldContent;
    var newContent;    
	// Hide the previous menu
	if(activeContentID != '' && activeContentID != pDivId){
		oldContent = document.getElementById(activeContentID);
		if(oldContent != null) {
			oldContent.style.display = 'none';
		}
	}   
	newContent = document.getElementById(pDivId);

	// Show the current menu
	if(newContent != null) {
	    newContent.style.display = '';    
	}
	
	var newContentIndex = -1;
	switch(newContent.id) {
	    case "col1_startmenublock":
	        newContentIndex = 0;
	        break;
	    case "col2_startmenublock":
	        newContentIndex = 1;
	        break;
	    case "col3_startmenublock":
	        newContentIndex = 2;
	        break;
	    case "col4_startmenublock":
	        newContentIndex = 3;
	        break;
	    case "col5_startmenublock":
	        newContentIndex = 4;
	        break;
	}
	
	// Unknown contaier
	if(newContentIndex < 0) {
	    return;
	}
	
	if(newContentIndex <= 3) {
	    var leftDiv = getElementByClassName(newContent, "menublockleft");
	    var rightDiv = getElementByClassName(newContent, "menublockright");
	        	
        // This code is only executed once per page cycle
    	if(menuHeights[newContentIndex] == 0) {            	        	
    	    // Calcualte the height of all menu items and store to a global variable
            for(var i = 0 ; i < leftDiv.getElementsByTagName("div").length ; i++)
            {
                var el = leftDiv.getElementsByTagName("div")[i];
            	var currentHeight = el.clientHeight;
	            menuHeights[newContentIndex] += currentHeight;
                // Add an other class, if the menu item use 2 or 3 lines
                if(currentHeight > 44 && currentHeight < 62) {
                    el.className += " item_2lines";
                } else if(currentHeight > 67 && currentHeight < 77) {
                    el.className += " item_3lines";
                }                
            }
                        
            // Move the second half of the menu items to the right div
            var height = 0;
            var fullHeight = 0;
            var moveToRight = false;
            for(var i = 0 ; i < leftDiv.getElementsByTagName("div").length ; i++)
            {
                var el = leftDiv.getElementsByTagName("div")[i];
            	height += el.clientHeight;
                if(moveToRight) {                    
                    leftDiv.removeChild(el);
                    rightDiv.appendChild(el);	            
                    if((height / 2) == fullHeight) {
	                    // Show no lines for the last item
	                    el.className += " item_last";						
					} 
					i--; // Hack: we removed one item on the left --> correct the index                                        
                }
                else if(height >= (menuHeights[newContentIndex] / 2)) {
                    // The next item should be displayed on the right
                    moveToRight = true;
                    fullHeight = height;
                    // Show no lines for the last item
                    el.className += " item_last";
                }
            }
    
            // Set the height of the menu container
            leftDiv.style.height = fullHeight + "px";
            rightDiv.style.height = fullHeight + "px";
    	}
	}
	else if(newContentIndex == 4 && menuHeights[newContentIndex] == 0) {
		var leftDiv = getElementByClassName(newContent, "menublockleft");
		var el = leftDiv.getElementsByTagName("div")[leftDiv.getElementsByTagName("div").length-1];
		el.className += " item_last";
		menuHeights[newContentIndex] == 1;
	}

	activeContentID = pDivId;
	
	//Swap the title
	if(activeTitleID != '' && activeTitleID != pTitleId) 
	{
		oldContent = document.getElementById(activeTitleID);
		if(oldContent != null) {
		    oldContent.style.background = '#ffffff';
	    }
	}   
	newContent = document.getElementById(pTitleId);
	if(newContent != null) {
	    newContent.style.background = '#d6dde8';    
	}
	activeTitleID = pTitleId;
}

/*
	Search Helper Script (copied from search.js)
*/
function XmlEscape(text)
{
  return (text)?text.replace('&','&amp;').replace('>', '&gt;').replace('<','&lt;'):'';
}

function SendSoap(postUrl, soapAction, env, clickUrl, pos, bestBet, cont, nonClickedXml)
{
    var req = (window.XMLHttpRequest) ? (new XMLHttpRequest())
            : (window.ActiveXObject) ? (new ActiveXObject('Msxml2.XMLHTTP')) : null;
    if (req)
    {
        req.open('POST', postUrl, true);
        req.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
        req.setRequestHeader('SOAPAction', soapAction);
        if (clickUrl) env = env.replace("</i>", '<c>' + XmlEscape(clickUrl) + '</c>' + "</i>");
        if (pos) env = env.replace('<r>0','<r>'+pos);
        if (cont) env = env.replace('<f>false','<f>true');
        if (bestBet) env = env.replace("</i>", '<y>' + XmlEscape(bestBet) + '</y>' + "</i>");
        if (nonClickedXml) env = env.replace("</i>", nonClickedXml + "</i>");
        req.send(env);
    }
}
/*
	Handling

*/
function swapContentBoard(pDivId, pTitleId, pDelay)
{
    var oldContent;
    var newContent;
    
	if(pDelay > 0) 
	{
		window.setTimeout("swapContent('" + pDivId+ "','" + pTitleId+ "', 0)", pDelay);
		return;
	}
	// Swap the div 
	if(activeContentID != '' && activeContentID != pDivId) 
	{
		oldContent = document.getElementById(activeContentID);
		if(oldContent != null) {
			oldContent.style.display = 'none';
		}
	}   
	newContent = document.getElementById(pDivId);
	if(newContent != null) {
	    newContent.style.display = '';    
	}
	activeContentID = pDivId;
	
	//Swap the title
	if(activeTitleID != '' && activeTitleID != pTitleId) 
	{
		oldContent = document.getElementById(activeTitleID);
		if(oldContent != null) {
		    oldContent.style.background = '#D6DDE8';
	    }
	}   
	newContent = document.getElementById(pTitleId);
	if(newContent != null) {
	    newContent.style.background = '#ffffff';    
	}
	activeTitleID = pTitleId;
}


function SendClick(postUrl, soapAction, env, startPos, elem) 
{
    var id = elem.id;
    var relPosStr = /\d+$/.exec(id);
    var relPos = (relPosStr ? parseInt(relPosStr[0],10) : 0)
    var cont = /^(CSR_RV|CSR_MRL|SRP_)/.exec(id);
    var bestBet = null;
    var nonClickedXml = null;

    if (/^BBR_/.exec(id))
    {
        var bbelem = document.getElementById('BBR_'+relPosStr);
        if (bbelem) bestBet = bbelem.innerHTML;
    }
    else if (relPos > 1 && startPos == 0 && /^CSR_/.exec(id))
    {
        nonClickedXml = '';
        for (var i = 1; i < relPos && i < 10; i++)
        {
            var nonClickedElem = document.getElementById('CSR_'+i);
            if (nonClickedElem)
            {
                nonClickedXml += '<z>'+nonClickedElem.href+'</z>';
            }
        }
    }
    SendSoap(postUrl, soapAction, env, (cont ? null : elem.href), relPos + startPos, bestBet, cont, nonClickedXml);
}