divs = ['monument','city','province','origin','date','description'];
record = new Array;
var currentEdit = false; // to track which field is on edit mode
var record_id = false;
var container = false;
var errorCode = 1;

if(document.layers) {// NS resize bug
	origWidth=innerWidth;
	origHeight=innerHeight;
	onresize=function(){if(innerWidth!=origWidth||innerHeight!=origHeight)location.reload()}
}


function init(){
  for (index = 0; index <  divs.length; index++){ // loop through layers
    current = divs[index];	
		eval ("record[\'" + current + "\'] = \'\'"); //init values with empty string
		eval(current + "= new DynLayer(\'" + current + "Div\')");    // init this layer
		eval(current + "Edit = new DynLayer(\'" + current + "EditDiv\')");    // init this layer's edit layer
		//writeDiv(current); //write it
		placeLayer(current + 'Edit', current + 'Plc');
		placeLayer(current, current + 'Plc');
		eval(current + ".clipInit()");
		var t,b,l = null;
		if (is.ns) {eval(current + ".elm.captureEvents(Event.MOUSEDOWN)")}
		eval (current + ".elm.onmousedown = new Function(\"editMode(\'" + current + "\')\")");	 
  }
  container = new DynLayer('containerDiv'); // this one outside the loop because its not 'clickable'
  container.hide();
  initMouseEvents(); 
}

function loadRecord(id){
	loadrecord = id;
  window.defaultStatus = "Contacting server...";
  URL = "index.php?action=load&mode=js&id=" + loadrecord  +  cacheBuster();
  errorCode = true; // we turn error code on, so the iframe turns it off
  command = "continueLoad()";
	container.load(URL,command); // first part of load only until file load is complete
	return false;
}

function continueLoad(){ // second part or load where fields get updated	
	if (currentEdit) {
		eval (currentEdit + "Edit.hide()");
		eval (currentEdit + ".show()");
		currentEdit = false;
	}
	writeAllDivs();
  if (document.images){
 		document.images['photo'].src = "img/" + record['picture'] + ".jpg";
 	}
  window.defaultStatus = "Record " + record['id'] + " loaded.";
	return true;
}

function sendForm(field){
  window.defaultStatus = "Updating record on the server...";
	value = escape(eval(formStr(field) + field + "Form.elements." + field + "Input.value"));
  URL = 'index.php?action=update&mode=js&id=' + record['id'] + '&modifyField=' + field + '&newValue=' + value + cacheBuster();
	errorCode = true; // we turn error code on, so the iframe turns it off
  command = 'continueForm()';
	//document.location = URL;
  container.load(URL,command);
 	return false;
}
  
function continueForm(){
  if (!errorCode){
    window.defaultStatus = "Record updated.";
		eval (currentEdit + "Edit.hide()");
		eval (currentEdit + ".show()");
	}		
	writeAllDivs();
}

function editMode(editField){
	if (!record['id']){
		return false;
	}
	
	else {
	  window.defaultStatus = "Editing \"" + editField + "\"";
		if (currentEdit) { // if there is an edit field already showing we must hide it
			eval (currentEdit + "Edit.hide()");
			eval (currentEdit + ".show()"); // and show the data layer for the edit field we just hid
		}
		currentEdit = editField;
		
		eval (editField + ".hide()"); // hide data layer for current edit field
		eval (editField + "Edit.show()"); // show current edit field
		return true;
	}
}

function restore(){
	if (!container){
		return true;
	}
	else if (confirm('Do you want to restore the database to its original state?')){
	  URL = 'index.php?action=restore&mode=js' + cacheBuster();
		errorCode = true; // we turn error code on, so the iframe turns it off
  	command = 'continueRestore()';
		//document.location = URL;
  	container.load(URL,command);
		return false;
	}
	else return false;
}

function continueRestore(){
  if (!errorCode){
    window.defaultStatus = "Database restored.";
		if (record['id']){
			loadRecord(record['id']);
		}
	}		
}

function writeAllDivs(){
  for (index = 0; index <  divs.length ; index++){
    current = divs[index];	
    writeDiv(current);
		value = record[current];
		eval (formStr(current) + current + "Form.elements." + current + "Input.value = '" + value + "'");	
  }
}

function writeDiv(current){   
	 divhtml = "<span class=\"dataDiv\">";
	 divhtml += eval("record[\'" + current + "\']");
	 divhtml += "</span>";
	 eval(current + ".write(divhtml)");
}

function formStr(field){ // to supply for the different ways to access forms in layers with NS and IE
	string = (document.layers)? ("document." + field + "EditDiv.document.forms.") : "document.forms.";
	return string;
}

function cacheBuster(){
	cache = Math.round(Math.random()*10000);
	return "&cacheBuster=" + cache;
}		

function placeLayer(name, imgName){ // locate an img, extract img x,y and move the layer to x,y
  //alert('about to place ' + name + ' in ' +imgName);
	var img;
  img = eval("document.images[\'" + imgName + "\']");
  x = getImgLeft(img);
  y = getImgTop(img);
  //x = document.images['monumentPlc'].x; it would be this simple to get rid of extra functions in NS
  //y = document.images['monumentPlc'].y;

  eval (name + ".moveTo(" + x + "," + y + ")");
  eval (name + ".show"); 
}

function changeColor (obj){
	if (obj.className){
		obj.className = "linksVisited";
		return true;
	}
}

function getImgLeft(img) {
  var x, obj;
  if (is.ns) {
    if (img.container != null)
      return img.container.pageX + img.x;
    else
     return img.x
  }
  if (is.ie) {
    x = 0;
    obj = img;
    while (obj.offsetParent != null) {
      x += obj.offsetLeft;
      obj = obj.offsetParent;
    }
    x += obj.offsetLeft;
    return x;
  }
  return -1;
}

function getImgTop(img) {
  var y, obj;
  if (is.ns) {
    if (img.container != null)
      return img.container.pageY + img.y;
    else
      return img.y;
  }
  if (is.ie) {
    y = 0;
    obj = img;
    while (obj.offsetParent != null) {
      y += obj.offsetTop;
      obj = obj.offsetParent;
    }
    y += obj.offsetTop;
    return y;
  }
  return -1;
}
 
function loaded(param){
alert('loaded!' + param);
}

function doFocus(){
	eval (formStr(currentEdit) + currentEdit + "Form.elements." + currentEdit + "Input.select()");
}

