var selectedCompany = 0;
var selectedAnalysis = 0;
var hilight=true;

$(function(){
  goText = $('#cvGoButton').attr("value");

  /* Update analyst menu when company menu is changed: */
  $("select.companySelect").change(function(){
      setGoButtonState(false);
      var analystIdSelector = "select#analysts";
      var aspId;
      aspId = $("select.aspSelect").val();
      if( isNaN(aspId) ) {
        aspId=0;
      }
    $.getJSON("/CompanyViewsAJAX_getAnalystMenu.action",{menuAspId: aspId, menuGlobalCompanyId: $(this).val(), liveMenuUpdate: true}, function(j){
      var options = '';
      for (var i = 0; i < j.length; i++) {
          
          var selected = "";
          
          if (j[i].optionValue==selectedAnalysis) {
              selected = " selected=\"selected\"";
          }
          
        options += '<option'+selected+' value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
        
      }
      $(analystIdSelector).html(options);
      var color = $(".views_list_header").css("background-color");
      if(hilight) {
        $(analystIdSelector).effect("highlight", {color: color}, 400);
      } else {
          hilight=true;
      }
      setGoButtonState(true);
    })
  })

  /* Update company menu when industry menu is changed: */
  $("select.industrySelect").change(function(){
      setGoButtonState(false);
      var companyIdSelector = "select#companies";
      var selectedCompanySelector = "select#companies option[value='"+selectedCompany+"']";
      var aspId;
      var industryId = $(this).val();
      aspId = $("select.aspSelect").val();
      if( isNaN(aspId) ) {
        aspId=0;
      }
     if( isNaN(industryId) ) {
        industryId=0;
      }      
    $.getJSON("/CompanyViewsAJAX_getCompanyMenu.action",{menuAspId: aspId, menuIndustryId: industryId, liveMenuUpdate: true}, function(j){
      var options = '';
      for (var i = 0; i < j.length; i++) {
        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
      }
      $(companyIdSelector).html(options);
      if($(selectedCompanySelector).val()!=null) {
          $(selectedCompanySelector).attr('selected','selected');
      }
      var color = $(".views_list_header").css("background-color");
      $(companyIdSelector).effect("highlight", {color: color}, 400);

      $(companyIdSelector).trigger("change");
    });
  })

  /* Update industry menu when asp menu is changed: */
  $("select.aspSelect").change(function(){
      setGoButtonState(false);
      var industryIdSelector = "select#industries";
      var aspId = $(this).val();
      if( isNaN(aspId) ) {
        aspId=0;
      }
    $.getJSON("/CompanyViewsAJAX_getIndustryMenu.action",{menuAspId: aspId, liveMenuUpdate: true}, function(j){
      var options = '<option value="none">' + i18n["cv_menu_select_industry"] + '</option>';
      for (var i = 0; i < j.length; i++) {
        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
      }
      $(industryIdSelector).html(options);
      var color = $(".views_list_header").css("background-color");
      $(industryIdSelector).effect("highlight", {color: color}, 400);

      $(industryIdSelector).trigger("change");
    });
  })
})

function setGoButtonState(state) {
    if( state ) {
        $('#cvGoButton').removeAttr('disabled');
        /*$('#cvGoButton').attr("value", goText);*/
    } else {
        $('#cvGoButton').attr('disabled','disabled');
        /*$('#cvGoButton').attr("value", "...");*/
    }
}

function setSelectedCompany() {
    selectedCompany= $("select.companySelect option:selected").val();
}

function setSelectedAnalysis() {
    selectedAnalysis= $("select.analystSelect option:selected").val();
}

$(document).ready(function () {
   setSelectedCompany();
   setSelectedAnalysis();
   hilight=false;
   $("select.companySelect").trigger("change");
});
