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 marker,			
						markerLatitude,
						markerLongitude,
						markerLatLng,
						markerCount,
						locationList = $(".siteInfo"),
						geocoder = new google.maps.Geocoder(),
						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
						},
						map = new google.maps.Map(document.getElementById("mapLG"), mapOptions);
					
					if (locationList.size() > 0) {
						// Create a LatLngBounds that will contain all properties
						var latlngbounds = new google.maps.LatLngBounds();
						var markerWindow = new google.maps.InfoWindow();
						
						// Create a marker for each property
						locationList.each(function(i){
							var $this = $(this);
							
							markerLatitude = $this.attr("data-lat");	
							markerLongitude = $this.attr("data-long");	
							markerLatLng = new google.maps.LatLng(markerLatitude,markerLongitude);
							
							latlngbounds.extend(markerLatLng);
							
							var marker = new google.maps.Marker({
								position: markerLatLng,
								map: map
							});
					
							google.maps.event.addListener(marker, 'click', function(){
								markerWindow.close();
								markerWindow.open(map,marker);
							});
						});	
						// if >1 Fit map to the bounds that contains all properties else map will be full zoom
						if (locationList.size() > 1){
							map.fitBounds(latlngbounds);
						}
					}
				} //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 });
