/**
 * jQuery VGrid v0.1.5 - variable grid layout plugin
 *
 * Terms of Use - jQuery VGrid
 * under the MIT (http://www.opensource.org/licenses/mit-license.php) License.
 *
 * Copyright 2009-2010 xlune.com All rights reserved.
 * (http://blog.xlune.com/2009/09/jqueryvgrid.html)
 */
(function(f){function j(a){var b=a.data("_vgchild"),c=[[0,a.width(),0]],e=0,d,g,h;b.each(function(){d=f(this);g=r(d);h=s(c,g[0]);c=t(c,h,g);e=Math.max(e,h[1]+g[1]);d.data("_vgleft",h[0]);d.data("_vgtop",h[1])});a.data("_vgwrapheight",e);u(a)}function s(a,b){a=a.concat().sort(o);for(var c=a[a.length-1][2],e=0,d=a.length;e<d;e++){if(a[e][2]>=c)break;if(a[e][1]-a[e][0]>=b)return[a[e][0],a[e][2]]}return[0,c]}function t(a,b,c){a=a.concat().sort(o);b=[b[0],b[0]+c[0],b[1]+c[1]];c=0;for(var e=a.length;c<
e;c++)if(b[0]<=a[c][0]&&a[c][1]<=b[1])delete a[c];else a[c]=v(a[c],b);return w(a,b)}function o(a,b){if(!a||!b)return 0;return a[2]==b[2]&&a[0]>b[0]||a[2]>b[2]?1:-1}function x(a,b){if(!a||!b)return 0;return a[0]>b[0]?1:-1}function w(a,b){a=a.concat([b]).sort(x);b=[];for(var c=0,e=a.length;c<e;c++)if(a[c])if(b.length>0&&b[b.length-1][1]==a[c][0]&&b[b.length-1][2]==a[c][2])b[b.length-1][1]=a[c][1];else b.push(a[c]);return b}function v(a,b){if(a[0]>=b[0]&&a[0]<b[1]||a[1]>=b[0]&&a[1]<b[1])if(a[0]>=b[0]&&
a[0]<b[1])a[0]=b[1];else a[1]=b[0];return a}function r(a){var b=a.width(),c=a.height();b+=Number(a.css("margin-left").replace("px",""))+Number(a.css("padding-left").replace("px",""))+Number(a.get(0).style.borderLeftWidth.replace("px",""))+Number(a.css("margin-right").replace("px",""))+Number(a.css("padding-right").replace("px",""))+Number(a.get(0).style.borderRightWidth.replace("px",""));c+=Number(a.css("margin-top").replace("px",""))+Number(a.css("padding-top").replace("px",""))+Number(a.get(0).style.borderTopWidth.replace("px",
""))+Number(a.css("margin-bottom").replace("px",""))+Number(a.css("padding-bottom").replace("px",""))+Number(a.get(0).style.borderBottomWidth.replace("px",""));return[b,c]}function u(a){var b=a.data("_vgchild").length*(a.data("_vgopt").delay||0)+a.data("_vgopt").time||500;a.stop();if(a.height()<a.data("_vgwrapheight"))f.browser.msie?a.height(a.data("_vgwrapheight")):a.animate({height:a.data("_vgwrapheight")+"px"},a.data("_vgopt").time||500,"easeOutQuart");else{clearTimeout(a.data("_vgwraptimeout"));
a.data("_vgwraptimeout",setTimeout(function(){f.browser.msie?a.height(a.data("_vgwrapheight")):a.animate({height:a.data("_vgwrapheight")+"px"},a.data("_vgopt").time||500,"easeOutQuart")},b))}}function y(a){var b;a.each(function(){b=f(this);b.css("left",~~b.data("_vgleft")+"px");b.css("top",~~b.data("_vgtop")+"px")})}function m(a,b,c,e){var d=f(a).parent(),g=false,h=a.length,k,l,n;for(k=0;k<h;k++){l=f(a[k]);n=l.position();if(n.left!=l.data("_vgleft")&&n.top!=l.data("_vgtop"))g=true}if(g){typeof d.data("_vgopt").onStart==
"function"&&d.data("_vgopt").onStart();a.each(function(p){var i=f(this),q={duration:c,easing:b};if(a.size()-1==p)q.complete=d.data("_vgopt").onFinish||null;clearTimeout(i.data("_vgtimeout"));i.data("_vgtimeout",setTimeout(function(){i.animate({left:i.data("_vgleft")+"px",top:i.data("_vgtop")+"px"},q)},p*e))})}}function z(a){clearTimeout(a.data("_vgtimeout"));j(a);a.data("_vgtimeout",setTimeout(function(){m(a.data("_vgchild"),a.data("_vgopt").easeing||"linear",a.data("_vgopt").time||500,a.data("_vgopt").delay||
0)},500))}function A(){var a=f("<span />").text(" ").attr("id","_vgridspan").hide().appendTo("body");a.data("size",a.css("font-size"));a.data("timer",setInterval(function(){if(a.css("font-size")!=a.data("size")){a.data("size",a.css("font-size"));f(window).resize()}},250))}f.fn.extend({vgrid:function(a){var b=f(this);b.data("_vgopt",a||{});b.data("_vgchild",b.find("> *"));b.data("_vgdefchild",b.data("_vgchild"));b.css({position:"relative",width:"auto"});b.data("_vgchild").css("position","absolute");
j(b);y(b.data("_vgchild"));if(b.data("_vgopt").fadeIn){var c=typeof b.data("_vgopt").fadeIn=="object"?b.data("_vgopt").fadeIn:{time:b.data("_vgopt").fadeIn};b.data("_vgchild").each(function(e){var d=f(this);d.css("display","none");setTimeout(function(){d.fadeIn(c.time||250)},e*(c.delay||0))})}f(window).resize(function(){z(b)});A();return b},vgrefresh:function(a,b,c,e){var d=f(this);if(d.data("_vgchild")){d.data("_vgchild",d.find("> *"));d.data("_vgchild").css("position","absolute");j(d);b=typeof b==
"number"?b:d.data("_vgopt").time||500;c=typeof c=="number"?c:d.data("_vgopt").delay||0;m(d.data("_vgchild"),a||d.data("_vgopt").easeing||"linear",b,c);typeof e=="function"&&setTimeout(e,d.data("_vgchild").length*c+b)}return d},vgsort:function(a,b,c,e){var d=f(this);if(d.data("_vgchild")){d.data("_vgchild",d.data("_vgchild").sort(a));d.data("_vgchild").each(function(){f(this).appendTo(d)});j(d);m(d.data("_vgchild"),b||d.data("_vgopt").easeing||"linear",typeof c=="number"?c:d.data("_vgopt").time||500,
typeof e=="number"?e:d.data("_vgopt").delay||0)}return d}})})(jQuery);
