var isZoomToExtent = false;
var mapImageRequestId = null;
var mapUpdateRequestId = null;
function cancelJsonResponse() {
    if (mapImageRequestId != null)
            JSONRpcClient.cancelRequest(mapImageRequestId);
    if (mapUpdateRequestId != null)
            JSONRpcClient.cancelRequest(mapUpdateRequestId); 
}





function getMapImage(datasource, pminx, pminy, pmaxx, pmaxy) {
        if(pminx < limitMinx)pminx=limitMinx;
        if(pminy < limitMiny)pminy=limitMiny;
        if(pmaxx > limitMaxx)pmaxx=limitMaxx;
        if(pmaxy > limitMaxy)pmaxy=limitMaxy;
        //mapImageRequestId = jsonrpc.builder_mapImage.getMapImageInfo(setMapImage, datasource, pminx, pminy, pmaxx, pmaxy, mapWidth, mapHeight);
		//myAlert="SIMULATION:\n\nThese variables are ready to send to Server via AJAX:\n\nminx:\t"+pminx+"\nmaxx:\t"+pmaxx+"\n\nminy:\t"+pminy+"\nmaxy:\t"+pmaxy+"\n\n\nmapwidth:\t"+mapWidth+"\nmapheight:\t"+mapHeight+"\n\nZoom Level:\t"+parseInt(whichZOOM);

		//setTimeout("alert(myAlert);doSuccessMapSet();setMapImageUrl('img/map.gif')",2000);
		//DWREngine._execute(_cfscriptLocation, null, 'sendAJAX','component','ArcIMS.getMap', 'minx',pminx, 'miny',pminy, 'maxx',pmaxx, 'maxy',pmaxy, 'mapwidth',mapWidth, 'mapheight',mapHeight, recieveMapImage);
		DWREngine._execute(_cfscriptLocation, null, 'sendAJAX','component','Tiger.getMap', 'minx',pminx, 'miny',pminy, 'maxx',pmaxx, 'maxy',pmaxy, 'mapwidth',mapWidth, 'mapheight',mapHeight,'returnLayers',true, recieveMapImage);
}



function recieveMapImage(obj){
		if(obj.SUCCESS==1) setMapImage(obj,null);
			
		else{	
			//showError(_mapObject.ERROR);
			//alert(_mapObject.MESSAGE);
			doFailMapSet(obj.ERROR,obj.MESSAGE)
				}
				//var mapObject = _mapObject;
				
				//document.images[mapImageName].src = result;
		    	//showLayer(mapImageId);
		        //doSuccessMapSet();
}


function getZoomToFeature(Axl_ID,KeyField,featureID){
		DWREngine._execute(_cfscriptLocation, null, 'sendAJAX','component','ArcIMS.getExtentOfFeature', 'Axl_ID', Axl_ID, 'KeyField', KeyField, 'featureID', featureID, doZoomToFeature);
}

function doZoomToFeature(results){
    	hideLoading();
		newMinx=results.MINX;newMiny=results.MINY;newMaxx=results.MAXX;newMaxy=results.MAXY;
		refreshMap(newMinx,newMiny,newMaxx,newMaxy);
}

function disableLayerList(item){
for(var i=0;i<document.getElementsByTagName("Input").length;i++){
	myID=document.getElementsByTagName("Input")[i].id;
	if(myID.indexOf("treeItem_") >= 0){
	document.getElementById(myID).disabled=true;
	}
}

}
// CLICK on Layer Item		
		function checkTreeItem(item,id){
		if(!loading){
		disableLayerList(item);
				//alert("item:"+item+"\nid:"+id+"\naxl_ID:"+axl_ID+"\nloadMap:"+loadMap+"\ndatasource:"+datasource);
					isChecked=item.checked;
					myAction=(isChecked)?1:0;
				//alert("checkTreeItem  = "+item+"\nisChecked:"+isChecked+"\nid:"+id+"\n");
				if(id==1 || id ==2 || id ==3){
						DWREngine._execute(_cfscriptLocation, null, 'sendAJAX','component','MapGUI.toggleLayerScript','script','1=0,2=0,3=0,'+id+"="+myAction, updatemap);
						}
						
						else {
						DWREngine._execute(_cfscriptLocation, null, 'sendAJAX','component','MapGUI.toggleLayer','layerID',id,'action',myAction, updatemap);
						}
				}
				else{
				alert("Please wait until current update is complete.");
				}
			}
		
function resetLayers(){
				DWREngine._execute(_cfscriptLocation, null, 'sendAJAX','component','MapGUI.resetLayers', updatemap);

			}

function loadMetadata(id){
				tmpz=window.open('Metadata.cfm?id='+id+'&','Metadata','width=600px,height=600px,scrollbars,resizable');
				//DWREngine._execute(_cfscriptLocation, null, 'sendAJAX','component','MapGUI.getMetaDataHTML','LayerId',id, showInformation);

			}			

function showInformation(obj){
				var	myText=obj;
				tmpz=window.open('Metadata','Metadata','width=600px,height=600px,scrollbars,resizable');
				tmpz.document.write("<html>"+myText+"</body></html>");
				tmpz.document.close();
				//tmpz.moveTo(1255,0);
}		
function updatemap(blank){
	refreshMap(minx,miny,maxx,maxy);
}

function setMapImage(result, e) {
    
    if (e != null) {
        doFailMapSet(e);
    } else {
        setMapImageUrl(result.MAPURL);
        setOutputExtent(+result.MINX, +result.MINY, +result.MAXX, +result.MAXY);
		document.getElementById("div_layerList").innerHTML=result.LAYERTREE;
		//document.getElementById("div_legendList").innerHTML=result.LEGENDTREE;
		document.getElementById("div_MapExperience").innerHTML=result.MAPEXPERIENCE;
		document.getElementById("imgSrc").value=result.MAPURL+"\n"+result.MINX+"\n"+ result.MINY+"\n"+ result.MAXX+"\n"+ result.MAXY;
        setOutputScale(result.SCALE,result.TEXTSCALE);
		
	document.getElementById("div_mapScaleSLA").innerHTML='1:'+result.SLA_SCALE;
        if (isZoomToExtent) {
            doSuccessExtentSet();
        } else {
            doSuccessMapSet();
        }
    }
}

function getPointMapImage(datasource, px, py, getGcs, pminx, pminy, pmaxx, pmaxy) {
       
        mapImageRequestId = jsonrpc.builder_mapImage.getPointMapImageInfo(setMapImage, datasource, px, py, getGcs, pminx, pminy, pmaxx, pmaxy, mapWidth, mapHeight);
}

function updateMapDatasourceByAddLayers(datasource, mapStyleSheet, mapLayers, startIndex, len) {
    
    mapUpdateRequestId = jsonrpc.builder_mapImage.updateMapDatasourceByGetLayers(setNewLayers, datasource, mapStyleSheet, mapLayers, startIndex, len);
}

function updateMapDatasourceByAddDS(datasource, mapStyleSheet, mapLayers, newDataSources, startIndex) {
   
    //alert("style "+mapStyleSheet+" datasource "+datasource+" newDataSource "+newDataSources[0]+" len "+newDataSources.length+" startIndex "+startIndex);
    //if it is from opened window, we'll have to reset array for json to work, weird but working...
    var ds = new Array();
    for (var i=0; i<newDataSources.length; i++) {
        ds[i] = newDataSources[i];
    }
    mapUpdateRequestId = jsonrpc.builder_mapImage.updateMapDatasourceByAddDS(setNewLayers, datasource, mapStyleSheet, mapLayers, ds, startIndex);
}

function updateMapDatasourceByExtent(datasource, pminx, pminy, pmaxx, pmaxy) {
    mapUpdateRequestId = jsonrpc.builder_mapImage.updateMapDatasourceByExtent(setNewExtent, datasource, pminx, pminy, pmaxx, pmaxy);
}
function cleanMapDatasourceExtent(datasource) {
    mapUpdateRequestId = jsonrpc.builder_mapImage.cleanMapDatasourceExtent(setCleanExtent, datasource);
}
function setCleanExtent(extent, e) {
    if (e != null) {
        doFailCleanExtent(e);
    } else {
        setSuccessCleanExtent(extent);
    }
}
function setNewExtent(extent, e) {
    if (e != null) {
        doFailNewExtent(e);
    } else {
        setSuccessNewExtent(extent);
    }
}
function setNewLayers(newMap, e) {
    
    if (e != null) {
        doFailAddNewLayers(e);
    } else {
        setNewLayersToList(newMap);
    }
}
function updateDatasourceStyle(datasource, mapStyleSheet, mapLayers, styleLayerIndex) {
    
    mapUpdateRequestId = jsonrpc.builder_mapImage.updateMapDatasourceByGetLayers(setNewLayerStyle, datasource, mapStyleSheet, mapLayers, styleLayerIndex, 1);
}
function setNewLayerStyle(newMap, e) {
    
    if (e != null) {
        doFailStyleUpdate(e);
    } else {
        setSuccessStyleUpdate(newMap.layers[0]);
    }
}

function updateMapDatasource(datasource, mapLayers) {
    mapUpdateRequestId = jsonrpc.builder_mapImage.updateMapDatasource(setUpdatedMap, datasource, mapLayers);
}
function setUpdatedMap(env, e) {
    
    if (e != null) {
        doFailUpdateMapLayers(e);
    } else {
        setSuccessUpdateMapLayers(env);
    }
}

function getMapImageInfoByStylesheet(datasource, mapStyleSheet, pminx, pminy, pmaxx, pmaxy, extentEnv) {
    mapUpdateRequestId = jsonrpc.builder_mapImage.getMapImageInfoByStylesheet(setMapImage, datasource, mapStyleSheet, pminx, pminy, pmaxx, pmaxy, mapWidth, mapHeight, extentEnv);      
}

function getMapImageInfoByScale(datasource, mapStyleSheet, x, y, scale, extentEnv) {
    mapImageRequestId = jsonrpc.builder_mapImage.getMapImageInfoByScale(setMapImage, datasource, mapStyleSheet, x, y, scale, mapWidth, mapHeight, extentEnv);      
}

function getMapDatasourceStyle(datasource, mapStyleSheet) {
    mapUpdateRequestId = jsonrpc.builder_mapImage.getLocalMapDatasource(setMapDatasourceStyle, datasource, mapStyleSheet);
}

function setMapDatasourceStyle(map, e) {

    if (e != null) {
        doFailMapDatasourceStyle(e);
    } else {
        setSuccessMapDatasourceStyle(map);
    }
}
function createMapDatasource(mapStyleSheet, mapLayers) {
    mapUpdateRequestId = jsonrpc.builder_mapImage.createTempMapDatasource(getCreatedMapDatasource, mapStyleSheet, mapLayers);
}
function createMapDatasourceByDS(mapStyleSheet, datasources) {
    
    mapUpdateRequestId = jsonrpc.builder_mapImage.createMapDatasourceByDS(getCreatedMapDatasource, mapStyleSheet, datasources);
}
function getCreatedMapDatasource(newDatasource, e) {
    if (e != null) {
        doFailCreatedMapDatasource(e);
    } else {
        setSuccessCreatedMapDatasource(newDatasource);
    }
}
function addLayersToNewDataSource(newLayers, newName, newTitle) {
    mapUpdateRequestId = jsonrpc.builder_mapImage.addLayersToNewDataSource(getNewDataSource, newLayers, newName, newTitle);
}
function copyToNewDataSource(datasources, newName, newTitle) {
    mapUpdateRequestId = jsonrpc.builder_mapImage.copyToNewDataSource(getNewDataSource, datasources, newName, newTitle);
}
function getNewDataSource (result, e) {
    if (e != null) {
        doFailNewDataSource(e);
    } else {
        setSuccessNewDataSource(result);
    }
}
function addLayersToDataSource(datasource, newLayers) {
    mapUpdateRequestId = jsonrpc.builder_mapImage.addLayersToDataSource(getAddResponse, datasource, newLayers);
}
function copyToDataSource(datasource, datasources) {
    mapUpdateRequestId = jsonrpc.builder_mapImage.copyToDataSource(getAddResponse, datasource, datasources);
}
function getAddResponse (result, e) {
    if (e != null) {
        doFailAdd(e);
    } else {
        setSuccessAdd(result);
    }
}

function deleteThisMapDataSource(datasource) {
    jsonrpc.builder_mapImage.deleteMapDataSource(datasource);
}