/** jQuery.misDestacados.js **/
jQuery.fn.miDestacado = function( opciones ) {
  
  var configuracion = {
    orientacion : 'h'
  }
  
  jQuery.extend(configuracion, opciones);
  
  this.each( function() {
    var i;
    var e = $(this);
    var sn, ss;

    if(configuracion.orientacion=='v') {
      
      /** vertical **/

      e.addClass('misDestacados');
      e.addClass('misDestacados-v');
      e.children('div').addClass('misDestacados-elemento');
      e.children('div').addClass('fondo-centro');
      e.find('.misDestacados-elemento:last').addClass('misDestacados-ultimo-elemento');
      e.find('.misDestacados-elemento').not(':last').after('<div class="wrapper-fondo-separador"><div class="fondo-separador-w" /><div class="fondo-separador-e" /><div class="fondo-separador-c" /></div>');
      
      e.find('.misDestacados-elemento').each( function() {
        $(this).wrap('<div class="wrapper-fondo-c" />').before('<div class="fondo-w" />').after('<div class="fondo-e" />');
      });

      sn = '<div class="wrapper-fondo-n"><div class="fondo-nw" /><div class="fondo-ne" /><div class="fondo-n" /></div>';
      ss = '<div class="wrapper-fondo-s"><div class="fondo-sw" /><div class="fondo-se" /><div class="fondo-s" /></div>';
      contenido = e.html();
      e.html(sn + contenido + ss);
      
      e.find('.wrapper-fondo-n').each( function() {
        $(this).find('.fondo-n').width(($(this).width()-$(this).find('.fondo-nw').width() - $(this).find('.fondo-ne').width()+1) + 'px').css('left', $(this).find('.fondo-nw').width()+'px');
        $(this).height( Math.max( $(this).find('.fondo-nw').height(), $(this).find('.fondo-ne').height(), $(this).find('.fondo-n').height())+'px');
      });
      e.find('.wrapper-fondo-s').each( function() {
        $(this).find('.fondo-s').width(($(this).width()-$(this).find('.fondo-sw').width() - $(this).find('.fondo-se').width()+1) + 'px').css('left', $(this).find('.fondo-sw').width()+'px');
        $(this).height( Math.max( $(this).find('.fondo-sw').height(), $(this).find('.fondo-se').height(), $(this).find('.fondo-s').height())+'px');
      });
      e.find('.wrapper-fondo-c').each( function() {
        $(this).find('.fondo-centro').css({
          'margin-left' : $(this).find('.fondo-w').width()+'px',
          'margin-right' : $(this).find('.fondo-e').width()+'px'
        });
      });
      e.find('.wrapper-fondo-separador').each( function() {
        $(this).find('.fondo-separador-c').width(($(this).width()-$(this).find('.fondo-separador-w').width() - $(this).find('.fondo-separador-e').width()+1) + 'px').css('left', $(this).find('.fondo-separador-w').width()+'px');
        $(this).height( Math.max( $(this).find('.fondo-separador-w').height(), $(this).find('.fondo-separador-e').height(), $(this).find('.fondo-separador-c').height())+'px');
      });
      
      e.find('.misDestacados-tipo-1').each( function() {
        var ee = $(this);
        
        ee.children('h4').appendTo($(this));
        ee.children('p').appendTo($(this));
        ee.children('a').appendTo($(this));
        $(this).append('<br class="clear" />');
        
        if(ee.children('img').attr('complete') == true) {
          ee.children('h4').css('margin-left', ee.children('img').outerWidth(true)+'px');
          ee.children('p').css('margin-left', ee.children('img').outerWidth(true)+'px');
          ee.children('a').css('margin-left', ee.children('img').outerWidth(true)+'px');
        } else {
          ee.children('img').load( function() {
            ee.children('h4').css('margin-left', ee.children('img').outerWidth(true)+'px');
            ee.children('p').css('margin-left', ee.children('img').outerWidth(true)+'px');
            ee.children('a').css('margin-left', ee.children('img').outerWidth(true)+'px');
          });
        }
      });

    } else {
      
      /** horizontal **/
      
      e.addClass('misDestacados');
      e.addClass('misDestacados-h');
      e.children('div').addClass('misDestacados-elemento');
      e.find('.misDestacados-elemento').addClass('misDestacados-'+e.find('.misDestacados-elemento').size()+'-elementos');
      e.find('.misDestacados-elemento:first').addClass('misDestacados-primer-elemento');
      e.find('.misDestacados-elemento:last').addClass('misDestacados-ultimo-elemento');
      e.find('.misDestacados-elemento').not(':last').after('<div class="misDestacados-h-separador" />');

      e.find('.misDestacados-tipo-1').each( function() {
        var ee = $(this);
        
        ee.children('h4').appendTo($(this));
        ee.children('p').appendTo($(this));
        ee.children('a').appendTo($(this));
        
        if(ee.children('img').attr('complete') == true) {
          ee.children('h4').css('margin-left', ee.children('img').outerWidth(true)+'px');
          ee.children('p').css('margin-left', ee.children('img').outerWidth(true)+'px');
          ee.children('a').css('margin-left', ee.children('img').outerWidth(true)+'px');
        } else {
          ee.children('img').load( function() {
            ee.children('h4').css('margin-left', ee.children('img').outerWidth(true)+'px');
            ee.children('p').css('margin-left', ee.children('img').outerWidth(true)+'px');
            ee.children('a').css('margin-left', ee.children('img').outerWidth(true)+'px');
          });
        }
      });
      
      var maxH = 0;
      e.find('.misDestacados-elemento').each( function() {
				maxH = Math.max(maxH, $(this).height());
			});
			e.find('.misDestacados-elemento').height((maxH+10)+'px');
      
    }
    
  });
  
  return this;
}

jQuery.fn.miDestacado_2 = function() {
  this.each(function() {
    var e=$(this);
    var w;
    
    e.addClass('misDestacados-2');
    e.append('<br class="clear" />');
    e.wrapInner('<div class="content" />');
    e.append('<div class="fondo-nw" />');
    e.append('<div class="fondo-ne" />');
    e.append('<div class="fondo-sw" />');
    e.append('<div class="fondo-se" />');
    e.find('img').load( function() {
      w = e.find('.content').width() - e.find('.content').css('padding-left').replace('px','') - e.find('.content').css('padding-right').replace('px','') - e.find('img').outerWidth();
      e.find('h4').width(w);
      e.find('p').width(w);
      e.find('.content').css('min-height', e.find('img').height()+'px');
    });
  });
  
  return this;
}

