(function( $ ){


//jQuery({percent:100}).animate({percent:0}, {step:function(){console.log(this.percent);}});


//______________________________________________________________| INIT |__________________________________________________________________//

    $.fn.typoGenerator = function( options ) {
        var cfg = $.extend({}, $.fn.typoGenerator.cfg, options);

        return this.each(function() {
			
			var $this = $(this);
			$this.css("cursor", "pointer");
//______________________________________________________________| SETUP |__________________________________________________________________//

			pharseWord("pixel");
			pharseWord("monster");

			var page_animation = "easeInOutCirc";
			var page_animation_speed = 600;
			
			var nr_blocks = $('.char_block').length;

			function pharseWord($str){
				for (var i=0;i<$str.length;i++){
					if(typeof WPW.alphabet[$str.charAt(i)] != undefined){
						//alert(WPW.alphabet[$str.charAt(i)].character);
						var character_obj = WPW.alphabet[$str.charAt(i)];
						var character_html = $(character_obj.character);
						character_html.appendTo($this);
						
						var arr = character_obj.blocks;
						for (var j = 0; j < arr.length; j++) {
							$(arr[j]).appendTo(character_html);
						}
						
					}
				}
				
				$("<div style='clear:both'></div").appendTo($this);
			}

			$this.bind("click", function(){
				randomizeTypo(true);
			});
			
			function randomizeTypo(with_animation){
					var r_delay = 3000;
					if(with_animation)r_delay = 500;
					$(".char_block").each(function(index){
							var r_x = Math.ceil(Math.random()*200);
							var r_y = Math.ceil(Math.random()*200);
							if(Math.random()<0.5)r_x *=-1;
							if(Math.random()<0.5)r_y *=-1;
							if (!with_animation){
								$(this).css("margin-left", r_x + "px");
								$(this).css("margin-top", r_y + "px");
								//$(this).css("opacity", 0);
							} else {
								$(this).delay(index * 100).animate({
									marginLeft: r_x,
									marginTop: r_y
									//opacity:0
									
								}, {
									duration: page_animation_speed,
									easing: page_animation
								});
							}
							
						});
						
						
						
					if (with_animation == true) {
						setTimeout(resetTypo, 2000);
					} else resetTypo(r_delay);
				}
			

		
			function resetTypo(r_delay){
				if(!r_delay)r_delay = 3000;
				$('.char_block').each(function(index){
					page_animation_speed = Math.random()*1000+300;
					$(this).delay(index*30 + r_delay).animate({
						marginLeft: 0,
						marginTop: 0
						//opacity:1
					}, {
						duration: page_animation_speed, 
						easing:page_animation,
						complete:function(){if (index == nr_blocks - 1) {
							setTimeout(function(){
								//randomizeTypo(true);
							}, 1000);
						}}
					});
				});
			}
			randomizeTypo(false);
			
//______________________________________________________________| END |__________________________________________________________________//

          });
    };

//______________________________________________________________| USER VARIABLES - DEFAULTS |__________________________________________________________________//

    $.fn.typoGenerator.cfg = {
        width: 260,
        background: 'yellow'
    };
    
//______________________________________________________________| ___THE___END___ |__________________________________________________________________//

})(jQuery);



