/*var par1 = '';
var par2 = '';
var par3 = '';
var par4 = '';
var par5 = '';*/
var p    = 0;

function wines(section, h, s) {
  h = $H(h);
  
  /*if(section == 'types') par1 = h.toQueryString();
  else if(section == 'years') par2 = h.toQueryString();
  else if(section == 'countries') par3 = h.toQueryString();
  else if(section == 'regions') par4 = h.toQueryString();
  else if(section == 'prices') par5 = h.toQueryString();*/
  //$('cregions').update();
  //$('regions').update();
  h.each(function(pair) {
    new Insertion.Top('search-wines', new Element('input', { 'type': 'hidden', 'name': pair.key, 'id': 'wines-' + section, 'value': pair.value }));
    if(section == 'countries') {
      if(pair.value == 'IT') { // Update regions
        new Ajax.Updater('regions', 'actionh-beta.php', {method: 'post', postBody: $('search-wines').serialize() + '&action=update&column=regions'});
      }
    }
  });

  if($('s' + section)) {
    $('s' + section).remove();
  }
  new Insertion.Top($('c' + section), new Element('span', { 'id': 's' + section }).update(s + ' <img alt="' + selected + '" src="images/haken.png" /><br />'));
  
  switcher(section);
}
function unhide(section) {
  /*if(section == 'types') par1 = '';
  else if(section == 'years') par2 = '';
  else if(section == 'countries') par3 = '';
  else if(section == 'regions') par4 = '';
  else if(section == 'prices') par5 = '';*/
  if(section == 'countries') { // Unhide countries
    $('regions').update(); // Clear countries
    if($('cregions').visible()) unhide('regions'); // Unhide regions
  }
  if($('wines-' + section)) {
    $('wines-' + section).remove();
  }
  
  switcher(section);
}
function switcher(section) {
  //new Ajax.Updater('wines', 'actionh-beta.php', {method: 'post', postBody: par1 + '&' + par2 + '&' + par3 + '&' + par4  + '&' + par5});
  goPage();
  
  new Ajax.Updater('types', 'actionh-beta.php', {method: 'post', postBody: $('search-wines').serialize() + '&action=update&column=types'});
  new Ajax.Updater('years', 'actionh-beta.php', {method: 'post', postBody: $('search-wines').serialize() + '&action=update&column=years'});
  new Ajax.Updater('countries', 'actionh-beta.php', {method: 'post', postBody: $('search-wines').serialize() + '&action=update&column=countries'});
  if(!$('regions').empty()) { // Empty countries
    new Ajax.Updater('regions', 'actionh-beta.php', {method: 'post', postBody: $('search-wines').serialize() + '&action=update&column=regions'});
  }
  new Ajax.Updater('prices', 'actionh-beta.php', {method: 'post', postBody: $('search-wines').serialize() + '&action=update&column=prices'});
  new Ajax.Updater('val', 'actionh-beta.php', {method: 'post', postBody: $('search-wines').serialize() + '&action=update&column=val'});
  
  $(section).toggle();
  $('c' + section).toggle();
}
function goPage(p) {
  //new Ajax.Updater('wines', 'actionh-beta.php', {method: 'post', postBody: $('searchByVino').serialize() + '&p=' + p + ''});
  new Ajax.Updater('wines', 'actionh-beta.php', {method: 'post', postBody: $('search-wines').serialize() + '&p=' + p});
}
Event.observe(window, 'load', function() {
  new Ajax.Updater('types', 'actionh-beta.php', {method: 'post', postBody: $('search-wines').serialize() + '&action=update&column=types'});
  new Ajax.Updater('years', 'actionh-beta.php', {method: 'post', postBody: $('search-wines').serialize() + '&action=update&column=years'});
  new Ajax.Updater('countries', 'actionh-beta.php', {method: 'post', postBody: $('search-wines').serialize() + '&action=update&column=countries'});
  //new Ajax.Updater('regions', 'actionh-beta.php', {method: 'post', postBody: $('search-wines').serialize() + '&action=update&column=regions'});
  new Ajax.Updater('prices', 'actionh-beta.php', {method: 'post', postBody: $('search-wines').serialize() + '&action=update&column=prices'});
  new Ajax.Updater('val', 'actionh-beta.php', {method: 'post', postBody: $('search-wines').serialize() + '&action=update&column=val'});
});
