﻿//jQuery version of AjaxHelper.js - helper functions to support gemstones pages (add to cart popup mainly)

function closePopup(ctlName)
{
   $('#' + ctlName).css('visibility', 'hidden');
}

// Moves an element relative to a parent element
function moveWindowRelative(ctlname, parentname, xoffset, yoffset, addwidth) {   
   var ctl = $('#' + ctlname);
   var parentctl = $('#' + parentname);      
   var position = parentctl.position();

   if(addwidth) {
      ctl.css('top', position.top + yoffset);      
   }
   else {
      ctl.css('top', position.top + yoffset);      
   }      
   
   setTimeout(function(){ctl.css('visibility','visible');}, 50);      
}     

function moveWindowRelativeByPos(ctlname, rootPosition, xoffset, yoffset, addwidth) {   
   var ctl = $('#' + ctlname);
      
   if(addwidth) {
      ctl.css('top', rootPosition.top + yoffset);
      ctl.css('left', rootPosition.left + parentctl.outerWidth);       
   }
   else {
      ctl.css('top', rootPosition.top + yoffset);
      ctl.css('left', rootPosition.left + parentctl.outerWidth + xoffset);       
   }      
   
   setTimeout(function(){ctl.css('visibility','visible');}, 50);      
} 

function buildBreaksTable(objTable, PriceBreaks, showPricePerCarat) {
   // First remove all but the first row in reverse order so we don't mess up the indexer
   var i;
   var last_row = objTable.rows.length;       
   for(i=last_row-1;i>0;i--) {
      objTable.deleteRow(i);
   }
   
   // Now add the new item's breaks
   var num_breaks = PriceBreaks.length;
   for(i=0;i<num_breaks;i++) {
      var row = objTable.insertRow(1+i);
      if(i%2==1)     // Odd index
         row.setAttribute('class', 'alt');
         
      var cell1 = row.insertCell(0);
      cell1.innerHTML = PriceBreaks[i].QuantityRange;
      var cell2 = row.insertCell(1);
      cell2.innerHTML = PriceBreaks[i].EachPrice;
      
      if(showPricePerCarat) {
         var cell3 = row.insertCell(2);
         cell3.innerHTML = PriceBreaks[i].CaratPrice;
      }
   }
}


function showWait(ctlName, delay) {
   var w = $('#' + ctlName);
   var timer = setTimeout(function(){w.css('display', 'block');}, delay);   
   
   return timer;
}   
function hideWait(ctlName, timer) {
      clearTimeout(timer);           // If the original wait hasn't expired, clear it
      var w = $('#' + ctlName);     //Make sure it's visible
      
      setTimeout(function(){w.css('display', 'none');}, 25);  
}