// JavaScript Document

var oldstate = 0;
var state = 0;
// state 0 is basket closed and busy icon visible
// state 1 is basket open and close button visible
// state 2 is basket closed and total visible

var param=null;
var callBack=null;
var timeoutID=null;
var basketTableID=null;
var isEmpty = true;

var callDelay = 1000;        // delay before we make a call
var callbackDelay = 1000;   // delay after callback is received to allows for procesing of response by the DOM
var updateDelay = 750;      // delay after which we update the basket once callbacks have executed

function newCall(theparam,thecallBack,thedelay) {
  clearCall(); // clear any old queued calls
  param=theparam;
  if (thecallBack) callBack=thecallBack;
  if (!thedelay || thedelay<10) thedelay=10;
  timeoutID=setTimeout(makeCall,thedelay)
}
  
function clearCall() {
  clearTimeout(timeoutID)
  this.param='';
  this.callBack=null;
  this.timeoutID=null;
}
  
function makeCall() {
  new ajax ('/basket.aspx', {postBody: param, update: $(basketTableID), onComplete: callBackApply}); 
}
  
function callBackApply(request) {
  if (callBack) callBack.apply();
  // find any javascript returned and evaluate;
  if (request.responseText.indexOf('<script>') >= 0)
    eval(request.responseText.split('<script>')[1].split('</script>')[0]);
  clearCall();
}

function loadBasket(productID, cartID) {
  newCall('action=load&productID='+productID+'&cartID='+cartID,basketLoaded,callbackDelay);
}

function addToBasket(productID, cartID, itemID, quantity) {
  changeBasket();
  newCall('action=add&productID='+productID+'&cartID='+cartID+'&itemID='+itemID+'&quantity='+quantity,basketChanged,callDelay);
}

function removeFromBasket(productID, cartID, cartEntryID) {
  changeBasket();
  newCall('action=remove&productID='+productID+'&cartID='+cartID+'&cartEntryID='+cartEntryID,basketChanged,callDelay);
}

function updateQuantity(productID, cartID, itemID, quantity) {
  changeBasket();
  newCall('action=update&productID='+productID+'&cartID='+cartID+'&itemID='+itemID+'&quantity='+quantity,basketChanged,callbackDelay);
}

// creates moo objects that can be shown and hidden
function registerBasket(_basketID, _basketTableID, display) {
  basketTableID = _basketTableID;
  basketObj = new fx.Height(_basketID, {duration: 250});
  totalObj = new fx.Height('total', {duration: 250});
  closeObj = new fx.Height('close', {duration: 250});
  busyObj = new fx.Height('busy', {duration: 50});

  // set state to 0 (busy)
  oldstate = 0;
  update(0);
  
}

// state 0 is basket closed and busy icon visible
// state 1 is basket open and close button visible
// state 2 is basket closed and total visible

function changeBasket() {  // represents change from 1 to 0
  if ((state == 1) || (state == 2)) setTimeout('update(0);',updateDelay);
}

function basketChanged() {  // represents change from 0 to 1
  if ((state == 0) || (state == 2)) setTimeout('update(1);',updateDelay);
}

function basketLoaded() {  // repesents change from 0 to 2
  if (state == 0) setTimeout('update(2);',updateDelay/2);
}

function toggleBasket() {
  if ((state == 2) && !isEmpty) setTimeout('update(1);',updateDelay);        // open: repesents change from 2 to 1
  else if (state == 1) setTimeout('update(2);',updateDelay);   // close: repesents change from 1 to 2
}

// performs the actual transitions
function update(newState) {

  oldstate = state;
  state = newState;
  
  // alert( oldstate + ' > ' + state );
  
  switch (oldstate + '' + state) {
  
    case '00':
      // set everything to hidden initially, then show the busy icon
      // hide the basket panel and close button
      basketObj.hide();
      closeObj.hide();
      // show the busy icon
      busyObj.hide();
      busyObj.toggle();
      // hide the total panel
      totalObj.hide();
      break;
      
    case '01':
      // show the basket panel and close button
      // show the close button
      basketObj.toggle();
      closeObj.toggle();
      // hide the busy icon
      busyObj.toggle(); 
      // do nothing to the total panel
      setTimeout(updateTotal,callbackDelay);
      break;
      
    case '02':
      // do nothing to the basket panel
      // do nothing to the close button
      // hide the busy icon
      busyObj.toggle();
      // show the total panel
      //totalObj.hide();
      totalObj.toggle();
      setTimeout(updateTotal,callbackDelay);
      break;
      
    case '10':
      // hide the basket panel and close button
      basketObj.toggle();
      closeObj.toggle();
      // show the busy icon
      busyObj.toggle();
      // do nothing to the total panel
      break;
      
    case '12':
      // hide the basket panel and close button
      basketObj.toggle();
      closeObj.toggle();
      // do nothing to the busy icon
      // show the total panel
      totalObj.toggle();
      break;
      
    case '20':
      // do nothing to the basket panel and close button
      // show the busy icon
      busyObj.toggle();
      // hide the total panel
      totalObj.toggle();
      break;
      
    case '21':
      // show the basket panel and close button
      basketObj.toggle();
      closeObj.toggle();
      // do nothing to the busy icon
      // hide the total panel
      totalObj.toggle();
      setTimeout(updateTotal,callbackDelay);
      break;
  }
  
}


function updateTotal() {
  // update the total panel
  var hiddenItemObj = document.getElementById("hiddenitems");
  var hiddenTotalObj = document.getElementById("hiddentotal");
  if (hiddenItemObj && hiddenTotalObj) {
    totalObj.el.innerHTML = hiddenItemObj.innerHTML + " item" + ((hiddenItemObj.innerHTML!='1') ? "s" : "") + " &#163;" + hiddenTotalObj.innerHTML;
    // check if cart is empty
    isEmpty = ((hiddenItemObj.innerHTML + '') == '0');
  }
}

