/*!
 * ShoppingCart
 * Version 1.0
 * Author MFE
 * Copyright (c) 2008 Denbel - www.denbel.nl
 */

Denbel.load( 'ShoppingCart' );
Denbel.load( 'ShoppingCart.ShoppingCartItem' );

/**
 * ShoppingCart static object
 */
Denbel.ShoppingCart =
{
  /**
   * Shopping cart items
   * @var Array
   */
  items: null,
  
  /**
   * the container element
   * @var HTMLElement
   */
  container: null,
  
  /**
   * A small box element
   * @var HTMLElement
   */
  box: null,
  
  /**
   * initializes the cart
   * @param Object configuration
   * @return boolean
   */
  init: function( cfg )
  {
    if( YAHOO.lang.isString( cfg.container ) )
    {
      this.container = YAHOO.util.Dom.get( cfg.container );
    }
    else
    {
      this.container = cfg.container;
    }
    
    if( YAHOO.lang.isString( cfg.box ) )
    {
      this.box = YAHOO.util.Dom.get( cfg.box );
    }
    else
    {
      this.box = cfg.box;
    }
    
    //this.sync();
    this.initRemoveButtons();
    
    return true;
  },
  
  /**
   * Syncs the client ShoppingCart with the server
   * @return void
   */
  sync: function()
  {
    var rpc = new Denbel.rpc.XmlRpcClient();
    var msg = rpc.createMessage( 'getShoppingCartItems' );
    
    rpc.callService( msg,
    {
      success: function( res )
      {
        if( !YAHOO.lang.isArray( res.data[0] ) )
        {
          return;
        }
        
        var sci = null;
        res.argument.items = [];
        
        for( var i = 0; i < res.data[0].length; i++ )
        {
          sci = new Denbel.ShoppingCart.ShoppingCartItem();
          sci.productId = res.data[0][i].get( 'productId' );
          sci.quantity = res.data[0][i].get( 'quantity' );
          sci.product = res.data[0][i].get( 'product' );
          
          res.argument.items.push( sci );
        }
      },
      failure: function( res )
      {
      },
      argument: this
    } );
    
    this.initRemoveButtons();
  },
  
  /**
   * Sets the client ShoppingCart to loading state
   * @return void
   */
  setLoading: function()
  {
    if( YAHOO.util.Dom.inDocument( this.container ) )
    {
      this.container.innerHTML = '<div class="align-center"><img alt="Loading..." src="/media/img/body-loader.gif" /></div>';
    }
    
    if( YAHOO.util.Dom.inDocument( this.box ) )
    {
      this.box.innerHTML = '<div class="align-center"><img alt="Loading..." src="/media/img/box-loader.gif" /></div>';
    }
  },
  
  /**
   * Sets the client containers contents
   * @param Array contents
   * @return void
   */
  setContents: function( contents )
  {
    var fail = false;
    
    try
    {
      if( this.box && YAHOO.util.Dom.inDocument( this.box ) && contents[0] )
      {
        this.box.innerHTML = contents[0];
      }
    }
    catch( ex )
    {
      fail = true;
    }
    
    try
    {
      if( this.container && YAHOO.util.Dom.inDocument( this.container ) && contents[1] )
      {
        this.container.innerHTML = contents[1];
        this.initRemoveButtons();
        
        if( YAHOO.util.Dom.hasClass( this.container, 'refresh' ) )
        {
          window.location = window.location;        
        }
      }
    }
    catch( ex )
    {
      fail = true;
    }
    
    if( fail )
    {
      window.location = window.location;
    }
  },
  
  /**
   * initializes the remove item buttons
   * @return void
   */
  initRemoveButtons: function()
  {
    var removeButtons = YAHOO.util.Dom.getElementsByClassName( 'btn-remove' );
    
    for( var i = 0; i < removeButtons.length; i++ )
    {
      YAHOO.util.Event.addListener( removeButtons[i], 'click', function( e )
      {
        YAHOO.util.Event.stopEvent( e );
        
        if( confirm( 'Do you wish to remove this item from your cart?' ) )
        {
          var productId = this.getAttribute( 'href' ).indexOf( '#' );
          productId = this.getAttribute( 'href' ).substring( ( productId + 1 ) );
          
          if( productId )
          {
            Denbel.ShoppingCart.removeItem( productId );
          }
        }
      } );
    }
  },
  
  /**
   * Adds an item to the ShoppingCart
   * @param Number productId
   * @return void
   */
  addItem: function( productId )
  {
    if( !productId )
    {
      return;
    }
    
    this.setLoading();
    
    var rpc = new Denbel.rpc.XmlRpcClient( '/proxy.php' );
    var msg = rpc.createMessage( 'addItemToCart' );
    msg.createAndAddParameter( productId );
    
    rpc.callService( msg,
    {
      success: function( res )
      {
        res.argument.setContents( res.data[0].get( 'contents' ) );
        
        if( res.data[0].get( 'status' ) !== false )
        {
          alert( 'The item is added to your shopping cart.' );
        }
        else
        {
          alert( 'The item could not be added to your shopping cart because the number of ordered items is larger than the current stock.' );
        }
      },
      failure: function( res )
      {
        res.argument.init();
      },
      argument: this
    } );
  },
  
  /**
   * Removes an item from the ShoppingCart
   * @param Number productId
   * @return void
   */
  removeItem: function( productId )
  {
    this.setLoading();
    
    var rpc = new Denbel.rpc.XmlRpcClient( '/proxy.php' );
    var msg = rpc.createMessage( 'removeItemFromCart' );
    msg.createAndAddParameter( productId );
    
    rpc.callService( msg,
    {
      success: function( res )
      {
        res.argument.setContents( res.data[0] );
      },
      failure: function( res )
      {
      },
      argument: this
    } );
  },
  
  /**
   * Clears the cart
   * @return void
   */
  empty: function()
  {
    this.setLoading();
    
    var rpc = new Denbel.rpc.XmlRpcClient( '/proxy.php' );
    var msg = rpc.createMessage( 'clearCart' );
    rpc.callService( msg,
    {
      success: function( res )
      {
        res.argument.setContents( res.data[0] );
      },
      failure: function( res )
      {
      },
      argument: this
    } );
  }
};

/**
 * constructor
 * @return Denbel.ShoppingCart.ShoppingCartItem
 */
Denbel.ShoppingCart.ShoppingCartItem = function()
{
  this.init();
};

// proto
Denbel.ShoppingCart.ShoppingCartItem.prototype =
{
  productId: null,
  quantity: null,
  product: null,
  
  /**
   * initializer
   * @return bool
   */
  init: function()
  {
    this.productId = null;
    this.quantity = 0;
    this.product = null;
    
    return true;
  }
};
