/* jQuery Custom Plugins */
(function($) {
	// Input box default text plugin
	$.fn.basInputDefaultText = function() {
		return this.each(function() {
			var $obj = $(this);
			$obj.focus(function() {
				if( $obj.val() == $obj[0].defaultValue ) {
					$obj.val('');
			}});
			$obj.blur(function() {
				if( ! $obj.val().length ) {
					$obj.val($obj[0].defaultValue);
				}
			});
		});
	};
}) (jQuery);

/* Google Maps Proxy Object */
var basGmaps = {
	map:					null,
	directionsDisplay:		null,
	directionsService:		null,
	siteAddress:			'143 North Milwaukee Street, Milwaukee, WI',
	latlng:					null,
	infoWindowContent:		'<h3>Babcock Auto Spring</h3><p>143 N. Milwaukee St.</p>',
	infoWindow:				null,
	markerTitle:			'Babcock Auto Spring',
	mapCanvasId:			'gmapCanvas',
	mapZoom:				11,
	mapDirectionsId:		'gmapDirections',
	mapDirectionsStartId:	'directionsStart',
	
	initializeLocationMap: function() {
		var locationMapOptions = {
			zoom: this.mapZoom,
			mapTypeControl: false,
			center: this.latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		
		var locationMarker = new google.maps.Marker({
			position: this.latlng,
			title:	this.markerTitle
		});
		
		this.map = new google.maps.Map(document.getElementById(this.mapCanvasId), locationMapOptions);
		locationMarker.setMap(this.map);
		
		this.infoWindow = new google.maps.InfoWindow({content: this.infoWindowContent});
		google.maps.event.addListener(locationMarker, 'click', function() {
			basGmaps.infoWindow.open(basGmaps.map, locationMarker);
		});

		this.directionsService = new google.maps.DirectionsService();
		this.directionsDisplay = new google.maps.DirectionsRenderer();

		this.directionsDisplay.setMap(this.map);
		this.directionsDisplay.setPanel(document.getElementById(this.mapDirectionsId));
	},
	
	setLatLng: function(addr) {
		geocoder = new google.maps.Geocoder();
		geocoder.geocode({ 'address': addr}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
					basGmaps.latlng = results[0].geometry.location;
					basGmaps.initializeLocationMap();
			};
		}); 
	},

	getRoute: function(mapProxyObject) {
		var start = document.getElementById(this.mapDirectionsStartId).value;
		var request = {
			origin: start,
			destination: this.siteAddress,
			travelMode: google.maps.TravelMode.DRIVING
		};
		
		this.directionsService.route(request, function(response, status) {
			if (status == google.maps.DirectionsStatus.OK) {
				mapProxyObject.directionsDisplay.setDirections(response);
			}
		});
	}
};


/* Onload Actions */
$(function() {
    var isDesktopBrowser = ($(window).width() > 480);

    // Prevent left navigation & static map from scrolling out of viewport
    if (isDesktopBrowser) {
        $('#contentTertiary .noScroll').scrollToFixed({marginTop: 20});
    }

	// Set plugin to display / hide default text on select input elements
	$('#directionsStart').basInputDefaultText();

	// Initialize Google Maps, if container present
	if (typeof(google) !== 'undefined' && typeof(google.maps) !== 'undefined') {
        // Large map with dynamic driving directions
        if ($(".gmapContainerWithDirections").length > 0) {
            basGmaps.setLatLng(basGmaps.siteAddress);
            $('form.gmapGetDirections').submit(function(e) {
                e.preventDefault();
                basGmaps.getRoute(basGmaps);
            });
        }
	}
	
	// Initilize Google map size toggle control for large gmap, if present
	$('#contentPrimary .gmapContainerWithDirections a.zoom').click(function(e) {
		e.preventDefault();
		$(this).closest('.gmapContainerWithDirections').toggleClass('gmapContainerLarge');
        var centerPos = basGmaps.map.getCenter();
        google.maps.event.trigger(basGmaps.map, 'resize');
        basGmaps.map.setCenter(centerPos);
	});

	// Start animation of product use images if more than one present
	if ($('#productUseImages img').length > 1) {
		$('#productUseImages').cycle(
			{
				delay: 3000,
				speed: 1500
			});
	}
});

