(function() {
	$.fn.regulate = function() {

		var maxHeight = $(document).height();

		$(this).children().each(function() {
			$(this).css({"height":maxHeight});
		});
	};
	}());

	(function() {
		$.fn.dropit = function() {

			var $this = $(this);

			$this.find('li ul li.current-menu-item').parent().parent().show();

			var clicker = $this.find('li:has(h2)');

			clicker.each(function() {

				var $this = $(this);

				if( $this.next('li').is(':hidden') ) { 
					$this.next('li').addClass('hidden');
					$this.css({"borderBottom":"2px solid #787878"}).find('.arrow').show();
				} else {
					$this.next('li').find('ul li:first-child a').css({"marginTop":"2px"});
				}

				$this.click(function(e) { 

					e.preventDefault();

					var $this = $(this);

					$this.next('li').stop(true, true).animate({height:'toggle'}, 300, 
					function() {

						var $this = $(this);

						if( $this.is(':hidden')) {
							$this.prev('li').css({"borderBottom":"2px solid #787878"}).end().prev('li').find('.arrow').show();
							$this.find('li:first-child a').css({"marginTop":"0px"});
							$this.addClass('hidden');
						} else {
							$this.prev('li').css({"borderBottom":"0px"}).end().prev('li').find('.arrow').hide();
							$this.find('li:first-child a').css({"marginTop":"2px"});
							$this.removeClass('hidden');
							$this.removeAttr('style');

						}
						$('#page').regulate();
						return false;
					});
				});
			});
		};
		}());

		function height(curr, next, opts) {
			var slideHeight = $(this).height();
			$('#projectSlideshow').animate({height:slideHeight + 'px'}, 500);
		}
		function count(curr, next, opts) { 
			var caption = '<a class="count">'+ (opts.currSlide + 1) + ' / ' + opts.slideCount + '</a>';
			$('#projectCount').html(caption);
			var foo = $('#projectSlideshow').height();
			$('#page').children().each(function() {
				$(this).animate({height:foo+560}, 500);
			});
		}


		// Window Load
		$(window).load(function() {

			// if ( window.location.hash ) {
			// 	var destination = $( window.location.hash ).offset().top;
			// 	$('html:not(:animated),body:not(:animated)').scrollTop( destination );
			// }

			var $wall = $('#boxWrapper');

			// MASONRY
			$wall.masonry({
				columnWidth: 240,
				// resizeable: false,
				// singleMode: false,			    
				itemSelector: '.box'
				// animate: true
				// animationOptions: {
				// 	duration: 50,
				// 	easing: 'linear',
				// 	queue: false,
				// 	complete: function() {
				// 		$('.box').jrumble({ rumbleSpeed: 175 });
				// 	}
				// 
				// }
			}, 
			
			function(){
				$wall.fadeTo(0, 1);
			});

			// JRUMBLE
			// $('.box').jrumble({
			// 	rumbleSpeed: 175
			// });
			
			// // INFINITE SCROLL
			// $wall.infinitescroll({
			// 	navSelector  : '#postsNav',  // selector for the paged navigation 
			// 	nextSelector : '#postsNav a',  // selector for the NEXT link (to page 2)
			// 	itemSelector : '.box',     // selector for all items you'll retrieve
			// 	loadingImg : '/wp-content/themes/vvvvv/images/loader.gif',
			// 	loadingText : "Loading",
			// 	donetext : 'No more.',
			// 	bufferPx : '30',
			// 	animate: true,
			// 	errorCallback: function() { 
			// 		// fade out the error message after 2 seconds
			// 		$('#infscr-loading').animate({opacity: .8},2000).fadeOut('normal');   
			// 	}
			// },
			//   // call MASONRY & RUMBLE as a callback
			//   function( newElements ) { 
			//     setTimeout( function(){$wall.masonry({ appendedContent: $( newElements ) });}, 1000);  
			// 	// setTimeout( function(){$('.box').jrumble({ rumbleSpeed: 175 });}, 1000 );
			//   }
			// );


			var slideshow = $('#projectSlideshow');
			var slideHeight = slideshow.find('img:first-child').height();
			slideshow.stop(true, true).css({'height':slideHeight + 'px'});

			slideshow.cycle({ 
				fx: 'fade', 
				speed: 500, 
				timeout: 0, 
				next: '.next, #projectSlideshow', 
				prev: '.prev',
				before: height,
				after: count
			});

			$('#page').regulate();
		});

		// Window Resize
		$(window).resize(function() { $('#page').regulate(); });

		// Document Ready
		$(document).ready(function() {
			$('#nav').dropit();
			$('#projectSlideshow').find('img:first-child').fadeTo(0, 1);
			
		});
