$(document).ready(function(){

/* code to clear input box of label when clicked in */
	$('.fieldToClear').each(function() {
	       var default_value = this.value;
	       $(this).focus(function(){
	               if(this.value == default_value) {
	                       this.value = '';
	               }
	       });
	
	       $(this).blur(function(){
	               if(this.value == '') {
	                       this.value = default_value;
	               }
	       });
	});



/* home page image rotation */
	$('#homePageSplash').cycle({ 
		timeout: 6000,
		speed:   850,
		fx: 'scrollLeft',
		pause: 1,
		fastOnEvent: 1 ,
		startingSlide: 0});
		
	$('.gotoPictureOne').click(function() { 
	    $('#homePageSplash').cycle(0); 
	    return false; }); 
	 
	$('.gotoPictureTwo').click(function() {  
	    $('#homePageSplash').cycle(1);  
	    return false; });
	    
	$('.gotoPictureThree').click(function() {  
	    $('#homePageSplash').cycle(2);  
	    return false; });
	    
 	$('.gotoPictureFour').click(function() { 
    $('#homePageSplash').cycle(3); 
	    return false; }); 
	 
		$('.gotoPictureFive').click(function() {  
	    $('#homePageSplash').cycle(4);  
	    return false; });
	    
 	$('.gotoPictureSix').click(function() { 
    $('#homePageSplash').cycle(5); 
	    return false; });  



/* Show Fliker CTA Image on Homepage */
	$(".flickrExpandCTA").hover(function(){
	    $(this).parent().parent().siblings('#flickrCTAimage').fadeIn();
	},function(){
	    $(this).parent().parent().siblings('#flickrCTAimage').fadeOut();
	});


// usage:
// $(elem).infinitescroll(options,[callback]);
 
// infinitescroll() is called on the element that surrounds 
// the items you will be loading more of
$('#infiniteScrollHolder').infinitescroll({
 
    navSelector  : "div.paginationNavigation",            
                   // selector for the paged navigation (it will be hidden)
    nextSelector : "div.paginationNavigation div.alignright a",    
                   // selector for the NEXT link (to page 2)
    itemSelector : ".thumbnailHolder",       
                   // selector for all items you'll retrieve
 
  debug        : false,                        
                 // enable debug messaging ( to console.log )
 
  loadingImg   : "http://tenfouragency.com/wp-content/themes/tenFour/ajax-loader.gif",          
                 // loading image.
                 // default: "http://www.infinite-scroll.com/loading.gif"
 
  loadingText  : "<em>...more on the way...</em>",      
                 // text accompanying loading image
                 // default: "<em>Loading the next set of posts...</em>"
 
  animate      : false,      
                 // boolean, if the page will do an animated scroll when new content loads
                 // default: false
 
  extraScrollPx: 0,      
                 // number of additonal pixels that the page will scroll 
                 // (in addition to the height of the loading div)
                 // animate must be true for this to matter
                 // default: 150
 
  donetext     : "<em>...the end...</em>" ,
                 // text displayed when all items have been retrieved
                 // default: "<em>Congratulations, you've reached the end of the internet.</em>"
 
  bufferPx     : 40,
                 // increase this number if you want infscroll to fire quicker
                 // (a high number means a user will not see the loading message)
                 // new in 1.2
                 // default: 40
 
  errorCallback: function(){},
                 // called when a requested page 404's or when there is no more content
                 // new in 1.2                   
 
  localMode    : false
                 // enable an overflow:auto box to have the same functionality
                 // demo: http://paulirish.com/demo/infscr
                 // instead of watching the entire window scrolling the element this plugin
                 //   was called on will be watched
                 // new in 1.2
                 // default: false
 
 
    },function(arrayOfNewElems){
    // optional callback when new content is successfully loaded in.
 
	// wrap each block of 22 items in a grid
 	var divs = $("#infiniteScrollHolder > .thumbnailHolder");
	for(var i = 0; i < divs.length; i+=22) {divs.slice(i, i+22).wrapAll("<div class='insideGridHolder'></div>");} 



	// if last block is not 22 items adjust height
	if ($('.insideGridHolder div:last-child').is('.boxOne, .boxTwo, .boxThree, .boxFour')) {
    	$('#infiniteScrollHolder .insideGridHolder:last').addClass('firstHeightAdjust');
	} else if ($('.insideGridHolder div:last-child').is('.boxFive, .boxSix' )) {
		$('#infiniteScrollHolder .insideGridHolder:last').addClass('secondHeightAdjust');
	} else if ($('.insideGridHolder div:last-child').is('.boxSeven, .boxEight, .boxNine, .boxTen, .boxEleven, .boxTwelve, .boxThirteen')) {
		$('#infiniteScrollHolder .insideGridHolder:last').addClass('thirdHeightAdjust');
	} else if ($('.insideGridHolder div:last-child').is('.boxFourteen, .boxFifteen, .boxSixteen' )) {
		$('#infiniteScrollHolder .insideGridHolder:last').addClass('fourthHeightAdjust');
	} else {
	}
	
		// Repeat Show Grid Title on Rollover so new content gets behavior 
	$(".thumbnailHolder").hover(function(){
	    $(this).find('.gridBoxOverlay').slideDown();
	},function(){
	    $(this).find('.gridBoxOverlay').slideUp();
	});
	

	
	
});


/* Show Grid Title on Rollover */
	$(".thumbnailHolder").hover(function(){
	    $(this).find('.gridBoxOverlay').slideDown();
	},function(){
	    $(this).find('.gridBoxOverlay').slideUp();
	});



/* split ul when has the class .twoColumnList */
	$('.featuredClientList').easyListSplitter({ colNumber: 3});


/* Changes to Future Form - http://mypocket-technologies.com/jquery/SelectBoxPlugin/ */
	$("#input_1_1").custSelectBox({wrappername:'.ginput_container',selectwidth:140});
	$(".gform_footer input").addClass('buttonSwap futureFormSubmit');


/* IMAGE SWAPS  example of usage: give img the .buttonswap class and make sure to get the on (mouse on) and off (mouse off) */

	$(".buttonSwap").hover(
	function()
	 {this.src = this.src.replace("_up","_down");},
	 function()
	 {this.src = this.src.replace("_down","_up");}
	);


/* Open links in Tweets in a new window */
	$('#tenFourTweet a[href^="http://"]')
	  .attr({
	    target: "_blank", 
	    title: "Opens in a new window"
	});

//stick the footer at the bottom of the page if we're on an iPad/iPhone due to viewport/page bugs in mobile webkit
if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')
{
	$('#footer').addClass('ipadFooter');
};





/* Fade images on rollover */
	$(".fadeImage").hover(function(){
	    $(this).stop().animate({"opacity": .4},200);
	},function(){
	    $(this).stop().animate({"opacity": 1},600);
	});


/* Open colorbox when link has class colorbox */
	$('a.colorbox').colorbox({maxWidth:"90%"});

/* Open colorbox for posse with an iframe */
	$('.possePictures a.colorbox').colorbox({iframe:true,innerWidth:468, innerHeight:210});

/* Open colorbox for posse with an iframe */
	$('.posseURLfromSingle').colorbox({iframe:true,innerWidth:468, innerHeight:210});


/* Open + Close Contact Box */
	$('#contactButton').click(function(){
		$('#contactButton').slideUp('fast');
		$('#contactPullout').delay(200).slideDown('slow');
	});
	
	$('.contactClose').click(function(){
		$('#contactPullout').slideUp('fast');
		$('#contactButton').delay(200).slideDown('fast');
	});



function dynamicEvents() {
	$(".buttonSwap").hover(
	function()
	 {this.src = this.src.replace("_up","_down");},
	 function()
	 {this.src = this.src.replace("_down","_up");}
	);
	
	$('input:hidden, textarea:hidden').attr('disabled','disabled');
	$('input:visible, textarea:visible').removeAttr('disabled');
	
	listenForFocus();

	
    $('input[type="text"]').addClass("idleField");  
    $('input[type="text"]').focus(function() {  
        $(this).removeClass("idleField").addClass("focusField");  
        if (this.value == this.defaultValue){  
            this.value = '';  
        }  
        if(this.value != this.defaultValue){  
            this.select();  
        }  
    });  
    $('input[type="text"]').blur(function() {  
        $(this).removeClass("focusField").addClass("idleField");  
        if ($.trim(this.value == '')){  
            this.value = (this.defaultValue ? this.defaultValue : '');  
        }  
    });  
}



//post slider
$.fn.postSlide = function() {

	//initialize required variables
	var prevURL = $('#currentPost a[rel="prev"]').attr('href');
	var current = $('#currentPost');
	var nextURL = $('#currentPost a[rel="next"]').attr('href');
		
	if (!$('#currentPost').data('init')) {
		$('#currentPost').data('init', true);
		//do setup stuff here
		$('#prevPost').empty().load($('#currentPost a[rel="prev"]').attr('href') + ' #currentPost > div', function() {
			$('#currentPost a[rel="next"]').removeClass('disabled');
		});
		$('#nextPost').empty().load($('#currentPost a[rel="next"]').attr('href') + ' #currentPost > div', function() {
			$('#currentPost a[rel="prev"]').removeClass('disabled');
		});

		return this;
	}

	$('#currentPost, #currentBlog').data('focused', false);

	//going forward
	if ($(this).is('[rel="next"]')) {
				
		//empty the previous post
		$('#prevPost').empty();
		
		//slide out & empty the current post
		$('#currentPost').animate({
			'right' : '0',
			'left' : '-872px'
		}, 'normal', function() {
			$(this).css('left','872px').html($('#nextPost').html()).animate({
				'left':'0'
			}, 'normal', function() {
				$('.workPrevNext a').addClass('disabled');
				
				$('#nextPost').empty().load($('#currentPost a[rel="next"]').attr('href') + ' #currentPost > div', function() {
					$('#currentPost a[rel="next"]').removeClass('disabled');
				});
				$('#prevPost').empty().load($('#currentPost a[rel="prev"]').attr('href') + ' #currentPost > div', function() {
					$('#currentPost a[rel="prev"]').removeClass('disabled');
				});
			
				dynamicEvents();

			});
		
		});
		
	//going back
	} else if ($(this).is('[rel="prev"]')) {
		
		//empty the next post
		$('#nextPost').empty();
		
		//slide out & empty the current post
		$('#currentPost').animate({
			'right' : '0',
			'left' : '872px'
		}, 'normal', function() {
			$(this).css('left', '-872px').html($('#prevPost').html()).animate({
				'left':'0'
			}, 'normal', function() {
			
				$('.workPrevNext a').addClass('disabled');
			
				$('#nextPost').empty().load($('#currentPost a[rel="next"]').attr('href') + ' #currentPost > div', function() {
					$('#currentPost a[rel="next"]').removeClass('disabled');
				});
				$('#prevPost').empty().load($('#currentPost a[rel="prev"]').attr('href') + ' #currentPost > div', function() {
					$('#currentPost a[rel="prev"]').removeClass('disabled');
				});
				
				dynamicEvents();
				
			});
		
		});
		
	}
	
	prevURL == '';
	current == '';
	prevURL == '';
	
    return this; 
}


//blog slider
$.fn.blogSlide = function() {

	//initialize required variables
	var prevURL = $('#currentBlog a[rel="prev"]').attr('href');
	var current = $('#currentBlog');
	var nextURL = $('#currentBlog a[rel="next"]').attr('href');
		
	if (!$('#currentBlog').data('init')) {
		$('#currentBlog').data('init', true);
		//do setup stuff here
		$('#nextBlog').empty().load($('#currentBlog a[rel="next"]').attr('href') + ' #currentBlog > div', function() {
			$('#currentBlog a[rel="next"]').removeClass('disabled');
		});
		$('#prevBlog').empty().load($('#currentBlog a[rel="prev"]').attr('href') + ' #currentBlog > div', function() {
			$('#currentBlog a[rel="prev"]').removeClass('disabled');
		});

		return this;
	}
	
	$('#currentPost, #currentBlog').data('focused', false);
	
	//going forward
	if ($(this).is('[rel="next"]')) {
				
		//empty the previous post
		$('#prevBlog').empty();
		//slide out & empty the current post
		$('#currentBlog').animate({
			'right' : '0',
			'left' : '-872px'
		}, 'normal', function() {
			$(this).css('left','872px').html($('#nextBlog').html()).animate({
				'left':'0'
			}, 'normal', function() {
				$('.postNav a').addClass('disabled');
			
				$('#nextBlog').empty().load($('#currentBlog a[rel="next"]').attr('href') + ' #currentBlog > div', function() {
					$('#currentBlog a[rel="next"]').removeClass('disabled');
				});
				$('#prevBlog').empty().load($('#currentBlog a[rel="prev"]').attr('href') + ' #currentBlog > div', function() {
					$('#currentBlog a[rel="prev"]').removeClass('disabled');
				});
			
				dynamicEvents();

			});
		
		});
		
	//going back
	} else if ($(this).is('[rel="prev"]')) {

		//empty the next post
		$('#nextBlog').empty();
		
		//slide out & empty the current post
		$('#currentBlog').animate({
			'right' : '0',
			'left' : '872px'
		}, 'normal', function() {
			$(this).css('left', '-872px').html($('#prevBlog').html()).animate({
				'left':'0'
			}, 'normal', function() {
				$('.postNav a').addClass('disabled');
				$('#nextBlog').empty().load($('#currentBlog a[rel="next"]').attr('href') + ' #currentBlog > div', function() {
					$('#currentBlog a[rel="next"]').removeClass('disabled');
				});
				$('#prevBlog').empty().load($('#currentBlog a[rel="prev"]').attr('href') + ' #currentBlog > div', function() {
					$('#currentBlog a[rel="prev"]').removeClass('disabled');
				});
				
				dynamicEvents();
				
			});
		
		});
		
	}
	
	prevURL == '';
	current == '';
	prevURL == '';
	
    return this; 
}

//bind a focus listener to all inputs, text areas and select boxes. this will be used to prevent page sliding
function listenForFocus() {
	$('input, textarea, select').live('focus', function() {
		$(this).addClass('hasFocus');
	}).live('blur', function() {
		$(this).removeClass('hasFocus');
	});
}



	//event listeners for left and right page nav links
	$('#navPast, #navFuture').live('click touch', function(e) {
		e.preventDefault(); //prevent link from loading
		$(this).pageSlide(); //start page sliding
	});
	
	//event listeners for left and right post nav links
	$('.workPrevNext a').live('click touch', function(e) {
		e.preventDefault(); //prevent link from loading
		
		if ($(this).hasClass('disabled')) return;
		
		$(this).postSlide();	
	});
	
	$('.postNav a').live('click touch', function(e) {
		e.preventDefault(); //prevent link from loading
		
		if ($(this).hasClass('disabled')) return;
		
		$(this).blogSlide();	
	});
	

	
	//call postSlide once to setup
	$('html').postSlide();
	$('html').blogSlide();
	
	$('#currentPost, #currentBlog').live('click', function() {
		$(this).data('focused', true);
	})
	
	listenForFocus();
		
	//keyup event listener
	$(document).keyup(function(e) {
	
		var key = 0;
		if (e == null) {
			key = e.keyCode;
		} else { // mozilla
			key = e.which;
		}


		//check for right arrow key
		if (key == '39') {
		
			//check to make sure no form element has focus		
			if (($('.hasFocus').length) != 0) {
				return;
			}
			
			e.preventDefault(); //prevent normal behavior from a key click
			
			if ($('#currentPost').data('focused')) {
				$('#currentPost a[rel="next"]').click();
			} else if ($('#currentBlog').data('focused')) {
				$('#currentBlog a[rel="next"]').click();
			} else {
				$('#navFuture').click(); //trigger a click on the appropriate link
				
			}
		}
		
		//check for left arrow key
		if (key == '37') {
		
			//check to make sure no form element has focus		
			if (($('.hasFocus').length) != 0) {
				return;
			}
			
			e.preventDefault(); //prevent normal behavior from a key click

			if ($('#currentPost').data('focused')) {
				$('#currentPost a[rel="prev"]').click();
			} else if ($('#currentBlog').data('focused')) {
				$('#currentBlog a[rel="prev"]').click();
			} else {
				$('#navPast').click(); //trigger a click on the appropriate link
			}
		}
		
	});

	
});


