// Register onChange handler for the 'seasons' option box
jQuery('#fashionShowsSeason').change(function() {
  selectedSeason = jQuery(this).val();
  
  if (selectedSeason == '') {
    clearDesigners();
    return;
  }
  
  retrieveDesigners(selectedSeason);
  //designersReceived({designers: [{name: 'Hallo', latestEntryURL: "http://abc.de"}]});
});

// Register onChange handler for the 'designers' option box
jQuery('#fashionShowsDesigners').change(function() {
  latestEntryURL = jQuery(this).val();
  
  // Set this URL in the 'Go' link
  theGoLink = jQuery('#fashionShowsGo');
  theGoLink.attr('href', latestEntryURL);
  
  // Automatically navigate the link
  window.location = latestEntryURL;
});

// Initially fill the 'fashionShowsDesigners' list with "Select a designer"
clearDesigners();

function appendOption(selectElement, text, value) {
  var optn = document.createElement("option");
  optn.text = text.toString();
  optn.value = value.toString();
  selectElement[0].options.add(optn);
}

function clearDesigners() {
  // Remove all current displayed designers
  selectElement = jQuery('#fashionShowsDesigners');
  selectElement.attr("disabled", "disabled");
  selectElement.children().remove().end();
  // Show first item to select no designer as default
  appendOption(selectElement, "Select a designer", 'sdas');
}

// This function will be called whenever the list of designers
// have been received for a given season.
function designersReceived(designers) {
  // Remove all currently displayed designers
  clearDesigners();
  
  selectElement = jQuery('#fashionShowsDesigners');
  selectElement.removeAttr("disabled");
  jQuery.each(designers.designers, function(index, item) {
    // Add designer to list
    appendOption(selectElement, item.name, item.latestEntryURL);
  });
}

function retrieveDesigners(seasonString) {
  // Get the list of designers from the server
  jQuery.getJSON("/shows/findFashionShowsDesigners.htm",
                 { season: seasonString },
                 designersReceived);
}

