$(function(){
  app.events();
});

var app = {
  events: function(){
    $('.focus').live('focusin', helpers.form.focus).live('focusout', helpers.form.focus);
    
    app.home.box.calculateHeight();
  }, //events
  
  home: {
    box: {
      calculateHeight: function(){
        window.setTimeout(function(){
          var higher = 0;
          var box = $('#sidebar, #main');
          box.each(function(i){
            var height = $(this).height();
            
            higher = ( height > higher ? height : higher );
          });
          
          box.each(function(i){
            $(this).css('min-height', higher);
          });
        }, 1500);  
      } //calculateHeight
    }  //box
  }, //home

};


var helpers = {
  form: {
    showMessage: function(form, errors){
      helpers.form.hideMessage(form);
      if(errors.length){
        var html = '<div class="message"><p>Ocorreram os seguintes erros no envio do seu desafio, corrija-os e tente novamente:</p><ul>';
        $(errors).each(function(){
          html += '<li>'+ this +'</li>';
        });
        html += '</ul></div>';
        form.prepend(html);
      }
    }, //showMessage
    hideMessage: function(form){
      form.find('.message').remove();
    }, //hideMessage
    focus: function(event){
      var type = event.type;
    	var def_value = event.target.defaultValue;
    	var value = event.target.value;
    	if(def_value == value && type == 'focusin'){
    		event.target.value = '';
    	}//end if
    	if(value == '' && type == 'focusout'){
    		event.target.value = def_value;
    	}//end if
    }, //focus
    focusStyle: function(){
      $(this).toggleClass('focused');
    }, //focusStyle
    doValidateLengthField: function(){
      helpers.form.validateLengthField(this);
    }, //doValidateLengthField
    validateLengthField: function(elem){
      var elem = $(elem);
      var val = elem.val();
      var max = parseInt(elem.attr('rel'));
      var resto = max - val.length;
      resto = resto > 0 ? resto : 0;
      elem.siblings('.num-caracteres-restantes:first').find('.num').html(resto);

      if(val.length >= max ){
        elem.val(val.substring(0, max));
        return false;
      }
      return true;
    }, //validateLengthField
    submit: function(){
      var elem = $(this);
    	elem.parents('form').submit();
    } //submit
  }, //form

  tooltip: {
    show: function(){
      if($('html').attr('class').indexOf('ie6') == -1){
        var tooltip = $(this).find('.tool-tip');
        if(tooltip.length){
          var parent = $('#general:first');
          tooltip.show();
          if( (tooltip.offset().left + tooltip.width()) > parent[0].offsetWidth){
            tooltip.css({
              left: 'auto',
              right: '0'
            });
            tooltip.find('.seta').css({
              left: 'auto',
              right: '7px'
            });
          }
        }
      }
    },
    hide: function(){
      if($('html').attr('class').indexOf('ie6') == -1){
        $(this).find('.tool-tip').hide();
      }
    }//hide
  } //tooltip
};


// HACK para seleção dos browsers.
  var css_browser_selector = function() {
  var ua=navigator.userAgent.toLowerCase(),
  is=function(t){return ua.indexOf(t) != -1;},
  h=document.getElementsByTagName('html')[0],
  b=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?'gecko ff2':is('firefox/3')?'gecko ff3':is('gecko/')?'gecko':is('opera/9')?'opera opera9':/opera (\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'',
  os=(is('x11')||is('linux'))?' linux':is('mac')?' mac':is('win')?' win':'';
  var c=b+os+' js'; h.className += h.className?' '+c:c;
  }();
