/* timmyspreader - http://www.timmytag.it */
$(function() {
	$(".my_captcha").live('renew',function(e) {


			if ($(this).attr('data-original') == undefined) {
						
				$(this).attr('data-original',$(this).attr('src'));
	
			}
		
			$(this).attr('src',$(this).attr('data-original') + "?rnd=" + e.timeStamp);
			
		
	});
	$(".new_code").live('click',function() { $(".my_captcha").trigger('renew'); });
});

function random_display(percent) {

	var random_perc = Math.ceil(Math.random()*100);
	
	if (random_perc <= percent) return true;
	return false;

}


function random_unique_text(text,text_before) {

	var ret = text[Math.floor(Math.random()*text.length)];

	while (ret == text_before) ret = text[Math.floor(Math.random()*text.length)];
	
	return ret;
	
}

if (typeof console == "undefined" || typeof console.log == "undefined") var console = { log: function() {} }; 

(function(){
    
    var special = jQuery.event.special,
        uid1 = 'D' + (+new Date()),
        uid2 = 'D' + (+new Date() + 1);
        
    special.scrollstart = {
        setup: function() {
            
            var timer,
                handler =  function(evt) {
                    
                    var _self = this,
                        _args = arguments;
                    
                    if (timer) {
                        clearTimeout(timer);
                    } else {
                        evt.type = 'scrollstart';
                        jQuery.event.handle.apply(_self, _args);
                    }
                    
                    timer = setTimeout( function(){
                        timer = null;
                    }, special.scrollstop.latency);
                    
                };
            
            jQuery(this).bind('scroll', handler).data(uid1, handler);
            
        },
        teardown: function(){
            jQuery(this).unbind( 'scroll', jQuery(this).data(uid1) );
        }
    };
    
    special.scrollstop = {
        latency: 150,
        setup: function() {
            
            var timer,
                    handler = function(evt) {
                    
                    var _self = this,
                        _args = arguments;
                    
                    if (timer) {
                        clearTimeout(timer);
                    }
                    
                    timer = setTimeout( function(){
                        
                        timer = null;
                        evt.type = 'scrollstop';
                        jQuery.event.handle.apply(_self, _args);
                        
                    }, special.scrollstop.latency);
                    
                };
            
            jQuery(this).bind('scroll', handler).data(uid2, handler);
            
        },
        teardown: function() {
            jQuery(this).unbind( 'scroll', jQuery(this).data(uid2) );
        }
    };
    
})();


function isValueInArray(arr, val) {
	inArray = false;
	for (z = 0; z < arr.length; z++)
	{		
		if (val == arr[z])
			inArray = true;

	}
	return inArray;
}

var spreader_debug = 0;

var y_exclusion = new Array(1,3,4,5);
var margins = {
	
		top: 20,
		left: 10
	
};

var box_info = {
	
		width: 140,
		height: 150,
		marginLeft: 20,
		marginTop: 20
		
};

var boxes = {
	
	 count: 0,
	 x_count: 0,
	 y_count: 0
	
};

var boxarray = new Array;

function print_boxes() {
	
	$(".virtualbox").remove();
	

	var marginTop = margins.top;
	
	for (var i = 0; i < boxes.y_count; i++) {
			
			var marginLeft = margins.left;
			
			for (var j = 0; j < boxes.x_count; j++) {
				
			
				
				$("<div></div>").addClass('virtualbox')
								.attr('bx',j)
								.attr('by',i)
								.css({
									
									position: 'fixed',
									top: marginTop,
									left: marginLeft,
									width: box_info.width,
									height: box_info.height,
									border: (isValueInArray(y_exclusion,j) == false)? '1px solid black' : '1px solid red'
									
								 })
								.appendTo('body');
								
				marginLeft += box_info.marginLeft + box_info.width;
				
			}
			
			
			marginTop += box_info.marginTop + box_info.height;

			
	}
	
}

function make_boxes_array() {
	

	$(".overlayquote").remove();

	var marginTop = margins.top;
	
	
	for (var i = 0; i < boxes.y_count; i++) {
			
			var marginLeft = margins.left;
			
			for (var j = 0; j < boxes.x_count; j++) {
				
				if (boxarray[j] == undefined) boxarray[j] = new Array;
	

				if (boxarray[j][i] == undefined) {
				
					
				
					var boxdata = {
						
						testo: text[Math.floor(Math.random()*text.length)],
						xpos: Math.floor(Math.random()*(Math.floor(box_info.width/2))),
						ypos: Math.floor(Math.random()*(Math.floor(box_info.height*1.5))),
						display: random_display(55)
						
					};
									
					boxarray[j][i] = boxdata;
					

				}
				
					if (isValueInArray(y_exclusion,j) == false && boxarray[j][i].display == true) {
							$("<div></div>").addClass('overlayquote')
									.attr('bx',j)
									.attr('by',i)
									.css({
										
										position: 'fixed',
										top: marginTop+boxarray[j][i].xpos,
										left: marginLeft,
										width: box_info.width
										//height: box_info.height,

									 })
									 .text(boxarray[j][i].testo)
									.appendTo('body');
									
					}
					
								
				marginLeft += box_info.marginLeft + box_info.width;
				
			}
			
			
			marginTop += box_info.marginTop + box_info.height;

			
	}
	
}

function refresh_boxes() {
	
	var width = $(window).width();
	var height = $(window).height();
	
	var x_count = Math.floor(($(window).width() - margins.left) / ( box_info.width + box_info.marginLeft));
	var y_count = Math.floor(($(window).height() - margins.top) / ( box_info.height + box_info.marginTop));
	
	boxes.count = x_count * y_count;
	boxes.x_count = x_count;
	boxes.y_count = y_count;
	
	console.log("- Box Count:" + boxes.count);
	
	if (spreader_debug == 1) print_boxes();
	
	make_boxes_array();
	
}

var animations = new Array;

animations.push('easeOutSine');

animations.push('easeInSine');


var scroller_timer = null;


function scroll_boxes() {
	
	$(".overlayquote").each(function() {
		
		var verso = Math.floor(Math.random()*2);
		
		var qt = 100+Math.floor(Math.random()*(box_info.height-50));
		
		var index = $(this).attr('bx') * $(this).attr('by');

		

		
		var current_top = parseInt($(this).css('top').replace("px"));
		
		if (verso == 0) {

				$(this).animate({ top: current_top-qt },400+Math.floor(Math.random()*2000),animations[Math.floor(Math.random()*animations.length)]);

		} else {

				$(this).animate({ top: current_top+qt },400+Math.floor(Math.random()*2000),animations[Math.floor(Math.random()*animations.length)]);

		
		}
		
	});
	
	if (scroller_timer == null) {
		
		scroller_timer = setInterval("scroll_boxes()",2000);
		
	}
	
}


$(function() {
	
	refresh_boxes();
	

	$(window).bind('resize',refresh_boxes);
	$(window).bind('scrollstart',scroll_boxes);
	$(window).bind('scrollstop',function() { $(".overlayquote").stop(); clearInterval(scroller_timer); scroller_timer = null; });
	
	
});
/*
$(window).load(function() {
	
	$("body, html").animate({ scrollTop: Math.floor($(document).height() / 4) },1);
	
});
*/




	

