var logError = function(e,myLog) {
  if (!myLog) {
    myLog = '/Admin/logs/debug.log';
  }
  alert(e);
  parameters = {
    log:myLog,
    message:e
  },
  pars = $H(parameters).toQueryString();
  myAjax = new Ajax.Request('logEvent.php',{method:'post',parameters:pars});
  //try {
    throw(e);
  //} catch(e) {alert(e);}
};
var initCart=function(){
  if (!document.all&&!document.getElementById) {
    return false;
  }
  else {
    var myCart = new shoppingCart();
    //floatObject();
    document.getElementById("float_area").style.display='block';
  }
}
var floatObject=function() {
  element = $('float_area');
  myDims = Element.getDimensions(element);
  myWidth = myDims.width;
  myHeight = myDims.height;
  myPosition = Position.cumulativeOffset(element);
  //xPos = document.body.scrollLeft + window.document.body.clientWidth - myWidth - 10;
  mainAreaPos = Position.positionedOffset($('mainArea'));
  xPos = (mainAreaPos[0])+10;
  yPos = myPosition[1];
  yPos += (document.body.scrollTop + document.body.clientHeight - myHeight - yPos - 10)/8;
  element.style.top=yPos+'px';
  element.style.left=xPos+'px';
  setTimeout('floatObject()', 3);
};
shoppingCart = Class.create();
shoppingCart.prototype = {
  // method that updates model and view when an item is inserted from any lookup
  initialize:function() {
    // go through meals, add handlers, get $ total (in case they are redirecting back to page).
    this.myItems = [];
    mySelects = document.getElementsByClassName('myMenuItems');
    if (mySelects.length > 0) {
      this.myType = 'meals';
      this.sidesPrice = parseFloat($('mysidesPrice').value)||0;
      this.mySideCount = parseFloat($('mysideCount').value)||0;
    } else {
      mySelects = document.getElementsByClassName('mySideItems');
      this.myType = 'sides';
      this.mealsPrice = parseFloat($('mymealsPrice').value)||0;
      this.myFullCount = parseFloat($('myfullCount').value)||0;
      this.myHalfCount = parseFloat($('myhalfCount').value)||0;
    }
    mySelects.each(function(mySelect,i) {
      myItem = {};
      var myRE = /(\d+)-(\d+)-(\d+)/;
      myIdNum = myRE.exec(mySelect.id);       // array with menu id, version id and version type
      myItem.versionId=myIdNum[1];
      myItem.versionType=myIdNum[2];
      myItem.menuId=myIdNum[3];
      myItem.price=parseFloat($('price'+myIdNum[1]).innerHTML)||0;  // get price printed in html
      myItem.qty = parseFloat(mySelect.options[mySelect.selectedIndex].value)||0;
      myItem.element=mySelect;                                   // get element with qty value
      mySelect.onchange = this.updateQty.bindAsEventListener(this);
      this.myItems.push(myItem);                                 // add new object to this.myItems
    }.bind(this));
    $('menuItemsForm').onsubmit=this.save.bindAsEventListener(this);
    $('summary_continue').onclick=function() {this.save();$('menuItemsForm').submit()}.bindAsEventListener(this);
    this.element = $('float_area');
    myDims = Element.getDimensions(this.element);
    this.myWidth = myDims.width;
    this.myHeight = myDims.height;
    myBottomPos = Position.cumulativeOffset($('footer'));
    this.bottom = (myBottomPos[1] - this.myHeight);
    myTopPos = Position.cumulativeOffset($('floatTop'));
    myTopDims = Element.getDimensions($('floatTop'));
    this.top = (myTopPos[1] + myTopDims.height);
    this.mainAreaPos = Position.cumulativeOffset($('mainArea'));
    this.floatObject();
    this.updateTotal();
  },
  floatObject:function() {
    myPosition = Position.cumulativeOffset(this.element);
    xPos = (this.mainAreaPos[0]);
    yPos = myPosition[1];
    yPos += (document.body.scrollTop + ((document.body.clientHeight+this.myHeight)/2) - this.myHeight - yPos - 10)/8;
    if ((this.top) > yPos)
      yPos = (this.top);
    else if (yPos > (this.bottom))
      yPos = (this.bottom);
    this.element.style.top=yPos+'px';
    this.element.style.left=xPos+'px';
    setTimeout(function(){this.floatObject()}.bind(this), 5);
  },
  updateQty:function(e) {
    try {
    if (!e.target) e.target = e.srcElement;
      this.myItems.each(function(myItem) {
        if (e.target == myItem.element) {
          myItem.qty = parseFloat(e.target.options[e.target.selectedIndex].value)||0;
        }
      }.bind(this));
      this.updateTotal();
    } catch(e) {logError(e);}
  },
  updateTotal:function() {
    try {
    subtotal=0;
    if (this.myType=='meals') {
      this.mealsPrice=0;
      this.myFullCount=0;
      this.myHalfCount=0;
    } else {
      this.sidesPrice=0;
      this.mySideCount=0;
    }
    this.myItems.each(function(myItem,i) {
      myprice = parseFloat(myItem.price * myItem.qty)||0;
      if (this.myType=='meals') {
        this.mealsPrice+=myprice;
        if (myItem.versionType == 1) {
          this.myFullCount+=myItem.qty;
        } else {
          this.myHalfCount+=myItem.qty;
        }
      } else {
        this.sidesPrice+=myprice;
        this.mySideCount+=myItem.qty;
      }
/*
// DEBUG - view each row's properties
alert($H(myItem).inspect());
*/
    }.bind(this));
    subtotal+=(this.mealsPrice+this.sidesPrice);
    $('myfullitems').innerHTML=this.myFullCount;
    $('myhalfitems').innerHTML=this.myHalfCount;
    $('mysides').innerHTML=this.mySideCount;
    myServings = parseFloat(this.myFullCount*6 + this.myHalfCount*3)||0;
    $('myservings').innerHTML=myServings;
    $('mysubtotal').innerHTML='$'+subtotal.toFixed(2);
    myAvg = parseFloat(this.mealsPrice/myServings)||0;
    $('myavg').innerHTML='$'+myAvg.toFixed(2);
    } catch(e) {logError(e);}
  },
  save:function() {
    try {
    var qtys=[];
    var prices=[];
    var versions=[];
    var versionIds=[];
    var menuIds=[];
    this.myItems.each(function(item,i) {
      qtys[i]=item.qty;
      prices[i]=item.price;
      versions[i]=item.versionType;
      versionIds[i]=item.versionId;
      menuIds[i]=item.menuId;
    });
    $('myqtys').value=qtys;
    $('myprices').value=prices;
    $('myversions').value=versions;
    $('myversionIds').value=versionIds;
    $('mymenuIds').value=menuIds;
    $('mymealsPrice').value=this.mealsPrice;
    $('mysidesPrice').value=this.sidesPrice;
    } catch(e) {logError(e);}
  }
};
