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.projectGallery();
			this.homePageGallery();
		},
		common: function(){
			$("tr:odd").addClass("odd");

			var searchField = $(".search-form input.query"); 
			
			function addSearchText(input, type){
				var defaultValue = "Site 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;
				    }
				});
			}
			
		},
		
		projectGallery: function(){
			var projects = $("#projects a"),
				projectPage,
				overlay = $("#overlay"),
				overlayContent = $("#overlay-content"),
				closeButton = $("#overlay .close"),
				pauseButton = $("#overlay .pause"),
				projectWrap = $("#project-list"),
				ajaxHTML,
				projectImageCount;
		
			var cycleOptions = {
				speed: 750,
				timeout: 3000 
			};
			
			projects.click(function(e){
				e.preventDefault();	

				projectPage = $(this).attr("href");
				
				if( !$(this).hasClass("open")) {
					$.ajax({
						url: projectPage,
						success: function(data){
							ajaxHTML = $("#ind-project", $(data));
							ajaxHTML.appendTo(overlayContent);
							projectImageCount = ajaxHTML.find("img").length;

							if ( projectImageCount > 1 ) {
								pauseButton.show();
							} else {
								pauseButton.hide();
							}

							projectWrap.hide();
							overlay.fadeTo(500, 1, function(){
								$("#project-gallery img").fadeTo(500,1);
								$("#project-gallery-wrap").cycle(cycleOptions);
							});
						}
					});
				}				

				$(this).addClass("open");
				return false;
			});

			pauseButton.click(function(){
				if( $(this).hasClass("play") ){
					$(this).removeClass("play");
					$("#project-gallery-wrap").cycle("resume");	
				} else {
					$(this).addClass("play");
					$("#project-gallery-wrap").cycle("pause");	
				}	
			});
	
			closeButton.click(function(){
				overlay.hide()
				overlayContent.empty();
				projectWrap.fadeIn(750);
				$("#overlay .pause").removeClass("play");
				$("#projects .open").removeClass("open");
			});
			
		}, // Project Gallery
		
		homePageGallery: function(){
			$("#animation").cycle({
				speed: 1500,
				timeout: 3000
			}); 	
		}	
	}; 
	
	ui.init();
})(jQuery);


