﻿/* IVBAM */
$(function () {
  /************* IE7 e-mail icon fix ***************/
  $('#iconprint').each( function() {
    if ((jQuery.browser.msie) && (jQuery.browser.version == 7.0) ) {
      $(this).css("padding-top","2px");
    }
  });
  /*******************End**********************/
  
  correctMenus("#menu");
  $('#menu ul:first > li').bind('mouseover', jsddm_open);
  $('#menu ul:first > li').bind('mouseout',  jsddm_timer);
  
  $('.news-rcol-title-Galeria').click(function() {
    $(this).next().slideToggle("medium");
    if ($(this).find(".fechar").css("display") == "none") {
      $(this).find(".fechar").css("display", "block");
      $(this).find(".abrir").css("display", "none");
    }
    else {
      $(this).find(".fechar").css("display", "none");
      $(this).find(".abrir").css("display", "block");
    }
  });
  
  if ($("#ThirdLevel").size() !=0) {
    var  i = 0;
    $("#ThirdLevel").prepend("<div class='columnLeft'></div><div class='columnRight'></div>");
    $("#ThirdLevel").find("li").each( function() {
      if (i == 0) {
        $(this).appendTo(".columnLeft");
        i=1;
      }
      else {
        $(this).appendTo(".columnRight");
        i=0;
      }
    });
  }
});
  
var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;
 
function jsddm_open() {
  jsddm_canceltimer();
  jsddm_close();
  ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');
}




function jsddm_close() {
  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');
}




function jsddm_timer() {
  closetimer = window.setTimeout(jsddm_close, timeout);
}




function jsddm_canceltimer() {
  if(closetimer) {
    window.clearTimeout(closetimer);
    closetimer = null;
  }
}
 
function CreateGallery(path, SmallHeight, BigWidth, BigHeight) {
  path = path.substring(0,path.lastIndexOf("/"));
  $.ajax({
      url: location.protocol + "//" + location.hostname + "/Files/GetFolderContents.aspx?Path=" + path,
      success: function(data){
        var xml;
        if ($.browser.msie && typeof data == "string") {
          xml = new ActiveXObject("Microsoft.XMLDOM");
          xml.async = false;
          xml.loadXML(data);                
        } else {
           xml = data;
        }
        $(".ParagraphModule").append('<div id="ListGallery"></div>');
        
        $(xml).find("File").each(function() {
          PathImg=($(this).attr('href'));
          
          $("#ListGallery").append('<div class="ItemGallery"><a href="/admin/public/getimage.aspx?&Image='+PathImg+'&Height='+BigHeight+'&Width='+BigWidth+'&Crop=0"><img src="/admin/public/getimage.aspx?&Image='+PathImg+'&Height='+SmallHeight+'&Crop=0" alt="" border=""/><\/a><\/div>');
          $('#ListGallery a').lightBox({fixedNavigation:false});
        });
        $("#ListGallery").append("<div style='clear: both;'></div>")
      }
  });
}
 
/* clears the general search box text */
function clearField( input, defaultValue ) {
    if( ( $('#'+input).val() == "" ) || ( $('#'+input).val().toLowerCase() == defaultValue.toLowerCase() ) )
        $('#'+input).val("");
}




/* resets the general search box text to its default value */
function checkIfEmpty( input, defaultValue ) {
    if( ( $('#'+input).val() == "" ) || $('#'+input).val().toLowerCase() == defaultValue.toLowerCase() )
        $('#'+input).val( defaultValue );
}




/* checks if the general search box value isn't empty or if its not equal to the default value */
function ValidateSearch( errorMessage, defaultValue ) {
    if( ( $('#searchText').val() == "" ) || ( $('#searchText').val().toLowerCase() == defaultValue.toLowerCase() ) ) {
      alert( errorMessage );
      return false;
    }
}




/*checks ih the newsletter subscribe form isn't empty or if username  ou e-mail field is default, uses DW default validator*/
function ValidateNewsletter(nomeDefaultValue, nomeErrorMessage, emailDefaultValue, emailErrorMessage ) {
  if (( $('#nomeInput').val() == "" ) || ( $('#nomeInput').val().toLowerCase() == nomeDefaultValue.toLowerCase() )) {
    alert( nomeErrorMessage );
    return false;
  }
  if (( $('#emailInput').val() == "" ) || ($('#emailInput').val().toLowerCase() == emailDefaultValue.toLowerCase() )) {
    alert( emailErrorMessage );
    return false;
  }
  Send();
}
 
function correctMenus(selector) {
  
  var menuWidth = $(selector).width(),
      totalLiWidth = 0,
      $li = $(selector + " > ul > li");
   
  $li.each(function() {
    totalLiWidth += $(this).outerWidth(true);
  });
  
  // Work out how much padding we need and account for rounding errors, the error is going to be forced into the first tab.
  var requiredPadding = Math.floor(((menuWidth-totalLiWidth)/$li.length)/2),
      leftRoundingErrorFix = Math.round((menuWidth-((requiredPadding*$li.length*2)+totalLiWidth))/2),
      rightRoundingErrorFix = Math.floor((menuWidth-((requiredPadding*$li.length*2)+totalLiWidth))/2);
  
  // Apply padding to list items
  $li.each(function(i) {
    if(i == 1)
      $(this).find("a").css('padding-left',requiredPadding+leftRoundingErrorFix+'px').css('padding-right',requiredPadding+rightRoundingErrorFix+'px');
    else
      $(this).find("a").css('padding-left',requiredPadding+'px').css('padding-right',requiredPadding+'px');
  });
  
  $(".M1").each(function() {
    $(this).find("li, a").css({
      "padding" : "10px 0",
      "width": $(this).parent().find("a:first").outerWidth(true)+"px"
    })
  });
  
  //Safari and Chrome correction
  if($.browser.safari || $.browser.chrome) {
    $(selector + " ul").css("display", "block");
    $(selector + " li").css("display", "list-item");
  }
}
