var HOME_GRID_FADED_OUT = false;
var PORTFOLIO_GRID_FADED_OUT = false;

$(function(){
	initCufon();
	initHomeSquares();
});

var initCufon = function(){
	Cufon.replace('.cufon_replace');
	Cufon.replace('#nav ul li a',{hover: 'true', textShadow: '#000 0px -2px'});
	Cufon.replace('.event h4');
	Cufon.replace('#footer_text h4');
	Cufon.replace('.cufon-rockwell',{textShadow: '2px 2px #FFFFFF', hover: {
		textShadow: '2px 2px #000',
		color:'#FFF'
	}});
}

var initHomeSquares = function(){
	if($('#home_image_grid').length > 0){
		//show the images in sequence
		var sequenceImages = setInterval(function(){
			$(".home_image_grid_image:hidden:first").fadeIn();
			if($(".home_image_grid_image:hidden:first").length == 0){
				clearTimeout(sequenceImages);
			}
		},100);
		
		//clicks and hovers, bitches.
		$(".home_image_grid_image").click(function(){
			if(!HOME_GRID_FADED_OUT){
				HOME_GRID_FADED_OUT = true;
				$(".home_image_grid_image").fadeTo('slow',0.1);
		
				//populate popup
				$("#home_image_grid_detail h4").html("Featured Work #" + Math.round(Math.random() * 100000));		
				$("#home_image_grid_detail_txt h3").html($(this).data('title'));
				$("#home_image_grid_detail_content").html(unescape($(this).data('blurb')));
				$("#home_image_grid_detail_large img").attr('src',$(this).data('imglarge'));
				Cufon.refresh()
				$("#home_image_grid_detail").fadeIn();
			}			
			
		}).hover(
			function(){
				if(!HOME_GRID_FADED_OUT){
					$(this).stop().fadeTo('fast',0.4);			
				}
			},
			function(){
				if(!HOME_GRID_FADED_OUT){
					$(this).stop().fadeTo('fast',1);			
				}
			}
		);
		
		//
		$("#home_image_grid_detail_close").click(function(){
			$("#home_image_grid_detail").fadeOut();
			$(".home_image_grid_image").fadeTo('slow',1);
			HOME_GRID_FADED_OUT = false;
		});
		
		//start cycling the images out randomly with a new one
		var fadeInRandomImages = setInterval(function(){
			if(!HOME_GRID_FADED_OUT){
				//pick a random image to replace
				var elems = $('.home_image_grid_image');
				var rand = Math.floor(Math.random() * elems.length);
				var el = elems.eq(rand);
				//pipe seperated string of currently displayed images
				var imageStr = "";
				elems.each(function(idx, el){
					imageStr += $(el).find('img').attr('src') + "|";
				});
				//load the random image
				el.fadeTo('slow',.01,function(){
					el.load("/randomHomeImage.php", {images:escape(imageStr)}, function(){
						if(!HOME_GRID_FADED_OUT){
							el.fadeTo('slow',1);
						}else{
							el.fadeTo('slow',.2);
						}
						
					});
				})
			}
		},3000);
	}
}

