Number.prototype.formatMoney = function(c, d, t)
{
  var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
  return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};

var colors = new Array();

colors[6] = '';


var prices = new Array();

prices[5] = new Array();

prices[5][6] = new Array();

prices[5][6][6] = new Array();

prices[5][6][30] = new Array();

prices[5][6][31] = new Array();

prices[5][6][32] = new Array();

prices[5][6][33] = new Array();


prices[5][6][6] = {
  price: 19.95,
  page_count_min_price: 19.99,
  page_count_max_price: 99.99,
  page_count_enabled: true,
  page_count_min: 20,
  page_count_max: 100,
  page_count_step: 1,
  price_id: 5
};
prices[5][6][30] = {
  price: 0.00,
  page_count_min_price: 39.98,
  page_count_max_price: 199.98,
  page_count_enabled: true,
  page_count_min: 20,
  page_count_max: 100,
  page_count_step: 1,
  price_id: 29
};
prices[5][6][31] = {
  price: 0.00,
  page_count_min_price: 59.97,
  page_count_max_price: 299.97,
  page_count_enabled: true,
  page_count_min: 20,
  page_count_max: 100,
  page_count_step: 1,
  price_id: 30
};
prices[5][6][32] = {
  price: 0.00,
  page_count_min_price: 79.96,
  page_count_max_price: 399.96,
  page_count_enabled: true,
  page_count_min: 20,
  page_count_max: 100,
  page_count_step: 1,
  price_id: 31
};
prices[5][6][33] = {
  price: 0.00,
  page_count_min_price: 99.95,
  page_count_max_price: 499.95,
  page_count_enabled: true,
  page_count_min: 20,
  page_count_max: 100,
  page_count_step: 1,
  price_id: 32
};



var quantities = new Array();

quantities[6] = 1;

quantities[30] = 2;

quantities[31] = 3;

quantities[32] = 4;

quantities[33] = 5;


var subtotal         = 0;
var shipping_price   = 0;
var valid_page_count = 0;
var shipping_pricing = { first:0, each_add:0 };

function updateGrandTotal()
{
  var grand_total = subtotal + shipping_price;
  $('grand_total').innerHTML = '$' + grand_total.formatMoney(2, ".", ",") + ' USD';
}

function getShippingPrice( quantity )
{
  var price = shipping_pricing.first;
  if ( quantity > 0 )
    price += ( quantity - 1 ) * shipping_pricing.each_add;
  return price / 100.0;
}

function getQuantity()
{
  var quantity = parseInt( $('product_quantity').value );
  if ( isNaN( quantity ) || ( quantity <= 0 ) ) 
    { quantity = 1; $('product_quantity').value = 1; }
  return quantity;
}

function updateShippingPrice()
{
  shipping_price = getShippingPrice( getQuantity() );
  $('shipping_price').innerHTML = '$' + shipping_price.formatMoney(2, ".", ",");
  updateGrandTotal();
}

function updateSubtotal()
{
  price = prices[$('product_size_product_id').getValue()][$('product_color_product_id').getValue()][$('product_quantity_product_id').getValue()];

  var quantity = getQuantity();

  var string_to_pass = 'price_id=' + encodeURIComponent( price.price_id ) + '&quantity=' +
    encodeURIComponent( quantity );

  if (price.page_count_enabled)
  {
    $('page_count_view').style.display = "block";
    page_count = parseInt($('page_count').value);

	if ( isNaN( page_count ) ) { page_count = 1; }
	string_to_pass += '&page_count=' + page_count;

	new Ajax.Request('/product_prices/price_pre_set', {asynchronous:false, evalScripts:true, method:'get', parameters:string_to_pass})

	$('page_count').value = valid_page_count;

/*

	if ( isNaN( page_count ) )
	{
	  page_count = price.page_count_min;
	}
    if (page_count > price.page_count_max )
    {
      page_count = price.page_count_max;
      subtotal = price.page_count_max_price;
    }
    else if (page_count < price.page_count_min)
    {
      page_count = price.page_count_min;
      subtotal = price.page_count_min_price;
    }
    else
    {
      for (var i = price.page_count_min; i <= price.page_count_max; i += price.page_count_step)
      {
        if ( page_count <= i )
        {
          page_count = i;
          break;
        }
      }
      price_per_step = (price.page_count_max_price - price.page_count_min_price) / ((price.page_count_max - price.page_count_min) / price.page_count_step);
      subtotal = price.page_count_min_price + (price_per_step * (page_count - price.page_count_min) / price.page_count_step);
    }

    if ( !isNaN(page_count) )
      $('page_count').value = page_count;
*/
  }
  else
  {
    $('page_count_view').style.display = "none";

	new Ajax.Request('/product_prices/price_pre_set', {asynchronous:false, evalScripts:true, method:'get', parameters:string_to_pass})
/*
    subtotal = price.price;
*/
  }

  subtotal = ProductCustoms.addProductCustoms(subtotal);
  $('subtotal').innerHTML = '$' + subtotal.formatMoney(2, ".", ",");
  updateShippingPrice();
}

function getCountryName()
{
  return encodeURIComponent( $('country_name').getValue() );
}

function processCountryChange()
{
  var string_to_pass = 'country_name=' + getCountryName() + '&quantity=' +
    encodeURIComponent( getQuantity() );
  new Ajax.Request('/shipping/setup', {asynchronous:true, evalScripts:true, method:'get', parameters:string_to_pass})
}

function getShippingService()
{
  return encodeURIComponent( $('shipping_service_id').getValue() );
}

function processServiceChange()
{
  var string_to_pass = 'country_name=' + getCountryName() + '&shipping_service_id=' +
    getShippingService() + '&quantity=' + encodeURIComponent( getQuantity() );
  new Ajax.Request('/shipping/provider_change', {asynchronous:true, evalScripts:true, method:'get', parameters:string_to_pass})
}

function processMethodChange()
{
  var string_to_pass = 'method_link_id=' + 
  	encodeURIComponent( $('method_link_id').getValue() ) + '&quantity=' +
	encodeURIComponent( getQuantity() );
  new Ajax.Request('/shipping/method_change', {asynchronous:true, evalScripts:true, method:'get', parameters:string_to_pass})
}

var ProductCustoms = new Object();
ProductCustoms["addProductCustoms"] = function(price)
{
    return price;
}

function updateColorImage()
{
  element = $('product_color_product_id');
  if (colors[element.getValue()] != "")
    $('product_color_image').innerHTML = '<div class="form-box"><div class="form-img"><img src="' + colors[element.getValue()] + '"></div></div>';
  else
    $('product_color_image').innerHTML = '';
}

function updateCustomItemImage(custom_id)
{
  element = $('product_custom_' + custom_id);
  if (customs[custom_id][element.getValue()].image != "")
    $('product_custom_' + custom_id + '_image').innerHTML = '<div class="form-box"><div class="form-img"><img src="' + customs[custom_id][element.getValue()].image + '"></div></div>';
  else
    $('product_custom_' + custom_id + '_image').innerHTML = '';
}

document.observe('dom:loaded', function() {
  $('product_color_product_id').observe('change', function() {
    updateColorImage();
    updateSubtotal();
  });
  /* $('product_quantity_product_id').observe('change', updateSubtotal ); */

  /* updateSubtotal(); */
  updateColorImage();

  new Ajax.Request('/product_sizes/update_customs_dropdowns/5', {asynchronous:false, evalScripts:true, method:'get'})
  new Ajax.Request('/shipping/setup', {asynchronous:true, evalScripts:true, method:'get'})
});