$(function() {
  $('#poly-custom-thick').change(calcCustomPrice);
  $('#poly-traffic-size').change(calcTrafficPrice);
  
  $('#poly-custom-size').change(function() {
    var size = $(this).attr('value');
    $('#shapes-for-'+size).show().siblings('.shape-list').hide().children().removeClass('selected');
    $('#poly-custom-shape').attr('value', '');
    $('#poly-custom-preview').attr('src', '/images/poly-custom/no-preview.png');
	calcCustomPrice();
  });

  $('.shape-list img').addClass('hoverable').click(function() {
    $(this).siblings('img').removeClass('selected');
    $(this).addClass('selected');
    var n = $(this).parent().children('img').index(this) + 1;

    if ($('#poly-custom-shape').size()) {
      $('#poly-custom-shape').attr('value', n);
      var size = $('#poly-custom-size').attr('value');
      $('#poly-sign-name').attr('value', shapeNames[size+'-'+n]);
      $('#poly-custom-preview').attr('src', '/images/poly-custom/shape-'+size+'-'+n+'-preview.png');
	  
    } else {
      $('#poly-traffic-sign').attr('value', n);
      var ns = n < 10 ? '0'+n : n;
      $('#poly-traffic-preview').attr('src', '/images/poly-traffic/sign-'+ns+'-preview.png');
	  calcTrafficPrice();
    }
  });

  $('.poly-custom-colors img').click(function() {
    $('.poly-custom-colors img').removeClass('selected');
    $(this).addClass('selected');

    var n = $(this).parent().children('img').index(this);
    var colors = ['red on white','blue on white','black on white','green on white','white on red','white on blue','white on black','white on green'];
    $('#poly-color').attr('value', colors[n]);

  });

  $('.poly-custom-fonts img').click(function() {
    $(this).siblings('img').removeClass('selected');
    $(this).addClass('selected');

    var n = $(this).parent().children('img').index(this);
    var colors = ['Helvetica Medium', 'Antique Olive', 'Baskerville', 'Baskerville Italic', 'Balloon Bold', 'De Vinne'];
    $('#poly-font').attr('value', colors[n]);

  });
  calcCustomPrice();
  calcTrafficPrice();
});

var customPrice = {
	0:{0:'37.00',1:'49.00',2:'110.00',3:'73.00',4:'73.00',5:'130.00'},
	1:{0:'52.00',1:'62.00',2:'155.00',3:'104.00',4:'104.00',5:'183.00'},
	2:{0:'44.00',1:'59.00',2:'110.00',3:'88.00',4:'88.00',5:'156.00'},
	3:{0:'59.00',1:'79.00',2:'178.00',3:'119.00',4:'119.00',5:'210.00'}
};
var trafficPrice = {
	'18" x 12"':'58.50','12" x 18"':'58.50','18" x 18"':'87.75','18" x 24"':'117.00','24" x 24"':'156.00','30" x 30"':'243.00'
};
function calcCustomPrice(){
	if($('#poly-custom-thick').length <= 0) return false;
	$('#price').text(customPrice[$('#poly-custom-thick').attr('selectedIndex')][$('#poly-custom-size').attr('selectedIndex')]);
};
function calcTrafficPrice(){
	if($('#poly-traffic-size').length <= 0) return false;
	$('#price').text(trafficPrice[$('#poly-traffic-size').val()]);
};

function polyCustomSubmit() {
  if ($('#poly-custom-shape').attr('value') != '') {
    $('#poly-custom-form').submit();
    return false;
  } else {
    alert('You must select a shape.');
    return false;
  }
}
