/**
 *	fires when the dom is ready
 *
 */
$(document).ready(function() {
	blockLink();
	externalLinks();
	autoLightBox();
	animatedScroll();
});

/**
 * 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() {	
    $(include).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 Scroll
 *
 */
function animatedScroll() {
	// list of exceptions
	var exceptions = 'respond'; // wordpress 'cancel reply' button 
	
	$('a[href*="/#"]').click( function(event){		
		var full_url 	= this.href;
		var parts 		= full_url.split("#");
		var trgt 		= parts[1];		
		
		console.log(trgt);
		
		// check if element exists
		if ( !$("#"+trgt).length )
			return;
		
		// check exceptions
		if ( exceptions.indexOf(trgt) != -1 ) 
			return;
		
		// element found, prevent click		
		event.preventDefault();	
		
		// get offset
		var target_offset 	= $("#"+trgt).offset();
		var target_top 		= target_offset.top;
		
		// animate
		$('html, body').animate({scrollTop:target_top}, 500);
	});
	
	// Back To Top
	$('.back_to_top').click(function(){
		$('html, body').animate({scrollTop:0}, 1000);
		return false;
	});
	
	// display message on scroll
	var scroll_timer;
	var displayed 	= false;
	var $message 	= $('.back_to_top');
	var $window 	= $(window);
	var top 		= $(document.body).children(0).position().top;
	
	$window.scroll(function () {
		window.clearTimeout(scroll_timer);
		scroll_timer = window.setTimeout(function () { 
			if($window.scrollTop() <= top) {
				displayed = false;
				$message.fadeOut(500);
			}
			else if(displayed == false) {
				displayed = true;
				$message.stop(true, true).show().click(function () { $message.fadeOut(500); });
			}
		}, 100);
	});
	
	// when user reaches page by hash
	if (window.location.hash) {
		$message.fadeIn(800);
	}	
}

