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 +"'.");
			}
		},
		flowplayer: { // Defaults for recording flowplayer actions
			clip: {
				onStart: function(clip) {
					plauditAnalytics.trackEvent("Videos", "Play", clip.url);
				},
				onPause: function(clip) {
					plauditAnalytics.trackEvent("Videos", "Pause", clip.url, parseInt(this.getTime()));
				},
				onStop: function(clip) {
					plauditAnalytics.trackEvent("Videos", "Stop", clip.url, parseInt(this.getTime()));
				},
				onFinish: function(clip) {
					plauditAnalytics.trackEvent("Videos", "Finish", clip.url);
				}
			},
			plugins: {
				controls: {
					stop: true // show stop button so we can see stop events too
				}
			}
		},
		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");
			$(".listing:odd").addClass("oddList");
			
			contHeight = $("#main").height();
			docHeight = $(document).height();
			footHeight = $('#footer').height();
			
			shadowHeight = docHeight - footHeight;
			
			$(".has-nav #shadowDiv").height(shadowHeight);
			
			
			$(".listing:even").addClass("evenList");
			$('#carousel').cycle({
				fx:     'fade', 
			    speed:   700, 
			    timeout: 5000
			});
			$("a[rel='project-photo']").colorbox({
				maxWidth: 800,
				maxHeight: 600
				
			});
			
			
			runTitle = function(){
				
				winWidth = $(window).width();
				contWidth = $('#homeContent').width();
				sideWidth = Math.round((winWidth-contWidth)/2);
				
				$('#homeTitle').animate({
					'width' : sideWidth + 287
					
				}, 500, function(){
					$('#carousel').animate({'opacity' : '1'}, 2000);
					$('#homeMainContent').slideDown('slow');
				});
			};
			
			runTitle();
			
			
			
			/*
			$(window).resize(function() {
				
				winWidth = $(window).width();
				contWidth = $('#homeContent').width();
				sideWidth = Math.round((winWidth-contWidth)/2);
				
				if (winWidth < 1053) {
					$('#homeTitle').animate({
						'width' : 362 +'px';
					});
				}else {
					$('#homeTitle').animate({
						'width' : sideWidth + 287 + 'px';
					});
				}
				
				
			});*/
			
			
			
			$(".search-form input.query").placeHolder("Site 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;
				    }
				});
			}
		}
	};
	
})(jQuery);

jQuery(function(){
	plauditAnalytics.setupBinding();
	plauditUI.common();
});

