WR.WizardForm = {

  init: function() {
    $('#morePages').click(function(){
      $('#wizard-pages li').show();
      $(this).parent().remove();
    });
    
    $('.tabs li').click(function(){
      $('.tabs li').removeClass('selected');
      $(this).addClass('selected');
      
      $('.tabBody').hide();
      var thisId = $(this).attr('id').split('-')[1];
      $('#wizard-' + thisId + '-tabBody').show();
    });

    $('input[name=onCloseClickStay]').click(function(){
      if (this.value == 'no') {
        $('#wizard-closeUrl-wrapper').show();
        $('#wizard-closeUrl').get(0).focus();
      }
      else {
        $('#wizard-closeUrl-wrapper').hide();
      }
    });
    
    $('input[name=doCoverBody]').click(function(){
      if (this.value == 'no') {
        $('#wizard-bodyCoverCloseOnClick-wrapper').hide();
      }
      else {
        $('#wizard-bodyCoverCloseOnClick-wrapper').show();
      }
    });

  },
  
  submit: function() {
    $('#wizard-form input[type=submit]').val('Update tour code').blur();
  
    var tourId     = $('#wizard-tourId');
    var tourIdHint = $('#wizard-tourId-hint');

    tourId.removeClass().addClass('std');
    tourIdHint.removeClass().addClass('hint');
    
    var firstErrorElement = false;
    
    if (tourId.val() === '' || !tourId.val().isAlphaNum()) {
      WR.WizardForm.showAdvancedOptions();
      if (!firstErrorElement) firstErrorElement = tourId;
      tourId.removeClass().addClass('std').addClass('error');
      tourIdHint.removeClass().addClass('hint-error');
      if (tourId.val() === '') {
        tourIdHint.html('Please choose a tour ID');
      }
      else {
        tourIdHint.html('Only alphanumerical values allowed (a-z, A-Z, 0-9)');
      }
    }
    
    var firstValidUrl = false;
    for (i = 1; i <= 10; i++) {
      pageUrl = $('#wizard-page' + i + 'Url');
      pageUrlHint = $('#wizard-page' + i + 'Url-hint');
      
      // initial state first
      pageUrl.removeClass().addClass('std');
      pageUrlHint.removeClass().addClass('hint');
      
      if (pageUrl.val() !== '' && !pageUrl.val().isURL()) {
        if (!firstErrorElement) { firstErrorElement = pageUrl; }
        pageUrl.removeClass().addClass('std').addClass('error');
        pageUrlHint.removeClass().addClass('hint-error');
        pageUrlHint.html('Please provide a valid URL');
      }
      
      if (!firstValidUrl && pageUrl.val().isURL()) {
        var firstValidUrl = pageUrl.val();
      }
    }
    
    if (!firstValidUrl) {
      page1Url = $('#wizard-page1Url');
      if (!firstErrorElement) { firstErrorElement = page1Url; }
      page1Url.removeClass().addClass('std').addClass('error');
      page1UrlHint = $('#wizard-page1Url-hint');
      page1UrlHint.removeClass().addClass('hint-error');
      page1UrlHint.html('Provide the URL of 1 tour page at least');
    }
    
    if (firstErrorElement) {
      $('#wizard-output').hide();

      // @todo fix hack: always jumping to first tab on error
      $('.tabs li').removeClass('selected');
      $('#tab-basic').addClass('selected');      
      $('.tabBody').hide();
      $('#wizard-basic-tabBody').show();      
      
      firstErrorElement.get(0).focus();
      firstErrorElement.prev().ScrollTo();
    }
    else {
      if (firstValidUrl) {
        var skinId = $('#wizard-skinId').val();
        var entryUrl = firstValidUrl + 
          (firstValidUrl.indexOf('?') == -1 ? '?' : '&') + 
          'tourId=' + tourId.val() + 
          (skinId != 'model_t' ? '&skinId=' + skinId : '');
          
        $('#wizard-startUrl').show().html('<a target="_blank" href="' + entryUrl + '">' + entryUrl + '</a>');
        $('#wizard-buttonCode').show().html('&lt;a href=&quot;' + entryUrl + '&quot;&gt;&lt;img src=&quot;' + config.SITE_URL + 'src/stable/amberjack.png&quot; alt=&quot;Click to start tour!&quot; /&gt;&lt;/a&gt;');
        $('#wizard-button').show().html('<a target="_blank" href="' + entryUrl + '"><img src="' + config.SITE_URL + 'src/stable/amberjack.png" alt="Click to start tour!" /></a>');
      }
      else {
        $('#wizard-startUrl').hide();
        $('#wizard-buttonCode').hide();
        $('#wizard-button').hide();
      }

      $.ajax({
        url:      config.WIZARD_URL,
        type:     'POST',
        data:     $.param($('#wizard-form').serialize().vars),
        dataType: 'html',

        success:  WR.WizardForm.showResult
      });
    }
  },
  
  showResult: function(data) {
    $('#wizard-code').html('<pre>' + data + '</pre>');
    $('#wizard-output').show();
    $('#wizard-output').ScrollTo();  
  }
  
};

$(document).ready(WR.WizardForm.init);