jQuery(function($) {

  $('.field')
    .focus(function(){
      if( $(this).val() == $(this).attr('title') ) {
        $(this).val('');
      }
    }).blur(function(){
      if( $(this).val() == '' ) {
        $(this).val( $(this).attr('title') );
      }
    });

  addClassToWidgets('.three-cols .col', 3);
  addClassToWidgets('#footer .cols .col', 3);

  $('#footer .cols .col p').each(function() {
    $(this).html($(this).html().replace('&nbsp;', ''));
    if ($.trim($(this).html())=="") {
      $(this).remove();
    }
  });

  function mycarousel_initCallback(carousel) {
    $('#slider .nav a').bind('click', function() {
      carousel.scroll($.jcarousel.intval($(this).attr('data-idx')));
      $('#slider .nav a').removeClass('active');
      $(this).addClass('active');
      return false;
    });
  }
  function mycarousel_itemFirstInCallback(carousel, item, index, state) {
    $('#slider .nav a').removeClass('active');
    idx = ((index - 1) % $('#slider .nav a').length) + 1;
    $('#slider .nav a[data-idx="' + idx + '"]').addClass('active');
  }

  $("#slider .carousel ul").jcarousel({
    scroll: 1,
    initCallback: mycarousel_initCallback,
    itemFirstInCallback: mycarousel_itemFirstInCallback,
    buttonNextHTML: null,
    buttonPrevHTML: null,
    auto: 6,
    wrap: 'circular'
  });

  function addClassToWidgets(selector, count) {
    $(selector).each(function(i) {
      if (i<count) {
        var n=i+1;
        $(this).addClass('col-' + n);
      }
    });
  }

  $("#footer .col-1").each(function() {
    var $ul = $("ul", this);
    var $col1 = $("<div class='list' />");
    var $col2 = $("<div class='list last' />");
    var rows = 0;

    $ul.each(function(){
      var thisRows = $("li", this).size();
      rows += thisRows;
      $(this).data("numrows", thisRows);
    });

    var half = Math.floor(rows/2);
    var currRows = 0;
    $ul.each(function(){
      if( currRows<half ) {
        $(this).appendTo($col1);
      } else {
        $(this).appendTo($col2);
      }
      currRows += $(this).data("numrows");
    });

    $col1.appendTo(this);
    $col2.appendTo(this);
  });

  var classes=new Array(5);
  classes[0]=new Array('differe', 'img-1');
  classes[1]=new Array('virtualiza', 'img-2');
  classes[2]=new Array('solution', 'img-3');
  classes[3]=new Array('industr', 'img-4');
  classes[4]=new Array('partner', 'img-5');

  $('#menu-main-menu > li').each(function() {
    $(this).wrap('<div class="col ' + $(this).attr('class') + '" />');
    $('> a', this).wrap('<span />');
    for (var i=classes.length-1; i>=0; i--) {
      if ($(this).text().toLowerCase().indexOf(classes[i][0])!=-1) {
        $('span', this).after('<img src="' + tempPath + '/images/' + classes[i][1] + '.png">');
        break;
      }
    };

    $('> *', this).unwrap();
  });
  $('#menu-main-menu ul').wrap('<div class="dd" />');
  $('.col:first').addClass('first');


  $('.container .col').hover(
    function(){
      $(this).addClass('hover');
      //$(this).find('.dd').stop(true , true).slideDown();
    },
    function(){
      $(this).removeClass('hover');
      //$(this).find('.dd').stop(true , true).slideUp();
    }
  );
  $('.container .col').click(function() {
    window.location = $(this).find('a').attr('href');
    return false;
  });

  $('.article:first').addClass('first');

  $('.wp-pagenavi > *:first').addClass('first');

});

