// Dropdown Menus
	
var mTo=0;
var hDir=1000;
var mWdh=220;

function hideMenu(){
	$("#undermenu").fadeOut( 100 );
}

var last_height = 0; /* Initiate height logging variable in global scope */
function sizeThumbs(){
    var thumb_height = $(".nivo-controlNav").height();
    var gal_height = $("#slider").height();
    var this_height = (thumb_height+gal_height); /* Calculate total height */
    $("#gallery").height( this_height );
	/* Compare to last height and call function again if necessary */
    if ( this_height != last_height ) setTimeout( function(){ sizeThumbs(); }, 750 );
    last_height = this_height;
}

$(document).ready( function(){

	$("#menu a").live("mouseenter", function(){
		var rel = $(this).attr("rel");
		if ( rel == "" || rel == undefined || rel == "undefined" ) return false;
		
		clearTimeout( mTo); mTo=0;
		
		var tOff = $(this).offset();
		var w = $(this).width();
		
		if (w < mWdh) w=mWdh;
		$("a.on").removeClass('on');
		$(this).addClass('on');
		$("#undermenu").css( { "left": (12+tOff.left) + "px", "top": (60+tOff.top) + "px" } );
		$("#undermenu ul").width( w ).html( $("#menu_" + rel).html() );
		$("#undermenu ul li:first").addClass("first");
		$("#undermenu ul li:last").addClass("last");
		$("#undermenu").show();
		setTimeout( function(){ $("#undermenu").show(); }, 50);
	}).live("mouseleave", function(){
		clearTimeout( mTo ); mTo=0;
		mTo = setTimeout( function(){ $("a.on").removeClass('on'); hideMenu(); }, hDir);
	}).live("click", function(){
		if ( mTo>0) { clearTimeout( mTo); mTo=0; }
	});
	$("#undermenu").live("mouseenter", function(){
		clearTimeout( mTo); mTo=0;
	}).live("mouseleave", function(){
		clearTimeout( mTo ); mTo=0;
		mTo = setTimeout( function(){ $("a.on").removeClass('on'); hideMenu(); }, hDir);
	});
	
	$(".input, .textarea").live("focus", function(){
		$(this).addClass("on");
	}).live("blur", function(){
		$(this).removeClass("on");
	});
	
	$("a.gmaps").live("click", function(){
		window.open("http://maps.google.com/maps?q=Brock%27's+Photography+11001+NW+107th+Circle,+Oklahoma+City+OK&daddr=Brocks+Photography,+11001+NW+107th+Circle,+Oklahoma+City,+OK+73099&hl=en&client=firefox-a&fb=1&gl=us&view=map&z=17");
		return false;
	});
	
	$("#your_name, #telephone, #email, #message").live("focus", function(){
		if ( $(this).val() == $(this).attr("rel") ){
			$(this).val('');
		}
	}).live("blur", function(){
		if ( $(this).val() == '' ) {
			$(this).val( $(this).attr("rel") );
		}
	}).each( function(){
		$(this).attr("rel", $(this).val() );
	});
	
	
	$("#contact_form").live("submit", function(){

		var p = $(this).find(".input:first");
		var valt = $(this).find(".textarea:last").val();
		var valn = p.val();
		if ( valt == "" || valt == "Message" || valn == "" || valn == "Your Name" ) {
			alert('Your information was not submitted. You must provide your name and a message.');
			return false;
		}
		
		var serdata = $(this).serialize();
		$.post( "/submit.php", serdata, function(data) {
			_gaq.push(['_trackPageview', '/submit-contact-form/' ]);
			$("p.note").remove();
			if ( data == "[OK]" ) {
				$('<p class="note">Thanks! We have received your information and will be in touch soon.</p>').insertBefore(".inputform");
			} else {
				$('<p class="note err">There was a problem. Please try again.</p>').insertBefore(".inputform");
			}
		});
		
		return false;
	});	

});
