
function displayResource(data) {
 if (app.options.resource) {
   for (var prop in data.resource) {
      var o = data.resource[prop];
      var min = 8;
      var max = 10;      
      if ($('onlyShowResults') && $('onlyShowResults').checked && !in_results('resource',o.object_id)) {
         if (app.markers.resource[o.object_id]) map.removeOverlay(app.markers.resource[o.object_id]);
         delete(app.markers.resource[o.object_id]);
         continue;          
      }
      if (!app.markers.resource[o.object_id] && !(o.loc_x == 0 && o.loc_y == 0)) {
         if (map.getZoom() >= min && map.getZoom() <= max) {
            var icon = app.icons.resource;
            if ((isNaN(o.lat) || isNaN(o.lng)) || (o.lng == 0 || o.lat == 0)) {
               var p = coordsToLatLng(o.loc_x, o.loc_y);
            } else {
               var p = new GLatLng(o.lat,o.lng);  
            }
            var m = new GMarker(p,{'icon': icon});
            GEvent.addListener(m,"mouseover", function() {showTooltip(this)});
            GEvent.addListener(m,"mouseout", function() {tooltip.style.visibility="hidden"});             
            m.data = o;
            m.type_id = o.object_id;
            m.detailType = 'Resource';
            m.title = jsonToText(o.title);
            m.isMarker = true;
            app.markers.resource[o.object_id] = m;
            map.addOverlay(app.markers.resource[o.object_id]);
         }
      } else if (app.markers.resource[o.object_id]) {
         if (map.getZoom() < min && map.getZoom() > max) {
            map.removeOverlay(app.markers.resource[o.object_id]);
         }
      }
      if (map.getZoom() >= min && map.getZoom() <= max) {
            var pcoords = coordsToText(String(o.loc_x),String(o.loc_y));
            var ptext = pcoords[1].replace('.','p')+pcoords[0].replace('.','p');           
            if (!app.markerCoords[ptext]) app.markerCoords[ptext] = new Array();
            app.markerCoords[ptext].push(app.markers.resource[o.object_id]);          
      }
      
   }   
 } else {
  hideMarkers('resource');  
 }
}

function resourceInfo(o,m) {
       var coordsArry = coordsToText(o.data[0].loc_x,o.data[0].loc_y);
       var urlTitle = textToURL(o.data[0].title);       
       app.reportMenuCount++;
       
       var drops = '';
       for (prop in o.drop) {
         var dTitle = textToURL(o.drop[prop].title);
         drops += '<div><a href=http://www.arda-online.com/lotro/item.php?'+dTitle+'_'+o.drop[prop].object_id+' target=_blank>'+o.drop[prop].title+'</a></div>';
       }       
       
       $('ResourceInfo_'+m.type_id).innerHTML = '<div><table width=100%><tbody><tr><td width=65%></td><td width=35% class=aRight>'
       +'<a href=javascript:editResource()>[edit]</a> | <a href="javascript:reportEntryMenu('+app.reportMenuCount+')">[report]</a>'
       +'</td></tr><tr><td></td><td><div class=Dnone id=rm'+app.reportMenuCount+'></div></td></tr></tbody></table></div>'       
       +'<table><tr><td class=infoLabel>Area</td><td class=infoField>'+o.parentName+'</td></tr>'
       +'<tr><td class=infoLabel>Name</td><td class=infoField>'+o.data[0].title+'</td></tr>'
       +'<tr><td class=infoLabel>Type</td><td class=infoField>'+o.data[0].type2+'</td></tr>'
       +'<tr><td class=infoLabel>Level</td><td class=infoField>'+o.data[0].type3+'</td></tr>'
       +'<tr><td class=infoLabel>Description</td><td class=infoField>'+o.data[0].descript+'</td></tr>'
       +'<tr><td class=infoLabel>Location</td><td class=infoField>'+coordsArry[1]+','+coordsArry[0]+'</td></tr>'
       +'<tr><td class=infoLabel>Drops Items</td><td class=infoField>'+drops+'</td></tr>'       
       +'<tr><td class=infoLabel>PermaLink</td><td class=infoField><a href=http://www.arda-online.com/map/?Resource:'+urlTitle+'_'+m.type_id+'>Link to this entry</a></td></tr>'       
       +'<tr><td class=infoLabel>Added By</td><td class=infoField><a href=http://www.arda-online.com/user.php?id.'+o.creatorID+'>'+o.creatorName+'</a></td></tr>'       
       +'</table>';
       
       //var html = 'Coming Soon...';
       //$('ResourceRelated_'+m.type_id).innerHTML = '<div class=infoEditBar><a href=javascript:editLocation()>[edit]</a> | <a href=\'#\' onclick=reportEntryMenu(this)>[report]</a><div class=Dnone></div></div>'
       //+html;       
       
       html = '<div class=aCenter><table><tr>';
       var count = 0;
       for (var prop in o.image) {
         var row = o.image[prop];
         if (count == 2) {
            html += '</tr><tr>';  
            count = 0;
         }
         html += '<td><a href=../images/public/'+row.full_src+' target=_blank><img src=../images/public/thumbs/'+row.full_src+' width=125 border=0></a></td>';
         count++;
       }
       html += '</tr></table></div>';
       app.reportMenuCount++;
       $('ResourceScreenshot_'+m.type_id).innerHTML = '<div><table width=100%><tbody><tr><td width=65%></td><td width=35% class=aRight>'
       +'<a href=javascript:editResource()>[edit]</a> | <a href="javascript:reportEntryMenu('+app.reportMenuCount+')">[report]</a>'
       +'</td></tr><tr><td></td><td><div class=Dnone id=rm'+app.reportMenuCount+'></div></td></tr></tbody></table></div>'
       +html;      
       
       var cc = 0;
       var type_id = 'object_id';
       html = '<table id=c_'+type_id+'_'+m.type_id+' width=100%><tbody>';
       for (prop in o.comments) {
         cc++;
         var comment = o.comments[prop];
         var postDate = new Date(comment.timestamp*1000);
         var dateString = postDate.getMonth()+'.'+postDate.getDate()+'.'+postDate.getFullYear()+' '+(postDate.getHours()+1)+':'+postDate.getMinutes();
         html += '<tr><td class=commentText>'+comment.text+'</td></tr>'
         +'<tr><td class=commentInfo>'+comment.user_name+' - '+dateString+'</td></tr>';
       }
       html += '</tbody></table><br><a href="javascript:addComment(\''+type_id+'\','+m.type_id+')">Add a Comment</a>';
       app.reportMenuCount++;
       $('ResourceComments_'+m.type_id).innerHTML = '<div><table width=100%><tbody><tr><td width=65%></td><td width=35% class=aRight>'
       +'<a href=javascript:editResource()>[edit]</a> | <a href="javascript:reportEntryMenu('+app.reportMenuCount+')">[report]</a>'
       +'</td></tr><tr><td></td><td><div class=Dnone id=rm'+app.reportMenuCount+'></div></td></tr></tbody></table></div>'
       +html;
             
}


function addResource() {
   //app.adding = true;
   app.addType = 'Resource';
   if (app.user_id == '0') {
      hideAllMenus();     
      frontAndCenter($(app.addType+'Div'),800,500);
      displayBlock($(app.addType+'Div'));
   } else {    
      app.addPath = 'addResource.php';
      app.addStart = addResourceStart;
  // app.mapStatus.innerHTML = 'Click the map to add a '+app.addType+'...';
  // displayBlock(app.mapStatus);   
       resetForm('Resource');
       app.currentMarker = null;
       $('ResourceLocationList').innerHTML = '';
       displayInline($('resourceAddLocButton'));
       
    addResourceStart();  
   }
}

function addResourceStart() {


   var f = document.forms.Resource;
     // f.ResourceLocXY.value = app.addCoords;
    //  f.ResourcePoint.value = app.addPoint.toUrlValue(12);
      f.ResourceLocParentText.onkeyup = autoSuggest;
      f.ResourceLocParentText.onkeypress = autoSuggest;
      f.ResourceLocParentText.onfocus = autoSuggest;   
            hideAllMenus();   
      frontAndCenter($(app.addType+'Div'),800,500);
   displayBlock($(app.addType+'Div'));

}

function resourceSuggest(q) {
	var d = new Date();
         app.autoSuggest.u = 'run/getLocation.php?q='+q+'&d='+d.valueOf();
         app.autoSuggest.returnFunc = getLocationReturn;
         app.autoSuggest.loading = $('ResourceLoading');
         app.autoSuggest.drop = $('ResourceLocParentDrop');
         app.autoSuggest.idField = document.forms.Resource.ResourceLocParentID;
         app.autoSuggest.span = $('ResourceLocParentSpan');  
         $('ResourceLocParentSpan').idField  =  document.forms.Resource.ResourceLocParentID;
         $('ResourceLocParentSpan').textField = document.forms.Resource.ResourceLocParentText;            
}

function submitResource() {
   var f = document.forms['Resource'];
   if (!f.ResourceLocParentID.value.match(/[1-9][0-9]*/gi)) {
      alert('Please select a parent area!');
      f.ResourceLocParentText.focus();
      return false;
   }
   var v = f.ResourceTitle.value;
   if (v == '' || v.match(/[^a-z0-9 '-]/gi)) {
      alert('Please enter a Title. It may only contain letters, numbers, spaces, and the apostrophe.');
      f.ResourceTitle.focus();
      return false;
   }

   var locArry = new Array();
   var pointArry = new Array();
   var list = $('ResourceLocationList');
   for (var i = 0; i < list.childNodes.length; i++) {
      var xy = list.childNodes[i].firstChild.innerHTML;
      var point = list.childNodes[i].childNodes[1].innerHTML;
      locArry.push(xy);
      pointArry.push(point);
   }
   var locText = locArry.join('|');
   var pointText = pointArry.join('|');
   f.ResourceLocXYs.value = locText;
   f.ResourcePoints.value = pointText;
   
   var list = $('ResourceDropList');
   var dropList = '';
   var p = '';
   for (var i = 0; i < list.childNodes.length; i++) {
      dropList += p+list.childNodes[i].firstChild.innerHTML;
      p = '|';
   }
   f.ResourceDrops.value = dropList;   
   
   var postText = preparePost(f);
   var url = 'run/addResource.php?action=add';
   addPostMessage('Resource Submitted!');
   disableMe(f.ResourceButton);
   app.editButton = f.ResourceButton;
   getRequest(url,'POST',postText,submitResourceReturn);
}

function submitResourceReturn(responseText) {
   enableMe(app.editButton);
   if (!responseText) return false;
   var o = eval(responseText);
   if (o.status) {
      app.gettingResource = false;  
      app.adding = false;  
      addPostMessage('OK');
      var id = (app.currentMarker) ? app.currentMarker.type_id : o.new_id;
      if (app.currentMarker && app.markerDetails[app.currentMarker.detailType][app.currentMarker.type_id]) {
      	   delete(app.markerDetails[app.currentMarker.detailType][app.currentMarker.type_id]);
      }      
      if (app.currentMarker && app.markers.resource[app.currentMarker.type_id]) {
  
         map.removeOverlay(app.markers.resource[app.currentMarker.type_id]);
         delete(app.markers.resource[app.currentMarker.type_id]);
      }     
      app.currentMarker = null;
      app.options.resource = true;
      resetForm('Resource');
      $('ResourceLocationList').innerHTML = '';
      $('f6').firstChild.src = 'on.png';
      app.resultToShow = id;
      app.showResult = true;  
      app.typeToShow = 'resource';    
      closeAddWindow($("ResourceDiv"),true);
   } else {
     addPostMessage('Failed! '+o.error);
   }
}


function getResourceForEdit() {
   hideAllMarkers();
   $('mapStatus').innerHTML = 'Click for new Resource Location...'; 
   displayBlock($('mapStatus'));
   app.gettingLocation = true;
   
   map.closeInfoWindow();


   displayNone($(app.addType+'Div'));
   app.editFunction = setNewLocationResource;
   $('map').firstChild.firstChild.style.cursor = 'crosshair';   
   
   
}
function setNewLocationResource() {
   var f = document.forms.Resource;
   f.ResourceLocXY.value = app.editCoords;
   f.ResourcePoint.value = app.editPoint.toUrlValue(12);
   displayBlock($(app.addType+'Div'));
}

function editResource(newLocation) {
    app.addType = 'Resource';
    resetForm('Resource');
    $('ResourceLocationList').innerHTML = '';
    displayNone($('resourceAddLocButton'));
    if (newLocation) {
             var p = app.currentMarker.getPoint();
             var m = new GMarker(p);


             map.addOverlay(m);
             var d = new Date();

             
             var pro = map.getCurrentMapType().getProjection();
             var pnt = pro.fromLatLngToPixel(p,map.getZoom());
             var c = latLngToCoords(pnt,map.getZoom());
             var coords = coordsToText(String(c.x),String(c.y));               
             
             app.editResourceCoords = coords[1]+','+coords[0];
             app.editPoint = p;             
} else {
             var m = app.currentMarker;
             var o = m.detailData.data[0];
             var coords = coordsToText(String(o.loc_x),String(o.loc_y));   
             app.editResourceCoords = coords[1]+','+coords[0];
             if (o.lat == 0 || o.lng == 0) {
                app.editPoint = coordsToLatLng(o.loc_x,o.loc_y);
             } else {
                app.editPoint = new GLatLng(o.lat,o.lng); 
             }
}
             
             
             editResourceStart();
}


function editResourceStart() {

      var f = document.forms.Resource;
      var m = app.currentMarker;
      var o = m.detailData.data[0];
      f.ResourceLocXY.value = app.editResourceCoords;
      f.ResourcePoint.value = app.editPoint.toUrlValue(12);      
      f.ResourceLocParentText.onkeyup = autoSuggest;
      f.ResourceLocParentText.onkeypress = autoSuggest;
      f.ResourceLocParentText.onfocus = autoSuggest;
         $('ResourceLocParentSpan').idField  =  f.ResourceLocParentID;
         $('ResourceLocParentSpan').textField = f.ResourceLocParentText;   
         displayInline($('ResourceLocParentSpan'));        
      f.ResourceUpdateID.value = o.object_id;
      f.ResourceUpdating.value = 1;
      f.ResourceButton.value = 'Update Resource';
      f.ResourceLocParentID.value = o.location_id_parent;
      f.ResourceUpdateLocationID.value = o.location_id;
      displayNone(f.ResourceLocParentText);
      app.autoInput = f.ResourceLocParentText;
      displayInline($('ResourceGetNew'));
      $('ResourceLocParentSpan').innerHTML = m.data.parentName+'<img src=cancel.png class=Hand onclick=cancelLocation(this) style=\'padding-left: 3px\'>';
      f.ResourceTitle.value = o.title;
      f.ResourceType.value = o.type2;
      f.ResourceLevel.value = o.type3;
      f.ResourceDescript.value = o.descript;
      $('ResourceDropList').innerHTML = '';
       for (prop in m.detailData.drop) {
         f.ResourceDrop.value = m.detailData.drop[prop].title;
         resourceAddDrop();
       }        
      
      
      frontAndCenter($(app.addType+'Div'),800,500);
   displayBlock($(app.addType+'Div'));
}

function addResourceLocation(f) {
   var xy = f.ResourceLocXY.value;
   var p = f.ResourcePoint.value;
   if (xy == '' || p == '') return false;
   var list = $('ResourceLocationList');
   if (!app.ResourceLocCount) app.ResourceLocCount = 1;
   var html = '<div id=r'+app.ResourceLocCount+'><span>'+xy+'</span><span class=Dnone>'+p+'</span><span><a href="javascript:removeResourceLoc('+app.ResourceLocCount+')"><img src=cancel.png border=0 style="padding-left: 3px"></a></span></div>';
   app.ResourceLocCount++;   
   list.innerHTML += html;
   f.ResourceLocXY.value = '';
   f.ResourcePoint.value = '';
}

function removeResourceLoc(id) {
   $('r'+id).parentNode.removeChild($('r'+id));
}


function resourceAddDrop() {
   var f = document.forms.Resource;
   var list = $('ResourceDropList');
   var inp = f.ResourceDrop;
   var v = inp.value.replace(/|/g,'');
   if (v == '') return false;
   var html = '<div><span>'+v+'</span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=cancel.png class=Hand onclick=cancelResourceDrop(this)></span></div>';
   list.innerHTML += html;
   inp.value = '';
}

function cancelResourceDrop(o) {
   o.parentNode.parentNode.parentNode.removeChild(o.parentNode.parentNode);
}