$(document).ready(function(){
  // Set the search box to empty upon click
	$('.search_box').bind('click', function(){
		var field =  $(this);							   
		if (field.val()=="Search") field.val("");
	});
  
  /* Handle tabs */
  var selected = "items"
  var tabs = new Array("items","colours","specs","wheretobuy");
  $('#tabs_alt li a').bind('click',function(){
    // Get the clicked cloass
    clickedclass = $(this).attr('class');
    // Remove the active id 
    $('#tab_active').attr('id','');
    $('.' + clickedclass).parent().attr('id','tab_active')  ;
    
    
    clickedclass = clickedclass.substr(4);
    // Show only clicked tab
//    $('#tabs .current').hide('fast');
    $('#tabs .current').hide(0);
    //$('.tabs_alt').removeClass('current');    
//    $('#' + clickedclass  + '.pane').addClass('current').show('fast');  
    $('#' + clickedclass  + '.pane').addClass('current').show(0); 
    return false;
  });
  
  $('#product_categories > li:nth-child(5),#product_categories > li:nth-child(9),#product_categories > li:nth-child(13),#product_categories > li:nth-child(17)').attr('class','newrow');

  // add clears to drop menu sub menus that are multiple of 4
  $('#product_menu > li:eq(4),#product_menu > li:eq(8),#product_menu > li:eq(12)').addClass('clearleft')

  // Products nav
  $('.nav-products').parents('li:eq(0)').hover(
    function()
    {
      $(this).addClass('visible');
      //$('.products-menu').addClass('visible');
    },
    function()
    { 
      //$('.products-menu').removeClass('visible');
      $(this).removeClass('visible');
    }
  
  );
  $('.category-filter').bind('change',function(){
//    alert("clicked:" + this.value);
    window.location=this.value;
  });
  
  $('.home .questions').bind('click',function(){
    window.location='/index.php/askjohn';
  });
  $('.home .questions').bind('hover',function(){
    $(this).addClass('hover');
  }, function(){
    $(this).removeClass('hover');
  });
  
  // Order form
  $('.order-form-stockcode').bind('change',function(){

    // When a drop down list is selected show it's text box and button to add a quantity
    
    // Save the clicked value
    var clickedval = $(this).val();
    // Return drop downs to defaul (no selection state)
    $('.group-item-qty').removeClass('visible').children("input[name='item_qty']").val('');
    $('.order-form-stockcode').val(0);
    // Make the form fields for the selected drop-down value visible
    if(clickedval!='')
    {
      $(this).siblings('.group-item-qty').addClass('visible').children("input[name='item_qty']").focus();
      // Set the clicked items value back to what was clicked (after our previous clear out)
      $(this).val(clickedval);
    }
  });
  
  // Bind all trough calculators to the TroughCalculate Function
  $('.troughcalcin').bind('keyup',TroughCalculate);
  $('.troughcalcin').bind('blur',TroughCalculate);
  
  // bind the siding calculator
  $('.btn_recalculate').bind('click',SidingCalculator);
  $('.btn_printform').bind('click',function(){
    window.print();
  });
  // bind the soffit calculator
  $('.btn_soffitcalc').bind('click',SoffitCalculate);
  
});

TroughCalculate = function()
  {
    var field = $(this).attr('Name');
    var clickedval = $(this).val();
    var returnval = "";
    if(clickedval>0)
    {
      switch(field)
      {
        case "5AluminumFtIn":
		  $('#5AluminumFtOut').val(roundNumber(clickedval*0.367/2.202,2));
          break;
        case "5AluminumKgIn":
          $('#5AluminumKgOut').val(roundNumber(clickedval*2.202/0.367,2));
          break;
        case "6AluminumFtIn":
          //$('#6AluminumFtOut').val(roundNumber(clickedval*0.489333/2.202,2));
          $('#6AluminumFtOut').val(roundNumber(clickedval*0.57252/2.202,2));
          break;
        case "6AluminumKgIn":
          //$('#6AluminumKgOut').val(roundNumber(clickedval*2.202/0.489333,2));
          $('#6AluminumKgOut').val(roundNumber(clickedval*2.202/0.58,2));
          break;  
        case "5SteelFtIn":
          $('#5SteelFtOut').val(roundNumber(clickedval*0.734/2.202,2));
          break;
        case "5SteelKgIn":
          $('#5SteelKgOut').val(roundNumber(clickedval*2.202/0.73,2));
          break;
        case "5GutterCoil16ozFtIn":
          $('#5GutterCoil16ozFtOut').val(roundNumber(clickedval*0.991/2.202,2));
          break
        case "5GutterCoil16ozKgIn":
          $('#5GutterCoil16ozKgOut').val(roundNumber(clickedval*2.202/0.991,2));
          break;
        case "6GutterCoil16ozFtIn":
          $('#6GutterCoil16ozFtOut').val(roundNumber(clickedval*1.565/2.202,2));
          break;
        case "6GutterCoil20ozKgIn":
          $('#6GutterCoil20ozKgOut').val(roundNumber(clickedval*2.202/1.565,2));
          break;
        default:
       }    
    }
  }
SoffitCalculate = function()
{
	var soffit_depth = $('#soffit_depth').val();
	var fascia_gable_length = $('#fascia_gable_length').val();
	$('#d5_soffit_required').val(roundNumber(
		Math.ceil(
		fascia_gable_length*(12/10)
		,1)/(Math.floor(144/soffit_depth)),2));
	$('#t3_soffit_required').val(roundNumber(Math.ceil(fascia_gable_length*(12/16))/(Math.floor(144/soffit_depth)),2));
	$('#quad_soffit_required').val(roundNumber(Math.ceil(fascia_gable_length*(12/15))/(Math.floor(144/soffit_depth)),2));
}

  
function IsNumeric(value) 
{ 
    if (value.match(/^\d+$/) == null) 
        return false; 
    else 
        return true; 
} 
function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
  var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
  return newnumber; // Output the result to the form field (change for your purposes)
}


function SidingCalculator() {
  var sa = 0;
  for(i=1;i<9;i++)
  {
    width = $('#width' + i).val();
    height = $('#height' + i).val();
    sa_row = ((width  *  height)/12)/12;
    $('#sa' + i).val(sa_row);
    sa = sa + sa_row;
  }
  $('#sa').val(sa);
  

 // var sa = document.chkout.sa.value;
  var widthg1 = $('#widthg1').val();
  var widthg2 = $('#widthg2').val();
  var widthg3 = $('#widthg3').val();
  var widthg4 = $('#widthg4').val();
  var heightg1 = $('#heightg1').val();
  var heightg2 = $('#heightg2').val();
  var heightg3 = $('#heightg3').val();
  var heightg4 = $('#heightg4').val();
  var sag1 = ((widthg1 * heightg1/12) / 12);
  var sag2 = ((widthg2 * heightg2/12) / 12);
  var sag3 = ((widthg3 * heightg3/12) / 12);
  var sag4 = ((widthg4 * heightg4/12) / 12);
  $('#sag1').val(sag1/2);
  $('#sag2').val(sag2/2);
  $('#sag3').val(sag3/2);
  $('#sag4').val(sag4/2);
  var sag = ((sag1/1) + (sag2/1) + (sag3/1) + (sag4/1))/2; 
  $('#sag').val(sag);
  gsa = (sa/1 + sag/1);
  $('#gsa').val(gsa);  
  
  
  var open = $('#open').val();
  var netsa = $('#chkout').val();
  var netsa = (gsa - open);
  $('#netsa').val(gsa - open);
  var waste = (netsa * .10);
  $('#waste').val(waste);
  
  var ts = ((netsa/1) + (waste/1));
  $('#ts').val(((netsa/1) + (waste/1)));
  var soffw1 = $('#soffw1').val();
  var soffw2 = $('#soffw2').val();
  var soffw3 = $('#soffw3').val();
  var soffw4 = $('#soffw4').val();
  var soffh1 = $('#soffh1').val();
  var soffh2 = $('#soffh2').val();
  var soffh3 = $('#soffh3').val();
  var soffh4 = $('#soffh4').val();
  var soffsa1 = ((soffw1 * soffh1/12) / 12);
  var soffsa2 = ((soffw2 * soffh2/12) / 12);
  var soffsa3 = ((soffw3 * soffh3/12) / 12);
  var soffsa4 = ((soffw4 * soffh4/12) / 12);
  $('#soffsa1').val(((soffw1 * soffh1/12) / 12));
  $('#soffsa2').val(((soffw2 * soffh2/12) / 12));
  $('#soffsa3').val(((soffw3 * soffh3/12) / 12));
  $('#soffsa4').val(((soffw4 * soffh4/12) / 12));
  $('#soffsat').val(((soffsa1/1) + (soffsa2/1) + (soffsa3/1) + (soffsa4/1)));
}


