
// --- GENERAL PURPOSE ---------------------------------------------------------

window.addEvent('load', function(){
  if (document.getElementById('lookatme')) $('lookatme').focus();
});

function openPopup(url,wName) {
  var xpos = screen.availWidth/2 - 549/2;
  var ypos = screen.availHeight/2 - 549/2;
  window.open(url, wName, 'width=405,height=565,left='+xpos+',top='+ypos+',toolbar=false,directories=false,menubar=false,location=false,status=false,resizable=false,scrollbars=yes');
}

function pause() {
  var pauser = document.getElementById('pauser');
  pauser.innerHTML = '<img class="inline" src="../i/loading.gif" alt="" title="Adding Item to Cart..." />';
}

function endPause() {
  var pauser = document.getElementById('pauser');
  pauser.innerHTML = '';
}

// --- UI ----------------------------------------------------------------------

var UI = {

  overlay: null,

  coverUp: function() { UI.overlay.inject(document.body); },

  unCover: function() { UI.overlay.dispose(); },

  attachEvents: function(){ 

    // --- CATALOG FUNCTIONALITY 

    if (document.getElementById('catalog-details')) {
      $each($('catalog-details').getElements('a.addItem'), function(el){
        var id = el.getElement('input[name=item_id]').value;
        var style = el.getElement('input[name=item_category]').value;
        el.addEvent('click', function(){ return Cart.add(id, style, $(style+'_qty').value); });
      });
    }    

    // --- CATALOG EDITING FUNCTIONALITY

    if (document.getElementById('addItem')) { $('addItem').addEvent('click', function(){
      return Admin.doAction('item_detail', 'add');
    }); }
    if (document.getElementById('updateItem')) { $('updateItem').addEvent('click', function(){
      var id = $('item_detail').getElement('input[name=id]').value;
      return Admin.doAction('item_detail', 'update', 'id');
    }); }
    if (document.getElementById('deleteItem')) { $('deleteItem').addEvent('click', function(){
      var id = $('item_detail').getElement('input[name=id]').value;
      return Admin.doAction('item_detail', 'delete', 'id');
    }); }

    // --- SHOPPING CART FUNCTIONALITY

    if (document.getElementById('cart')) {
      $each($('cart').getElements('tr.item'), function(el){
        var id = el.getElement('input[name=item_id]').value;
        var style = el.getElement('input[name=item_style]').value;
        el.getElement('input[name=item_qty]').addEvent('blur', function(){ return Cart.update(id, style, this.value) });
        el.getElement('a.removeItem').addEvent('click', function(){ return Cart.remove(id, style); });
      });
    }

    if (document.getElementById('removeAll')) { $('removeAll').addEvent('click', function(){
      if (confirm('This will remove all items from your cart. Are you sure?')) { return Cart.empty(); }
      else { return false; }
    }); }

    if (document.getElementById('dismissPopUp')) { $('dismissPopUp').addEvent('click', function(){ return Cart.dismissPopUp(); }); }

    if (document.getElementById('setShipping')) { $('setShipping').addEvent('change', function(){ return Cart.setShipping(this.getSelected()); }); }
    if (document.getElementById('newShipping')) { $('newShipping').addEvent('click', function(){ return Cart.newShipping(); }); }
    if (document.getElementById('addShipping')) { $('addShipping').addEvent('click', function(){ return Cart.addShipping(); }); }

    if (document.getElementById('setBilling')) { $('setBilling').addEvent('change', function(){ return Cart.setBilling(this.getSelected()); }); }
    if (document.getElementById('newBilling')) { $('newBilling').addEvent('click', function(){ return Cart.newBilling(); }); }
    if (document.getElementById('addBilling')) { $('addBilling').addEvent('click', function(){ return Cart.addBilling(); }); }

    if (document.getElementById('checkout')) { $('checkout').addEvent('click', function(){ $('cart').submit(); return false; }); }
    if (document.getElementById('nickCart')) { $('nickCart').addEvent('click', function(){ return Cart.setNickname($('cart').nickname.value); }); }
    if (document.getElementById('saveCart')) { $('saveCart').addEvent('click', function(){ return Cart.save(); }); }

  }

};

// --- CATALOG -----------------------------------------------------------------

var Catalog = {

  handler: new Request.HTML({
    url: '/handler.php',
    update: 'catalog-details',
    evalScripts: true,
    evalResponse: true,
    onSuccess: function(){ UI.attachEvents(); }
  }),

  handlerEdit: new Request.HTML({
    url: '/handler.php',
    update: 'admin-detail',
    evalScripts: true,
    evalResponse: true,
    onSuccess: function(){ UI.attachEvents(); }
  }),

  showCategory: function(category) {
    ['knives', 'shapers', 'combos'].each(function(id){
      if (id == category) {
        $(id).setStyle('display', 'block');
        $(id+'-title').setStyle('display', 'block');
      } else {
        $(id).setStyle('display', 'none');
        $(id+'-title').setStyle('display', 'none');
      }
    });
    return false;
  },

  showItem: function(category, item){
    Catalog.handler.get({
      'page': 'catalog',
      'action': 'show',
      'item': item,
      'category': category
    });
    return false;
  },

  showItemEdit: function(item){
    Catalog.handlerEdit.get({
      'page': 'admin',
      'action': 'show',
      'item': item
    });
    return false;
  }

};

// --- SHOPPING CART -----------------------------------------------------------

var Cart = {

  popup: null,

  handler: new Request.HTML({
    url: '/handler.php',
    update: 'cart-active',
    onSuccess: function(){ UI.attachEvents(); Cart.getSize(); }
  }),

  handler2: new Request.HTML({
    url: '/handler.php',
    update: 'popup',
    onSuccess: function(){ UI.attachEvents(); }
  }),

  counter: new Request.HTML({
    url: '/handler.php',
    update: 'cartCounter'
  }),

  oneway: new Request.HTML({
    url: '/handler.php'
  }),

  add: function(id, style, quantity) {
    Cart.counter.get({
      'page': 'cart',
      'action': 'add',
      'id': id,
      'style': style,
      'quantity': quantity
    });
    return false;
  },

  update: function(id, style, quantity) {
    Cart.handler.get({
      'page': 'cart',
      'action': 'update',
      'id': id,
      'style': style,
      'quantity': quantity
    });
    return false;
  },

  remove: function(id, style) {
    Cart.handler.get({
      'page': 'cart',
      'action': 'remove',
      'id': id,
      'style': style
    });
    return false;
  },

  empty: function() {
    Cart.handler.get({'page': 'cart', 'action': 'empty'});
    return false;
  },

  refresh: function() {
    Cart.handler.get({'page': 'cart', 'action': 'refresh'});
    return false;
  },

  getSize: function() {
    Cart.counter.get({'page': 'cart', 'action': 'getSize'});
    return false;
  },

  setShipping: function(id) {
    Cart.oneway.get({'page': 'cart', 'action': 'setShipping', 'id': id});
    return false;
  },
  
  newShipping: function() {
    UI.coverUp();
    $(document.body).scrollTo(0, 0);
    Cart.popup.inject(document.body);
    Cart.handler2.get({'page': 'cart', 'action': 'setShipping', 'id': 'new'});
    return false;
  },

  addShipping: function() {
    $('shipping').send();
    Cart.dismissPopUp();
    Cart.refresh();
    return false;
  },

  setBilling: function(id) {
    Cart.oneway.get({'page': 'cart', 'action': 'setBilling', 'id': id});
    return false;
  },

  newBilling: function() {
    UI.coverUp();
    $(document.body).scrollTo(0, 0);
    Cart.popup.inject(document.body);
    Cart.handler2.get({'page': 'cart', 'action': 'setBilling', 'id': 'new' });
    return false;
  },

  addBilling: function() {
    $('billing').send();
    Cart.dismissPopUp();
    Cart.refresh();
    return false;
  },

  setNotes: function(notes) {
    Cart.oneway.get({'page': 'cart', 'action': 'setNotes', 'notes': notes});
    return false;
  },

  setNickname: function(text) {
    UI.coverUp();
    $(document.body).scrollTo(0, 0);
    Cart.popup.inject(document.body);
    Cart.handler2.get({'page': 'cart', 'action': 'setNickname', 'nickname': text});
    return false;
  },

  dismissPopUp: function() {
    Cart.popup.dispose();
    UI.unCover();
    return false;
  },

  save: function() {
    $('cart').nickname.value = $('nickname_popup').nickname.value;
    Cart.dismissPopUp();
    $('cart').action = 'cart.php';
    $('cart').action.value = 'save';
    $('cart').submit();
    return false;
  }

};

// --- ADMIN -------------------------------------------------------------------

var Admin = {

  doAction: function(form, arg1, arg2) {
    if (arg1 == 'delete') {
      if (confirm('Are you sure?')) {
        $(form).action.value = arg1;
        $(form).id.value = arg2;
        $(form).submit();
      }
    } else {
      $(form).action.value = arg1;
      $(form).id.value = arg2;
      $(form).submit();
    }
    return false;
  }

};

// --- ONDOMREADY EVENTS -------------------------------------------------------

window.addEvent('domready', function(){
  UI.overlay = new Element('div', {
    'id': 'overlay',
    'styles': {
      'width': document.body.scrollWidth,
      'height': document.body.scrollHeight,
      'opacity': .6
    }
  });
  Cart.popup = new Element('div', {
    'id': 'popup',
    'styles': {
      'top': 60,
      'left': (document.body.scrollWidth/2) - (400/2)
    }
  });
  UI.attachEvents();
});