/**
 *	fires when the dom is ready
 *
 */
$(document).ready(function() {
	blockLink();
	externalLinks();
	autoLightBox();
	sliderPrints();
	sliderPress();
	frontSplash();
	footer();
	workSplash();
	calenderItemHeight();
});

function frontSplash() {

    if ( $('#splash').length == 0 )
        return false;
   
   var win_w = $(window).width();
	
    $('#splash').click( function() {
       $('#splash').stop().fadeOut(300); 
    });
	
	if ( win_w < 1600 ) {
	
		// resize image
		var img_w = $('#splash img').width();
		var img_h = $('#splash img').height();
		
		// new height
		var img_h_new = (win_w / img_w) * img_h;
		
		$('#splash img').css( {
			height 	: img_h_new,
			width	: win_w // fit to window
		});
		
		$('#splash').show().delay(2000).fadeOut(1000); 
	}
}

/**
 * wordpress theme url
 *
 */
function wpUrl() {
	return $('link[href$="normalize.css"]').attr('href').replace('/assets/css/normalize.css','');
}

/**
 *	Adds a link/hover to it's parent
 *
 */
function blockLink() {
	// click
	var elements = '.blocklink';	
	$(elements).each(function(){
		var thelink = $(this).attr('href');		
		var theDiv 	= $(this).parent();		
		if ((thelink != '')&&(thelink != '#')) {
			theDiv.css({'cursor':'hand','cursor':'pointer'})			
			.click(function(){
				window.location.href = thelink;			
			});
		}		
	});
	
	// hover
	var elements = '.blockhover';
	$(elements).each(function(){			
		var theDiv 	= $(this).parent();
		if ($(this).hasClass('blockhover')) {			
			theDiv.hover( function(){
				theDiv.addClass('active')
			}, function() { 
				theDiv.removeClass('active') 
			});
		}
	});	
};

/**
 * External Link; New-Window Links in a Standards-Compliant W3C
 */
function externalLinks() {	
	$("a[rel*=external]").each(function(i){
		this.target="_blank";
	});
}

/**
 *	Adds lightbox to post images that are wrapped in a imagelink
 *
 */
function autoLightBox() {	
	var include = '.entry img';
	var exclude = '.entry .gallery img';
	
	// add auto lightbox
	$(include).not(exclude).each( function() {		
		var title 		= $(this).attr('title');
		var link 		= $(this).parents('a');		
		if( link ) {
			var href 	= link.attr('href');
			if ( href ) {
				if ( href.match(/\.(jpg|png|gif|bmp)/) ) {
					link.fancybox({
						'opacity'			: true,
						'href'				: href,
						'overlayColor'		: '#000',
						'hideOnContentClick': true,
						'overlayOpacity'	: 0.7,
						'centerOnScroll'	: true				
					});	
				}
			}
		}
	});	
}

/**
 *	animated Slider
 *
 */
function sliderPrints() {
	var slider 	 	= $('#slider .slides');
	var nextprev 	= $('#slider .control');
	var slidecount 	= $('li',slider).length;
	
	if ( slidecount > 0 ) {
	
		// slider
		slider.cycle({ 
			fx:     'fade', 
			speed:  1500,
			timeout:6000, 
			pause: 	1,
			prev:   '#control_prev', 
			next:   '#control_next', 	
			pager: 	'#controls',
			pagerAnchorBuilder: pagerFactory 
		});			
		
		// on hover
		$('#slider').hover(
			function() { 
				nextprev.show(); 			
			},
			function() { 
				nextprev.hide(); 
			}		
		);
	}
	
	// display pagers
	function pagerFactory(idx, slide) {	
		if (slidecount < 2 ) 
			return false;
		return '<li><a href="#">'+(idx+1)+'</a></li>';
	};
}

/**
 *	animated Slider
 *
 */
function sliderPress() {
	var slider 	 	= $('#press_slider .slides');
	var nextprev 	= $('#press_slider .control');
	var slidecount 	= $('li',slider).length;
	
	// Using default configuration
	$("#press_slides").carouFredSel({
		items				: 4,
		scroll 	: {
			items			: 1,	
			duration		: 1000,							
			pauseOnHover	: true
		},
		prev	: {
			button			: '#press_control_prev'
		},
		next	: {
			button			: '#press_control_next'
		}
	});

}

/**
 *	footer
 *
 */
function footer() {
	// footer resizing
	var el = $('#footer_spacer');
	if ( el.length > 0 ) {
		var h = $(document).height();
		var p = el.position();
		var new_h = h - p.top;
		el.css('height', new_h);
	}
}

/**
 *	work Splash
 *
 */
function workSplash() {
	
	var el = $('#work_splash');
	
	// fade
	if ( el.length == 0 )
        return false;
    
    el.click( function() {
       el.stop().fadeOut(300); 
    });    
    el.delay(2000).fadeOut(1000); 
	
	// size
	var h = $(document).height();
	var w = $(document).width();
	var p = el.position();
	var new_h = h - p.top;
	
	el.css({
		height: new_h,
		width: w
	});
	
}

/**
 *	calender item height
 *
 */
function calenderItemHeight() {

	var maxHeight = Math.max.apply(null, $("#calendar .item").map(function() {
		var w = $(this).outerHeight();
		return w;
	}).get());

	$('#calendar .item').height(maxHeight);	
}
