var plauditSite = {
	
	common: {
		init: function(){
			$("body").addClass("jsEnabled");
			$("legend").each(function(){
				var $this = $(this); 
				var legendText = $this.text();
				$this.parent("fieldset")
					.before("<h2>" + legendText + "</h2>")
					.end()
					.remove();
			});
			
			$('#switcher a#d1').click(function(ev){
				$('body').removeClass('design2');
				$('body').addClass('design1');
				$('#switcher a.active').removeClass('active');
				$(this).addClass('active');
				ev.preventDefault();
		 	   	ev.stopPropagation();
			});
			$('#switcher a#d2').click(function(ev){
				$('body').removeClass('design1');
				$('body').addClass('design2');
				$('#switcher a.active').removeClass('active');
				$(this).addClass('active');
				ev.preventDefault();
		 	   	ev.stopPropagation();
			});
			$('#switcher a#d3').click(function(ev){
				$('body').removeClass('design1');
				$('body').removeClass('design2');
				$('#switcher a.active').removeClass('active');
				$(this).addClass('active');
				ev.preventDefault();
		 	   	ev.stopPropagation();
			});
			
			
			
			// Adding hover in IE for non-anchor elements
			$(".hoverEnabled").hover(
				function() {
					$(this).addClass("hover");
				},
				function(){
					$(this).removeClass("hover");
				}
			);
			
			//Add zebra-striping to tables
			if ( !$("table").hasClass("noStripe") ) {
				$("table tr:odd").addClass("odd");
			}
			
			// setup Pretty photo lightbox
			$("a[rel^='gal']").prettyPhoto({ 
				theme: 'dark_rounded', 
				showTitle: false 
			});

		}
	},
	
	/*
	 * flash Object
	 */
	flash: {
		init: function(){
			swfobject.embedSWF("assets/flash/home2.swf", "animationInner", "491", "507", "8.0.0", "assets/flash/expressInstall.swf", {}, { wmode: "transparent"}, {});
		}
	},  
	
	/*
	 * Media Gallery Object
	 */
	mediaGallery: {
		init: function(){
			//Initialize Media Gallery
			$(".photoGallery").mediaGallery();
			$(".videoGallery").mediaGallery();
			$(".wallPaper").mediaGallery();
		}
	}, 
	
	
	/*
	 * Cycle Object
	 */
	cycle: {
		init: function(){
			$("#featurePhotoInner").cycle({
				timeout: 3000,
				speed: 750
			});
		}
	}, 
	
	/*
	 * Honors Table
	 */
	honorsTable: {
		init: function(){
			var yearHonors = null;
			
			$("#honors div:last").show().addClass("current");
			
			$("#careerStats tr").click(function(){
				yearHonors = $(this).attr("class");
				$(".current").removeClass("current").hide();
				$("." + yearHonors, $("#honors")).fadeIn("slow").addClass("current");
			});
		}
	},
	
	/*
	 * Homepage Callouts
	 */
	slideCallouts: {
		init: function(){
	
			$("ul.slide li").each(function(){
				$(this).append('<span class="callout-bg-left" />');
				$(this).append('<span class="callout-bg-right" />');
			});
			
			$("ul.slide li a").hover(
				function(){
					$(this).siblings(".callout-bg-left").stop().animate({"left" : "0px"}, 250);
					$(this).siblings(".callout-bg-right").stop().animate({"right" : "0px"}, 250);
				},
				function(){
					$(this).siblings(".callout-bg-left").stop().animate({"left" : "-13px"}, 250);
					$(this).siblings(".callout-bg-right").stop().animate({"right" : "-13px"}, 250);
				}
			);
		}
	}, // homeCallouts
	
	/*
	 * Video Player Module
	 */
	
	videoPlayer: {
		
		init: function(){
		
			var obj = this,
			
				// Empty vars for use later
				selectedClip,
			
				// DOM Elements
				overlayContainer = "video-container-wrap",
				videoContainer = "video-container",
				videoFile = $("a.video"),
				
				// Flow Player Vars
				flowPlayerSWF = "assets/flash/flowplayer-3.2.5.swf",
				flowPlayerOptions = {
					clip: {
						autoPlay: false,
						autoBuffering: true,
						provider: 'pseudostreaming'
					},
					
					plugins: {
						pseudostreaming: {
							url: 'flowplayer.pseudostreaming-3.2.5.swf'
						}
					}
				},
				
				// Overlay Vars 
				overlayOptions = {
					mask: {
						color: '#000000',
						loadSpeed: 200,
						opacity: 0.8
					},
					
					onLoad: loadVideo,
					onBeforeClose: hideVideo
				},
				
				videoPlayer = null,
				// Flowplayer video setup 
				
				// Overlay setup
				overlay = $("#" + overlayContainer).overlay(overlayOptions),
				overlayApi = overlay.data("overlay");
	
			// End Variable Setup
		
			
			// Start function definitions
			
			function getClipURL(elem){
				return elem.attr("data-filename");
			}
			
			function loadVideo(){
				if (videoPlayer == null){
					videoPlayer = flowplayer(videoContainer,flowPlayerSWF,flowPlayerOptions);
					videoPlayer.load(function(){
						videoPlayer.play(selectedClip);
					});
				} else {
					videoPlayer.show();
					videoPlayer.play(selectedClip);
				}
			}
			
			function hideVideo(){
				videoPlayer.hide();
			}
			
			videoFile.click(function(e){
				e.preventDefault();
				selectedClip = getClipURL($(this));
				overlayApi.load();
			});
			
		}
	
	},
	
	/*
	 * Photo Submission 
	 */
	photoSubmission: {
		init: function(){
			var photos = $("#caught-film .photo-submissions li"),
				photoSelectBtn = '<span class="select-photo">Select this photo</span>';
			
			photos.find("label").hide();
			photos.append(photoSelectBtn);
			
			photos.find(".select-photo").live("click", function(){
				$(".select-photo").removeClass("select-photo-active");
				$(this).addClass("select-photo-active");
				
				$(this).siblings("label").children("input").attr("checked","checked");
			});
			
		}
	},
	
	/*
	 * Twitter
	 */
	
	twitter: {
		init: function(){
			$('#twitter-feed').tweet({
//				query: "from:larryfitzgerald",
				username: "larryfitzgerald",
				avatar_size: 48,
				count: 4
			});
		}
	}
	
};

/*
 * Start Utility Functions
 * 
 */

var UTIL = {
	
	loadLogFunc: function(){
		//http://paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
		window.log = function(){
			log.history = log.history || [];   // store logs to an array for reference
			log.history.push(arguments);
			if(this.console){
			 console.log( Array.prototype.slice.call(arguments) );
			}
		};	
	},	
		
	fire: function(module){
		var nameSpace = plauditSite;	
		nameSpace[module].init();
		log(module + "loaded");
	},
	
	loadEvents: function(){
		var eventToFire;
		var modulePrefix = "module-";
		
		UTIL.fire("common");
		
	    // do all the classes too.
	    $.each(document.body.className.split(/\s+/),function(i,classnm){
	    	if (classnm.indexOf(modulePrefix)!= -1){
	    		eventToFire = classnm.replace(modulePrefix,"");
	    		UTIL.fire(eventToFire);
	    	};
	    });

	}
};

$(document).ready(function(){
	UTIL.loadLogFunc();
	UTIL.loadEvents();
	
});


jQuery.fn.mediaGallery = function(options)
{
	
	var gallery = $(this);
	var scrollBar = gallery.next(".photoGalleryControls").children(".scrollBar");
    
	var mediaItemContainer = gallery.children('.mediaItems'); // Get the container for the media items
    var mediaItems = mediaItemContainer.children();  // Get the children for the selected media items
    var mediaCount = mediaItems.length; // Get the number of media items
    
    var mediaItemSize = mediaItems.width(); // Gets the width of a single media item
    var mediaMarginRight = parseInt(mediaItems.css("margin-right")); // Gets any right margins that might be applied to media items
    
    var lastMediaItem = $(mediaItems.get(mediaCount - 1)); // Selects the last media item...
    lastMediaItem.css("margin-right", "0"); // and removes its right margin
    
	var mediaTotalWidth = (mediaItemSize + mediaMarginRight) * mediaCount; // Calculate what the container total width should be...
	
	mediaItemContainer.css("width" , mediaTotalWidth); // and apply css style
	
	var itemsWidth = (mediaTotalWidth - gallery.outerWidth()) - 12;
	
	scrollBar.slider({
		min: 0,
		max: itemsWidth,
		slide: function (event, ui) {
			var handle = $(ui.handle);
			var closestGallery = handle.closest(".photoGalleryControls").siblings(".scrollGallery").children(".mediaItems");
			closestGallery.css("left", ui.value * -1);
		}
    });
}

