﻿
var Fmp = {

  notificationIntervalId:null,

  Request: function(data){
    new Ajax.Request('/store/ajaxhandler.aspx', { parameters:data });
    return false;
  },
  
  SubmitForm: function(form){
    return Fmp.Request(Form.serialize(form));
  },
  
  Notify: function(x, y, content){
    var notification = $('notification');
    notification.hide();
    $('notification-content').update(content);
    notification.setStyle({top:y+'px', left:x+'px'}).show();
    clearInterval(Fmp.notificationIntervalId);
  },
  
  HideNotification: function(){
    Event.observe('notification', 'mouseover', Fmp.CancelHideNotification);
    Fmp.notificationIntervalId = setInterval(function(){ 
        var notification = $('notification');
        notification.hide(); 
        Fmp.CancelHideNotification();
        Event.observe(notification, 'mouseover', Fmp.CancelHideNotification);
      }, 5000);
  },
  
  CancelHideNotification: function(){
    clearInterval(Fmp.notificationIntervalId);
    Event.stopObserving('notification', 'mouseover', Fmp.CancelHideNotification);
  },
  
  RemoveFromCart: function(cartProductId){
    Fmp.Request({ ActionType:'RemoveFromCart', CartProductId:cartProductId });
  },
  
  AddToCart: function(e, data, message){
    var x = (Event.pointerX(e) - 97);
    var y = (Event.pointerY(e) - 78 - 30);
    Fmp.Notify(x, y, message);
    Fmp.Request(data);
    return false;
  },
  
  AddToCartViaForm: function(form, message){
    var f = $(form);
    var offset = f.cumulativeOffset();
    var x = (offset.left);
    var y = (offset.top - 162 - 10);
    Fmp.Notify(x, y, message);
    Fmp.Request(Form.serialize(form));
    // Scroll to message if needed:
    var availableTop = f.viewportOffset().top;
    if(availableTop < 170){ $('notification').scrollTo(); }
    return false;
  },
  
  Checkout: function(form){
    $('SubmitButton').hide();
    $('ProcesingMessage').show();
    $('ProccessorErrors').hide();
    Fmp.SubmitForm(form);
  },
  
  IncreaseValue: function(input){
    var el = $(input);
    el.value = Fmp.GetValue(el) + 1;
    Fmp.InspectValue(el);
  },

  DecreaseValue: function(input){
    var el = $(input);
    el.value = Fmp.GetValue(el) - 1;
    Fmp.InspectValue(el);
  },

  InspectValue: function(input){
    var el = $(input);
    var i = Fmp.GetValue(el);
    if(i < 1){ i = 1; }
    el.value = i;
  },

  GetValue: function(input){
    var el = $(input);
    var i = parseInt(el.value);
    return (isNaN(i)) ? 0 : i;
  },
  
  Debug: function(value){
    var el = new Element('div', { style: 'background:#FFF; padding:10px; border:10px solid #CCC;' });
    el.update(value);
    $(document.body.appendChild(el)).scrollTo();
  },
  
  Popup: function(url){
    window.open(url, 'win','status=1,width=480,height=450,resizable=1').focus();
    return false;
  }

};

Ajax.Responders.register({
  onException: function(xhr, ex){ alert('Exception was thrown while executig Ajax request: \n\n' + ex); },
  onComplete: function(xhr){ if(xhr.transport.status == 500){ Fmp.Debug(xhr.transport.responseText); } }
});
