(function(d){d.fn.startPuzzle=function(g){for(var f=0;f<this.length;f++){e(this[f],g)}};var b={randomorder:false,width:250,height:250,margin:1,startatimage:false,fadetime:500,delaytime:100,movetime:100,shufflemoves:1000,showimageonbg:true,showtextonwin:"Nice!",loopimages:true,puzzlesettings:new Object()};function a(h,g){for(var f=0;f<g;f++){if(!h[f]){h[f]=new Object()}if(!h[f]["cols"]){h[f]["cols"]=4}if(!h[f]["rows"]){h[f]["rows"]=4}if(!h[f]["bgopacity"]){h[f]["bgopacity"]=0.3}}}function e(o,p){var v=d(o);var u=d.extend({},b,p);a(u.puzzlesettings,v.children().length);v.css({width:u.width,height:u.height});u.shufflemoves+=parseInt(Math.random()*2)-1;var w=new Array();v.children("img").each(function(i,z){w.push(z.src)});v.html("");var y=new Object();var h=false;var l=new Array();for(var r=0;r<w.length;r++){l.push(r)}if(u.randomorder){c(l)}var t=0;var f;if(u.startatimage==false){f=l[t]}else{f=u.startatimage%l.length;for(var r=0;r<l.length;r++){if(f==l[r]){t=r}}}q();function q(){var C=0;var B=u.puzzlesettings[f];for(var A=0;A<B.cols;A++){for(var z=0;z<B.rows&&!(A==B.cols-1&&z==B.rows-1);z++){C+=u.delaytime;fragment=d("<div class='fragment frag"+A+"_"+z+"'></div>");fragment.css({"background-image":"url("+w[f]+")",left:(u.margin+A*u.width/B.cols)+"px",top:(u.margin+z*u.height/B.rows)+"px","background-position":-1*(u.margin+A*u.width/B.cols)+"px "+-1*(u.margin+z*u.height/B.rows)+"px"}).width(u.width/B.cols-2*u.margin).height(u.height/B.rows-2*u.margin);fragment.hide();v.append(fragment);fragment.delay(C).fadeIn(u.fadetime)}}if(u.showimageonbg){fullPuzzle=d("<div class='bgpuzzle'></div>");fullPuzzle.css({"background-image":"url("+w[f]+")"}).width(u.width).height(u.height);fullPuzzle.hide();v.append(fullPuzzle);fullPuzzle.delay(C+u.delaytime).fadeTo(u.fadetime,B.bgopacity)}y=s();h=false;g(y)}function s(){var C=u.puzzlesettings[f];var F={x:C.cols-1,y:C.rows-1};var E;do{for(var B=0;B<u.shufflemoves;B++){E=new Array();if(F.x-1>=0){E.push({x:F.x-1,y:F.y})}if(F.x+1<C.cols){E.push({x:F.x+1,y:F.y})}if(F.y-1>=0){E.push({x:F.x,y:F.y-1})}if(F.y+1<C.rows){E.push({x:F.x,y:F.y+1})}var D=parseInt(Math.random()*E.length);var A=v.children(".frag"+E[D]["x"]+"_"+E[D]["y"]).last();A.attr("class","fragment frag"+F.x+"_"+F.y);F=E[D]}for(var B=0;B<C.cols;B++){for(var z=0;z<C.rows;z++){var A=v.children(".frag"+B+"_"+z).last();if(typeof A!=="undefined"){A.css({left:(u.margin+B*u.width/C.cols)+"px",top:(u.margin+z*u.height/C.rows)+"px"})}}}}while(m());return F}function g(){var A=y;var z=u.puzzlesettings[f];v.find(".fragment").unbind("click").css({cursor:"default"});var i=new Array();if(A.x-1>=0){i.push({x:A.x-1,y:A.y})}if(A.x+1<z.cols){i.push({x:A.x+1,y:A.y})}if(A.y-1>=0){i.push({x:A.x,y:A.y-1})}if(A.y+1<z.rows){i.push({x:A.x,y:A.y+1})}d.each(i,function(B,C){var D=v.children(".frag"+C.x+"_"+C.y).last();D.click(x).css({cursor:"pointer"})})}function x(){var z=u.puzzlesettings[f];if(!h){h=true;var i=this.className.split(" ").last().substr(4).split("_");this.className="fragment frag"+y.x+"_"+y.y;d(this).animate({left:(u.margin+y.x*u.width/z.cols)+"px",top:(u.margin+y.y*u.height/z.rows)+"px"},u.movetime,function(){h=false;g();if(m()){j()}});y={x:parseInt(i[0]),y:parseInt(i[1])}}}function j(){h=true;v.find(".fragment").unbind("click").css({cursor:"default"});fullPuzzle=v.children(".bgpuzzle").last();if(typeof fullPuzzle=="undefined"){fullPuzzle=d("<div class='bgpuzzle'></div>");fullPuzzle.css({"background-image":"url("+w[f]+")"}).width(u.width).height(u.height);fullPuzzle.hide();v.append(fullPuzzle)}fullPuzzle.fadeTo(u.fadetime,1,function(){if(u.showtextonwin!=false){var i=d("<div class='hovertext'><span>"+u.showtextonwin+"</span><div class='blackbg'></div></div>");i.hide();v.append(i);i.css({left:(u.width/2-i.width()/2)+"px",top:(u.height/2-i.height()/2)+"px"});i.children(".blackbg").width(i.width()).height(i.height());i.fadeIn(u.fadetime,function(){i.delay(500).fadeOut(u.fadetime,function(){i.remove()})})}setTimeout(n,(u.showtextonwin!=false)?u.fadetime*2+500:500)})}function n(i){v.children(".fragment").remove();alert(i);alert(i=="RESTART");if(i=="RESTART"){t=0}else{t++}if(u.loopimages){t%=l.length}if(l.length>t){f=l[t];v.children(".bgpuzzle").last().fadeOut(u.fadetime,function(){v.children(".bgpuzzle").last().remove();q()})}}function k(){alert("Called");h=true;v.find(".fragment").unbind("click").css({cursor:"default"});if(v.children(".bgpuzzle")){v.children(".bgpuzzle").last().fadeOut(u.fadetime)}v.children(".fragment").fadeOut(u.fadetime);setTimeout(function(){n("RESTART")},u.fadetime)}function m(){var C=u.puzzlesettings[f];var D=v.children(".fragment");for(var B=0;B<C.cols;B++){for(var A=0;A<C.rows&&!(B==C.cols-1&&A==C.rows-1);A++){var z=D.get(B*C.rows+A).className.split(" ").last().substr(4).split("_");if(!(z[0]==B&&z[1]==A)){return false}}}return true}}function c(g){for(var h,f,k=g.length;k;h=parseInt(Math.random()*k),f=g[--k],g[k]=g[h],g[h]=f){}return g}Array.prototype.last=function(){return this[this.length-1]}})(jQuery);
