/**
 * jQuery VGrid v0.1.2 - variable grid layout plugin
 *
 * Terms of Use - jQuery VGrid
 * under the MIT (http://www.opensource.org/licenses/mit-license.php) License.
 *
 * Copyright 2009 xlune.com All rights reserved.
 * (http://blog.xlune.com/2009/09/jqueryvgrid.html)
 */
(function(H){function B(P){var O=P.data("_vgchild");var N=P.width();var R=[[0,N,0]];var Q=0;O.each(function(U){var T=H(this);var S=M(T);var V=F(R,S[0]);R=J(R,V,S);Q=Math.max(Q,V[1]+S[1]);T.data("_vgleft",V[0]);T.data("_vgtop",V[1])});P.data("_vgwrapheight",Q);G(P)}function F(R,Q){var S=R.concat([]).sort(I);var N=S[S.length-1][2];for(var P=0,O=S.length;P<O;P++){if(S[P][2]>=N){break}if(S[P][1]-S[P][0]>=Q){return[S[P][0],S[P][2]]}}return[0,N]}function J(R,N,Q){var S=R.concat([]).sort(I);var T=[N[0],N[0]+Q[0],N[1]+Q[1]];for(var P=0,O=S.length;P<O;P++){if(T[0]<=S[P][0]&&S[P][1]<=T[1]){delete S[P]}else{S[P]=C(S[P],T)}}return E(S,T)}function I(O,N){return((O[2]==N[2]&&O[0]>N[0])||O[2]>N[2])?1:-1}function D(O,N){return(O[0]>N[0])?1:-1}function E(R,N){var S=R.concat([N]).sort(D);var O=[];for(var Q=0,P=S.length;Q<P;Q++){if(!S[Q]){continue}if(O.length>0&&O[O.length-1][1]==S[Q][0]&&O[O.length-1][2]==S[Q][2]){O[O.length-1][1]=S[Q][1]}else{O.push(S[Q])}}return O}function C(O,N){if(O[0]>=N[0]&&O[0]<N[1]||O[1]>=N[0]&&O[1]<N[1]){if(O[0]>=N[0]&&O[0]<N[1]){O[0]=N[1]}else{O[1]=N[0]}}return O}function M(P){var N=P.width();var O=P.height();N+=Number(P.css("margin-left").replace("px",""))+Number(P.css("padding-left").replace("px",""))+Number(P.get(0).style.borderLeftWidth.replace("px",""))+Number(P.css("margin-right").replace("px",""))+Number(P.css("padding-right").replace("px",""))+Number(P.get(0).style.borderRightWidth.replace("px",""));O+=Number(P.css("margin-top").replace("px",""))+Number(P.css("padding-top").replace("px",""))+Number(P.get(0).style.borderTopWidth.replace("px",""))+Number(P.css("margin-bottom").replace("px",""))+Number(P.css("padding-bottom").replace("px",""))+Number(P.get(0).style.borderBottomWidth.replace("px",""));return[N,O]}function G(O){var N=O;var P=N.data("_vgchild").length*(N.data("_vgopt").delay||0)+N.data("_vgopt").time||500;N.stop();if(N.height()<N.data("_vgwrapheight")){if(H.browser.msie){N.height(N.data("_vgwrapheight"))}else{N.animate({height:N.data("_vgwrapheight")+"px"},(N.data("_vgopt").time||500),"easeOutQuart")}}else{clearTimeout(N.data("_vgwraptimeout"));N.data("_vgwraptimeout",setTimeout(function(){if(H.browser.msie){N.height(N.data("_vgwrapheight"))}else{N.animate({height:N.data("_vgwrapheight")+"px"},(N.data("_vgopt").time||500),"easeOutQuart")}},P))}}function L(N){N.each(function(P){var O=H(this);O.css("left",O.data("_vgleft")+"px");O.css("top",O.data("_vgtop")+"px")})}function K(O,Q,P,N){O.each(function(S){var R=H(this);clearTimeout(R.data("_vgtimeout"));R.data("_vgtimeout",setTimeout(function(){R.animate({left:R.data("_vgleft")+"px",top:R.data("_vgtop")+"px"},P,Q)},S*N))})}function A(O){var N=O;clearTimeout(N.data("_vgtimeout"));B(N);N.data("_vgtimeout",setTimeout(function(){K(N.data("_vgchild"),N.data("_vgopt").easeing||"linear",N.data("_vgopt").time||500,N.data("_vgopt").delay||0)},250))}H.fn.extend({vgrid:function(P){var N=H(this);var O=P||{};N.data("_vgopt",O);N.data("_vgchild",N.find("> *"));N.data("_vgdefchild",N.data("_vgchild"));N.css({"position":"relative","width":"auto"});N.data("_vgchild").css("position","absolute");B(N);L(N.data("_vgchild"));if(N.data("_vgopt").fadeIn){var Q=(typeof (N.data("_vgopt").fadeIn)=="object")?N.data("_vgopt").fadeIn:{time:N.data("_vgopt").fadeIn};N.data("_vgchild").each(function(S){var R=H(this);R.css("display","none");setTimeout(function(){R.fadeIn(Q.time||250)},S*(Q.delay||0))})}H(window).resize(function(R){A(N)});return N},vgrefresh:function(R,P,N,O){var Q=H(this);if(Q.data("_vgchild")){Q.data("_vgchild",Q.find("> *"));Q.data("_vgchild").css("position","absolute");B(Q);P=typeof (P)=="number"?P:Q.data("_vgopt").time||500;N=typeof (N)=="number"?N:Q.data("_vgopt").delay||0;K(Q.data("_vgchild"),R||Q.data("_vgopt").easeing||"linear",P,N);if(typeof (O)=="function"){setTimeout(O,Q.data("_vgchild").length*N+P)}}return Q},vgsort:function(O,R,P,N){var Q=H(this);if(Q.data("_vgchild")){Q.data("_vgchild",Q.data("_vgchild").sort(O));B(Q);K(Q.data("_vgchild"),R||Q.data("_vgopt").easeing||"linear",typeof (P)=="number"?P:Q.data("_vgopt").time||500,typeof (N)=="number"?N:Q.data("_vgopt").delay||0)}return Q}})})(jQuery);



