/* 	CLEAR FORM VALUES
	----------------------------------------------------------*/
	(function($){
		$.fn.clearDefault = function(){
			return this.each(function(){
				var default_value = $(this).val();
				$(this).focus(function(){
					if ($(this).val() == default_value) $(this).val("");
				});
				$(this).blur(function(){
					if ($(this).val() == "") $(this).val(default_value);
				});
			});
		};
	})(jQuery);


/* 	SETUP PORTFOLIO ANIMATION
	----------------------------------------------------------*/
	var animationSpeed = 500;
	var scrollSpeed = 500;
	var fadeDelay = 100;
	var currentContent = "";
	
/* 	PORTFOLIO FUNCTIONS
	----------------------------------------------------------*/
	
	function loadContent(contentName, contentPath) {
		// load work from external page
		$("#ajax_container").load(contentPath +" #content_"+contentName, "", function() {
			//JQUERY PERFORMED ON AJAX LOADED CONTENT MUST GO HERE
	
			// Expand work section
			$("#ajax_container").delay(fadeDelay).animate({
				height: $("#content_"+contentName).outerHeight()
			}, animationSpeed );
			
			// Hide content on load then fade in
			$("#content_"+contentName).hide().delay(fadeDelay).fadeIn(animationSpeed);
			
			//Fade in Close Button
			$("#content_close").fadeIn(animationSpeed);
			
			setupWorkSlides();
			setupStatSlides();
			
			//fade in images after load
			$('.work_slides img, .person_details img').each(function() {
				$(this).css({opacity: 0}).bind('load', function() {
					$(this).animate({opacity: 1}, animationSpeed);
				});
			});
			
			
			/*$('.work_slide_nav a').bind('click', function() {
				//alert('clicked');
				$('.work_slides li').children('iframe').each(function() {
					//Stop Youtube
                	var getID = $(this).attr('id');
				   	callPlayer(getID, "pauseVideo");
				   
				   	//Stop Vimeo
				   	var f = $(this),
					url = f.attr('src').split('?')[0];
					
					function post(action, value){
						var data = '{"method":"' + action + '", "value" : "' + value + '" }';
						f[0].contentWindow.postMessage(data, url);
					};
					
					post( 'pause' );
					
                });
				
				
				
			});
			*/
			
			$('.work_slide_nav a').bind('click', function() { 
				stopVideo();
			});
			
			/*
			$(".work_prev").live("click", function(){ 
				alert("Prev!"); 
				stopVideo();
			});
			
			$(".work_next").live("click", function(){ 
				alert("Next!"); 
				stopVideo();
			});
			*/
	
			
			/*
			// on click events for showing next/prev project via ajax
			$(".work_project_nav a").click(function(event) {
				event.preventDefault();
				showContent($(this).attr("id").substr(5), $(this).attr("href"));
	
			});*/
			
		});
	
		
	}
	
	
	
	function showContent(contentName, contentPath) {
		// check if there is an open project
		if ($("#content_list a.selected").attr("id")) {
			currentContent = $("#content_"+$("#content_list a.selected").attr("id"));
		}
		
		// if the selected link is not already selected load new work
		if (!$("#"+contentName).hasClass("selected")) {
			
			// if current project exists
			if (currentContent) {
				
				//fade out current project then fade in new project
				currentContent.fadeOut(250, function(){ loadContent(contentName, contentPath) });
				
				//fade out current close button
				$("#content_close").fadeOut(250);
				
			// otherwise only fade in new content
			} else {
				
				loadContent(contentName, contentPath);
			}
			
			//Remove old selected class
			$("#content_list a.selected").removeClass("selected");
			
			//Set new selected class
			$("#"+contentName).addClass("selected");
			
			
			//Fade in Close Button
			//$("#content_close").fadeIn(animationSpeed);
			
			window.location.hash = "view_"+contentName;  
			
		}
		
		//Scroll back to top
		$.scrollTo("#content", scrollSpeed);
	}
		
	function hideContent() {
		stopVideo();
		
		// fade out displayed work
		$("#content_"+$("#content_list a.selected").attr("id")).fadeOut(animationSpeed);
		
		// remove selected class
		$("#content_list a.selected").removeClass("selected");
		
		// hide work section
		$("#ajax_container").delay(fadeDelay).animate({
			height: 0
		}, animationSpeed );
		
		// fade out close button
		$("#content_close").fadeOut(animationSpeed);
		
		
				
	}
	
	function setupCloseButton() {
		// on click actions for close button
		$("#content_close").live('click',function() {
			hideContent();
			//return false;
			
		});
	}
	
	function setupWorkSlides() {
		if ($(".work_slides").length ) {
			// portfolio slideshow
			$(".work_slides ul").cycle({
				fx: 'scrollHorz',
				speed: 800,
				timeout: 0,
				easeIn: 'easeOutQuint',
         		easeOut: 'easeOutQuint', 
				next: '.work_next', 
				prev: '.work_prev',
				pager: '.work_nav',
				after: function() {
            				$('.work_slide_caption p').html($(this).children('img').attr('alt'));
				}
			});
		}
	}
	
	function setupStatSlides() {
		if ($(".work_stats").length ) {
			// stats slideshow
			$(".work_stats ul").cycle({
				fx: 'fade',
				speed: animationSpeed,
				timeout: 0, 
				pager: '.work_stats_nav'
			});
		}
	}
	
	
	function imgSlideCount(curr,next,opts) {
		var imgCount = 'Image ' + (opts.currSlide + 1) + ' of ' + opts.slideCount;
		$('#img_count').html(imgCount);
	}
	
	
	
/* 	FILE INPUTS
	-------------------------------------------------------------*/

	var SITE = SITE || {};
	
	SITE.fileInputs = function() {
	  var $this = $(this),
		  $val = $this.val(),
		  valArray = $val.split('\\'),
		  newVal = valArray[valArray.length-1],
		  $button = $this.siblings('.button'),
		  $fakeFile = $this.siblings('.file-holder');
	  if(newVal !== '') {
		//$button.text('Photo Chosen');
		if($fakeFile.length === 0) {
		  $button.after('<span class="file-holder">' + newVal + '</span>');
		} else {
		  $fakeFile.text(newVal);
		}
	  }
	};
	

/* 	Youtube callPlayer Function
	-------------------------------------------------------------*/

	function callPlayer(frame_id, func, args){
		if(!frame_id) return;
		if(frame_id.id) frame_id = frame_id.id;
		else if(typeof jQuery != "undefined" && frame_id instanceof jQuery && frame_id.length) frame_id = frame_id.get(0).id;
		if(!document.getElementById(frame_id)) return;
		args = args || [];
	
		/*Searches the document for the IFRAME with id=frame_id*/
		var all_iframes = document.getElementsByTagName("iframe");
		for(var i=0, len=all_iframes.length; i<len; i++){
			if(all_iframes[i].id == frame_id || all_iframes[i].parentNode.id == frame_id){
			   /*The index of the IFRAME element equals the index of the iframe in
				 the frames object (<frame> . */
			   window.frames[i].postMessage(JSON.stringify({
					"event": "command",
					"func": func,
					"args": args,
					"id": frame_id
				}), "*");
			}
		}
	}


/* 	Stop Videos
	-------------------------------------------------------------*/

	function stopVideo() {
	
		$('.youtube_vid').each(function() {
			var getID = $(this).attr('id');
			callPlayer(getID, "pauseVideo");
		});
		
		$('.vimeo_vid').each(function() {
			var f = $(this),
				url = f.attr('src').split('?')[0],
				data = '{"method":"pause"}';
			f[0].contentWindow.postMessage(data, url);
		});
	}

    
$(document).ready(function() {
	

	/* 	AJAX CONTENT ACTION
		----------------------------------------------------------*/

		// on click events for showing ajax content
		$("#content_list a").click(function(event) { 
			event.preventDefault();
			showContent($(this).attr("id"), $(this).attr("href"));
			//return false;

		});
		
		setupCloseButton();
		
		// Check for hash value in URL to display content on incoming lnks
		var hash = window.location.hash.substr(6);  
		var contentID = $('#content_list li a').each(function(){ 
			var contentID = $(this).attr('id');
			var href = $(this).attr('href');    
			if(hash==contentID){  
				showContent(contentID, href);  
			}  
		});
		
		
		//Next Prev Projects
		$(".work_project_nav a").live('click',function(event) {
			event.preventDefault();
			showContent($(this).attr("id").substr(5), $(this).attr("href"));

		});
	
	
	/* 	Greyscale Transition
		----------------------------------------------------------*/	
		
		$("#content_list a").hover(
			function() {
			$(".grey", this).stop().animate({"opacity": "0"}, animationSpeed);
			$(".work_view", this).stop().addClass("hover").fadeTo("slow");
			
			},
			function() {			
			$(".grey", this).stop().animate({"opacity": "1"}, animationSpeed);
			$(".work_view", this).stop().removeClass("hover").fadeTo("slow");

		});
		 
		
		
		
	/* 	INFO GRAPHICS
		----------------------------------------------------------*/
		
		$("#info_tabs a").mouseover(function(event) {
			event.preventDefault();
			$("#info_tabs a").removeClass("active"); //Remove current active class
			$(this).addClass("active");	//Add new active class
			$("#info_container > div").hide(); //Hide all content blocks

			var activeTab = $(this).attr("href"); //Find the href attribute value to identify the active tab + content
			$(activeTab).show(); //show the active ID content
		});
		
		$("#info_tabs a").click(function(event) {
			event.preventDefault();
		});
		
		
		
	/* 	SCROLL LINKS
		----------------------------------------------------------*/
		
		$("#skip_list a, .scroll_up").click(function(event){ 
			event.preventDefault()
			$.scrollTo($(this).attr("href"), scrollSpeed);
			
		});
		
		
	/* 	SLIDESHOWS
		----------------------------------------------------------*/
		// home page
		$("#slideshow ul").cycle({
			fx: 'scrollLeft',
			timeout: 7000,
          	speed: 600,
          	easeIn: 'easeOutQuint',
         	easeOut: 'easeOutQuint',
          	cleartypeNoBg: true,
          	pager: '#slide_nav'
		});
		
		// post image	
		$("#post_slideshow .slides").cycle({
			fx: 'scrollHorz',
			speed: animationSpeed,
			timeout: 0, 
			next: '.image_next', 
			prev: '.image_prev',
			after: imgSlideCount
		});
		
		// single entry work page slides
		setupWorkSlides();
		setupStatSlides();
		
		
		
	/*	CLEAR INPUT FIELDS ONFOCUS
		-------------------------------------------------------------*/
		
		$("input.clearinput, textarea.clearinput").clearDefault();	


	/*	EXTERNAL LINKS
		-------------------------------------------------------------*/

		$("[rel=external]").live('click',function() {
			window.open( $(this).attr("href") );
			return false;
		});
		
		
	/* 	JQUERY FORM VALIDATION
		----------------------------------------------------------*/
		
		$(".validate").each(function(){
			$(this).validate({
				rules: {
					cover: {
					  required: true,
					  accept: "pdf|doc|docx"
					},
					resume: {
					  required: true,
					  accept: "pdf|doc|docx"
					}

				},
				messages: {
					cover: "Not an accepted file",
					resume: "Not an accepted file"
				}
		
			});
		});
		
		// Deny default value
		$.validator.addMethod(
    		'noPlaceholder', function (value, element) {
        		return value !== element.defaultValue;
   		 	}, 'This field is required.'
		);
		

	/*	FILE INPUT FIELDS
		-------------------------------------------------------------*/		
	
		 $('.file-wrapper input[type=file]').bind('change focus click', SITE.fileInputs);
		 
		 
	/* 	IE PIE
		-------------------------------------------------------------*/
		if (window.PIE) {
			$(".view_all, .sort_bar a.active, .apply").each(function() {
				PIE.attach(this);
			});
		}
		
		
	/* 	IE Selector Support
		-------------------------------------------------------------*/
		if ( ($.browser.msie) && ($.browser.version <= '8.0') ){
			$('#skip_list li:last-child').css('margin', '0');
			$('#careers .entry:last-child').css('background', 'none');	
		}
		
		
		
	
//END DOCUMENT READY	
}); 
