/* ###########################################################

   Layer Positioning

 * ########################################################### */

function resetLayersAndDivs() {

   // automagically set the zoom factor

   /*
    * For this function to work correctly and not reset the zoom factor too often, we are going
    * to look at the width of the ring. This is the major determining factor in working out how
    * how big we make the VML image look. We have broken the height of the VML image up into these blocks:
    * <8.1        : 90
    * 8.2 - 12.1  : 130
    * >12.1       : 160
    */

   var ringWidth = getFieldValue ("goForm","ringWidth");
   var height = 160;

   if (ringWidth < 8.1) {
      height = 90;
   } else if ((ringWidth > 8.0) && (ringWidth < 12.1)) {
      height = 130;
   } else {
      height = 160;
   }
   var currentZoom  = getFieldValue("goForm","zoom");
   var zoomFactor = getZoomFactor (260,height,getWindowWidth()-80,getWindowHeight()-getObjectHeight("options")-30,0.25);
	if (currentZoom != zoomFactor) {
			//VERY VERY VERY WIERD
			//info(30,'The displayed size of the ring, as shown on your screen varies depending on how large your window is. As you have adjusted the size of your window we have rescaled the ring to best fit. This does not affect the real ring size.');
	}
   setFieldValue("goForm","zoom",zoomFactor);

}


function getWindowWidth () {
   return (window.innerWidth == null) ? document.body.clientWidth : window.innerWidth;
}

function getWindowHeight () {
   return (window.innerHeight == null) ? document.body.clientHeight : window.innerHeight;
}

// This function is called by the document.onmousemove from the drags() function
function genericLayerResizer() {

   if (event.button==1 && dragapproved) {
      changeDropdown(resizerForm,resizerField,getFieldValue("goForm","zoom"));
   }

}

function positionLayerTop (layerToMove, baseLayer, amount) {
   changeLayerTop(layerToMove,getObjectTop(baseLayer) + parseFloat(amount));
}
function positionLayerRealTop (layerToMove, baseLayer, amount) {
   changeLayerTop(layerToMove,getObjectY(baseLayer) - getObjectRenderedHeight(layerToMove) + parseFloat(amount));
}

function positionLayerYCenter (layerToMove, baseLayer, amount) {
   changeLayerTop(layerToMove,parseInt(getObjectHeight(baseLayer) / 2) - parseInt(getObjectHeight(layerToMove) / 2) + amount);
}
function positionLayerRealYCenter (layerToMove, baseLayer, amount) {
   changeLayerTop(layerToMove,getObjectY(baseLayer) + parseInt(getObjectRenderedHeight(baseLayer) / 2) - parseInt(getObjectRenderedHeight(layerToMove) / 2) + amount);
}

function positionLayerBottom (layerToMove, baseLayer, amount) {
   changeLayerTop(layerToMove,getObjectTop(baseLayer) + getObjectHeight(baseLayer) - getObjectHeight(layerToMove) - amount);
}
function positionLayerRealBottom (layerToMove, baseLayer, amount) {
   changeLayerTop(layerToMove,getObjectY(baseLayer) + getObjectRenderedHeight(baseLayer) - amount);
}

function positionLayerLeft (layerToMove, baseLayer, amount) {
   changeLayerLeft(layerToMove,getObjectLeft(baseLayer) + parseFloat(amount));
}
function positionLayerRealLeft (layerToMove, baseLayer, amount) {
   changeLayerLeft(layerToMove,getObjectX(baseLayer) -  getObjectWidth(layerToMove) + parseFloat(amount));
}

function positionLayerXCenter (layerToMove, baseLayer, amount) {
   changeLayerLeft(layerToMove,parseInt(getObjectWidth(baseLayer) / 2) - parseInt(getObjectWidth(layerToMove) / 2) + amount);
}
function positionLayerRealXCenter (layerToMove, baseLayer, amount) {
   changeLayerLeft(layerToMove,getObjectX(baseLayer) + parseInt(getObjectRenderedWidth(baseLayer) / 2) - parseInt(getObjectRenderedWidth(layerToMove) / 2) + amount);
}

function positionLayerRight (layerToMove, baseLayer, amount) {
   changeLayerLeft(layerToMove,getObjectWidth(baseLayer) - getObjectWidth(layerToMove) - amount);
}
function positionLayerRealRight (layerToMove, baseLayer, amount) {
   changeLayerLeft(layerToMove,getObjectX(baseLayer) + getObjectRenderedWidth(baseLayer) - amount);
}

// Position the layerToMove in the absolute center of the baseLayer.
// Offset it by amount
function positionLayerAbsCenter (layerToMove, baseLayer, amount) {
   positionLayerYCenter (layerToMove, baseLayer, amount);
   positionLayerXCenter (layerToMove, baseLayer, amount);
}


/* ###########################################################

   Layer Resizing

 * ########################################################### */


/*
 * Go through each form element and see if we should adjust a layers
 * width or height.
 */
function resizeLayers () {

   var debug=0;
   var formNodes = xmlStyle.getElementsByTagName("form");
   var window_width = getInsideWindowWidth();
   var leave_spacing_between_divs = 15;
   var rightColumn_width;
	var helpDivHeight;

	if (maximisehelp) {
		nodisplay("ring");
		nodisplay("options");
		rightColumn_width = window_width;
		helpDivHeight = getInsideWindowHeight();
	} else {
		display("ring");
		display("options");
		var options_width = getObjectWidth("options");
		rightColumn_width = window_width - options_width - leave_spacing_between_divs;
		//helpDivHeight = options.offsetHeight - helpheader.offsetHeight -2;
		//The helpheader is 25pixels but only becomes that high when the CSS overrides the list into a tab. This shouldn't change so we hard code it
		helpDivHeight = options.offsetHeight - 27;
	}
	if (window_width < 730) info(1000,'Please maximise your window to use this page. You can try pressing F11 to fully maximise your window. Pressing F11 again will restore the window back to normal.');
   if(debug >=1) alert('Set the right column width:' + window_width + '-' + options_width + '-' + leave_spacing_between_divs + '=' + rightColumn_width);
   setObjectWidth('rightColumnDiv',Math.abs(rightColumn_width));
   setObjectWidth('helpheader',Math.abs(rightColumn_width));

   setObjectHeight('help-information_div',helpDivHeight);
   setObjectHeight('help-generalhelp_div',helpDivHeight);
   setObjectHeight('help-savering_div',helpDivHeight);
   setObjectHeight('help-loadring_div',helpDivHeight);

	//The rightColumnDiv width is known, we have a few form objects in it which need to be dynamically sized.
	//They are not measured in pixels but in characters, hence we will make the a percentage of current column width
	document.getElementById("save_username").size = Math.abs(rightColumn_width) / 13;
	document.getElementById("save_name").size = Math.abs(rightColumn_width) / 13;
	document.getElementById("save_comments").cols = Math.abs(rightColumn_width) / 17;

	if (window_width < 900) {
		if (loaded_xml) displayDesigns(loaded_xml,1);
	} else {
		if (loaded_xml) displayDesigns(loaded_xml,0);
	}

   for (var ii=0; ii < formNodes.length; ii++) {
      var formNode = formNodes.item(ii);
      var formName = fetchAttributeFromNode(formNode,'id');
      if (debug >= 2) alert("Form " + ii + " : " + formName);
      // Lets go through each field on the form
      var formsChildNodes = formNode.childNodes;
      for (var x=0; x < formsChildNodes.length; x++) {
         var fieldName = fetchAttributeFromNode(formsChildNodes.item(x),'id');
         //debug = (fieldName == "topinlayWidth") ? 99 : 0;
         if (debug >= 2) alert("Field: " + fieldName);
         var vmlWidth  = fetchAttributeFromNode(formsChildNodes.item(x),'vmlwidth');
         var vmlHeight = fetchAttributeFromNode(formsChildNodes.item(x),'vmlheight');
         if (debug >= 99) alert("vmlWidth: " + vmlWidth + " vmlHeight: " + vmlHeight);
         if (vmlWidth) {
            // This field has the attribute for vml width. Lets fetch the value of the width.
            var width = fetchAttributeFromSelectedFormElement(formName,fieldName,"width");
            if (width) {
               if (debug >= 1) alert("Width: " + formName + " " + fieldName + " " + width + " layers: " + vmlWidth);
               // We need to go through each VML layer and adjust it. We know that there are multiple
               // layers if there is a comma in the list
               if (vmlWidth.substr(',')) {
                  var layers = vmlWidth.split(",");
                  for (i in layers) {
                     changeLayerWidth(layers[i],width);
                  }
               } else {
                  // There are not multiple listed layers, only 1
                  changeLayerWidth(vmlWidth,width);
               }
            }
         }


         if (vmlHeight) {
            // This field has the attribute for vml height. Lets fetch the value of the height.
            var height = fetchAttributeFromSelectedFormElement(formName,fieldName,"height");
            if (debug >= 99) alert("Form: " + formName + " Field: " + fieldName + " height: " + height);
            if (height) {
               if (debug >= 1) alert("Height: " + formName + " " + fieldName + " " + height + " layers: " + vmlHeight);
               // We need to go through each VML layer and adjust it. We know that there are multiple
               // layers if there is a comma in the list
               if (vmlHeight.substr(',')) {
                  var layers = vmlHeight.split(",");
                  for (i in layers) {
                     changeLayerHeight(layers[i],height);
                  }
               } else {
                  // There are not multiple listed layers, only 1
                  changeLayerHeight(vmlHeight,height);
               }
            }
         }

      }

   }

}

/*
 * Go through each checkbox form element and see if we need to hide or show a particular layer
 */

function showHideLayers() {

   var debug=0;
   var formNodes = xmlStyle.getElementsByTagName("form");

   for (var ii=0; ii < formNodes.length; ii++) {
      var formNode = formNodes.item(ii);
      var formName = fetchAttributeFromNode(formNode,'id');
      if (debug >= 2) alert("Form " + ii + " : " + formName);
      // Lets go through each field on the form
      var formsChildNodes = formNode.childNodes;
      for (var x=0; x < formsChildNodes.length; x++) {
         var fieldName = fetchAttributeFromNode(formsChildNodes.item(x),'id');
         if (debug >= 2) alert("Field: " + fieldName);
         var vmlVisibility  = fetchAttributeFromNode(formsChildNodes.item(x),'vmlvisibility');
         if (vmlVisibility) {
            // This field has the attribute for vmlvisibility. Lets set each layer as appropriate.
            var fieldObj = document.forms[formName].elements[fieldName];
            var myVisibility = (fieldObj.checked == true) ? "visible" : "hidden";

            if (vmlVisibility.substr(',')) {
               var layers = vmlVisibility.split(",");
               for (i in layers) {
                  getRawObject(layers[i]).style.visibility = myVisibility;
               }
            } else {  // There are not multiple listed layers, only 1
               getRawObject(vmlVisibility).style.visibility = myVisibility;
            }
         }

      }

   }

}

function layerIsVisible(layer) {
   return (getRawObject(layer).style.visibility == "visible") ? true : false;
}


/*
All of the following changeLayer functions ONLY change the particular
attribute of the layer IF it has actually changed. We do this for performance
reasons - as we should not modify something that hasn't changed.
*/

function changeLayerHeight (obj,height) {
   try {
		height = parseInt(height);

	   if (getObjectHeight(obj) != height) {
   	   setObjectHeight(obj,height);
	   }
	} catch(e) {
		info('1000','changeLayerHeight: Object: ' + obj + ' Height: ' + height);
	}
}

function changeLayerWidth (obj,width) {
   width = parseInt(width);

   if (getObjectWidth(obj) != width) {
      setObjectWidth(obj,width);
   }
}

function changeLayerTop (obj, positionY) {
   positionY = parseInt(positionY);

   if (getObjectTop(obj) != positionY) {
      shiftTo(obj, getObjectLeft(obj), positionY);
   }
}

function changeLayerLeft (obj, positionX) {
try {

   positionX = parseInt(positionX);

   if (getObjectLeft(obj) != positionX) {
      shiftTo(obj, positionX, getObjectTop(obj));
   }

	} catch(e) {
		info(1000,'changeLayerLeft: Obj: ' + obj + ' Pos: ' + positionX );
	}
}



function changeLayerFillSrc(layer,value) {
   layer = getRawObject(layer);
   if (layer.fill.src != value) {
      layer.fill.src = image_server+"images/loading.gif";
      layer.fill.src = value;
   }
}

function changeLayerImageSrc(layer,value) {

	try {
   	layer = getRawObject(layer);
	   if (layer.imagedata.src != value) {
      	layer.imagedata.src = image_server+"images/loading.gif";
   	   layer.imagedata.src = value;
	   }
	} catch(e) {
		alert('Layer: ' + layer + ' Value: ' + value);
	}

}

function setZoomForPrint() {
  if(document.getElementById('priceDiv').innerHTML == '...') {
    alert('Please wait for a price to be displayed before printing.');
  }
}
function resetZoomForScreen() {
	setTimeout("resetLayersAndDivs()","3000");
	setTimeout("redraw()","3000");
}


