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

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

/**
 *	Adds a link/hover to it's parent
 *
 */
function blockLink() {
	// click
	var elements = '.blocklink';	
	jQuery(elements).each(function(){
		var thelink = jQuery(this).attr('href');		
		var theDiv 	= jQuery(this).parent();		
		if ((thelink != '')&&(thelink != '#')) {
			theDiv.css({'cursor':'hand','cursor':'pointer'})			
			.click(function(){
				window.location.href = thelink;			
			});
		}		
	});
	
	// hover
	var elements = '.blockhover';
	jQuery(elements).each(function(){			
		var theDiv 	= jQuery(this).parent();
		if (jQuery(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() {	
	jQuery("a[rel*=external]").each(function(i){
		this.target="_blank";
	});
}

function addToTopMenu() {
    
    if ( jQuery('#top_nav li').length == 0 ) 
        return false;
    
    jQuery('#top_nav li').each( function(i, e) {
        
        jQuery(e).mouseenter( function() {
           var a_text = jQuery(e).find('a').first().text();

           jQuery(e).append('<div class="title">'+ a_text +'</div>');
        });
        
        jQuery(e).mouseleave( function() {
           jQuery(e).find('div').remove();
        });
        
    });
}

/**
 *	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
	jQuery(include).not(exclude).each( function() {		
		var title 		= jQuery(this).attr('title');
		var link 		= jQuery(this).parents('a');		
		if( title && link ) {
			var href 	= link.attr('href');
			if ( href ) {
				if (href.match(/\.(jpg|JPG|png|gif|bmp)/) ) {
					link.fancybox({
						'opacity'			: true,
						'href'				: href,
						'overlayColor'		: '#000',
						'hideOnContentClick': true,
						'overlayOpacity'	: 0.7,
						'centerOnScroll'	: true				
					});	
				}
			}
		}
	});	
}

/**
 *	Change Class from noJS to activeJS
 */
function modernizrActiveJS() {
	if (jQuery("body").hasClass("noJs")) {
		jQuery("body").removeClass("noJs").addClass("activeJs");
	}
}

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

