jQuery.noConflict();

var plauditAnalytics = (function($) {
	
	var isPageTrackerLoaded = function() {
		return typeof(_gaq) != "undefined" && _gaq != null;
	};
	
	return {
		trackPage: function(url) {
			if ( url.substring(0,1) != '/' )
				url = '/' + url;
			if ( isPageTrackerLoaded() ) {
				_gaq.push(['_trackPageview', url]);
			} else if ( typeof(console) != "undefined" ) {
				console.log("Google Analytics not loaded. Trying to track page: " + url);
			}
		},
		trackEvent: function(category, action, opt_label, opt_value) {
			if ( isPageTrackerLoaded() ) {
				_gaq.push(['_trackEvent', category, action, opt_label, opt_value]);
			} else if ( typeof(console) != "undefined" ) {
				console.log("Google Analytics not loaded. Trying to track event with category '" + category +"', action '" + action +"', opt_label '" + opt_label +"', and opt_value '" + opt_value +"'.");
			}
		},
		setupBinding: function() {
			$("a").filter("[href^='http://'], [href^='https://']").filter(":not([href*='://"+location.hostname+"'])").click(function(){
				plauditAnalytics.trackEvent("Outbound Link", "Click", $(this).attr("href"));
			});
			$("a").filter("[href^='mailto:']").click(function(){
				plauditAnalytics.trackEvent("Email Link", "Click", $(this).attr("href"));
			});
			$("form").filter("[action^='http://'], [action^='https://']").filter(":not([action*='://"+location.hostname+"'])").submit(function(){
				plauditAnalytics.trackEvent("Outbound Form Submissions", "Submit", $(this).attr("action"));
			});
		}
	};
})(jQuery);


var plauditUI = (function($){
	return {
		common: function(){
			$("tr:odd").addClass("odd");
			$(".search-form").placeHolder("Search...");
			if ( $.browser.msie	&& parseInt($.browser.version) < 9 ) {
				// Some versions of ie do not submit the form using the first submit if the field only contains
				// one input field. We must have it use the first button so the correct JSF action runs.
				$('input').keypress(function(event){
					var input = $(this);
				    if (event.keyCode == 13) {
				    	var submitButtons = input.closest("form").find(":submit:first, input.submit:first, input[type='image']:first");
				    	if ( submitButtons.length > 0 ) {
				    		// handle the submit
				    		submitButtons.first().click();
				        	return false;
				    	}
				    	return true;
				    }
				});
			}
		} // END common
		,carousel: function(){
			if ($('#carousel-wrap').length==0) return;
			$('#carousel-wrap').append('<div class="carousel-controls"><a id="carousel-l" class="control-l" href="#">Prev</a><a id="carousel-r" class="control-r" href="#">Next</a><span id="carousel-paginate"></span>');
			$('#carousel').cycle({
			     fx:     'fade'
			    ,pager:  '#carousel-paginate'
			    ,next:   '#carousel-r' 
			    ,prev:   '#carousel-l' 
			   	,containerResize: 0
			    ,timeout: 4000
				,delay:  1000
			    ,speed:  700
		        ,speedIn:  1000 
		        ,speedOut: 500
			});
			$('#newsList').cycle({
			     fx:     'scrollLeft'
			    ,timeout: 7000
			});
		} // END carousel
		,locationMap: function(){
			if ( $('#map-and-amenities').length==0 ) return;
			//$('#location-list').hide();			
			/* Var Def */		
			var marker,			
				markerLatitude,
				markerLongitude,
				markerLatLng,
				markerCount,
				locationList = $("#location-list .location"),
				geocoder = new google.maps.Geocoder(),
				mapCenter = new google.maps.LatLng(44.9792189,-93.1836059),
				mapOptions = {
					zoom: 14,
					center: mapCenter,
					navigationControl: true,
					navigationControlOptions: {
						style: google.maps.NavigationControlStyle.SMALL
			    	},
			    	mapTypeId: google.maps.MapTypeId.ROADMAP
				},
				mapHTML = '<div id="location-map-wrap">' +
								'<div id="location-map">'+
									'<div id="location-map-inner"></div>' +
								'</div>' +
						  '</div>';		
			/* Setup Map */
			$('#map-and-amenities').prepend(mapHTML);		
			var map = new google.maps.Map(document.getElementById("location-map-inner"), 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.data("lat");	
					markerLongitude = $this.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.setContent('<div id="info-window-content">\
								<div class="fn org">CTC</div>\
								<a href="http://maps.google.com/?saddr=&amp;daddr=Commonwealth+Terrace+Cooperative+1250+Fifield+Street+Falcon+Heights,+MN+55108&amp;aq=&amp;sll=44.940277,-93.243924" target="_blank">Driving Directions</a>\
						</div>');
						markerWindow.open(map,marker);
					});
				});	
				// Fit map to the bounds that contains all properties 
				// If 1, map will be full zoom
				if (locationList.size() > 1){
					map.fitBounds(latlngbounds);
				}
			}
		} // END Location Map
		,toolTip: function(){
			var toolTip;

			function displayToolTip(elem,adjust){
				toolTip = $("#tool-tip");
				
				toolTip.css({
					width: function(){
						return elem.data("version") == 'large' ? '450px' : '200px'  
					},
					fontSize: function(){
						return elem.data("version") == 'large' ? '11px' : '12px'
					}
				});
				
				var toolTipHeight = toolTip.height(); 
				var offset = elem.offset();
				
				toolTip.css({
					top: offset.top - (toolTipHeight + adjust.yAdjust), 
					left: offset.left - adjust.xAdjust
				}).fadeTo(250,1);
			}	
			
			function hideToolTip(){
				toolTip = $("#tool-tip");
				toolTip.hide();
				toolTip.children(".tool-tip-inner").empty();
			}
			
			$("#calendar-listing a").live("click", function(){ $("#calendar-tool-tip").hide(); });
			$("#calendar-listing a").live("mouseover mouseout", function(event){
				if ( event.type == 'mouseover'){
					$('<ul>').html( $(this).parent().find('ul').html() ).appendTo( "#tool-tip .tool-tip-inner" );
					displayToolTip($(this),{xAdjust: 97, yAdjust: 65});
				} else {
					hideToolTip();
				}
			});
			
		} // END toolTip
		,datePicker: function(){
			if ( $('.date').length==0 ) return;
			$('.date').datepicker();
		}
	};
})(jQuery);

jQuery(function(){
	plauditAnalytics.setupBinding();
	plauditUI.common();
	plauditUI.carousel();
	plauditUI.locationMap();
	plauditUI.toolTip();
	plauditUI.datePicker();
});

