
var textBuffer = "";
var level = 0;
var adminLogged = false;

$(document).ready(function(){

    checkAdminLogin();

});


function checkAdminLogin(){

    //alert( "checking..." );

    var serverUrl = "./ajax.php";
    var data      = "page=check_admin_login";

    var xmlHttp = $.get(serverUrl, data, onLoadCheckAdminLogin, "json");
}

function onLoadCheckAdminLogin( output ){

    //alert( output.status );

    var adminId = output.status;

    if(adminId>0){
        adminLogged = true;
    }

    loadMenuData();

}

function loadMenuData(){
    
    //alert("loading menu");

    var seqId = Math.floor(Math.random()*1000);

    //var serverUrl = menuXmlUrl;

    var serverUrl = "./ajax.php";
    var data      = "page=menu2&seqId="+seqId;

    var xmlHttp = $.get(serverUrl, data, onLoadMenuData, "json");

}

function onLoadMenuData( outputArray ){

    //alert(outputArray);

    var sections    = outputArray.childs;

    //alert(sections.length);

    if( sections != null && sections.length > 0 ){

        textBuffer += parseMainChilds(sections);

        updateMenuDiv(textBuffer);

        //alert("menu loaded");

    }else{

        alert("can't load menu");

    }

}


function parseMainChilds(sections){

    var buffer = '';
    var id     = '';
    var iName  = '';
    var iHref  = '';

    var forwardTo = '';
    var tempName = '';

    var section = null;

    var iClass = "main";

    buffer += '<ul class="'+iClass+'">\n';

        for(var i=0; i<sections.length; i++){

            section = sections[i];

            id  = section.id;

            if( section.name == "section" ){

                if( lang == "ar" ){
                    tempName = section.nameAr;
                }else{
                    tempName = section.nameEn;
                }

                iName  = tempName;
                //iName  = ''+unescapeText( tempName )+'';
                iName  = getBriefText(iName, 23);

                forwardTo = section.forwardTo;

                if( forwardTo!=null && forwardTo!="" ){
                    iHref = '?page='+forwardTo;
                }else{
                    iHref = '?page=sections&parentId='+id;
                }

            }

            //alert( 'section.active : '+section.active + ', adminLogged : ' + adminLogged );

            if( section.active>0 || adminLogged ){

                buffer += '<li>\n';

                //alert( "childs length : " + section.childs.length);

                if( section.childs != null && section.childs.length > 0 ){

                    if( section.childs.length > 0 ){

                        buffer += '<a href="'+iHref+'">'+iName+'</a>\n';

                        buffer += parseChilds( section, 0 );
                    }

                }else{

                    buffer += '<a href="'+iHref+'">'+iName+'</a>\n';

                }

                buffer += '</li>\n';

            }

        }

    buffer += '</ul>\n';

    return buffer;
}


function parseChilds(section, level){

    var buffer = '';
    var id     = '';
    var iClass = '';
    var iName  = '';
    var iHref  = '';

    var tempName = '';

    id     = section.id;
    iClass = (level>0)?"subsub":"sub";
    //iId    = xmlParentNode.tagName + '_' + id;
    iId    = iClass + '_' + id;


    var childs = section.childs;

    buffer += '<ul id="'+iId+'" class="'+iClass+'">\n';

    var subSection = null;
    for(var i=0; i<childs.length; i++){

        subSection = childs[i];

        id = subSection.id;

        if( subSection.name == "section" ){

            if( lang == "ar" ){
                tempName = subSection.nameAr;
            }else{
                tempName = subSection.nameEn;
            }

            iName  = tempName;
            //iName  = ''+unescapeText( tempName )+'';
            iName  = getBriefText(iName, 23);

            forwardTo = subSection.forwardTo;

            if( forwardTo!=null && forwardTo!="" ){
                iHref = '?page='+forwardTo;
            }else{
                iHref = '?page=sections&parentId='+id;
            }

        }

        //alert( 'section.active : '+section.active + ', adminLogged : ' + adminLogged );

        if( section.active>0 || adminLogged ){

            buffer += '<li>\n';

            if( subSection.childs != null && subSection.childs.length > 0 ){

                if( subSection.childs.length > 0 ){
                    level  += 1;
                    buffer += '<a href="'+iHref+'">'+iName+'</a>\n';
                    buffer += parseChilds( subSection, level );
                }

            }else{
                buffer += '<a href="'+iHref+'">'+iName+'</a>\n';
            }

            buffer += '</li>\n';

        }
    }

    buffer += '</ul>\n';

    return buffer;
}


function unescapeText(str){

    str = replaceAll (str, "+", " ");

    str = unescape( str );

    return str;
}

function updateMenuDiv(textBuffer){

    //alert( textBuffer );

    //document.getElementById("menu").innerHTML = textBuffer;

    $("#Menu").html( textBuffer );

    prepareMenu();

}

