﻿function buildNavToolBar(navXMLElements){
    navhtmlString = "";
    
    
    //navXMLElements.childNodes.length
    for (i = 0; i < navXMLElements.childNodes.length; i++) {
        
        if (navXMLElements.childNodes[i].nodeName.indexOf("button") > 0) {
            id = navXMLElements.childNodes[i].getElementsByTagName('id')[0].childNodes[0].nodeValue;
            image = navXMLElements.childNodes[i].getElementsByTagName('image')[0].childNodes[0].nodeValue;
            text = navXMLElements.childNodes[i].getElementsByTagName('text')[0].childNodes[0].nodeValue;
            action = navXMLElements.childNodes[i].getElementsByTagName('action')[0].childNodes[0].nodeValue;
            cursor = navXMLElements.childNodes[i].getElementsByTagName('cursor')[0].childNodes[0].nodeValue;
            navhtmlString = navhtmlString + "<li id='"+id+"' onClick='"+action+"'>"+
                                        "<a style='cursor:"+cursor+"' href='#'>"+
                                            "<span>"+
                                                "<img align='left' src='"+image+"'/>"+text+
                                            "</span>"+
                                        "</a>"+
                                    "</li>";    
        }                      
    }
    dojo.byId("navigation").innerHTML = navhtmlString;
}


//var items = xml.getElementsByTagName('item')[0];//grab the first item node
//var title = items.getElementsByTagName('title')[0].firstChild.data;//Gourmet gizmo


function extentHistoryChangeHandler() {
    //dijit.byId("zoomprev").disabled = navToolbar.isFirstExtent();
    //dijit.byId("zoomnext").disabled = navToolbar.isLastExtent();
}
function zoomIn(){
    selectButton("zoomin_button");
    navToolbar.deactivate();
    currentMapTool = esri.toolbars.Navigation.ZOOM_IN;
    navToolbar.activate(currentMapTool);
    //changeCursor(7);
   
}
function zoomOut(){
    selectButton("zoomout_button");
    navToolbar.deactivate();
    currentMapTool = esri.toolbars.Navigation.ZOOM_OUT;
    navToolbar.activate(currentMapTool);
    //changeCursor(8);
}
function pan(){
    selectButton("pan_button");
    navToolbar.deactivate();
    currentMapTool = esri.toolbars.Navigation.PAN;
    navToolbar.activate(currentMapTool);
    //changeCursor(1);
    
//            dojo.connect(map, "onMouseUp", doMouseGrab);
//            dojo.connect(map, "onMouseDown", doMouseGrabbing);
//            dojo.connect(map, "onMouseDrag", doMouseGrabbing);
//            dojo.connect(map, "onPan", doMouseGrab);
//            dojo.connect(map, "onPanStart", doMouseGrabbing);
//            dojo.connect(map, "onPanEnd", doMouseGrab);
    
}
function doMouseGrab(evt)
{
    mapdiv_root.style.cursor = "url(img/opendhand_8_8.cur)"
}
function doMouseGrabbing(evt)
{
    mapdiv_root.style.cursor = "url(img/closedhand_8_8.cur)"
}
function previousExtent(){
    navToolbar.deactivate();
    navToolbar.zoomToPrevExtent();
}
function nextExtent(){
    navToolbar.deactivate();
    navToolbar.zoomToNextExtent();
}
function fullExtent(){
    navToolbar.deactivate();
    navToolbar.zoomToFullExtent();
}

function identify(){
    selectButton("identify_button")
    navToolbar.deactivate();
  //  dojo.connect(map, "onClick", doIdentify);
    currentMapTool = "identify";
    //map.getContainer().currentStyle.cursor = "url(img/pan.cur)";
    //document.getElementById("mapdiv").style.cursor="url(img/pan.cur)";
   // mapdiv = document.getElementById("mapdiv");
}

function selectButton(buttonid)
{   
// try changing the cursor here
    deselectLastButton();
    button = document.getElementById(buttonid);
    xmlDoc = getXMLDoc("data/settings.xml");
    image = xmlDoc.getElementsByTagName(buttonid)[0].getElementsByTagName('image')[0].childNodes[0].nodeValue;
    text = xmlDoc.getElementsByTagName(buttonid)[0].getElementsByTagName('text')[0].childNodes[0].nodeValue;
    button.innerHTML = '<a style="background: url(img/left_selected.png) no-repeat left center; cursor:pointer;">'+
                            '<span style="background:url(img/right_selected.png) no-repeat right center;">'+
                                '<img align="left" src="'+image+'"/>'+text+
                            '</span>'+
                        '</a>';
    
}
function deselectLastButton()
{
    if(currentMapTool == 'identify'){
        var identifyEvent = dojo.connect(map, "onClick", doIdentify)
        dojo.disconnect(identifyEvent);
    }
    button = document.getElementById(currentMapTool+"_button");
    xmlDoc = getXMLDoc("data/settings.xml");
    image = xmlDoc.getElementsByTagName(currentMapTool+"_button")[0].getElementsByTagName('image')[0].childNodes[0].nodeValue;
    text = xmlDoc.getElementsByTagName(currentMapTool+"_button")[0].getElementsByTagName('text')[0].childNodes[0].nodeValue;
    button.innerHTML = '<a href="#">'+
                            '<span>'+
                                '<img align="left" src="'+image+'"/>'+text+
                            '</span>'+
                        '</a>';
} 
function changeCursor(action){
    // var cururl = "http://localhost/mapguide/stdicons/";
    //var curur1 = "../img/";
    //var map = parent.mapFrame.document.getElementById("mapdiv");
    switch(action)
    {

            case 1:
                    mapdiv_root.style.cursor = "url('img/openhand_8_8.cur')";
                    //map.style.cursor = cururl + "pan.cur"
                    break;
            case 7:
                    mapdiv_root.style.cursor = "url('img/zoomin.cur')";
                    //map.style.cursor = cururl + "zoomin.cur"
                   //debugger;
                    break;
            case 8:
                    mapdiv_root.style.cursor = "url('img/zoomout.cur')";
                   // map.style.cursor = cururl + "zoomout.cur"
                    break;
            case 9:
                    mapdiv_root.style.cursor = "url('img/zoomrect.cur')";
                    //map.style.cursor = cururl + "zoomrect.cur"
                    break;
            default:
                    mapdiv_root.style.cursor = "default";
                   // map.style.cursor = 'default';
                    break;
    } 
}       


