jQuery.noConflict();

var plauditSite = (function($){
	
	var defaultMethod = function(){
		$("body").addClass("jsEnabled");
		// Make legends into H2s for easier styling, while maintaining semantic markup.
		$("legend").each(function(){
			var $this = $(this); 
			var legendText = $this.text();
			$this.parent("fieldset")
				.before("<h3>" + legendText + "</h3>")
				.end()
				.remove();
		});
		
		// Adding hover in IE for non-anchor elements
		$(".hoverEnabled").hover(
			function() {
				$(this).addClass("hover");
			},
			function(){
				$(this).removeClass("hover");
			}
		);
		
		//Add zebra-striping to tables
		$("table tr:even").addClass("even");
		
		//Automatically selects focused field text and adds class for styling
		$('input[type="text"]').focus(function(){
			$(this).select().addClass("focused");
		});
		$('input[type="text"]').blur(function(){
			$(this).removeClass("focused");
		});
	}();
	
	var checkoutForm = function(){
		function shipToSameAsContactChange() {
			if (this.value == 'true') {
				$('#shipToAddressArea div').slideUp("slow");
			} else {
				$('#shipToAddressArea div').slideDown("slow");
			}
		}
		
		$(".shipToSameAsContact").each(function(){
			var $this = $(this);
			$this.change(shipToSameAsContactChange);
			shipToSameAsContactChange.call(this);
		});

		function shippingMethodChange() {
			if (this.value == 'pickup') {
				$('#shippingDetails').hide();
			} else {
				$('#shippingDetails').show();
			}
		}
		
		$(".shippingMethod").each(function(){
			var $this = $(this);
			$this.change(shippingMethodChange);
			shippingMethodChange.call(this);
		});
	}();
	
	var customElements = {
		
		init: function() {
			this.supplyTabs();
			this.googleMaps.init();
		},
		
		supplyTabs: function(){
			$("#movingSupplies").tabs();
		},
		
		googleMaps: {
			
			init: function(){
			
				// Default Vars
				var mapCanvas;
				var directionsDisplay;
				var markers;
				var currentLatLng;
				var currentMarker;
				var infoWindow;
				var baseHref = $("base").attr("href");
				
				
				// Marker Function
				function setupMarkers(markers,eventType,content) {
					for( i = 0; i < markers.length; i++ ) {
						currentLatLng = new google.maps.LatLng(markers[i][0], markers[i][1]);
						
						currentMarker = new google.maps.Marker({
							position: currentLatLng,
							map: mapCanvas,
							title: markers[i][3], // Title for Marker
							icon: 'assets/images/icons/iconr' + (i + 1) + '.png',
							target: markers[i][2] // Href for markers
						});
						
						if ( eventType === "link"){
							google.maps.event.addListener(currentMarker, 'click', function() {
								window.location.href = baseHref + this.getTarget();
							});
						} else if ( eventType === "info"){
							infoWindow = new google.maps.InfoWindow({
								content: content
							});
							google.maps.event.addListener(currentMarker, 'click', function() {
								infoWindow.open(mapCanvas,currentMarker);
							});
						}
					}
				}
				
				// Calculate Route Function
				function calcRoute(start, end){
				    
					var request = {
				        origin:start, 
				        destination:end,
				        travelMode: google.maps.DirectionsTravelMode.DRIVING
				    };
				    
					directionsService.route(request, function(response, status) {
				      if (status == google.maps.DirectionsStatus.OK) {
				        directionsDisplay.setDirections(response);
				        $(".print", $("#getDirections")).css("display","block");
				      }
				    });
				}
				
				// Setup for Homepage Map
				if ( $("#homeMap").length !== 0){
					mapCanvas = new google.maps.Map(document.getElementById("homeMap"),{
						zoom: 8,
						center: new google.maps.LatLng(44.876049,-93.4552),
						mapTypeId: google.maps.MapTypeId.ROADMAP,
						mapTypeControl: false
					});
					markers = this.homepageMap.locations;
					setupMarkers(markers,"link");
				}
				
				// Setup for Our Locations Map
				if ( $("#ourLocationsMap").length !== 0){
					mapCanvas = new google.maps.Map(document.getElementById("ourLocationsMap"), {
						zoom: 9,
						center: new google.maps.LatLng(44.876049,-93.4552),
						mapTypeId: google.maps.MapTypeId.ROADMAP,
						mapTypeControl: false
					});
					markers = this.ourLocationsMap.locations;
					setupMarkers(markers,"link");
					
					directionsDisplay = new google.maps.DirectionsRenderer();
					directionsDisplay.setMap(mapCanvas);
					directionsDisplay.setPanel(document.getElementById("directions"));
					
					var directionsService = new google.maps.DirectionsService();
					
					$("#getDirections").submit(function(){
						var selectedLocation = jQuery("#getDirections input:checked").attr("id");
						var userLocation = jQuery("#fromAddress").val();
						
						if (!userLocation) {
							alert("Please fill in the address field.");
							return false;
						}
						
						if (!selectedLocation) {
							alert("Please select a facility location.");
							return false;
						}
						
						if ( selectedLocation == 'bloomington' ) {
							selectedLocation = "44.80828,-93.362879";
						} 
						
						if ( selectedLocation == 'edenPrairie' ) {
							selectedLocation = '44.880917,-93.411755';
						} 
						
						if ( selectedLocation == 'mound' ) {
							selectedLocation = '5300 Shoreline Drive, Mound, MN';
						}
						
						calcRoute(userLocation,selectedLocation);
						
						return false;
					});
				}
				
				// Setup for Bloomington Map
				if ( $("#bloomingtonMap").length !== 0 ){
					mapCanvas = new google.maps.Map(document.getElementById("bloomingtonMap"),{
						zoom: 12,
						center: new google.maps.LatLng(44.808003,-93.362972),
						mapTypeId: google.maps.MapTypeId.ROADMAP,
						mapTypeControl: false
					});
					markers = this.bloomingtonLocation.locations;
					setupMarkers(markers,"info",this.bloomingtonLocation.infoWindowContents);
				}
				
				// Setup for Eden Prairie Map
				if ( $("#edenPrairieMap").length !== 0 ){
					mapCanvas = new google.maps.Map(document.getElementById("edenPrairieMap"), {
						zoom: 12,
						center: new google.maps.LatLng(44.880917,-93.411755),
						mapTypeId: google.maps.MapTypeId.ROADMAP,
						mapTypeControl: false
					});
					markers = this.edenPrairieLocation.locations;
					setupMarkers(markers,"info",this.edenPrairieLocation.infoWindowContents);
				}
				
				// Setup for Mound Map
				if ( $("#moundMap").length !== 0 ){
					mapCanvas = new google.maps.Map(document.getElementById("moundMap"), {
						zoom: 12,
						center: new google.maps.LatLng(44.936587,-93.657022),
						mapTypeId: google.maps.MapTypeId.ROADMAP,
						mapTypeControl: false
					});
					markers = this.moundLocation.locations;
					setupMarkers(markers,"info",this.moundLocation.infoWindowContents);
				}
				
				// Setup Printing 
				$(".print", $("#getDirections")).click(function(){
					window.print();
					return false;	
				});
				
			}, // init function
		
			homepageMap: {
				
				locations: [
				     [44.808003,-93.362972,"our-locations/bloomington-self-storage.html","Bloomington Location"],
				     [44.880917,-93.411755,"our-locations/eden-prairie-self-storage.html","Eden Prairie Location"],
				     [44.936587,-93.657022,"our-locations/mound.html","Mound Location"]       
				 ]
					
			},  // homepage map setup
			
			ourLocationsMap: {
				
				locations: [
				    [44.808003,-93.362972,"our-locations/bloomington-self-storage.html","Bloomington Location"],
				    [44.880917,-93.411755,"our-locations/eden-prairie-self-storage.html","Eden Prairie Location"],
				    [44.936587,-93.657022,"our-locations/mound.html","Mound Location"]       
				]
				
				
			}, // Our Location Map
			
			bloomingtonLocation: {
				locations: [
				   [44.808003,-93.362972,"our-locations/bloomington-self-storage.html","Bloomington Location"]         
				],
				
				infoWindowContents: '<div id="mapInfo">'+
				'<div id="mapInfoLeft">'+
				'<h3>Address:</h3>'+
				'<p>6200 W. Old Shakopee Rd Bloomington, MN 55438</p>'+
				'<p><a href="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=6200+W.+Old+Shakopee+Rd+Bloomington,+MN+55438&sll=37.0625,-95.677068&sspn=58.164117,135.263672&ie=UTF8&hq=&hnear=6800+W+Old+Shakopee+Rd,+Bloomington,+Hennepin,+Minnesota+55438&z=17&iwloc=A">Directions</a></p>'+
				'</div>'+
				'<div id="mapInfoRight">'+
				'<img src="assets/images/photos/map-image-bloomington.jpg" alt="Bloomington Location" />'+
				'</div>'
			}, // Bloomington
			
			edenPrairieLocation: {
				locations: [
				   [44.880917,-93.411755,"","Eden Prairie Location"]         
				],
				
				infoWindowContents: '<div id="mapInfo">'+
				'<div id="mapInfoLeft">'+
				'<h3>Address:</h3>'+
				'<p>6851 Flying Cloud Drive Eden Prairie MN 55344</p>'+
				'<p><a href="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=6851+Flying+Cloud+Drive++Eden+Prairie++Minnesota++55344&sll=44.808065,-93.371525&sspn=0.006455,0.016512&ie=UTF8&hq=&hnear=6851+Flying+Cloud+Dr,+Eden+Prairie,+Hennepin,+Minnesota+55344&z=17">Directions</a></p>'+
				'</div>'+
				'<div id="mapInfoRight">'+
				'<img src="assets/images/photos/map-image-edenprairie.jpg" alt="Eden Prairie Location" />'+
				'</div>'
			}, // Eden Prairie Location
			
			moundLocation: {
			
			locations: [
			   [44.936587,-93.657022,"","Mound Location"]         
			],
			             
             infoWindowContents: '<div id="mapInfo">'+
             '<div id="mapInfoLeft">'+
             '<h3>Address:</h3>'+
             '<p>5300 Shoreline Drive Mound MN 55364</p>'+
             '<p><a href="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=5300+Shoreline+Drive+Mound+MN+55364&sll=44.881532,-93.410139&sspn=0.006447,0.016512&ie=UTF8&hq=&hnear=5300+Shoreline+Dr,+Mound,+Hennepin,+Minnesota+55364&z=17">Directions</a></p>'+
             '</div>'+
             '<div id="mapInfoRight">'+
             '<img src="assets/images/photos/map-image-mound.jpg" alt="Mound Location" />'+
             '</div>'
			} // Mound Location
			
		} // google maps
		
	}; // Custom Elements
	
	var scrollTo = {
		down: function(){
			var windowHeight = $(window).height();
			$(window).scrollTop(windowHeight);
		}
	};
	
	return {
		customInit: customElements.init(),
		scrollTo: scrollTo
	};
	
})(jQuery);


plauditSite.customInit;

