/**
 * blockfade jQuery plugin
 *
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * @author     Codefocus (http://www.codefocus.ca/)
 * @copyright  2011 Codefocus
 * @license    http://www.gnu.org/licenses/gpl-3.0.html
 * @version    0.2
 * @link       http://www.codefocus.ca/
 */
(function($){var k=50;var l=250;var m=10;var n=8;$.blockfade_slide=function(g,h,i){var j=this;j.options=g;j.blocks=[];j.block_parents=[];j.callback=null;if(null===g.href){j.container=$('<div class="blockfader-container" />').hide()}else{j.container=$('<a class="blockfader-container" href="'+g.href+'" />').hide()}h.append(j.container);j.image=new Image();j.image.src=j.options.image;j.image.onload=function(){var a=(this.width/j.options.blocks_x);var b=(this.height/j.options.blocks_y);var x;var y;var c;var d;for(y=0;y<j.options.blocks_y;++y){d=y*b;for(x=0;x<j.options.blocks_x;++x){c=x*a;var e=$('<div class="blockfader-block-parent" />').css('float','left').width(a).height(b);var f=$('<div class="blockfader-block" />').width(a).height(b).css('background-image','url('+j.image.src+')').css('background-position','-'+c+'px -'+d+'px').css('background-repeat','no-repeat').hide();e.append(f);j.container.append(e);j.blocks.push(f);j.block_parents.push(e)}}setTimeout(i,k)};j.shuffle_random=function(a){var b=[];var c=a.length;var d;var e;var f;for(d=0;d<c;++d){b.push(a[d])}for(d=0;d<c;++d){f=Math.floor(Math.random()*c);if(f===d){continue}e=b[f];b[f]=b[d];b[d]=e}return b};j.show=function(){j.container.css('width',j.image.width+2).show()};j.hide=function(){j.container.hide()};j.transition_in=function(b){j.callback=b;switch(j.options.transition_in.toLowerCase()){case'random':var c=j.shuffle_random(j.blocks);break}var d=setInterval(function(){var a=c.pop();if(typeof a=='undefined'){clearInterval(d);setTimeout(b,l);return}a.fadeTo(l,1)},m)};j.transition_out=function(b){j.callback=b;switch(j.options.transition_out.toLowerCase()){case'random':var c=j.shuffle_random(j.blocks);break}var d=setInterval(function(){var a=c.pop();if(typeof a=='undefined'){clearInterval(d);setTimeout(b,l);return}a.fadeTo(l,0)},m)};j.run=function(a){j.show();j.transition_in(function(){setTimeout(function(){j.transition_out(function(){j.hide();a()})},j.options.delay)})}};$.fn.blockfade=function(a){var b={image:null,href:null,transition_in:'random',transition_out:'random',blocks_x:4,blocks_y:4,delay:5000};var c=$(this);c.addClass('blockfader-loading');var d=[];var e=a.length;var f=0;var g=function(){d[f].run(function(){if(++f>=d.length){f=0}g()})};var h=0;for(h=0;h<a.length;++h){var i=$.extend(b,a[h]);d.push(new $.blockfade_slide(i,c,function(){if(--e===0){c.removeClass('blockfader-loading');setTimeout(g,k)}}))}}})(jQuery);
