/* Testimonial Spinner */
var nT = 0;
var nL = 0;

function changeTestimonial(){
	nL = $(".testimonial").length;
	
	$(".testimonial").hide();
	$(".testimonial").eq( nT ).show();
	$(".youarehere").text(  (nT+1) + " of " + nL + " Customer Testimonials" );
}

function goTestimonial(x){
	if ( x > (nL-1) ) x = nL-1;
	if ( x < 0 ) x = 0;
	nT = x;
	changeTestimonial();
}

$(function(){
  changeTestimonial();
  
  $("#spin_controls .arrow.left").live("click", function(){
	goTestimonial( nT-1 );
	return false;
  });
  $("#spin_controls .arrow.right").live("click", function(){
	goTestimonial( nT+1 );
	return false;
  });
});
 
 

// Dropdown Menus
	
var mTo=0;
var hDir=1000;
var mWdh=180;

function hideMenu(){
	$("#undermenu").fadeOut( 100 );
}


$(document).ready( function(){

	
	$(".field, .textarea").live("focus", function(){
		$(this).addClass("on");
	}).live("blur", function(){
		$(this).removeClass("on");
	});
	
	$("#message, #name, #contact, #your_name, #email, #telephone, #findus").live("focus", function(){
		if ( $(this).val() == $(this).attr("rel") ){
			$(this).val('');
		}
		$(this).addClass("on");
	}).live("blur", function(){
		if ( $(this).val() == '' ) {
			$(this).val( $(this).attr("rel") );
		}
		$(this).removeClass("on");
	}).each( function(){
		$(this).attr("rel", $(this).val() );
	});
	
	$("a.gmaps").live("click", function(){
		window.open("http://maps.google.com/maps?q=Andresen+and+Butterworth+P.A.,+306+West+Superior+Street+Ste+1000,+Duluth,+Minnesota+55802&daddr=Andresen+and+Butterworth+P.A.,+306+West+Superior+Street+Ste+1000,+Duluth,+Minnesota+55802&hl=en&client=firefox-a&fb=1&gl=us&view=map&z=17");
		return false;
	});

	var doit = true;
	if ( $.browser.msie ) {
		doit = false;
		if ( $.browser.version > 8 ) {
			doit = true;
		}
	}
	
	if ( doit == true ) {
		$("#header a").live("mouseenter", function(){
			if ( $(this).attr("rel") == "" || $(this).attr('rel') == undefined) {
				$(this).fadeTo( 100, 0.7, function(){
					$(this).fadeTo( 200, 1.0 );
				});
			}
		});
	}
	$("#menu ul li a").each( function(){
		if ( $(this).attr("rel") == undefined) $(this).attr("rel", "menu");
	});
	

	$("#menu ul li a").live("mouseenter", function(){
		$(this).fadeTo( 250, 0 );
		
		var rel = $(this).attr("rel");
		if ( rel == "" || rel == undefined || rel == "undefined" || rel == "menu" ) 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": (0+tOff.left) + "px", "top": (40+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(){
		$(this).fadeTo( 300, 1.0 );
		clearTimeout( mTo ); mTo=0;
		mTo = setTimeout( function(){ $("a.on").fadeTo( 300, 1.0 ); $("a.on").removeClass('on'); hideMenu(); }, hDir);
	}).live("click", function(){
		if ( $(this).attr("rel") != "menu" ) { 
			$("#undermenu:visible").addClass("flash");
			setTimeout( function(){ $("#undermenu").removeClass("flash") }, 100 );
			return false; 
		}
		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);
	});	
	
});
