/**
 * altura da div de conteudo
 */
var wrap_height = 0;

/**
 * number_format function from phpjs
 */
function number_format(number,decimals,dec_point,thousands_sep){
  var n=!isFinite(+number)?0:+number,prec=!isFinite(+decimals)?0:Math.abs(decimals),sep=(typeof thousands_sep==='undefined')?',':thousands_sep,dec=(typeof dec_point==='undefined')?'.':dec_point,s='',toFixedFix=function(n,prec){
    var k=Math.pow(10,prec);
    return''+Math.round(n*k)/k;
  };

  s=(prec?toFixedFix(n,prec):''+Math.round(n)).split('.');
  if(s[0].length>3){
    s[0]=s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g,sep);
  }
  if((s[1]||'').length<prec){
    s[1]=s[1]||'';
    s[1]+=new Array(prec-s[1].length+1).join('0');
  }
  return s.join(dec);
}

/*
 * Retorna a tag base
 */
$(function(){
  jQuery.baseURL=function(url){
    var sBase=$("base").length>0?$("base").attr("href"):"";
    return sBase+url;
  }
});

// troca de texto dos componentes
$.fn.extend({
  clearOnFocus: function(default_text){
    $(this).focus(function(){
      if($(this).val() == default_text){
        $(this).val("");
      }
    })
    $(this).blur(function(){
      if($(this).val() == ""){
        $(this).val(default_text);
      }
    })
  }
});

$(function(){
  
  // fancybox padrao
  if($.fancybox != undefined){
    if($("a.fancybox").length > 0){
      $("a.fancybox").fancybox();
    }
    // links de avise-me
    $("a.avise").fancybox({
      'width'		:	580,
      'height'		:	240
    });

    $("a.esqueci").fancybox({
      'width'		:	300,
      'height'		:	200,
      'type': 'iframe'
    });

    // links de avise-me
    $("a.fancyorder").fancybox({
      'width'		:	670,
      'height'		:	410,
      'type': 'iframe'
    });
  }

})

function setContentHeight(){
  // recupera a altura do menu lateral
  var $menu = $("#sidebar");
  var $sidebar = $("#sidebar-2");
  // valida a altura maior para base
  var base_height_value = $sidebar.height() > $menu.height() ? $sidebar.height() : $menu.height();
  var base_height = base_height_value+"px";
  
  // elementos produtos-list
  if($("div.produtos-list").length > 0 && $("div.produtos-list").height() < base_height_value){
    $("div.produtos-list").css("height", (base_height_value-(($("div#detalhes").length > 0) ? 30 : 10))+"px");
  }
  
  // elementos padrao-wrap
  wrap_height = $("div#padrao-wrap").height();
  if($("div#padrao-wrap").length > 0 && $("div#padrao-wrap").height() < base_height_value){
    $("div#padrao-wrap").css("height", base_height);
    wrap_height = base_height_value;
    return;
  }
  // elementos content-wrap
  if($("div.content-wrap").length > 0 && $("div.content-wrap").height() < base_height_value){
    $("div.content-wrap").css("height", (base_height_value-30)+"px");
    return;
  }
  // elementos confirmacao
  if($("div#confirmacao").length > 0 && $("div#confirmacao").height() < base_height_value){
    $("div#confirmacao").css("height", (base_height_value-80)+"px");
    return;
  }
  // elementos content-wrap
  if($("div#meuspedidos-wrap").length > 0 && $("div#meuspedidos-wrap").height() < base_height_value){
    $("div#meuspedidos-wrap").css("height", (base_height_value-30)+"px");
    return;
  }

  // elementos cesta
  if($("div#cesta").length > 0 && $("div#cesta").height() < base_height_value){
    $("div#cesta").css("height", (base_height_value-30)+"px");
    return;
  }
  
  // elementos content-wrap
  if($("div#detalhes").length > 0 && $("div#detalhes").height() < base_height_value){
    $("div#detalhes").css("height", (base_height_value-30)+"px");
  //return;
  }
  
}
window.onload = setContentHeight
