jQuery.noConflict();

var plauditSite = (function($){
	
	var uiElements = {
		
		init: function() {
			uiElements.defaultMethods();
			uiElements.externalPlugins.googleLocationBox();
			uiElements.externalPlugins.mainGoogleMap();
			uiElements.externalPlugins.overlay.init();
			uiElements.customElements.init();
		},
		
		defaultMethods: function() {
			$("body").addClass("jsEnabled");
			$("tr:odd").addClass("odd");
			$("#mainSearchForm input.query").focus(function(){ $(this).select(); });
		},
		
		externalPlugins: {
			
			googleLocationBox: function(){
				
				if ( $("#map").length !== 0 ) {
					
					var $this = this; //cache google maps context
					var locationLat = parseFloat($("#locationLat").text());
					var locationLong = parseFloat($("#locationLong").text());
					var locationTitle = $("h1").text();
					var locationAddress = $("#locationAddress").text();
					
					var mapOptions = {
						zoom: 11,
						mapTypeControl: false,
						navigationControlOptions: {
					        position: google.maps.ControlPosition.TOP_RIGHT
					    },
						center: new google.maps.LatLng(locationLat,locationLong),
						mapTypeId: google.maps.MapTypeId.ROADMAP
					};
					
					var markerOptions = {
						position: new google.maps.LatLng(locationLat,locationLong)
					};
					
					var markerContent = {
						content: '<h4>' + locationTitle + '</h4>',
						maxWidth: 75
					};
					
					var map = new google.maps.Map(document.getElementById("map"), mapOptions);
					var marker = new google.maps.Marker(markerOptions);
					var infowindow = new google.maps.InfoWindow(markerContent);
					
					marker.setMap(map);
					
					google.maps.event.addListener(marker, 'click', function() {
						infowindow.open(map,marker);
					});
					
					$("#directions .submit").click(function(e){
						
						e.preventDefault();
						
						var startAddr = $("#startAddress").val();
						
						getMapURL(startAddr);
						
					});
					
					function getMapURL(address){
						var startAddress = address.replace(/ /g,'+');
						var url = "http://maps.google.com/maps?f=d&source=s_d&saddr=";
						
						url += encodeURIComponent(startAddress); 
						
						url += "&daddr=";
						
						url += encodeURIComponent(locationAddress);
						
						url += "&hl=en&mra=ls";
						
						url += "&ie=UTF8";
						
						//console.log(url);
						
						window.location.replace(url);
					}
				
					
				} //test to see if map div is present
				
			},// googleLocationBox
		
			mainGoogleMap: function(){
				
				if ( $("#mapLG").length !== 0 ) {
					
					var currentLatLng;
					
					var mapOptions = {
						zoom: 7,
						mapTypeControl: false,
						navigationControlOptions: {
					        position: google.maps.ControlPosition.TOP_LEFT
					    },
						center: new google.maps.LatLng(44.971534,-93.199768),
						mapTypeId: google.maps.MapTypeId.ROADMAP
					};
					
					var map = new google.maps.Map(document.getElementById("mapLG"), mapOptions);
					
					var locations = [
					                 new google.maps.LatLng(44.93596,-93.085445), // Main Office location          
					                 new google.maps.LatLng(44.761951,-93.025507), // Rosemount location
					                 new google.maps.LatLng(44.968933,-93.188262), // Vandalia ocation
					                 new google.maps.LatLng(44.972102,-93.211153), // Malcolm location
					                 new google.maps.LatLng(45.157491,-93.183951), // Blaine Location
					                 new google.maps.LatLng(44.779205,-93.0511), // Rich Valley Location
					                 new google.maps.LatLng(43.709341,-92.999935) // Austin Location
					                 ];
	
					var locationLinks = [
					                     'our-locations/corporate-office.html',
					                     'our-locations/rosemount-recycling.html',
					                     'our-locations/vandalia.html',
					                     'our-locations/malcolm.html',
					                     'our-locations/blaine.html',
					                     'our-locations/rich-valley.html',
					                     'our-locations/lansing.html'
					                     ];
					
					
					function setupLinkToPage(current){
						google.maps.event.addListener(marker, 'click', function(){
							window.location.href = locationLinks[current];
						});
					}
					
					for ( i = 0; i < locations.length; i++ ){
						
						var marker = new google.maps.Marker({
							map: map,
							position: locations[i]
						});
						
						setupLinkToPage(i);
						
					}
				
				} //test to see if mapLg div is present
				
			}, // mainGoogleMaps
			
			overlay: {
				init: function(){
					$("a[rel^=lightbox]").lightBox();
				},
					
				options: {
					overlayBgColor: '#000',
					overlayOpacity: 0.6,
					imageLoading: 'assets/images/lightbox/loading.gif',
					imageBtnClose: '../assets/images/lightbox/close.gif',
					imageBtnPrev: '/assets/images/lightbox/prev.gif',
					imageBtnNext: '/assets/images/lightbox/next.gif'
				}
			}
		},
		
		customElements: {
			
			init: function() {
				this.mainNavHover();
				this.slideShow();
				this.slideShow2();
				this.recycledMaterials();
			},
			
			mainNavHover: function() {
				$("#mainMenu > li").hover(
					function(){
						$(this).addClass("hover");
					}, function(){
						$(this).removeClass("hover");
					}
				);
			},
			
			//Slideshow setup - home page
			slideShow: function () {
				$("#rotatingCallouts").cycle({
				fx: 'fade',
				timeout: 5000
				});
			},
			//Slideshow setup - interior pages
			slideShow2: function () {
				$("#rotatingPictures").cycle({
				fx: 'fade',
				timeout: 5000
				});
			},
			
			recycledMaterials: function(){
				var materials = $("span", $("#materialsRecycled"));
				var materialPhotos = $("#materialPhotos");
				var image;
				
				materials
					.hover(function(){
						$(this).doTimeout( "slide", 250, function(){
							slideText($(this),"show");
							fadeImage($(this),1);
						});
					},function(){
						$(this).doTimeout( "slide", 250, function(){
							slideText($(this),"hide");
							fadeImage($(this),0);
						});
					}
				);
				
				function fadeImage(el,opacity){
					image = $("#" + el.attr("class"));
					
					if ( opacity !== 0 ){
						image.stop().fadeTo(250, opacity);
					} else {
						image.delay(300).hide();	
					}
				};
				
				function slideText(el,dir){
					if ( dir === 'show'){
						el.next("p").slideDown(250);
					} else if ( dir === 'hide'){
						el.next("p").hide();
					} else {
						return false;
					}
				};
				
			}
		}
		
	};
	
	return {
		setup: uiElements.init()
	}
})(jQuery);

jQuery(function(){ plauditSite.setup });