$(document).ready(function(){
   $("div.help").mouseover(function(){
      $(this).children("div.help-content").show();
   });
   $("div.help").mouseout(function(){
      $(this).children("div.help-content").hide();
   });
   
   $(".argentina_provincias_opciones input.master").click(function(){
      if($(this).is(":checked")) $(".argentina_provincias_opciones input:not(.master)").attr('checked','checked');
      else $(".argentina_provincias_opciones input:not(.master)").removeAttr('checked');
   });
   $(".argentina_provincias_opciones input:not(.master)").click(function(){
      if(!$(this).is(":checked")) $(".argentina_provincias_opciones input.master").removeAttr('checked');
   });

   $("div.attribute input[type=radio][value=]").click(function(){
      var name = $(this).attr('name').split('[')[0];
      if($(this).is(":checked")) $("div.attribute input[name^="+name+"][value!=]").removeAttr('checked').each(function(){ check(this); });;
   });
   $("div.attribute input[type=checkbox][value!=]").click(function(){
      var name = $(this).attr('name').split('[')[0];
      if($(this).is(":checked")) $("div.attribute input[name^="+name+"][value=]").removeAttr('checked');
   });

   $("div#topmenu ul.menu-level-0 > li").mouseenter(function(){
      $(this).children("ul").show();
   });
   $("div#topmenu ul.menu-level-0 > li").mouseleave(function(){
      $(this).children("ul").hide();
   });

   $("div.expandable").each(function(){
      $(this).hide();
      var that = this;
      $(this).children(":first").css('margin-top',0);
      $(this).children(":last").css('margin-bottom',0);
      $(this).prev().addClass("expand").click(function(){
         $(that).slideToggle();
      });
   });

//    DD_roundies.addRule('div#beneficios div.attribute-image', '9px', true);
    DD_roundies.addRule('div#beneficios div.listado', '11px', true);
    //DD_roundies.addRule('.button, .defaultbutton', '4px', true);

   $("div.pantalla").corner("28px");
   $("div#facets").corner("bottom 14px");
   $("div.content-view-form .help-content").corner("keep 6px");
   $("div.class-folder-emprendimientos div.title").corner("top 14px");
   $("div#home-filter div.title").corner("top 14px");

   $('.jquery-corner').next().css('margin-top',0);


   $("#beneficios .scroll").simplyScroll({horizontal: false, autoMode:'loop', frameRate: 20, speed: 1, startOnLoad: true});

});
function spellCheck( inputs, button ){
   var speller = new spellChecker( inputs, function(){ $(button).removeAttr('onclick').trigger('click'); } );
   speller.openChecker();
}

function find(arr, searchStr) {
    if( !(arr instanceof Array) ) arr = [arr];
    var returnArray = false;
    for (i=0; i<arr.length; i++) {
       if (arr[i]==searchStr) {
          if (!returnArray) { returnArray = [] }
             returnArray.push(i);
       }
    }
    return returnArray;
}


$(document).ready(function(){

   $("#ciudad").change(function(){
      if($(this).val() == 177){ $(".filter-otra_ciudad input").attr('disabled','disabled'); $(".attribute-otra_ciudad").show(); }
      else {
         $(".filter-otra_ciudad input").attr('disabled','disabled');
         $(".attribute-otra_ciudad").hide();
         $(".attribute-otra_ciudad input").val('');
      }
   }).trigger("change");

   $("#provincia").change(function(){
      if($(this).val() == 177){ $(".filter-otra_provincia input").removeAttr('disabled'); $(".attribute-otra_provincia").show(); }
      else {
         $(".filter-otra_provincia input").attr('disabled','disabled');
         $(".attribute-otra_provincia").hide();
         $(".attribute-otra_provincia input").val('');
      }
   }).trigger("change");

   var clones = {};
   clones.provincia = $("#provincia").clone();
   clones.ciudad = $("#ciudad").clone();

   $("#pais").change(function(){
      $("#provincia").empty().removeAttr('disabled');
      var pais = $(this).val();
      clones.provincia.children().each(function(){
         if(pais == 93 && $(this).val() == 177) return;
         if(pais && find(pais,$(this).attr("rel")) || $(this).attr("rel") == "*") $("#provincia").append($(this).clone());
      });
      if(!pais){
         $("#provincia").attr('disabled','disabled');
      }
      $("#provincia").trigger("change").trigger("blur");
   });
   $("#provincia").change(function(){
      $("#ciudad").empty().removeAttr('disabled');
      var provincia = $(this).val();
      clones.ciudad.children().each(function(){
         if(provincia && find(provincia, $(this).attr("rel")) || $(this).attr("rel") == "*") $("#ciudad").append($(this).clone());
      });
      if(!provincia) {
         $("#ciudad").attr('disabled','disabled');
      }
      $("#ciudad").trigger("change").trigger("blur");
   });
   $("#pais").trigger("change");


   /* MIEMBRO */
   $(".attribute-ocupacion select").change(function(){
      if($(this).val() == 90) $(".attribute-ocupacion_otro").show();
      else $(".attribute-ocupacion_otro").hide();
   }).trigger("change");

   $(".filter-ocupacion select").change(function(){
      if(find($(this).val(), 90)) $(".filter-ocupacion_otro input").removeAttr('disabled');
      else $(".filter-ocupacion_otro input").attr('disabled','disabled');
   }).trigger("change");

   $(".attribute-grupo select").change(function(){
      if($(this).val() == 401) $(".attribute-grupo_otro").show();
      else $(".attribute-grupo_otro").hide();
   }).trigger("change");

   $(".filter-grupo select").change(function(){
      if(find($(this).val(), 401)) $(".filter-grupo_otro input").removeAttr('disabled');
      else $(".filter-grupo_otro input").attr('disabled','disabled');
   }).trigger("change");

   /* EMPRENDIMIENTO Y PREFERENCIAS */
   $(".filter-necesidad select, .filter-ofrece select").change(function(){
      if(find($(this).val(), 139 )) $(".filter-alianza_rubros select").removeAttr('disabled');
      else $(".filter-alianza_rubros select").attr('disabled','disabled');
      if(find($(this).val(), 140 )) $(".filter-canales_venta select").removeAttr('disabled');
      else $(".filter-canales_venta select").attr('disabled','disabled');
      if(find($(this).val(), 141 )) $(".filter-contactos_negocio_rubros select").removeAttr('disabled');
      else $(".filter-contactos_negocio_rubros select").attr('disabled','disabled');
      if(find($(this).val(), 142 )) $(".filter-proveedor_rubros select").removeAttr('disabled');
      else $(".filter-proveedor_rubros select").attr('disabled','disabled');
      if(find($(this).val(), 143 )) $(".filter-recursos_humanos_area select").removeAttr('disabled');
      else $(".filter-recursos_humanos_area select").attr('disabled','disabled');
   }).trigger("change");
   $(".filter-rubro select").change(function(){
      if($(this).val() == 313) $(".filter-otro_rubro input").removeAttr('disabled');
      else $(".filter-otro_rubro input").attr('disabled','disabled');
   }).trigger('change');

   $(".attribute-necesidad input[type=checkbox]").click(function(){check(this)}).each(function(){check(this)});
   $(".attribute-ofrece input[type=checkbox]").click(function(){check(this)}).each(function(){check(this)});
   $(".attribute-rubros input[type=checkbox]").click(function(){check(this)}).each(function(){check(this)});

   $("div.filter input").click(function(){
      $('input[name='+$(this).attr('name')+']').each(function(){
         if($(this).is(':checked')) $(this).parent().addClass('selected');
         else $(this).parent().removeClass('selected');
      });
   });


   /* SCROLLBARS */
   $(".scrollable").each(function(){
      var real_height = $(this).height();
      if(real_height > 144){
         var cn = $(this).get(0).className;
         $(this).css('height','144px').get(0).className = 'scrollcontent';
         $(this).wrap("<div class='"+cn+"'></div>");
         $(this).after("<div class='scrollbar'></div>");
      }
   });
   $(".scrollbar").slider({
      axis: 'vertical',
      orientation: 'vertical',
      value: 100,
      change: handleSliderChange,
      slide: handleSliderSlide});

   $(".scrollcontent").mousewheel(function(e, value){
      var scrollbar = $(this).next();
      $(scrollbar).slider('value', $(scrollbar).slider('value') + value*8)
      return false;
   });

   /* SELECTS */
   $("div#home-filter div.filter").each(function(){
      $(this).wrapInner("<div class='slide'></div>");
      var slide = $(this).find(".slide");
      slide.css('height',slide.height()).hide();
   });
   $("div#home-filter div.select").click(function(e,i){
      var c = '.filter-' + $(this).attr('rel') + " .slide";;
      $(c).slideToggle(500);
   });
   $("div#home-filter input").removeAttr('checked');

});

function check(it){
   if($(it).is(":checked")){
     $(it).parent().next(".subinput,.subsubinput").show().find("input").removeAttr('disabled');
   } else {
     $(it).parent().next(".subinput,.subsubinput").hide().find("input").attr('disabled','disabled');
   }
}

   function handleSliderChange(e, ui){
      var elem = $(ui.handle).parent().prev();
      var maxScroll = elem.attr("scrollHeight") - elem.height();
      elem.attr({scrollTop: (100-ui.value) * (maxScroll / 100) });
   }

   function handleSliderSlide(e, ui){
      var elem = $(ui.handle).parent().prev();
      var maxScroll = elem.attr("scrollHeight") - elem.height();
      elem.attr({scrollTop: (100-ui.value) * (maxScroll / 100) });
   }

