var NetworkSelector = (function () {
  var campaignType,
      campaignMedium,
      currentPanel = 0,
      fauxSelects;

  return {
    initialize: function () {
      $('.network-selector input').click(NetworkSelector.selectionMade);
      $('.network-selector .previous-panel').click(function () {
        NetworkSelector.goToPanel('previous');
      });
      $('.network-selector .show-results').click(function () {
        NetworkSelector.fetchResults();
      });

      fauxSelects = $('.network-selector dl');
      fauxSelects.find('dd').bind('selectionMade', function () {
        if (campaignType == 'roc') {
          NetworkSelector.resetFauxSelects(fauxSelects.filter(':visible').find('dd'), $(this));
        }
      });

      $('#result').hide();
    },

    selectionMade: function () {
      var input = $(this),
          inputId = input.attr('id');

      switch (inputId) {
        case 'campaign-type-ros':
          campaignType = 'ros';
          break;

        case 'campaign-type-roc':
          campaignType = 'roc';
          break;

        case 'campaign-medium-display':
          campaignMedium = 'display';
          break;

        case 'campaign-medium-mobile':
          campaignMedium = 'mobile';
          break;

        case 'campaign-medium-video':
          campaignMedium = 'video';
          break;
      }

      NetworkSelector.segment();
      NetworkSelector.goToPanel('next');
    },

    goToPanel: function (panel) {
      var continueToPanel,
          panels = $('.network-selector fieldset');

      if (panel == 'previous') {
        continueToPanel = currentPanel - 1;

      } else if (panel == 'next') {
        continueToPanel = currentPanel + 1;

      } else if (typeof panel == 'number') {
        continueToPanel = panel;
      }

      if (continueToPanel < 0) {
        throw new Error('Illegal index');
      } else {
        currentPanel = continueToPanel;
      }

      panels.css({'display': 'none'});
      panels.filter(':eq(' + continueToPanel + ')').css({'display': 'block'});
      
      if (currentPanel == 2){
        
        if (campaignType == 'roc'){
          $('.network-selector h4').text('Maak hieronder je keuze tussen umfeldchannel of doelgroepchannel');
        }
        
        if (campaignType == 'ros'){
          $('.network-selector h4').text('Maak hieronder de keuze tussen umfeld en/of doelgroep en/of bezoekersaantallen');
        }
        
      }
    },

    segment: function () {
      var liNodes = $('.selectors > li'),
          nodesToHide;

      liNodes.css('display', '');

      nodesToHide = liNodes.filter(function () {
        var liNode = $(this),
            shouldBeHidden = false;  

        if (campaignType == 'ros' && liNode.hasClass('roc') ||
            campaignType == 'roc' && liNode.hasClass('ros')) {
          shouldBeHidden = true;
        }

        if (campaignMedium == 'display' && !liNode.hasClass('display') ||
            campaignMedium == 'mobile' && !liNode.hasClass('mobile') ||
            campaignMedium == 'video' && !liNode.hasClass('video')) {
          shouldBeHidden = true;
        }
        return shouldBeHidden;
      });
      nodesToHide.css('display', 'none');
    },

    resetFauxSelects: function (fauxSelects, keepIntact) {
      fauxSelects.each(function () {
        var firstOption,
            firstOptionData,
            firstOptionText,
            currentDD = $(this),
            currentDL = currentDD.parent();

        if (currentDD[0] !== keepIntact[0]) {
          firstOption     = currentDD.find('li:first-child');
          firstOptionData = firstOption.attr('data-value');
          firstOptionText = firstOption.text();

          currentDL.find('dt span:not(.trigger)').text(firstOptionText);
          currentDL.prev()[0].selectedIndex = 0;
        }
      })
    },

    fetchResults: function () {
      var form,
          url = '/adverteren/internet/netwerkselector/result';

      form = {
        'targetaudience': $('#' + campaignType + '-' + campaignMedium + '-ta').val(),
        'category': $('#campaign-medium-' + campaignMedium).val(),
        'selectiontype': $('#campaign-type-' + campaignType).val(),
        'channel': $('#' + campaignMedium + '-umfeldchannel').val(),
        'uniquevisitors': $('#unique-visitors').val(),
        'pageviews': $('#pageviews').val()
      }

      url += '/' + form.selectiontype;
      url += '/' + form.category;
      url += '/' + form.targetaudience;
      url += '/' + form.channel;
      url += '/' + form.uniquevisitors;
      url += '/' + form.pageviews;

      $('#result').html('<img src="/images/loader.gif" alt="Laden..."/>');
      $('#result').show();

      $.ajax({
        url: url,
        method: 'get',
        dataType: 'html',
        success: function (data) {
          $('#result').html(data);
          $('#result .infobox').infoBox();
        }
      });
    }
  }
})();
