function $(id) {
  return document.getElementById(id)
}

function initPanel () {
  // Instantiate a Panel from script
  cartDialog = new YAHOO.widget.Panel("panel",
               {
                  width:"320px",
                  visible:false,
                  fixedcenter: true,
                  constraintoviewport: true,
                  draggable: false,
                  underlay:"shadow",
                  effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25}
                } )
  cartDialog.setHeader("Shopping Cart Notice")
  cartDialog.render("container")

  cartDialog.hideEvent.subscribe(hideDialog, this, true);
}

YAHOO.util.Event.addListener(window, "load", initPanel);

function showDialogSuccess() {
  cartDialog.setBody('<span style="text-align:center"><img style="vertical-align: middle" src="/images/button_ok.png"><span style="font-weight: bold;margin-left: 10px;">Item successfully added to cart</span><br><br><form action="javascript:void(0);"><img src="/images/continue.png" style="cursor: pointer" id="cdContinue" alt="Continue"><img src="/images/view_cart.png" id="cdView" style="cursor: pointer; margin-left:20px" alt="View Cart"></form></span>')
  showDialog();
}

function showDialogFail() {
  cartDialog.setBody('<span style="text-align:center"><img style="vertical-align: middle" src="/images/icon_bomb.png"><span style="font-weight: bold;margin-left: 10px;color: #FF0000">Unable to add item to cart</span><br><br><form action="javascript:void(0);"><img src="/images/continue.png" style="cursor: pointer" id="cdContinue" alt="Continue"><img src="/images/view_cart.png" id="cdView" style="cursor: pointer; margin-left:20px" alt="View Cart"></form></span>')
  showDialog();
}

function addEvents() {
  // Hide the dialog after 5 seconds
  timerId = window.setTimeout(function() {cartDialog.hide()}, 5000)

  YAHOO.util.Event.addListener('cdContinue', "click", function() { cartDialog.hide() })
  YAHOO.util.Event.addListener('cdView', "click", function() { window.top.location="/cart/view"; })
}

function showDialog() {
  addEvents()
  cartDialog.render("container")
  cartDialog.show()
}
function hideDialog() {
  // Clear the timeout
  window.clearTimeout(timerId) 
}
function handleAddToCart(id) {
  // Make ajax call to add it
  URL = '/cart/add/' + id
  var request = YAHOO.util.Connect.asyncRequest('GET', URL, {
      // Pass the callback and scope
      success: this.success,
      failure: this.failure,
      scope: this
  })
}

function success(o) {
  if ( o.responseText.match(/^added/) ) {
    showDialogSuccess()
  } else {
    showDialogFail()
  }
}

function failure() {
  showDialogFail()
}

function isCheckout() {
  if ( window.top.location.pathname.match(/checkout/) ) return '/1'
  return ''
}

function updateQuantity(id) {
  window.top.location = '/cart/update/' + id + '/' + $('quantity_'+id).value + isCheckout()
}

function deleteItem(id) {
  window.top.location = '/cart/delete/' + id + isCheckout()
}

function clearCart() {
  window.top.location = '/cart/clear'
}
