$(document).ready(function() {
	
	
	handleLoading();
	
	generateNoise(0.03,".buttonSmall");
	
	
	/*	undbind the hover event for mobile devices	*/
	if(jQuery.browser.mobile == true){
		$("a, .buttonSmall, .link").unbind("hover");
	}	
});

function handleLoading(){
	handleImageFadeAfterLoad();
}

function handleImageFadeAfterLoad(){
	var imgs = getImgInitWidthHeight($(".fadeImg"));
	var fadeImgs = $(".fadeImg");
	
	fadeImgs.width(0).height(0).css({"opacity":"0"});
	
	$(".fadeImg",$(".fadeImg").parent("div")).imagesLoaded(function(){
		
		//$( ".fadeImg" ).animate({"width": "120", "height":"120", "opacity": 1}, 500);	
		$.each(imgs, function(key,value){
			setTimeout(function(){
				$(fadeImgs[key]).animate({"width": value[0], "height":value[1], "opacity": 1}, 600, "easeOutBounce"); //easeOutBounce,easeInOutBounce,easeOutElastic,easeInOutElastic
			}, 1000);
		
		});
		
	});
	
}

function getImgInitWidthHeight(fadeImg){
	var imgs = new Array();
	$.each($(fadeImg), function(key,value){
		imgs[key] = [$(value).width(),$(value).height()];
	});
	return imgs;
}







function generateNoise(opacity, elem){
	if(!!!document.createElement('canvas').getContext){
		return false;
	}
	
	if(!elem){
		elem = $("body");
	}
	
	var	canvas = document.createElement('canvas'),
		ctx = canvas.getContext('2d'),
		x, y,
		r, g, b,
		opacity = opacity || .2;
	
	canvas.width = 45;
	canvas.height = 45;
	
	ctx = canvas.getContext('2d');
	
	for(x=0; x<canvas.width; x++){
		for(y=0; y<canvas.height; y++){
			r = Math.floor(Math.random() * 255);
			g = Math.floor(Math.random() * 255);
			b = Math.floor(Math.random() * 255);
			
			ctx.fillStyle = 'rgba('+r+','+g+','+b+','+opacity+')';
			ctx.fillRect(x, y, 1, 1);
		}
	}
	
	$(elem).append("<div class='buttonSmallOverlay'></div>");
	
	$(".buttonSmallOverlay").css({"background-image":"url("+canvas.toDataURL("image/png")+")"});

	

	
	
}
