// base prototypes
if(typeof String.prototype.trim != 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
  };
}


$(function(){
   var mouseX, mouseY;
  
   $(document).mousemove(function(e){
     mouseX = e.pageX;
     mouseY = e.pageY;
   }); 
  
  $.inputTypeSwapper({
    usernameDefaultText: "Utente",
    passwordDefaultText: "Password"  
  });

  
  $.searchForm({
    inputSelector: "#searchFormString",
    buttonSelector: "#searchFormButton",
    resultSelector: "#searchFormResult",
    searchEngineUrl: "/services/search.aspx",
    searchQueryName: "search"
  });  
  
  $.searchForm({
    inputSelector: "#searchFormInlineString",
    buttonSelector: "#searchFormInlineButton",
    searchEngineUrl: "/services/search.aspx",
    searchUrl: "/ricerca.aspx",
    searchQueryName: "search"    
  });  
  
  $(".js_flmElem > a").removeAttr("title");
 
  var onMenuHover = function(){
    var showElem = function($el){  

      if($el.children("a").attr("class") != 'flm_title expanded'){
          
          $(".js_flmElem > a.expanded").removeClass("expanded");
          $el.children("a").addClass("expanded");
          var menuext = $("#" + $el.children("a").attr("rel"));
          var elToGrab = function(){
            if($('#slideshow').length > 0){
              return $('#slideshow')
                }else{
                return $('#banner')
             }
          }

          var top = elToGrab().offset().top-10;
          var left = $el.offset().left;
          var width = elToGrab().width();
          var height = elToGrab().height()-10;
          menuext.css({
            "position": "absolute",
            "top": top,
            "left": left
           /* "width": width,
            "height": height,*/
          });
           $(window).resize(function() {
            var menuext = $("#" + $el.children("a").attr("rel"));
          var top = elToGrab().offset().top-10;
          var left = $el.offset().left;
          var width = elToGrab().width();
          var height = elToGrab().height()-10;
          menuext.css({
            "position": "absolute",
            "top": top,
            "left": left
           /* "width": width,
            "height": height,*/
          });
          });          
          menuext.addClass("js_flmElemVisible");
          menuext.fadeIn(500, function(){
            $(".js_flmElem").bind('mouseenter', this);    
          });
        } 
    };
    var visible = $(".js_flmElemVisible");
    var current= $(this);
    
  //console.log($('.js_flmElem > a[rel!="' + current.children("a").attr('rel') + '"').length); //.each(function(){ $(this).parent(".flmElem").unbind('mouseenter'); });  
    if(visible.length > 0){
      if (current.children('.flmDescription ').attr('class') != "flmDescription js_flmElemVisible"){
        visible.removeClass("js_flmElemVisible");  
        current.children("a").removeClass("expanded");
        visible.hide();
      }
      showElem(current);
    }else{
      showElem(current);
    }     
  };

  var onMenuBlur = function(){
    var visible = $(".js_flmElemVisible");
    visible.removeClass('js_flmElemVisible').fadeOut(50);
     $(this).children("a").removeClass("expanded");
  };

  // fixes ie7 and older not triggering hover on block elements
  if($.browser.msie){
    var version = parseInt($.browser.version.substr(0,1));
    
    if(!isNaN(version) && version <= 7){
      $(".js_flmElem").hover(onMenuHover,onMenuBlur);  
    }else{
      $(".js_flmElem").hover(onMenuHover,onMenuBlur);  
    }
  }else{
    $(".js_flmElem").hover(onMenuHover,onMenuBlur);  
  }
  // left column height fix
  var maxColH = 0;
  if($("#leftColumn").length > 0 && $("#leftColumn").outerHeight() > maxColH) maxColH = $("#leftColumn").outerHeight();
  if($("#centralColumn").length > 0 && $("#centralColumn").outerHeight() > maxColH) maxColH = $("#centralColumn").outerHeight();  
  if($(".column3").length > 0 && $(".column3").outerHeight() > maxColH) maxColH = $(".column3").outerHeight();    
  $("#leftColumn").css({ "height": maxColH });
  
  
  // column wideness fix
  if($("#leftColumn").length == 0){
    $("#centralColumn").addClass("wide");
  }
  
  $.scroller({ selectorId: "js_scrlPics-" });
  
  $('a.lightbox').lightBox({
    fixedNavigation:true,
    imageLoading:   '/images/lightbox-ico-loading.gif',
    imageBtnPrev:   '/images/lightbox-btn-prev.gif',      
    imageBtnNext:   '/images/lightbox-btn-next.gif',      
    imageBtnClose:   '/images/lightbox-btn-close.gif',       
    imageBlank:     '/images/lightbox-blank.gif'          
  });
});

$(window).load(function(){
  $(".img_in_content").each(function(){
    var img = $(".img_div img", $(this));

    if(img.length > 0){

      var imgW = img.outerWidth();
      $(this).width(imgW + 12);
    }
  });
});

