$(document).ready (function () 
{      
  var choices = [];
  
  choices ['geen-keuze'] = [];
  choices ['geen-keuze'] ['geen-keuze']                              = [5, 1, 2, 3, 4, 6, 7, 8, 9, 10];
  choices ['geen-keuze'] ['starter']                                 = [1, 2, 5, 4, 6, 7, 8, 9];
  choices ['geen-keuze'] ['oversluiter']                             = [3, 5, 6, 7, 10];
  choices ['geen-keuze'] ['heb-een-huis-wil-een-nieuw-huis']         = [4, 5, 6, 1, 2, 3, 7];
  choices ['geen-keuze'] ['verbouwen']                               = [5, 6, 3, 4, 7, 10];
  
  choices ['heb-een-hypotheek'] = [];
  choices ['heb-een-hypotheek'] ['geen-keuze']                       = [5, 3, 4, 6, 7, 10];
  choices ['heb-een-hypotheek'] ['starter']                          = 'U kunt niet al een hypotheek hebben en tevens starter zijn.\nCorrigeer uw selectie.';           
  choices ['heb-een-hypotheek'] ['oversluiter']                      = [10, 3, 5, 6, 7];
  choices ['heb-een-hypotheek'] ['heb-een-huis-wil-een-nieuw-huis']  = [4, 5, 6, 1, 2, 3, 7];
  choices ['heb-een-hypotheek'] ['verbouwen']                        = [5, 6, 3, 4, 7, 10];
  
  choices ['heb-geen-hypotheek'] = [];
  choices ['heb-geen-hypotheek'] ['geen-keuze']                      = [5, 1, 2, 3, 4, 6, 7, 8, 9];
  choices ['heb-geen-hypotheek'] ['starter']                         = [6, 1, 2, 3, 4, 5, 7, 8, 9];
  choices ['heb-geen-hypotheek'] ['oversluiter']                     = 'U kunt niet nog geen hypotheek hebben en tevens oversluiter zijn.\nCorrigeer uw selectie.';
  choices ['heb-geen-hypotheek'] ['heb-een-huis-wil-een-nieuw-huis'] = 'U kunt niet nog geen hypotheek hebben en tevens al een huis hebben en een nieuw willen.\nCorrigeer uw selectie.';
  choices ['heb-geen-hypotheek'] ['verbouwen']                       = 'U kunt niet nog geen hypotheek hebben en tevens wilen verbouwen.\nCorrigeer uw selectie.';
  
  function getMortage ()
  {
    return $ ('input#mortage').val ();
  }
  
  function getSituation ()
  {
    return $ ('input#situation').val ();
  } 
  
  function countModules ()
  {  
    var hasChoices = ($.isArray (choices [$ ('input#mortage').val ()] [$ ('input#situation').val ()]));
    if (hasChoices == false)
    {  
      alert (choices [$ ('input#mortage').val ()] [$ ('input#situation').val ()]);
    }
    
    return (hasChoices ? choices [$ ('input#mortage').val ()] [$ ('input#situation').val ()].length : 0);
  }
  
    
  var mortage   = 'geen-keuze';  
  var situation = 'geen-keuze';    
 
  $ ('#eenhypotheek form#f1 label, #mijn_situatie form#f2 label').live ('click', function ()
  {                        
    $ ('label', $ (this).parent ()).each (function ()
    {
      $ (this).removeClass ('active');
    });
    
    $ (this).addClass ('active');

    if ($ (this).attr ('id').indexOf ('mortage') != -1)
    {
      mortage = $ (this).attr ('id').replace ('mortage-', '');
    }

    if ($ (this).attr ('id').indexOf ('situation') != -1)
    {
      situation = $ (this).attr ('id').replace ('situation-', '');
    }
    
    var type = $ (this).attr ('id').substring (0, $ (this).attr ('id').indexOf ('-'));     
    $ ('input#' + type).val ($ (this).attr ('id').replace (type + '-', ''));
    
    var countItems = countModules ();
    $ ('a#search_modules').html ('Toon ' + countItems + '/10 rekenmodules');
  });

  var searched = false;
  $ ('a#search_modules').live ('click', function ()
  {  
    searched = true;       
    if (countModules () > 0)
    {
      window.location.hash = '#' + mortage + '|' + situation; 
    }    
    
    showModules (choices);
  });
    
  if (searched == false)
  {
    showModules (choices);  
  }
  
  $ ('a#renew_search').live ('click', function ()
  {
    window.location.hash = '';
    $ ('#subpage').hide ();
    $ ('#left').show ();
  });
  
  $ ('a#show_all').live ('click', function ()
  {
    window.location = '#geen-keuze|geen-keuze';
    showModules (choices);
    
    $ ('html, body').animate ({scrollTop: 0}, 'slow');
  });
  
  makeGoBack ();
    
  $ ('#overons').hover (function ()
  {
    $ ('p', this).css ('text-decoration', 'underline');  
    $ ('.pijloverons', this).css ('background-image', 'url(images/pijloverons_hover.gif)');  
  }, function ()
  {
    $ ('p', this).css ('text-decoration', 'none'); 
    $ ('.pijloverons', this).css ('background-image', 'url(images/pijltje.gif)');  
  });
  
    
  $ ('.situatie').hover (function ()
  {
    $ ('p', this).css ('text-decoration', 'underline');  
    $ ('.pijloverons', this).css ('background-image', 'url(images/pijloverons_hover.gif)');  
  }, function ()
  {
    $ ('p', this).css ('text-decoration', 'none'); 
    $ ('.pijloverons', this).css ('background-image', 'url(images/pijltje.gif)');  
  });  
});

function showModules (choices)
{
  if (window.location.hash != '')
  {
    var hash = window.location.hash;
    if (hash.indexOf ('|') != -1)
    {
      var selMortage   = hash.substring (2, hash.indexOf ('|'));
      var selSituation = hash.substring ((hash.indexOf ('|') + 1)); 
      
      $ ('#modules .module-odd, #modules .module-even').hide ();   
      
      if (typeof choices [selMortage] == 'undefined')
      {  
        selMortage = 'geen-keuze';         
      }
      
      if (typeof choices [selMortage] [selSituation] == 'undefined')
      {
        selSituation = 'geen-keuze';
      }
      
      $ ('#geselecteerd strong').html ((selMortage + ' / ' + selSituation).replace (/-/g, ' '));
      $ ('span#advise_count').html (($.isArray (choices [selMortage] [selSituation]) ? choices [selMortage] [selSituation].length : 0));      
      
      if (typeof choices [selMortage] != 'undefined' && typeof choices [selMortage] [selSituation] != 'undefined')  
      {    
        var j = 1;
        for (i in choices [selMortage] [selSituation])
        {          
          var key = choices [selMortage] [selSituation] [i];
          $ ('#modules').append ($ ('#modules #item_' + key));
          
          $ ('#modules #item_' + key + ' h3 span.number').html (j);
          $ ('#modules #item_' + key).removeClass ('module-odd');
          $ ('#modules #item_' + key).removeClass ('module-even');
          $ ('#modules #item_' + key).addClass ('module-' + (j % 2 == 0 ? 'odd' : 'even'));          
          
          var className = $ ('#modules #item_' + key).attr ('class');
             
          $ ('#modules #item_' + key).live ('click', function () { window.location = $ (this).attr ('class').substring (0, $ (this).attr ('class').indexOf (' ')) + '#' + selMortage + '|' + selSituation; });
          
          $ ('#modules #item_' + key).show ();
          
          j++;
        }
         
        if (j > 1)
        {
          $ ('#left').hide ();
          $ ('#subpage').show ();       
        } 
      }
   
      $ ('#modules .module-odd, #modules .module-even').hover (function ()
      {
        $ ('p', this).css ('text-decoration', 'underline');  
        $ ('.inner', this).css ('background-image', 'url(images/pijloverons_hover.gif)');
      }, function ()
      {
        $ ('p', this).css ('text-decoration', 'none'); 
        $ ('.inner', this).css ('background-image', 'url(images/pijltje.gif)'); 
      });           
    }
  }
}

function makeGoBack ()
{                                 
  var hash = window.location.href;
  var pos  = hash.indexOf ('#');

  if (pos > 0 && hash.charAt ((pos - 1)) != '/')
  {
    var href = $ ('#go_back').attr ('href');
    $ ('#go_back').attr ('href', href.substring (0, href.indexOf ('#')) + '' + hash.substring (pos));
  }
}

function showPartner (value)
{
  if (value == 1)
  {
     $ ('#gross_salary_partner').show ();  
  }
  else
  {
    $ ('#gross_salary_partner').hide ();      
  }
}
