// JavaScript Document
jQuery(document).ready(function($){
	
	//
	//move labels off & put back the form input when user enters data
	//
	$( "input" ).focus( function() {
		$( this ).prev( 'label' ).hide();
		
	});
	$( "input" ).blur( function() {
		if ( $( this ).val() == "" ) {
			$( this ).prev( 'label' ).show();
			
		}
		
	});
	
	$( 'textarea' ).focus( function() {
		$( this ).prev( 'label' ).hide();
		
	});
	$( "textarea" ).blur( function() {
		if ( $( this ).val() == "" ) {
			$( this ).prev( 'label' ).show();

		}

	});

		$( 'select' ).focus( function() {
			$( this ).prev( 'label' ).hide();

		});
		$( "select" ).blur( function() {
			if ( $( this ).val() == "" ) {
				$( this ).prev( 'label' ).show();

			}

		});
	
	//
	// When form loads, check to see if values are present and remove labels
	//
	$( 'input' ).each( function() {
		if ( ( $( this ).val() ) !== "" ) {
			$( this ).prev( "label" ).hide();
		
		}
		
	});
	
	$( 'textarea' ).each( function() {
		if ( $( this ).val() !== "" ) {
			$( this ).prev( "label" ).hide();

		}
		
	});

	$( 'select' ).each( function() {
		if ( $( this ).val() !== "" ) {
			$( this ).prev( "label" ).hide();

		}

	});
	
	//
	// Remove labels when form is clicked
	//
	$( "#submit" ).click( function() {
		$( "input.required" ).prev( "label" ).hide();
		
	});
	
	//
	// Form validation
	//
    $( 'form' ).validate({
		invalidHandler: function( form, validator ) {
			var error = validator.invalid;
			$( error ).each( function() {
				$( this ).prev( "label" ).hide();
			
			});
		}
	});
	
	//
	// Click on dealer and fill out form
	//
	$( "#map_section li" ).click( function() {
		var dealer = $( this ).attr('class');
		var a = "a." + dealer;
		var link = $(a).attr('href');
		window.location.href = link;
		
	});
	
	//
	// Show map bubble when item moused over
	//
	$( "#map_section li" ).hover( function() {
		var loc = $( this ).attr( "class" );
		var location = "#" + loc + " .bubble";
		$( location ).css( "visibility", "visible" );
		var point = "#" + loc + " .pointer";
		$( point ).css( "background-position", "0px 43px" );
		
	}, function() {
		var loc = $( this ).attr( "class" );
		var location = "#" + loc + " .bubble";
		$( location ).css( "visibility", "hidden" );
		var point = "#" + loc + " .pointer";
		$( point ).css( "background-position", "0px 0px" );
		
	});
	
	//
	// Expand driving and accident tips section
	//
	$( ".expander" ).click( function() {
		var index = $( ".expander" ).index( this );
		if ( $( ".collapsible" ).eq( index ).is( ":visible" ) ) {
			$( ".collapsible" ).slideUp( "slow" );
			$( this ).css( "background-position", "0px 0px" );
			
		} else {
			$( ".collapsible" ).slideUp( "slow" );
			$( ".expander" ).css( "background-position", "0px 0px" );
			var index = $( ".expander" ).index( this );
			$( this ).css( "background-position", "0px -48px" );
			$( ".collapsible" ).eq( index ).slideDown( "slow" );
		
		}
		
	});
	
	//
	// Initialize Tabs for Repair page
	//
	$("#tabs").tabs();
	
});
