﻿// Copyright 2006-2007 javascript-array.com

var timeout = 500;
var closetimer = 0;
var ddmenuitem = 0;

// open hidden layer
function mopen(id, intLeft) {
    // cancel close timer
    mcancelclosetime();

    // close old layer
    if (ddmenuitem) ddmenuitem.style.visibility = 'hidden';

    // get new layer and show it
    ddmenuitem = document.getElementById(id);
    ddmenuitem.style.left = intLeft;
    ddmenuitem.style.visibility = 'visible';

}
// close showed layer
function mclose() {
    if (ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime() {
    closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime() {
    if (closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}

// close layer when click-out
document.onclick = mclose;

var menu = function() {
    this.pullmenu = null;
    this.readCookie = function(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    };
    this.load = function() {
        var params = "action=fetchpullmenu&callback=mymenu.storemenu";
        var jsonservice = new JSONService();
        jsonservice.loadScript("mymenufetchpullmenu_0001", "services/content.aspx?" + params);
    }
    this.storemenu = function(strJson) {
        var objJson = eval("(" + strJson + ")");
        this.pullmenu = objJson.rows;
        this.render();
    }
    this.render = function(idDiv) {
        var intCount = 0;
        var rcode = "";
        var isSuperUser = "false";
        var locationId = 1;
        var objJson = eval('(' + this.readCookie("pencilauth") + ')');
        if (objJson !== null) {
            rcode = objJson.login.rcode;
            isSuperUser = objJson.login.isSuperUser;
            locationId = objJson.login.locationId;
        }
        
        
        
        if($.query.get('locationId'))
        {
            locationId = $.query.get('locationId');
        }
        
        var isContentPage = (document.URL.indexOf("page.htm", 0) == -1) ? false : true;
        var strHtml = '';
        
        if(isSuperUser == 'true' || locationId==1)
        {
            strHtml += '<table cellspacing="0" width="100%" cellpadding="0" background="assets/tab_strp_BG.gif" border="0">';
            strHtml += '    <tr>';
            strHtml += '        <td>';
            strHtml += '            <table cellspacing="0" cellpadding="0" border="0">';
            strHtml += '                <tr>';
            strHtml += '                    <td width="181px"><a onmouseover=\'mopen("m2", 10)\';\'MM_swapImage("nav_1","","assets/nav_1_over.gif",1)\' onmouseout="MM_swapImgRestore();mclosetime();" href="page.htm?id=2"><img height="30" src="assets/nav_1.gif" width="181" border="0" id="nav_1" name1="nav_1"></a></td>';
            strHtml += '                    <td width="181px"><a onmouseover=\'mopen("m3", 191)\';\'MM_swapImage("nav_2","","assets/nav_2_over.gif",1)\' onmouseout="MM_swapImgRestore();mclosetime();" href="page.htm?id=3"><img height="30" src="assets/nav_2.gif" width="181" border="0" id="nav_2" name1="nav_2"></a></td>';
            strHtml += '                    <td width="181px"><a onmouseover=\'mopen("m5", 372)\';\'MM_swapImage("nav_3","","assets/nav_3_over.gif",1)\' onmouseout="MM_swapImgRestore();mclosetime();" href="page.htm?id=5"><img height="30" src="assets/nav_3.gif" width="181" border="0" id="nav_3" name1="nav_3"></a></td>';
            strHtml += '                    <td width="181px"><a onmouseover=\'mopen("m4", 553)\';\'MM_swapImage("nav_4","","assets/nav_4_over.gif",1)\' onmouseout="MM_swapImgRestore();mclosetime();" href="page.htm?id=4"><img height="30" src="assets/nav_4.gif" width="181" border="0" id="nav_4" name="nav_4"></a></td>';
            strHtml += '                </tr>';
            strHtml += '            </table>';
            strHtml += '        </td>';
            strHtml += '    </tr>';
            strHtml += '</table>';
        }
        else
        {
        strHtml += '<table cellspacing="0" width="100%" cellpadding="0" background="assets/tab_strp_BG.gif" border="0">';
        strHtml += '    <tr>';
        strHtml += '        <td>';
        strHtml += '            <table cellspacing="0" cellpadding="0" border="0">';
        strHtml += '                <tr height="30px">';
        strHtml += '                    <td width="181px">&nbsp;</td>';
        strHtml += '                    <td width="181px">&nbsp;</td>';
        strHtml += '                    <td width="181px">&nbsp;</td>';
        strHtml += '                    <td width="181px">&nbsp;</td>';
        strHtml += '                </tr>';
        strHtml += '            </table>';
        strHtml += '        </td>';
        strHtml += '    </tr>';
        strHtml += '</table>';
            
        }

        if (rcode == "CM" && isContentPage) {
            strHtml += '<div class="portlet-cm">';
            strHtml += '<table cellspacing="0" width="375px" cellpadding="4">';
            strHtml += '    <tr>';
            strHtml += '        <td><a class="portlet-link" style="text-decoration:none;" title="Edit this page" href="javascript:myContentMgr.updateContent();">Edit Page</a></td>';
            strHtml += '        <td><a class="portlet-link" style="text-decoration:none;" title="Add a new page" href="javascript:myContentMgr.addContent();">Add Page</a></td>';
            strHtml += '        <td><a class="portlet-link" style="text-decoration:none;" title="Add a new page" href="javascript:myContentMgr.addMenu();">Add Menu</a></td>';
            strHtml += '        <td><a class="portlet-link" style="text-decoration:none;" title="Upload media and other files" href="javascript:myContentMgr.uploadMedia();">Upload File</a></td>';
            strHtml += '    </tr>';
            strHtml += '</table>';
            strHtml += '</div>';
        }
        strHtml += '<div id="sddm">';
        strHtml += '<div id="m2" onmouseover="mcancelclosetime()" onmouseout="mclosetime()">';
        strHtml += '<table cellspacing="0" cellpadding="0">';
        for (intCount = 0; intCount < this.pullmenu.length; intCount++) {
            if (this.pullmenu[intCount].parent_id == "2") {
                if (rcode == "CM" && isContentPage && this.pullmenu[intCount].iscontent == "N") {
                    strHtml += '    <tr>';
                    strHtml += '        <td nowrap="nowrap"><a href="page.htm?id=' + this.pullmenu[intCount].id + '">' + this.pullmenu[intCount].title + '</a></td>';
                    strHtml += '        <td><a href="javascript:myContentMgr.updateMenu(' + this.pullmenu[intCount].id + ');">Edit</a></td>';
                    strHtml += '        <td><a href="javascript:myContentMgr.updateMenuSequence(' + this.pullmenu[intCount].id + ',\'U\');">&uarr;</a></td>';
                    strHtml += '        <td><a href="javascript:myContentMgr.updateMenuSequence(' + this.pullmenu[intCount].id + ',\'D\');">&darr;</a></td>';
                    strHtml += '    </tr>';
                }
                else {
                    if (this.pullmenu[intCount].isvisible == "Y") {
                        strHtml += '    <tr>';
                        strHtml += '        <td nowrap="nowrap"><a href="page.htm?id=' + this.pullmenu[intCount].id + '">' + this.pullmenu[intCount].title + '</a></td>';
                        strHtml += '        <td></td>';
                        if (rcode == "CM" && isContentPage) {
                            strHtml += '        <td><a href="javascript:myContentMgr.updateMenuSequence(' + this.pullmenu[intCount].id + ',\'U\');">&uarr;</a></td>';
                            strHtml += '        <td><a href="javascript:myContentMgr.updateMenuSequence(' + this.pullmenu[intCount].id + ',\'D\');">&darr;</a></td>';
                        }
                        strHtml += '    </tr>';
                    }
                }
            }
        }
        strHtml += '</table>';
        strHtml += '</div>';
        strHtml += '<div id="m3" onmouseover="mcancelclosetime()" onmouseout="mclosetime()">';
        strHtml += '<table cellspacing="0" cellpadding="0">';
        for (intCount = 0; intCount < this.pullmenu.length; intCount++) {
            if (this.pullmenu[intCount].parent_id == "3") {
                if (rcode == "CM" && isContentPage && this.pullmenu[intCount].iscontent == "N") {
                    strHtml += '    <tr>';
                    strHtml += '        <td nowrap="nowrap"><a href="page.htm?id=' + this.pullmenu[intCount].id + '">' + this.pullmenu[intCount].title + '</a></td>';
                    strHtml += '        <td><a href="javascript:myContentMgr.updateMenu(' + this.pullmenu[intCount].id + ');">Edit</a></td>';
                    strHtml += '        <td><a href="javascript:myContentMgr.updateMenuSequence(' + this.pullmenu[intCount].id + ',\'U\');">&uarr;</a></td>';
                    strHtml += '        <td><a href="javascript:myContentMgr.updateMenuSequence(' + this.pullmenu[intCount].id + ',\'D\');">&darr;</a></td>';
                    strHtml += '    </tr>';
                }
                else {
                    if (this.pullmenu[intCount].isvisible == "Y") {
                        strHtml += '    <tr>';
                        strHtml += '        <td nowrap="nowrap"><a href="page.htm?id=' + this.pullmenu[intCount].id + '">' + this.pullmenu[intCount].title + '</a></td>';
                        strHtml += '        <td></td>';
                        if (rcode == "CM" && isContentPage) {
                            strHtml += '        <td><a href="javascript:myContentMgr.updateMenuSequence(' + this.pullmenu[intCount].id + ',\'U\');">&uarr;</a></td>';
                            strHtml += '        <td><a href="javascript:myContentMgr.updateMenuSequence(' + this.pullmenu[intCount].id + ',\'D\');">&darr;</a></td>';
                        }
                        strHtml += '    </tr>';
                    }
                }
            }
        }
        strHtml += '</table>';
        strHtml += '</div>';
        strHtml += '<div id="m4" onmouseover="mcancelclosetime()" onmouseout="mclosetime()">';
        strHtml += '<table cellspacing="0" cellpadding="0">';
        for (intCount = 0; intCount < this.pullmenu.length; intCount++) {
            if (this.pullmenu[intCount].parent_id == "4") {
                if (rcode == "CM" && isContentPage && this.pullmenu[intCount].iscontent == "N") {
                    strHtml += '    <tr>';
                    strHtml += '        <td nowrap="nowrap"><a href="page.htm?id=' + this.pullmenu[intCount].id + '">' + this.pullmenu[intCount].title + '</a></td>';
                    strHtml += '        <td><a href="javascript:myContentMgr.updateMenu(' + this.pullmenu[intCount].id + ');">Edit</a></td>';
                    strHtml += '        <td><a href="javascript:myContentMgr.updateMenuSequence(' + this.pullmenu[intCount].id + ',\'U\');">&uarr;</a></td>';
                    strHtml += '        <td><a href="javascript:myContentMgr.updateMenuSequence(' + this.pullmenu[intCount].id + ',\'D\');">&darr;</a></td>';
                    strHtml += '    </tr>';
                }
                else {
                    if (this.pullmenu[intCount].isvisible == "Y") {
                        strHtml += '    <tr>';
                        strHtml += '        <td nowrap="nowrap"><a href="page.htm?id=' + this.pullmenu[intCount].id + '">' + this.pullmenu[intCount].title + '</a></td>';
                        strHtml += '        <td></td>';
                        if (rcode == "CM" && isContentPage) {
                            strHtml += '        <td><a href="javascript:myContentMgr.updateMenuSequence(' + this.pullmenu[intCount].id + ',\'U\');">&uarr;</a></td>';
                            strHtml += '        <td><a href="javascript:myContentMgr.updateMenuSequence(' + this.pullmenu[intCount].id + ',\'D\');">&darr;</a></td>';
                        }
                        strHtml += '    </tr>';
                    }
                }
            }
        }
        strHtml += '</table>';
        strHtml += '</div>';
        strHtml += '<div id="m5" onmouseover="mcancelclosetime()" onmouseout="mclosetime()">';
        strHtml += '<table cellspacing="0" cellpadding="0">';
        for (intCount = 0; intCount < this.pullmenu.length; intCount++) {
            if (this.pullmenu[intCount].parent_id == "5") {
                if (rcode == "CM" && isContentPage && this.pullmenu[intCount].iscontent == "N") {
                    strHtml += '    <tr>';
                    strHtml += '        <td nowrap="nowrap"><a href="page.htm?id=' + this.pullmenu[intCount].id + '">' + this.pullmenu[intCount].title + '</a></td>';
                    strHtml += '        <td><a href="javascript:myContentMgr.updateMenu(' + this.pullmenu[intCount].id + ');">Edit</a></td>';
                    strHtml += '        <td><a href="javascript:myContentMgr.updateMenuSequence(' + this.pullmenu[intCount].id + ',\'U\');">&uarr;</a></td>';
                    strHtml += '        <td><a href="javascript:myContentMgr.updateMenuSequence(' + this.pullmenu[intCount].id + ',\'D\');">&darr;</a></td>';
                    strHtml += '    </tr>';
                }
                else {
                    if (this.pullmenu[intCount].isvisible == "Y") {
                        strHtml += '    <tr>';
                        strHtml += '        <td nowrap="nowrap"><a href="page.htm?id=' + this.pullmenu[intCount].id + '">' + this.pullmenu[intCount].title + '</a></td>';
                        strHtml += '        <td></td>';
                        if (rcode == "CM" && isContentPage) {
                            strHtml += '        <td><a href="javascript:myContentMgr.updateMenuSequence(' + this.pullmenu[intCount].id + ',\'U\');">&uarr;</a></td>';
                            strHtml += '        <td><a href="javascript:myContentMgr.updateMenuSequence(' + this.pullmenu[intCount].id + ',\'D\');">&darr;</a></td>';
                        } 
                        strHtml += '    </tr>';
                    }
                }
            }
        }
        strHtml += '</table>';
        strHtml += '</div>';
        strHtml += '</div>';
        document.getElementById("menu1").innerHTML = strHtml;
    };
};
var mymenu = new menu();
mymenu.load();