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[131] = '';

colors[132] = '';

var prices = new Array();

prices[48] = new Array();

prices[48][131] = new Array();

prices[48][131][108] = new Array();

prices[48][131][109] = new Array();

prices[48][131][110] = new Array();

prices[48][131][111] = new Array();

prices[48][131][112] = new Array();

prices[48][131][113] = new Array();

prices[48][131][202] = new Array();

prices[48][132] = new Array();

prices[48][132][108] = new Array();

prices[48][132][109] = new Array();

prices[48][132][110] = new Array();

prices[48][132][111] = new Array();

prices[48][132][112] = new Array();

prices[48][132][113] = new Array();

prices[48][132][202] = new Array();
prices[48][131][108] = {
  price: 0.00,
  page_count_min_price: 85.00,
  page_count_max_price: 245.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[48][131][109] = {
  price: 0.00,
  page_count_min_price: 102.00,
  page_count_max_price: 329.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[48][131][110] = {
  price: 0.00,
  page_count_min_price: 119.00,
  page_count_max_price: 413.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[48][131][111] = {
  price: 0.00,
  page_count_min_price: 136.00,
  page_count_max_price: 497.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[48][131][112] = {
  price: 0.00,
  page_count_min_price: 153.00,
  page_count_max_price: 581.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[48][131][113] = {
  price: 0.00,
  page_count_min_price: 238.00,
  page_count_max_price: 999.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[48][132][108] = {
  price: 0.00,
  page_count_min_price: 145.00,
  page_count_max_price: 419.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[48][132][109] = {
  price: 0.00,
  page_count_min_price: 189.00,
  page_count_max_price: 749.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[48][132][110] = {
  price: 0.00,
  page_count_min_price: 233.00,
  page_count_max_price: 1079.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[48][132][111] = {
  price: 0.00,
  page_count_min_price: 277.00,
  page_count_max_price: 1409.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[48][132][112] = {
  price: 0.00,
  page_count_min_price: 321.00,
  page_count_max_price: 1739.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[48][132][113] = {
  price: 0.00,
  page_count_min_price: 541.00,
  page_count_max_price: 3389.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[48][131][202] = {
  price: 0.00,
  page_count_min_price: 408.00,
  page_count_max_price: 1839.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[48][132][202] = {
  price: 0.00,
  page_count_min_price: 981.00,
  page_count_max_price: 5039.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};


prices[106] = new Array();

prices[106][131] = new Array();

prices[106][131][108] = new Array();

prices[106][131][109] = new Array();

prices[106][131][110] = new Array();

prices[106][131][111] = new Array();

prices[106][131][112] = new Array();

prices[106][131][113] = new Array();

prices[106][131][202] = new Array();

prices[106][132] = new Array();

prices[106][132][108] = new Array();

prices[106][132][109] = new Array();

prices[106][132][110] = new Array();

prices[106][132][111] = new Array();

prices[106][132][112] = new Array();

prices[106][132][113] = new Array();

prices[106][132][202] = new Array();
prices[106][131][108] = {
  price: 0.00,
  page_count_min_price: 130.00,
  page_count_max_price: 345.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[106][132][108] = {
  price: 0.00,
  page_count_min_price: 230.00,
  page_count_max_price: 519.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[106][131][109] = {
  price: 0.00,
  page_count_min_price: 157.00,
  page_count_max_price: 480.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[106][132][109] = {
  price: 0.00,
  page_count_min_price: 294.00,
  page_count_max_price: 904.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[106][131][110] = {
  price: 0.00,
  page_count_min_price: 184.00,
  page_count_max_price: 615.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[106][132][110] = {
  price: 0.00,
  page_count_min_price: 358.00,
  page_count_max_price: 1289.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[106][131][111] = {
  price: 0.00,
  page_count_min_price: 211.00,
  page_count_max_price: 750.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[106][132][111] = {
  price: 0.00,
  page_count_min_price: 422.00,
  page_count_max_price: 1673.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[106][131][112] = {
  price: 0.00,
  page_count_min_price: 238.00,
  page_count_max_price: 885.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[106][132][112] = {
  price: 0.00,
  page_count_min_price: 486.00,
  page_count_max_price: 2058.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[106][131][113] = {
  price: 0.00,
  page_count_min_price: 373.00,
  page_count_max_price: 1560.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[106][132][113] = {
  price: 0.00,
  page_count_min_price: 806.00,
  page_count_max_price: 3983.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[106][131][202] = {
  price: 0.00,
  page_count_min_price: 508.00,
  page_count_max_price: 2235.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};
prices[106][132][202] = {
  price: 0.00,
  page_count_min_price: 1126.00,
  page_count_max_price: 5908.00,
  page_count_enabled: true,
  page_count_min: 8,
  page_count_max: 60,
  page_count_step: 4
};


var quantities = new Array();

quantities[108] = 50;

quantities[109] = 100;

quantities[110] = 150;

quantities[111] = 200;

quantities[112] = 250;

quantities[113] = 500;

quantities[202] = 750;

var discount_factor = 1.0;
var tax_rate        = 0.0;
var shipping_cost   = 0.0;
var tax             = 0.0;
var subtotal        = 0;

function displayPrice( target, value )
{
  $(target).innerHTML = '$' + value.formatMoney(2, ".", ",");
}

function updateGrandTotal()
{ displayPrice( 'grand_total', tax + subtotal + shipping_cost ); }

function updateTax()
{
  if ( tax_rate == 0.0 )
    {
	  tax = 0.0;
      $('tax_group').hide();
	}
  else
    {
	  tax = subtotal * tax_rate;
	  displayPrice( 'sales_tax', tax );
	  $('tax_group').show();
	}
}

function updateSubtotal()
{
  price = prices[$('project_product_size_id').getValue()][$('project_product_color_id').getValue()][$('project_product_quantity_id').getValue()];

  if (price.page_count_enabled)
  {
    $('page_count_view').style.display = "block";
    page_count = parseInt($('project_page_count').value);
    if (page_count > price.page_count_max || isNaN(page_count))
    {
      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) )
      $('project_page_count').value = page_count;
  }
  else
  {
    $('page_count_view').style.display = "none";
    subtotal = price.price;
  }

  subtotal  = ProductCustoms.addProductCustoms(subtotal);
  subtotal *= discount_factor;
/*
  $('subtotal').innerHTML = subtotal.formatMoney(2, ".", ",");
*/
  displayPrice( 'subtotal', subtotal );
  updateTax();
  updateGrandTotal();
}

var ProductCustoms = new Object();
ProductCustoms["addProductCustoms"] = function(price)
{
    return price;
}

function updateColorImage()
{
  element = $('project_product_color_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 = '';
}

calculationDisable = function() {
  $$('a.calculate').invoke('hide');
  $$('img.calculate').invoke('show');
}

calculationEnable = function() {
  $$('a.calculate').invoke('show');
  $$('img.calculate').invoke('hide');
}

shippingSelect = function(url) {
  calculationDisable();
  new Ajax.Request(url, {
    method:'put',
    asynchronous:true,
    evalScripts:true,
    parameters:Form.serialize($('calculate_form'))}
  );
}

onShippingSelect = function(cost) {
  calculationEnable();
  $('cost').innerHTML = "$0.00";
  if (isNaN(cost)) return;
  $('cost').innerHTML = "$" + cost.toFixed(2);

  shipping_cost = cost;

  updateTax();
  updateGrandTotal();
}


uploadDisable = function() {
  $$('input[type=submit]').invoke('hide');
  $$('img.upload').invoke('show');
}

uploadEnable = function() {
  $$('input[type=submit]').invoke('show');
  $$('img.upload').invoke('hide');
}


document.observe('dom:loaded', function() {
  $('project_product_color_id').observe('change', function() {
    updateColorImage();
    updateSubtotal();
  });
  $('project_product_quantity_id').observe('change', updateSubtotal );

  updateSubtotal();
  updateColorImage();

  
    new Ajax.Request('/product_sizes/update_customs_dropdowns?id=48', {asynchronous:true, evalScripts:true, method:'get', parameters:'authenticity_token=' + encodeURIComponent('5PHaZfgAuvix0W0wL62SnU7xtcrLU2ddDOrRSE95rd8=')})
  

  /* $('tab1').setStyle('display:block'); */
  $$('a.calculate').invoke('toggle');
});
