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);

jQuery(function(){
	plauditAnalytics.setupBinding();
});

var plauditSite = (function($){
	
	var ui = {
		
		init: function(){
			this.common(); 
			this.category();
			this.ticker();
			this.calendar();
			this.homepageGallery();
			this.gallery();
			this.toolTip();
		},
		
		common: function(){
			$("tr:odd").addClass("odd");

			var searchField = $(".searchForm input.query"); 
			
			// Open external pages and pdfs in a new window			$("a").filter("[href^='http://'], [href^='https://']").attr("target","_blank");
			$("a[href*='.pdf']").attr("target","_blank");
			
			function addSearchText(input, type){
				var defaultValue = "Search"
				
				if ( type === "removeText" ){
					if ( input.val() === defaultValue ) {
						input.val("")
					} 
				}
				
				if ( type === "insertText" ){
					if ( input.val() === "" ){
						input.val(defaultValue)
					}
				}
			}
			
			addSearchText(searchField,"insertText");
			
			searchField.blur(function(){
				addSearchText($(this), "insertText");
			});
			
			searchField.focus(function(){
				addSearchText($(this), "removeText");
			});
			
			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 function
		
		category: function(){
			var categoryListing = $("#category-listings");
			var openOnLoad;
			
			$(".section-wrap",categoryListing).each(function(){
				openOnLoad = $(this).data("open");
				if (!openOnLoad){
					$(".section-content", $(this)).hide();
				} else {
					$("h3", $(this)).addClass("open section-content-open").data("lastToggle3","1");
				}
			});
			
			$("h3", categoryListing).toggle(
				function(){
					
					$this = $(this);
					$this.next().children().css("visibility","visible");
					$this.addClass("open");
					$this.next().slideDown(function(){
						$this.addClass("section-content-open");
					});
					
				}, function(){
					
					var $this = $(this);
					
					$this.removeClass("open");
					$this.next().children().css("visibility", "hidden");
					
					$this.next().slideUp(function(){
						$this.removeClass("section-content-open");
					});
				}
			);
		}, // End category function
		
		ticker: function(){
			$(".ticker ul").liScroll();
		}, // End Marquee Function
		
		homepageGallery: function(){
			$("#animation").cycle();
		},
		
		gallery: function(){
			var sliderOptions = {
				size: 1,
				clickable: false,
				next: "#next",
				prev: "#prev",
				vertical: true,
				speed: 750
			};
			
			$("#gallery").scrollable(sliderOptions);
			
			$("#gallery a").fancybox({
				titlePosition: 'inside'
			});
			
			$("a.lightbox").fancybox({
				titlePosition: 'inside'
			});
			
		}, // End gallery function
	
		calendar: function() {
			// Start Var Def
			
			var calendar = $("#calendar-listing"),
				previousCalendarControl = $("#previous-calendar", $("#calendar")),
				nextCalendarControl = $("#next-calendar", $("#calendar")),
				previousCalendar,
				nextCalendar,
				currentCalendar;
			
			// End Var Def
			
			// Start Helper functions
			
			function updateClassNames() {
				$(".cal-group", calendar).each(function(i){
					if (i === 0){
						$(this).attr("class","cal-group group cal-group-previous");
					} else if (i === 1){
						$(this).attr("class","cal-group group cal-group-current");
					} else if (i === 2){
						$(this).attr("class","cal-group group cal-group-next");
					}
					$(this).removeAttr("style");
				});
			}
			
			function slideCalendar(direction, data){
				previousCalendar = $(".cal-group-previous",calendar); 
				nextCalendar = $(".cal-group-next",calendar); 
				currentCalendar = $(".cal-group-current",calendar); 
				
				if ( direction === "previous-calendar") {
					data.appendTo(previousCalendar);
					
					previousCalendar.stop().animate({ left: "740px"});
					
					currentCalendar.stop().animate({ left: "1480px"}, function(){
						nextCalendar.prependTo(calendar);
					
						currentCalendar.empty();
						
						updateClassNames();
					});
				} 
				
				if ( direction === "next-calendar") {
					data.appendTo(nextCalendar);
					
					nextCalendar.stop().animate({ left: "740px"});
					
					currentCalendar.stop().animate({ left: "0"}, function(){
					
						previousCalendar.appendTo(calendar);
						
						currentCalendar.empty();
						
						updateClassNames();
					});
				}
				
			}
			
			// End helper functions
			
			// Attach Event to DOM Elements
			
			$(".controls", $("#calendar")).click(function(e){
				e.preventDefault();
				var id = $(this).attr("id");
				var href = $(this).attr("href");
				
				// Show loading icon
				$("#layout-switcher p").show();
				
				$.ajax({
					url: href,
					success: function(data){
						// Hide loading icon
						$("#layout-switcher p").hide();
						var table = $("table",$(data));
						var previousCalendarHref = $("#previous-calendar",$(data)).attr("href");
						var nextCalendarHref = $("#next-calendar",$(data)).attr("href");
						
						slideCalendar(id,table);
						previousCalendarControl.attr("href",previousCalendarHref);
						nextCalendarControl.attr("href",nextCalendarHref);
					}
				});
			});
			
			$(".pagination a", $("#event-list")).live("click", function(e){
				e.preventDefault();
				var href = $(this).attr("href");
				
				// Show loading icon
				$(".pagination").append("<span class='loading' />");
				
				$.ajax({
					url: href,
					success: function(data){
						$(".pagination .loading").remove();
						var response = $(data);
						var newEvents = $("#all-events", response).html();
						var newPagination = $(".pagination", response).html();
						
						$("#all-events").empty().append(newEvents);
						$(".pagination").empty().append(newPagination);
					}
				});
			});
			
			$("#layout-switcher span").click(function(){
				var $this = $(this);
				
				if ( !$this.hasClass("current-layout") ){
					$("#layout-switcher span").removeClass("current-layout");
					$this.addClass("current-layout");
					
					if ( $this.attr("id") === "calendar-layout" ){
						$("#calendar").show();
						$("#event-list").hide();
					} else if ( $this.attr("id") === "list-layout" ){
						$("#event-list").show();
						$("#calendar").hide();
					}
				}
			});
			
		}, // calendar
		
		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").bind("click", function(){ $("#calendar-tool-tip").hide(); });
			
			$("#calendar-listing a").bind("mouseover mouseout", function(event){
				if ( event.type == 'mouseover'){
					$(this).next("ul").clone().appendTo("#tool-tip .tool-tip-inner");
					displayToolTip($(this),{xAdjust: 97, yAdjust: 45});
				} else {
					hideToolTip();
				}
			});
			
			$("#bill li").hover(
				function(){ 
					$(this).children(".content").clone().appendTo("#tool-tip .tool-tip-inner");
					displayToolTip($(this),{xAdjust: 101, yAdjust: 45});
				}, 
				function(){ 
					hideToolTip($(this));	
				}
			);
			
		} // toolTip
	}; 
	
	ui.init();
})(jQuery);


