/*
 * Project : IZI Shop
 * File : shop.front.js
 * Date : Fri Feb 20 13:54:11 CET 2009
 *
 * This JavaScript Class is part of the IZI Shop module and facilitates
 * productprice calculation.
 *
 * Changelog
 * [pieter] initial release
 *
 * @link      http://www.izi-services.nl
 * @author    Pieter Hensen (pieter at izi-services dot nl)
 * @copyright IZI-Services
 * @package   IZIFramework
 * @version   1.0
 */
var Front = new Class({
  Extends: Shop,
  useOptionPrice: false,
  limits: Array(),
  dimensions: Array(),
  aspectRatio: null,
  keepAspectRatio: true,
  startprice: null,
  price: null,
  debug: false,

  initialize: function ()
  { 
    this.parent(); // Contruct parent Shop Class
    if( document.getElement('input[name=useOptionPrice]').value == 'true') this.useOptionPrice = true;
    if( $$('input[name^=limits]').length > 0 ) this.limits = $$('input[name^=limits]');
    if( document.getElement('input[name=keepAspectRatio]') ) this.keepAspectRatio = ( document.getElement('input[name=keepAspectRatio]').value == 'false' ) ? false : true;
    if( $$('input[name^=standard_dimensions]').length > 0 ) this.dimensions = $$('input[name^=standard_dimensions]');
    if( $$('input[name^=standard_dimensions]').length > 0 ) this.aspectRatio = ( this.dimensions[0].value.toFloat() / this.dimensions[1].value.toFloat() );
    if( document.getElement('input[name=startprice]') ) this.startprice = ( document.getElement('input[name=startprice]').value.toFloat() ) ? document.getElement('input[name=startprice]').value.toFloat() : 0;
    if( document.getElement('input[name=price]') ) this.price = ( document.getElement('input[name=price]').value.toFloat() ) ? document.getElement('input[name=price]').value.toFloat() : 0;
    if ( this.bootstrap() ) {
     if(this.debug) console.log( 'Shop Front initialized..' );
    }
  },

  // Calculate counterdimension by aspect ratio
  calculateDimensions: function ( id )
  {
    if( id == '1' ) {
      var dimensions_0 = ( $('dimensions_1').value.toFloat()*this.aspectRatio ) ? ( $('dimensions_1').value.toFloat()*this.aspectRatio ).round(1) : '';
      $('dimensions_0').value = dimensions_0;
    } else {
      var dimensions_1 = ( $('dimensions_0').value.toFloat()/this.aspectRatio ) ? ( $('dimensions_0').value.toFloat()/this.aspectRatio ).round(1) : '';
      $('dimensions_1').value = dimensions_1;
    }
  },

  /**
   * Switches between default and manual dimension selection.
   * Calculates if given dimensions don't exceed preset limits.
   */
  changeDimension: function ( type )
  {
    if( type ) {
      if( type == 'manual' ) {
        document.getElement('input[value=manual]').checked = true;
        if( $('dimensions_0').value == '' ) $('dimensions_0').value = this.dimensions[0].value;
        if( $('dimensions_1').value == '' ) $('dimensions_1').value = this.dimensions[1].value;
      } else {
        var id = type.id.substr(11); // Get ID from dimensions_XxX string
        if( this.keepAspectRatio ) this.calculateDimensions( id );
        // Not bigger then limits
        if( type.value.toInt() > this.limits[id].value.toInt() ) {
          alert('Maximale afmeting is ' + this.limits[0].value + 'x' + this.limits[1].value + ' cm');
          $('dimensions_0').value = this.dimensions[0].value;
          $('dimensions_1').value = this.dimensions[1].value;
        }
      }
      this.updatePrice();
    }
  },

  /**
   * Calculates total productprice
   */
  updatePrice: function ()
  {
    var dimensions = new Array();
    if( this.dimensions.length > 0 ) {
       dimensions[0] = ( document.getElement('input[name=dimension_choice][checked][value=manual]') && document.getElements('input[name^=dimensions]')[0].value )
        ? document.getElements('input[name^=dimensions]')[0].value 
        : document.getElements('input[name^=standard_dimensions]')[0].value;
      dimensions[0] = dimensions[0].replace(',', '.').toFloat();

      dimensions[1] = ( document.getElement('input[name=dimension_choice][checked][value=manual]') && document.getElements('input[name^=dimensions]')[1].value ) 
        ? document.getElements('input[name^=dimensions]')[1].value 
        : document.getElements('input[name^=standard_dimensions]')[1].value;
      dimensions[1] = dimensions[1].replace(',', '.').toFloat();
    } else {
      dimensions[0] = 1;
      dimensions[1] = 1;
    }

    var amount = ( document.getElement('input[name=amount]').value ) ? document.getElement('input[name=amount]').value : 0;
    if( amount > 9 ) {
      alert('Als u meer dan 9 artikelen wilt bestellen kunt u even contact met ons opnemen voor een voordeligere prijs.');
      amount = 9;
      document.getElement('input[name=amount]').value = amount;
    }

    this.price = document.getElement('input[name=price]').value.toFloat();

    var total = ( this.useOptionPrice ) ? ( this.startprice + ( ( dimensions[0] * dimensions[1] ) * this.price ) ) * amount
      : ( this.startprice + this.price ) * amount;

    total = total.toFixed(2);
    if( total != 'NaN' ) $('totalprice').set('html', total.replace('.', ',') );
  },

  /**
  * Check customized product before adding it to shoppingcart
  */
  check: function ()
  {
    if( !$('product_details_form').amount.value || $('product_details_form').amount.value < 1 ) {
      alert('Vul bij aantal tenminste 1 op');
      return false;
    }
    if( $('dimensions_0') && $('dimensions_1') ) {
      if( ( $('dimensions_0').value != '' && $('dimensions_0').value.toFloat() < this.dimensions[0].value.toFloat() )
       || ( $('dimensions_1').value != '' && $('dimensions_1').value.toFloat() < this.dimensions[1].value.toFloat() ) ) {
        alert('Minimale afmeting is ' + this.dimensions[0].value + 'x' + this.dimensions[1].value + ' cm.');
        return false;

      } else if( ( $('dimensions_0').value != '' && $('dimensions_0').value.toFloat() > this.limits[0].value.toFloat() )
              || ( $('dimensions_1').value != '' && $('dimensions_1').value.toFloat() > this.limits[1].value.toFloat() ) ) {
               alert('Maximale afmeting is ' + this.limits[0].value + 'x' + this.limits[1].value + ' cm.');
               return false;
      }
    }
    cart.add( $('product_details_form') );
  },

  bootstrap: function ()
  {
    return true;
  }
});
