function bindWizardLink() {
  $('.falk_wizard_trigger, ' + $menu_link_selector).click(function(){
    openFalkWizard();
    return false;
  });
    
}

function openFalkWizard() {
  $.colorbox({
    href: FALK_WIZARD_LOAD_URL,
    overlayClose: false,
    escKey: false,
    scrolling: false,
    transition: 'fade',
    speed: 500,      
    onComplete: function() {
      step_0_script();
    }
  });
}

function moveSlider(step) {
  var $base = -516;
  if (!(step == $('#steps_slider li').length)) {
    var $offset = $base + (step*40);  
  } else {
    $offset = 0;
  }
  
  $('#steps_slider').animate({
    backgroundPosition: $offset+'px 0'
  });
  changeSliderLinks(step);
}

function changeSliderLinks(step) {
  $('#steps_slider li').each(function(){
    var $current_step = $(this).index() + 1;
    if($current_step <= step) {
      $(this).html('<a href="#_SELF" class="step_link" rel="' + $current_step + '">' + $current_step + '</a>');
    } else {
      $(this).html($current_step);
    }
    bindSliderLinks();
  });
}

function bindSliderLinks() {
  var $current_step = $('#wizard_current_step').val();
  $('.step_link').each(function(){
    if(!$(this).hasClass('bind_done')) {
      var $step_to_go_to = $(this).attr('rel');
      $(this).click(function(){
        if($current_step != $step_to_go_to) {
          goToStep($step_to_go_to);
          moveSlider($step_to_go_to);          
        }
      });
      $(this).addClass('bind_done');
    }
  });
}

function goToStep(step) {
  if((step <=6 ) && ($('#wizard_type').val() == '0'))
    $('#loaded_wizard_type').val(1);
  $('div.active_step_container').hide().removeClass('active_step_container');
  $('div[id^="step_container_"]').each(function(){
    var $div_step_no = $(this).index()-4;
    if($div_step_no == step)
      $(this).addClass('active_step_container').show();
    if($div_step_no > step)
      $(this).html('');
  });
  $('#wizard_current_step').val(step);
}

function loadText() {
  $('#step_text').hide();
  $('#step_text').load(CONTROLLER_URL, {
    'action': 'load_text',
    'type': $('#loaded_wizard_type').val(),
    'current_step': $('#wizard_current_step').val()
  }, function(){
    $('#step_text').show();
  });
}

function step_0_script(){
  $('#wizard_right').addClass('content_right_start').removeClass('content_right_step');
  $('#step_content').addClass('step_content_start').removeClass('step_content_step');
  $('.start_link').click(function(){
    $('#step_content').load(
      $(this).attr('href'), {
        'action': 'start_wizard',
        'type': $(this).attr('rel')
      },function(){
        $('#wizard_right').addClass('content_right_step').removeClass('content_right_start');
        $('#step_content').addClass('step_content_step').removeClass('step_content_start');
        loadText();
        step_1_script();
      });
    return false;
  });
}

function step_1_script() {
  showStepHeader();
  $('#falk_wizard_form').ajaxForm({
    target: 'div.active_step_container',
    beforeSubmit: function(){
      $('div.active_step_container').removeClass('active_step_container').hide().next().addClass('active_step_container');
    },
    success: function(){
      $('#wizard_current_step').val(parseInt($('#wizard_current_step').val())+1);
      $('div.active_step_container').show();
      loadText();
      bindNavigationLinks();
      moveSlider($('#wizard_current_step').val());
      if($('#wizard_current_step').val() == 2 || $('#wizard_current_step').val() == 8)
        bindProductRadios();
      if($('#wizard_current_step').val() == 6 || $('#wizard_current_step').val() == 12)
        bindFinalLinks();
    }
  });
  bindNavigationLinks();
}

function bindProductRadios() {
  $('.product_image_toggle').each(function(){
    var $image_container = $(this).closest('div[id^="step_container_"]').children('.wiz_images');
    if(!$(this).hasClass('bind_done')) {
      $(this).change(function(){
        if($(this).attr('checked')) {
          $image_container.hide();
          $image_container.children('.product_image').hide();
          if ($(this).hasClass('has_picture')) {
            $image_container.show();  
            $image_container.children('img#product_image_' + $(this).val()).show();
          }
        }
      });
      $(this).addClass('bind_done');
    }
  });
}

function showStepHeader() {
  $('#wizard_type_text').show();
  $('#header_' + $('#loaded_wizard_type').val()).show();
}

function bindNavigationLinks() {
  $('.next_link').each(function(){
    if(!$(this).hasClass('bind_done')) {
      $(this)
      .click(function(){
        if($('#falk_wizard_form').valid())
          $('#falk_wizard_form').submit();
        return false;
      })
      .addClass('bind_done');      
    }
  });
  
  $('.previous_link').each(function(){
    if(!$(this).hasClass('bind_done')) {
      $(this)
      .click(function(){
        $('div.active_step_container').html('').hide().removeClass('active_step_container').prev().addClass('active_step_container').show()
        $('#wizard_current_step').val(parseInt($('#wizard_current_step').val())-1);
        if($(this).hasClass('move_to_roof')) {
          $('#loaded_wizard_type').val(1);
        }
        moveSlider($('#wizard_current_step').val());
        loadText();
        return false;
      })
      .addClass('bind_done');      
    }
  });
  
  if(!$('#restart_link').hasClass('bind_done')){
    $('#restart_link').click(function(){
      $('#cboxLoadedContent').load($(this).attr('href'), function(){
        step_0_script();
      });
      return false;
    });
    $('#restart_link').addClass('bind_done');
  }
  $("#falk_wizard_form").validate({
    errorPlacement: function(error,element) {
      return true;
    }
  });
  styleForm();
}

function bindFinalLinks() {
  $('.final_link').click(function(){
    if(($('#wizard_type').val() != '0') || ($('#loaded_wizard_type').val() == '2')) {
      goToFinalStep();
    } else {
      $('#loaded_wizard_type').val(2);
      $('#falk_wizard_form').submit();
      $('#wizard_type_text div').hide();
      showStepHeader();
    }
  });
}

function styleForm() {
  if($('#falk_wizard_form').hasClass('jqtransformdone'))
    $('#falk_wizard_form').removeClass('jqtransformdone');
  $('#falk_wizard_form').jqTransform();
}

function goToFinalStep() {
  $('#wizard_container').hide();
  $('#wizard_final').show('fast', function(){
    $('#submit_name, #submit_address, #submit_company, #submit_postcode, #submit_city, #submit_phone').addClass('required');
    $('#submit_email').addClass('required').addClass('email');
    $("#falk_wizard_form").validate({
      errorPlacement: function(error,element) {
        return true;
      }
    });
    if(!$('#wizard_final select').hasClass('bind_done')) {
      $('#wizard_final select').removeClass('jqTransformHidden');
      styleForm();
      $('#wizard_final select').addClass('bind_done');
    }

    submitForm();
    if(!$('a#return_to_wizard_link').hasClass('bind_done')) {
      $('a#return_to_wizard_link').click(function(){
        $('#submit_name, #submit_address, #submit_company, #submit_postcode, #submit_city, #submit_phone').removeClass('required');
        $('#submit_email').removeClass('required').removeClass('email');
        $('#wizard_final').hide();
        $('#wizard_container').show();
        $('a#return_to_wizard_link').addClass('bind_done');
        return false;
      });      
    }      
  });
}

function submitForm() {
  if(!$('#ask_offer_link').hasClass('bind_done')) {
    SI.Files.stylizeAll();
    $('#ask_offer_link').click(function(){
      if($('#falk_wizard_form').valid()) {
        $('#action_field').val('send_mail');
        $('#wizard_wrapper').block({
          message: null
        });
        $('#falk_wizard_form').ajaxSubmit({
          success: function(){
            $('#wizard_wrapper').unblock();
            $.colorbox.close();
          }
        });
      }
    });
    $('#ask_offer_link').addClass('bind_done');
  }

}

$(document).ready(function(){
  bindWizardLink();
});
