var patterns = { "83": new Array(new RegExp("^(\\d?\\.\\d+)")),
		 "90": new Array(new RegExp("^(\\d?\\.\\d+)")),
                 "76": new Array(new RegExp("^(\\d?\\.\\d+)")),
                 "80": new Array(new RegExp("(\\d?\\.\\d+)")),
                 "61": new Array(new RegExp("(\\d?\\.\\d+)")),
                 "68": new Array(new RegExp("(\\d?\\.\\d+) Cts"), 
		                 new RegExp("(\\d?\\.\\d+)"))} 

//Invoked after the page is loaded
//Sets up the tooltip and the signature stone select list logic
function sigStonePageLoad() {
  attachSigStoneListener();
  $('#sigstone_tooltip').tooltip({
	bodyHandler: function() {
	  return "Our Certified Signature Series® Gemstones represent a new achievement and the utmost perfection in diamond simulant creation. The Certified Signature Series gems are subjected to incredibly rigorous standards resulting in extraordinary precision and quality. Click to learn more...";
	},
	showURL: false,
	delay: 0,
	fade: 250
   });
}

//Iterates over the attributes lists to check, looking to see if
//one of the carat weight selection lists is present (from which
//the signature stone price is generated). If so, the change listener
//is attached. Otherwise, the signature stone option is removed.
function attachSigStoneListener() {
  var needsSig = false;
  for(var toAttach in patterns) {
     if(lookForAttribute(toAttach)) {
	needsSig = true;
	break;
     }
  }
  if(!needsSig) {
     $('#sig-stone-cell').remove();
  }
}

//Helper function that checks for a particular attribute picker
//and attaches the change listener if found. Returns true if
//the attribute was found.
function lookForAttribute(attr) {
  var id = '#id\\[' + attr + '\\]';
  if($(id).length == 0) {
	return false;
  }
  else {
    $(id).change(function() {
	updateSignatureStoneSelect(attr);
    });
    //We call it here immediately so that the value is updated
    //to reflect the current state of the picker since it might
    //already have a value selected (such as when the user hits
    //'back' after adding the item to their cart).
    updateSignatureStoneSelect(attr);
    return true;
  }
}

//Invoked when the carat weight picker value is changed.
//It starts by iterating over the potential regexes that could
//extract the carat weight from that attribute. Since a particular
//attribute might show up with a variety of patterns, more than one
//regex is tried. If a match is found, the cost is calculated, 
//and the sig stone selector is updated. Additionally, the
//extra information needed to pass along the pricing info is
//inserted into the DOM.
function updateSignatureStoneSelect(attr) {
  var caratRegexes = patterns[attr];
  var id = '#id\\[' + attr + '\\]';
  var toMatch = $(id + ' option:selected').text();
  for(var i = 0; i < caratRegexes.length; i++) {
    var match = caratRegexes[i].exec(toMatch);
    if(match != null) {
	break;
    }
  }
  //If no match is found, then we can't calculate the weight.
  //Disable the sig stone picker and clear the pricing info.
  if(match == null) {
    $('#id\\[99\\]').attr('disabled', 'true');
    _pricingValues['id[99]']['7698'] = 0;
  }
  else {
    var caratWeight = parseFloat(match[0]);
    //We need a special hack here to get the correct center stone weight
    if(attr == '68' && toMatch.indexOf('Cts') != -1) {
      caratWeight -= .1;
    }
    //If the weight is less than .5, don't allow sig stone option.
    if(caratWeight > .5) {
      //Calculate the price, and update and enable the sig stone picker
      //Also insert the price into the special calculated carat cost attribute
      //Update the displayed price
      var price = Math.ceil(caratWeight * 50);
      $('#id\\[99\\] option[value=7698]').text('Certified Stone ($' + price + ')');
      $('#id\\[99\\]').removeAttr('disabled');
      $('#id\\[99\\]').val('7698');
      _pricingValues['id[99]']['7698'] = price;
      if($('#id\\[100\\]').length == 0) {
        $('form[name=cart_quantity]').append('<input type="hidden" id="id[100]" name="id[100]" value="' + price + '"></input>');
      }
      else {
	$('#id\\[100\\]').val(price);
      }
    }
  } 
  calcPrice();
}
