//////////////////////////////////////////////////////////////////////////////
// Global Variables


var timeout_images = [];
var loaded_images = [];


//////////////////////////////////////////////////////////////////////////////


$(document).ready(function(){
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Expanding Grid
	
	
	$(window).resize(function(){
		//column_resize();
	});
	
	//column_resize();
	
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Flash Panels
	
	
	setTimeout("flash_panels()", 1000);
	
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Set Height to 100%
	
	$(window).resize(function() {
		set_height = ($(window).height() < $(document).height())? $(window).height() : $(document).height();
		$('.100_percent_height').height( set_height );
	});
	set_height = ($(window).height() < $(document).height())? $(window).height() : $(document).height();
	$('.100_percent_height').height( set_height );
	
	//////////////////////////////////////////////////////////////////////////////
	
	more_state = "right";
	$("#more").click(function () {
		
		if(more_state == "right"){
			$("#more").attr("src", $("#more").attr("src").replace(/_right/, '_down'));
			$("#tags").animate({
				height: 95
			}, 200, function() {
				more_state = "down";
			});
		} else {
			$("#more").attr("src", $("#more").attr("src").replace(/_down/, '_right'));
			$("#tags").animate({
				height: 28
			}, 200, function() {
				more_state = "right";
			});
		}

	});
	
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Panel Rollover
	
	
	$('.panel:not(.be_gone)').hover(function(){
		$(this).find('.panel_info').stop(true, false).fadeTo(100,1);
	},function(){
		//$(this).find('.panel_info').hide();
	});
	
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Search Field Actions
	
	
	if($("#search").val() != 'Look for a tag...'){
		$("#search").removeClass('blurred');
		$("#search").addClass('focused');
	}
	
	$("#search").focus(function(){
		if($(this).val() == 'Look for a tag...'){
			$(this).val('');
			$(this).removeClass('blurred');
			$(this).addClass('focused');
		}
	});
	
	$("#search").blur(function(){
		if($(this).val() == ''){
			$(this).val('Look for a tag...');
			$(this).removeClass('focused');
			$(this).addClass('blurred');
		}
	});
	
	$("#search").keydown(function(event){
		if(event.keyCode == 13){
			
			needle = $(this).val();
			document.location.href='http://contactbenhaynes.com/'+needle+'/';
			
			/*
			$.ajax({
				type: "GET",
				url: "http://contactbenhaynes.com/business/projects.php",
				data: "q="+needle,
				success: function(msg){
					$("#panels").html(msg);
					column_resize();
				}
			});
			*/
		}
	});
	
	
	//////////////////////////////////////////////////////////////////////////////
	
	
	$(".tag").click(function () {
			
		tag = $(this).html();
		document.location.href='http://contactbenhaynes.com/'+tag+'/';
		
		/*
		$.ajax({
			type: "GET",
			url: "http://contactbenhaynes.com/business/projects.php",
			data: "q="+tag,
			success: function(msg){
				$("#panels").html(msg);
				column_resize();
			}
		});
		*/
		
		return false;
		
	});
	
	
	//////////////////////////////////////////////////////////////////////////////
	// Check if image is loaded to avoid "blink" in
	
	
	$('.panel_image').each(function(index) {
		if( $(this)[0].complete) {
			//console.log('cache: '+$(this).attr('parent_id'));
			loaded_images[ $(this).attr('parent_id') ] = true;
			
		} else {
			//console.log('else: '+$(this).attr('parent_id'));
			$(this).load(function() {
				//console.log('load: '+$(this).attr('parent_id'));
				loaded_images[ $(this).attr('parent_id') ] = true;
			});
		}
	});
	
	
	//////////////////////////////////////////////////////////////////////////////
});










//////////////////////////////////////////////////////////////////////////////


function flash_panels(){
	$(".panel_info:not(.be_still)").each(function(){
		flash( $(this).attr('id') );
	});
}


//////////////////////////////////////////////////////////////////////////////


function flash(id){
	//clearTimeout(timeout_images[id]);
	timeout_images[id] = setTimeout(
		function(){ flash(id) }, 
		Math.floor(Math.random()*5000)+1000
	);
	percent_rand = Math.random();
	if(percent_rand > .60){
	
		// Check if loaded
		if(loaded_images[ $("#"+id).attr('parent_id') ]){
			$("#"+id).fadeTo(2000,0);
			/*
			if( obj.is(':visible') ){
				obj.fadeOut(2000);
			} else {
				obj.fadeIn(2000);
			}
			*/
		}
	}
}


//////////////////////////////////////////////////////////////////////////////


function column_resize(){
	var h = $(document).height();
	var w = $(document).width();
	
	if(w > 900){
	
		available_width = w-60;
		number_accross = Math.floor(available_width / 338);
		remainder = available_width % 338;
		margin = Math.floor(((remainder / number_accross) + 40)/2);
		
		debug = number_accross * (298 + (margin*2));
		
		//$("#debug").html("available:"+w+"/"+available_width+"px holds: "+number_accross+" / with "+remainder+" leftover. Margin: "+margin+" = "+debug);
		//alert(margin);
		
		//$(".panel").css('margin',(margin > 10) ? margin : 10);
		
		$(".panel").css('margin-left',margin);
		$(".panel").css('margin-right',margin);
		
	} else {
		$(".panel").css('margin-left',61);
		$(".panel").css('margin-right',61);
	}
}


//////////////////////////////////////////////////////////////////////////////

