

var periodHasDropped = false;

$( function() {
	
/*
	$('.page-content img').lazyload({ 
		placeholder : "http://www.walmink.com/wp-content/themes/portfolio/script/js/img/grey.gif",
		effect : "fadeIn",
		event : "hover"
	});
*/

	$(".color-box-iframe").colorbox( { iframe:true, width: "90%", height: "90%" } );
	
	$(".color-box").colorbox( {photo:true} );
	
	$(".single-page a").has("img").addClass("inline").addClass("no-shadow");
	
	$('#selection_end').css( "backgroundColor", $('.menu a.selected').css( "backgroundColor" ) );
	

	$('.page-banner').wrap( function() {
		return '<span style="background-image:url(' + $(this).attr('src') + '); height: '+ $(this).attr('offsetHeight') + 'px; width: '+ $(this).attr('offsetWidth') + 'px;" class="rounded" />';
	});

	
	
	
	
	$( '.hoverFade' ).hover
	(
		function() { $(this).animate( { color: '#AAA' }, 350 ) },
		function() { $(this).animate( { color: '#444' }, 350 ) }
	); 
	
	
	var offsetTop = $( '.menu a.selected' ).attr('offsetTop');
	if( offsetTop ) {
		$('#selection_end').css( 'margin-top', offsetTop - parseInt($("#selection_end_shadow").css('top'))  ); 
		$('#selection_end_shadow').addClass( 'active' );
		$('#selection_end').css( 'width', "40px" );
	}
	
	
	$( '.menu a' ).click
	(
		function() { 
			$(this).parents('.menu').find( 'a' ).removeClass( 'selected' );
			$(this).parents('.menu').find( 'a' ).removeClass( 'extended' );
			
			$('#header').addClass( 'selected', 250 );
			
			$('.menu a').removeClass( 'selected' );
			$('.menu a').removeClass( 'extended' );
			
			$('.loader').fadeIn();
			
			
			$(this).addClass( 'selected' );
			$(this).addClass( 'extended', 300, 'easeInQuart' );
			
			var offsetTop = $(this).attr('offsetTop');
			
			
			$('#selection_end').css( 'margin-top', offsetTop - parseInt($("#selection_end_shadow").css('top'))  ); 
			//$('#selection_end_shadow').removeClass( 'active' );
			//$('#selection_end_shadow').delay(400).addClass( 'active', 200 ); 
			$('#selection_end').css( 'width', 0 );
			
			$('#selection_end').css( "backgroundColor", $(this).css( "backgroundColor" ) );
			$('#selection_end').delay(500).animate( { width: 40 }, 200, 'easeOutQuart' ); 
		}
	);
	
	
	$('#selection_end').mouseenter
	(
		function(event) {
			$('#selection_end').html(">&nbsp;");
		}
	);
	
	$('#selection_end').mouseleave
	(
		function(event) {
			$('#selection_end').html("");
		}
	);
	

	
	$('#selection_end').mousedown( pullSelection );
	
	
	$('.menu a.extended').mousedown( pullSelection );
	
	
	$('#period').click
	(
		function(event) {
			
			if( !periodHasDropped ) {
				
				var rangeX = 20;
				var randomX = (Math.random() - 0.5) * 2*rangeX;
				
				var bottomOffset = $('body').attr('offsetHeight') - ( $(this).attr('top') + $(this).attr('height') );
				
				alert( $(this).css('top') );
				
				$(this).css( { position: 'fixed', top: '', bottom: bottomOffset } );
				$(this).animate( { bottom: -34 }, { duration: 1000, queue: false, easing: 'easeOutBounce' } );
				$(this).delay(500).animate( { marginLeft: randomX },{ duration: 750, easing: 'easeOutSine' } );
				
				periodHasDropped = true;
			}
			else {
				
				var origX = $(this).attr('offsetTop');
				
				$(this).css( 'position', 'relative' );
				$(this).css( { top: origX, bottom: '' } );
				$(this).animate( { top: 0, marginLeft: 0 }, { duration: 1000, queue: false, easing: 'easeOutElastic' } );
				
				periodHasDropped = false;
			}
		}
	);
	
	

	//$( '.page-content' ).hide();
	
	$('.readMore').click
	(
	 	function(event) {
			
			var pageContent = $(this).siblings( '.page-content' )
			
			pageContent.css({'position':'absolute','visibility':'hidden','display':'block'});
			var height = pageContent.attr('offsetHeight');
			pageContent.css({'position':'static','visibility':'visible','display':'none'});

	 		pageContent.slideDown( 2*height );
			
			pageContent.find( "img" ).trigger( "hover" );
			
			$(this).fadeOut( 150 );
			
			
		}
	);
	
	}
	
	
);


function pullSelection(event) {
		
			// prevent Firefox dragging etc.
			event.preventDefault();
		
			// get base offset of selection end
			var shadowOffset = parseFloat( $('#selection_end_shadow').css( 'left' ) );
			var selectionOffset = parseFloat( $('#selection_end').css( 'marginLeft' ) );
			var relMouseX = event.pageX - shadowOffset - selectionOffset;
			
			$('body').mousemove( function(event) {
				var offset = event.pageX - shadowOffset - relMouseX;						  
				if( offset > 0 && offset < 400 ) {					  
					$('#selection_end').css('paddingLeft', offset);
					$('.menu a.selected').css('marginLeft', offset);
				}
			} );
			
			$('body').mouseleave( releaseSelection );
										   
			
			$('body').mouseup( releaseSelection );
		}
	
function releaseSelection(event) {
	
	$('body').unbind( 'mousemove' );
	$('body').unbind( 'mouseleave' );
	$('body').unbind( 'mouseup' );
	
	
	$('#selection_end').animate( { paddingLeft: 0 }, 500, 'easeOutBounce' );
	$('.menu a.selected').animate( { marginLeft: 0 }, 500, 'easeOutBounce' );
}

