//**********************************************************************//
// Quick Quote
// Source: n/a
//**********************************************************************//


jQuery(document).ready(function($){
  var productID = -1
  var url = window.location.href
  
  if (url.indexOf('quick-quote') != -1)
  {
    var hash = window.location.hash
    if (!hash){
      showStep(1, false)
    }
    else {
      productID = hash.substring(1)
      $('.wood_type').html(eval('details_' + productID).title)
      showStep(2)
    }
  }
  
  $('.choose_product_link').click(function(event){
    event.preventDefault()
    productID = $(this).attr('data-id')
    window.location.hash = '#' + productID
    $('.wood_type').html(eval('details_' + productID).title)
    
    showStep(2)
  })
  
  $('#calculate_quote').click(function(event){
    event.preventDefault()
    var width = $('#width').val()
    var height = $('#height').val()
    
    $('#width_display').html(width)
    $('#height_display').html(height)
    $('#quote_number').html(width * height * eval('details_' + productID).price)
    $('.wood_type').html(eval('details_' + productID).title)
    
    showStep(3)
  })
  
  $('#step_1_button').click(function(event){
    showStep(1)
  })
  
  $('#step_2_button').click(function(event){
    if (productID != -1)
      showStep(2)
  })
  
  $('#step_3_button').click(function(event){
    if (productID != -1)
      showStep(3)
  })
  
  $('#email_button').click(function(){
    var title = eval('details_' + productID).title
    var price = eval('details_' + productID).price
    var width = $('#width').val()
    var height = $('#height').val()
    var total = price * width * height
    var email = $('#email').val()
    var name = $('#name').val()
    var phone = $('#phone').val()
    var postcode = $('#postcode').val()
    
    //alert(title + ' - ' + price + ' ' + width + 'x' + height + ' total: ' + total)
    $.ajax({
      type: 'POST',
      url: '/wp-content/themes/woodfloors/mailer.php',
      data: {title: title, price: price, postcode: postcode, width: width, height: height, total: total, to: email, phone: phone, name: name},
      success: function(data){
        $('#status').html('<p>Your request was received and a quote should be on its way to your inbox.</p><p>Hit the \'Back\' button below to create a new quote.</p>').fadeIn()
        $('#error').hide()
        showStep(4)
      },
      error: function(XMLHttpRequest, textStatus, errorStatus){
        if (XMLHttpRequest.status == 422){
          $('#error').html(XMLHttpRequest.responseText).fadeIn()
        }
        else
          $('#error').html('An unknown error occured (' + XMLHttpRequest.status + ')').fadeIn()
      }
    })
  })
})

function showStep(step, scroll){
  scroll = typeof(scroll) != 'undefined' ? scroll : true;
  
  var scrollTop = $(document).scrollTop()
  $('#quickquote').offset()
  $('#step_1, #step_2, #step_3, #step_4').hide()
  $('#step_' + step).fadeIn()
  
  if (scroll){
    $(document).scrollTop(scrollTop)
    var target_offset = $('#quickquote').offset()
    var target_top = target_offset.top;
    $('html, body').animate({scrollTop:target_top}, 500)
  }
  
  $('.step_button_selected').removeClass('step_button_selected').addClass('step_button')
  for (var i = 1; i <= step; i++){
    $('#step_' + i + '_button').removeClass('step_button')
    $('#step_' + i + '_button').addClass('step_button_selected')
  }
}

