Wrap every 2nd image in a div if it's portrait
<p>I have a list of images and I want to wrap every 2 images in a div. However if the image is landscape I want to wrap it in it's own div.</p>

<p>Here is my code so far, it's working for the portraits but it's wrapping the landscapes too.</p>

<pre><code>$("#dps-inner").find('img').each(function (i) {
    jQuery(this).height(600);
    jQuery(this).width('auto');

    $(this).load(function () {
        var width = jQuery(this).width();
        //console.log(width);
        var height = jQuery(this).height();

        if (width &gt; 600) {
            jQuery(this).addClass("landscape");
        }
        else {
            jQuery(this).addClass("portrait");
        }

        accum_width += width + 1
        $("#dps-inner").width(w * i);
    });
});

var images = $("#dps-outer").find("#dps-inner &gt; img");

for (var i = 0; i &lt; images.length; i += 2) {
    images.slice(i, i + 2).wrapAll("&lt;div class='images'&gt;&lt;/div&gt;");
}
</code></pre>

<p>I have also tried </p>

<pre><code>var images = $("#dps-outer").find("#dps-inner &gt; img.portrait");

for (var i = 0; i &lt; images.length; i += 2) {
    images.slice(i, i + 2).wrapAll("&lt;div class='images'&gt;&lt;/div&gt;");
}
</code></pre>

<p>and even </p>

<pre><code>var images = $("#dps-outer").find("#dps-inner &gt; img");

for (var i = 0; i &lt; images.length; i += 2) {
    if (images.width() &lt; 600) {
        images.slice(i, i + 2).wrapAll("&lt;div class='images'&gt;&lt;/div&gt;");
    }
}
</code></pre>

<p>I'm not sure what I'm doing wrong, I have tried to unwrap the landscape image once everything has been completed but that doesn't work either, it's like there's no callback and everything is being done at the exact same time.</p>

<p>Has anyone any suggestions? </p>

<p>My fiddle isn't going to work as the screen needs to be wider but maybe you can open it in another window, it's just to get an idea of what I'm trying to do</p>

<p><a href="http://jsfiddle.net/tara_irvine/r46XA/" rel="nofollow">http://jsfiddle.net/tara_irvine/r46XA/</a></p>
