////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
// VARS

var delayKey = 0;

////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
// JQUERY ON LOAD

$(document).ready(function(){
	
	
	////////////
	// Navigation Rollovers
	
	$("#nav_about").hover(
		function () {
			$('#nav_about_on').not('.selected').fadeIn(100);
		}, 
		function () {
			$('#nav_about_on').not('.selected').fadeOut(100);
		}
	);
	
	$("#nav_featured").hover(
		function () {
			$('#nav_featured_on').not('.selected').fadeIn(100);
		}, 
		function () {
			$('#nav_featured_on').not('.selected').fadeOut(100);
		}
	);
	
	$("#nav_work").hover(
		function () {
			$('#nav_work_on').not('.selected').fadeIn(100);
		}, 
		function () {
			$('#nav_work_on').not('.selected').fadeOut(100);
		}
	);
	
	$("#nav_blog").hover(
		function () {
			$('#nav_news_on').not('.selected').fadeIn(100);
		}, 
		function () {
			$('#nav_news_on').not('.selected').fadeOut(100);
		}
	);
	
	
	
	$(".hit").hover(
		function () {
			$(this).siblings('.pad').fadeOut(200);
		}, 
		function () {
			$(this).siblings('.pad').fadeIn(200);
		}
	);

	$("#email").hover(
		function () {
			$(this).fadeTo(200, 0.7);
		}, 
		function () {
			$(this).fadeTo(200, 1.0);
		}
	);

	$('#byline').cycle({ 
		timeout: 10000,
		delay:   2000, 
		speed:   2000, 
		before: onBefore 
	}); 
	
	$('#office_images').cycle({ 
		timeout: 5000,
		delay:   0, 
		speed:   2000
	}); 
	
	
	
	function onBefore() { 
		//$('#title').html(this.alt); 
	}; 
	
	/*
	$("#project_fog").fadeTo(0, 0.9);
	$("#project_fog_cont").fadeOut(0);
	$("#project_detail").fadeOut(0);
	*/
	
	$("#search_bar").draggable();
	$("#search_bar").droppable ({
		drop: function() { alert('dropped'); }
	});
	
	// Set default text values for fields
	$("#search").DefaultValue("Search for stuff here...");
	
	/*
	// Add list on load
	$("#list").load("list.php", {init: true}, function(){
		// Show page on load
		$('#fog').fadeOut(300);
		initSlideShow();
		resize_script_width();
	});
	*/
	
	////////////////////////////////////////////////////////////////////
	
	// Click View All
	$("#view_all_featured").click(function(event){
		$("#all_featured").slideDown(500);
		$(this).fadeOut(500);
	});
	
	
	
	
	$(".view_all").click(function(event){
		$("#q").val("Live Search");
		getList(false, false, false, false);
	});
	
	// Start LiveSearch
	$("#search").keyup(function(event){
		// Delay counters missed keys when typing fast
		clearTimeout ( delayKey );
		delayKey = setTimeout ( function (q) {
				get_results(q, false);
			}, 500, $("#search").val());
	});
	
	// Document Resize
	var resizeTimer = null;
	$(window).bind('resize', function() {
		if (resizeTimer) clearTimeout(resizeTimer);
		resizeTimer = setTimeout(resize_script_width, 500);
	});

});

function get_results(qVar, cVar){
	$("#throbber").attr("src","media/site/heart_once.gif");
	$("#results").load("results.php", {q: qVar, c: cVar}, function(){
		//$("#throbber").attr("src","media/site/heart_static.gif");
	});
}

// Document Resize
function resize_script_width() {
	item_width 		= 327;
	avail_width 	= $(document).width() - 40;
	avail_spots 	= Math.floor( avail_width / (item_width + 10) );
	avail_margin 	= avail_width - (avail_spots * item_width);
	item_margin 	= Math.floor( avail_margin / ( avail_spots * 2 ) );
	
	//$("#q").val( avail_spots + ' = ' + avail_margin + ' = ' + item_margin );
	$('.project_grid').css('margin-left', item_margin + 'px');
	$('.project_grid').css('margin-right', item_margin + 'px');
	$('#nav_pad_adjust').css('margin-left', (item_margin+20) + 'px' );
	
};

////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
// AJAX Image

$ajax_image = false;

function ajax_image(v_name, v_id, v_width) {
	
	if($ajax_image == false){
		
		$ajax_image = true;
		
		$("#"+v_name+v_id).fadeTo(100, 0.2, function(){
		
			v_image = $("#"+v_name+v_id+"_on").val();
			
			$.ajax({
				type: "POST",
				url: "_inc/media.php",
				data: "id="+v_id+"&name="+v_name+"&image="+v_image+"&width="+v_width,
				success: function(msg){
					
					$("#"+v_name+v_id).html(msg);
					
					$("#"+v_name+v_id+"_image").load(function(){
						$("#"+v_name+v_id).fadeTo(100, 1.0);
					});
					
					$ajax_image = false;
				
				}
			});
			
		});
		
	} else {
		alert("Whoa, slow down...");	
	}	
}


////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
// FIELD DEFAULT VALUES

jQuery.fn.DefaultValue = function(text){
    return this.each(function(){
		//Make sure we're dealing with text-based form fields
		if(this.type != 'text' && this.type != 'password' && this.type != 'textarea')
			return;
		
		//Store field reference
		var fld_current=this;
		
		//Set value initially if none are specified
        if(this.value=='') {
			this.value=text;
		} else {
			//Other value exists - ignore
			return;
		}
		
		//Remove values on focus
		$(this).focus(function() {
			if(this.value==text || this.value=='')
				this.value='';
		});
		
		//Place values back on blur
		$(this).blur(function() {
			if(this.value==text || this.value=='')
				this.value=text;
		});
		
		//Capture parent form submission
		//Remove field values that are still default
		$(this).parents("form").each(function() {
			//Bind parent form submit
			$(this).submit(function() {
				if(fld_current.value==text) {
					fld_current.value='';
				}
			});
		});
    });
};

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