/* ###########################################################

   Settings / Gemstones

 * ########################################################### */

function moveSetting(){

   var debug=0;

   if (event.button==1 && dragapproved){

      zoom  = getFieldValue("goForm","zoom");

      xmove = parseInt((oldClientX - event.clientX)/zoom);
      ymove = parseInt((oldClientY - event.clientY)/zoom);
      var layer = getRawObject(layerToMove);
      
      var newLeft = currentx - xmove;
      var newTop  = currenty - ymove;
      checkAndChangeSetting(layer, settingNumber, newLeft, newTop);
      
      /*
       * If the "invalid" layer is currently switched on, this means that the setting
       * has previously been involved in a collision. If this is the case, then
       * we want to re-check the collisions so that we can remove the "invalid" layer
       * if there is no longer a collision
       */

      if (event.srcElement.id.substr(event.srcElement.id.length-7,7) == "invalid") {
         if (elapsedTime() > 0) { // We do this to ensure that it is not called too often.
            //debugMessage(debug,"moveSetting"," Will check for collissions as we just moved in invalid layer");
            handleCol = true;
            setTimeout("handleCollisions()",25); // We have a delay rather then realtime for performance reasons
            resetTimer();
         }
         
      }

      //debugMessage(debug,"moveSetting","layerToMove: " + layerToMove + " left:" + currentx + " top:" + currenty + " xmove:" + xmove + " ymove:" + ymove + " eventy:" + event.clientY);
      
      return false;
   }
}

function checkAndChangeSetting(layer, settingNumber, left, top) {

   var debug = 0;

   //debugMessage(debug,"checkAndChangeSetting","was called");

   /* This check ensures that the gemstone setting does not go outside
    * of the base. i.e. we don't want the user dragging the gemstone
    * into no mans land!
    *
    * The first test is on the top left corner of the gemstone setting
    * The second test is on the bottom right of the gemstone setting
    */

   // Top left tests
   left = (left < 0) ? 0 : left;
   top  = (top  < 0) ? 0 : top;
   
   // Bottom right tests
   if (left + getObjectWidth(layer) > getObjectWidth(base)) {
      left = getObjectWidth(base) - getObjectWidth(layer);
   }
   
   if (top + getObjectHeight(layer) > getObjectHeight(base)) {
      top = getObjectHeight(base) - getObjectHeight(layer);
   }

   changeLayerLeft(layer,left);
   changeLayerTop (layer,top);

   // Set the hidden fields on the form that store the gemstones top and left positions      
   setFieldValue("gemstoneForm","positionTopGemstone" + settingNumber,getObjectTop(layer));
   setFieldValue("gemstoneForm","positionLeftGemstone" + settingNumber,getObjectLeft(layer));

}


/* ###########################################################

   Gemstone Utilities
   
 * ########################################################### */


function SetGemstone(Gemstone) {
   var debug = 0;
   if (Gemstone == 'auto') {
   
      Gemstone = ActiveGemstone();
      
   }

   // Set the old (existing) gemstone to unselected
   var currentGemstone = getFieldValue("UI_gemstoneForm","suffix");
   var layer = getRawObject("selector_setting_" + currentGemstone);
   if (layer) {   
      layer.stroked = "f";
   }
   
   //debugMessage(debug,"SetGemstone","Setting gemstone: " + Gemstone);
   setFieldValue("UI_gemstoneForm","suffix",Gemstone);
   //rename all the field value so that it will update the correct thing
   var children = getRawObject("options-gemstone_div").getElementsByTagName("*");
   
   for (var i=0; i < children.length; i++) {
      var id = children[i].id;

      //Renbame the VML objects
      if (id.substr(0,3) == "gui") {   
         var components = id.split("__");
         children[i].id = components[0] + "__" + components[1] + "__" + components[2].substring(0,components[2].length-1) + Gemstone + "__" + components[3];
      }
      //Rename what??
      if (id.substr(0,20) == "UI_div_gemstoneForm_") {
         children[i].id = id.substring(0,id.length-1) + Gemstone;
      }
   }
   
   opentab('options-gemstone');
   
}

function AvailableGemstone() {
   
   var numGemstones = getFieldValue("goForm","maxGemstones");
   for (gNum=1;gNum<=numGemstones;gNum++) {   
      if (getFieldValue("gemstoneForm","showGemstone" + gNum) == 0) {
         return gNum;
      }
   }
   info(100,'You cannot add any more gemstones');
   return(1);
   
}

function ActiveGemstone() {
   
   var numGemstones = getFieldValue("goForm","maxGemstones");
   for (gNum=1;gNum<=numGemstones;gNum++) {   
      if (getFieldValue("gemstoneForm","showGemstone" + gNum) == 1) {
         return gNum;
      }
   }
   var gNum = 1;
// info(100,'You cannot add any more gemstones');
   setFieldValue("gemstoneForm","showGemstone" + gNum,1);
   setFieldValue("UI_gemstoneForm","suffix",gNum);
//   CenterGemstone();
   redraw();
   return(gNum);
   
}

function AddGemstone() {

   var gNum = AvailableGemstone();
   SetGemstone(gNum);
   setFieldValue("gemstoneForm","showGemstone" + gNum,1);
   setFieldValue("UI_gemstoneForm","suffix",gNum);
   //CenterGemstone();
	opentab('options-gemstone');
   redraw();
}

function CenterGemstone() {
   CenterGemstoneHorizontal();
   CenterGemstoneVertical();
}
function CenterGemstoneHorizontal() {
   var gNum = getFieldValue("UI_gemstoneForm","suffix");
   var layerName  = "settingGroup" + gNum;
   var layer = getRawObject(layerName);

   positionLayerYCenter (layer, "base", 0)

   setFieldValue("gemstoneForm","positionLeftGemstone" + gNum,getObjectLeft(layer));
   setFieldValue("gemstoneForm","positionTopGemstone" + gNum,getObjectTop(layer));

   redraw();
}
function CenterGemstoneVertical() {
   var gNum = getFieldValue("UI_gemstoneForm","suffix");
   var layerName  = "settingGroup" + gNum;
   var layer = getRawObject(layerName);

   positionLayerXCenter (layer, "base", 0)

   setFieldValue("gemstoneForm","positionLeftGemstone" + gNum,getObjectLeft(layer));
   setFieldValue("gemstoneForm","positionTopGemstone" + gNum,getObjectTop(layer));

   redraw();
}

// This sets the maximum number of gemstones based upon the base style XML
function setMaxGemstones() {
   // Find out how many gemstones are listed in our style definition and set the
   // maxGemstones form field to it
   var formNodes = xmlStyle.getElementsByTagName("form");
   var gemNode   = fetchNode(formNodes, "id", "gemstoneForm");
   var maxGemstones = 0;
   
   for (var i=1;i < 25;i++) { // We assume that 25 is the maximum number of gemstones that we will ever have
      var idName = "showGemstone" + i;
      if (fetchNode(gemNode.childNodes, "id", idName)) {
         maxGemstones = i;
      } else {
         break;
      }
   }
   
	setFieldValue("goForm","maxGemstones",maxGemstones); 
   
}




function rescaleGemstones (field) {
   /*
      When the width or height of the ring changes, we also need to update where the
      gemstones are positioned in order to keep them scaled corectly
   */
   if ((field == "ringWidth") || (field.substr(0,12) == "gemstoneSize")) {
      var numGemstones = getFieldValue("goForm","maxGemstones");
      for (gNum=1;gNum<=numGemstones;gNum++) {
         var topScale  = getFieldValue("gemstoneForm","gemstoneScaleTop" + gNum);
         var height    = fetchAttributeFromSelectedFormElement ("goForm","ringWidth","height");
         // We must take the setting height off the total height
         var settingHeight = fetchAttributeFromSelectedFormElement ("gemstoneForm","gemstoneSize" + gNum,"height");
         height = height - settingHeight;
         setFieldValue("gemstoneForm","positionTopGemstone" + gNum,parseInt(topScale*height));
      }
   } 
   
   if ((field == "ringSize") || (field.substr(0,12) == "gemstoneSize")) {
      var numGemstones = getFieldValue("goForm","maxGemstones");
      for (gNum=1;gNum<=numGemstones;gNum++) {
         var topScale  = getFieldValue("gemstoneForm","gemstoneScaleLeft" + gNum);
         var width     = fetchAttributeFromSelectedFormElement ("goForm","ringSize","width");
         // We must take the setting width off the total width
         var settingWidth = fetchAttributeFromSelectedFormElement ("gemstoneForm","gemstoneSize" + gNum,"width");
         width = width - settingWidth;
         setFieldValue("gemstoneForm","positionLeftGemstone" + gNum,parseInt(topScale*width));
      }         
   }

}


function setGemstoneTopScale(gNum,value) {
   var height = fetchAttributeFromSelectedFormElement ("goForm","ringWidth","height");
   // We must take the height of the setting off the height of the ring to work out the correct scale
   var settingHeight = fetchAttributeFromSelectedFormElement ("gemstoneForm","gemstoneSize" + gNum,"height");
   height = height - settingHeight;
   var scale = value/height;
   setFieldValue("gemstoneForm","gemstoneScaleTop" + gNum,scale);
}

function setGemstoneLeftScale(gNum,value) {
   var width = fetchAttributeFromSelectedFormElement ("goForm","ringSize","width");
   // We must take the width of the setting off the width of the ring to work out the correct scale
   var settingWidth = fetchAttributeFromSelectedFormElement ("gemstoneForm","gemstoneSize" + gNum,"width");
   var width = width - settingWidth;
   var scale = value/width;
   setFieldValue("gemstoneForm","gemstoneScaleLeft" + gNum,scale);
}

