var objXmlHTTP;
var mintParentId = 0;

var mobjOpen    = new Array();
var mobjVisited = new Array();

window.onload = 
function page_Load() {
    objXmlHTTP = createXMLHttpRequestObject();
    // a slight delay before loading is good due to strange
    // behaviour i Mozilla
    window.setTimeout("loadMenu(" + mintParentId + ")", 1000);
    init_Page();
};

function loadMenu( inintParentId, inintArticles, inintChilds ) {
    mintParentId = inintParentId;

    
    //if( !inintChilds && mintParentId ) return;
    
    if( inintChilds || !mintParentId ) {
		if( mobjVisited["l" + mintParentId] ) {
		    renderMenu(mobjVisited["l" + mintParentId]);
		    if (inintArticles) {
		        getArtList();
		    }
			return;
		}
	    
		objXmlHTTP.open("GET", "menu.aspx?pid=" + inintParentId, true);
		objXmlHTTP.onreadystatechange = xmlHTTP_Loaded;
		objXmlHTTP.send(null);
    }    
    if( inintArticles ) {
        getArtList();
		//window.setTimeout('getArtList()', 250);
		//getArtList(inintParentId);
	}
    return false;
}

function getArtList(inintId) {
	if(!inintId)
	    inintId = mintParentId;
	    if (getQuerystring('searchby') == 'ArticleTreeGroupId' && getQuerystring('query') == inintId) {
            // Do not load same articlelist if already loaded in frameMid
	    }
	    else {
	        top.frameMid.location.href = '/shop/artlist/index2.aspx?searchby=ArticleTreeGroupId&query=' + inintId;
	    }
}


function getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(top.frameMid.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}

/**
 *
 */
function xmlHTTP_Loaded( ) {
    // if xmlhttp shows "loaded"
    if (objXmlHTTP.readyState==4) {
        // if "OK"
        if (objXmlHTTP.status==200) {
            var objLevels = eval('(' + objXmlHTTP.responseText + ')');
            mobjVisited.pushValue(objLevels, "l" + mintParentId);
            renderMenu(objLevels);
        }
        else {
            alert("Problem retrieving XML data")
        }
    }
}
/**
 *
 */
function renderMenu( inobjLevels ) {
    var objMenuDiv;

    objMenuDiv = document.getElementById("i_" + mintParentId);
    var objMenu = inobjLevels; //eval('(' + objXmlHTTP.responseText + ')');
    var intLevel = objMenu["rows"][0][2];
    var objCurr;

	var blnPOPULATED = 0;

    if( intLevel == '1' ) {
        for( i = (mobjOpen.length-1); i >= 0; i-- ) {
            objCurr = document.getElementById("i_" + mobjOpen[i]);
            objCurr.innerHTML = "";
        }
        mobjOpen.length = 0;
    }

    if( mobjOpen.popValue(mintParentId) ) {
        objMenuDiv.innerHTML = '';
        return;
    } else if( mintParentId != '0' ) {
        mobjOpen.pushValue(mintParentId);
    }

    var strOut = "";
    for( var i = 0; i < objMenu["rows"].length; i++ ) {
        var strAtgId   = objMenu["rows"][i][0];
        var strDesc    = objMenu["rows"][i][1];
        var strId      = "i_" + strAtgId;

        var strArtCount = objMenu["rows"][i][3];
        if (strArtCount == '0') {
            strArtCount = objMenu["rows"][i][5]
        }

        if(objMenu["rows"][i][4] == '0')
            var strCss = ' class="l' + objMenu["rows"][i][2] + '"';
        else
            var strCss = ' class="l' + objMenu["rows"][i][2] + 's"';
        //var strOnClick = ' onclick="return loadMenu(' + strAtgId + ', ' + objMenu["rows"][i][3] + ', ' + objMenu["rows"][i][4] + ');"';
        var strOnClick = ' onclick="return loadMenu(' + strAtgId + ', ' + strArtCount + ', ' + objMenu["rows"][i][4] + ');"';
        
        strOut += '<a href="#"' + strOnClick + strCss + ' title="' + strDesc + '">'
                + strDesc + '</a>'
                + '<span id="' + strId + '"></span>';
    }
    objMenuDiv.innerHTML = strOut;
}

// Some array prototypes
Array.prototype.popValue = function( inobjValue ) {
    var objTmpArray = new Array();
    for( var i = 0; i < this.length; i++ )
        if( this[i] != inobjValue )
            objTmpArray[objTmpArray.length] = this[i];
    var intOut = objTmpArray.length - this.length;
    this.length = 0;
    for( i = 0; i < objTmpArray.length; i++ )
        this[i] = objTmpArray[i];
    return intOut;
}
//
Array.prototype.pushValue = function( inobjValue, instrKey ) {
    if( instrKey ) {
        this[instrKey] = inobjValue;
        return;
    }
    this[this.length] = inobjValue;
}
//
Array.prototype.contains = function( inobjValue ) {
    var blnFOUND = false;
    var i = 0;
    while(!blnFOUND && i < this.length) {
        
        blnFOUND = (this[i++] == inobjValue);
    }
    return blnFOUND;
}

// HELP METHODS
// creates an XMLHttpRequest instance
function createXMLHttpRequestObject() {
    // xmlHttp will store the reference to the XMLHttpRequest object
    var xmlHttp;
    // try to instantiate the native XMLHttpRequest object
    try {
        // create an XMLHttpRequest object
        xmlHttp = new XMLHttpRequest();
    }
    catch(e) {
        // assume IE6 or older
        try {
          xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
        }
        catch(e) { }
    }
    // return the created object or display an error message
    if (!xmlHttp)
        alert("Error creating the XMLHttpRequest object.");
    else 
        return xmlHttp;
} // createXMLHttpRequestObject

function evalToggle( instrId ) {
	var objElement = document.getElementById(instrId);
	objElement.style.display = ( objElement.style.display == 'none' ) ? 'block' : 'none';
}

function init_Page() {
    if( !document.getElementsByTagName) return;
    var a = document.getElementsByTagName('a');
    for (var i=0; i<a.length; i++) {
        if ( a[i].getAttribute('href') && (
                    a[i].getAttribute('rel') == 'nofollow'
                 || a[i].getAttribute('rel') == 'external')) {
            a[i].target = '_blank';
        }
    }
}