/* 
 * Created by Synergema for Legacy Planning Partners
 * June 2011
 * Authors: Michael Reiner & Chad Clark / Synergema
 * http://legacy-online.com
 * http://synergema.com
 *
 */
;(function(){
	
	// Mobile detection
	var mobile =
	(
		navigator.userAgent.match(/Android/i) ||
		navigator.userAgent.match(/webOS/i) ||
		navigator.userAgent.match(/iPhone/i) ||
		navigator.userAgent.match(/iPod/i) ||
		navigator.userAgent.match(/iPad/i)
	);
	
	// Nav
	if (mobile) // We have to use .live events instead of hoverIntent
	{
		$('#nav li').live('mouseenter', function() {
			$(this).addClass('active');
			$('.panel', this).animate({opacity: 'show', top: '45'}, 250);
		}).live('mouseleave', function() {
			$(this).removeClass('active');
			$('.panel', this).animate({opacity: 'hide', top: '40'}, 100);	
		});
	}
	else
	{
		$('#nav li').hoverIntent(function() {
			$(this).addClass('active');
			$('.panel', this).animate({opacity: 'show', top: '45'}, 250);
		}, function() {
			$(this).removeClass('active');
			$('.panel', this).animate({opacity: 'hide', top: '40'}, 100);	
		});
	};

	// Nivo slider
    $('#hero .inner').nivoSlider({
        effect:'fold', // Specify sets like: 'fold,fade,sliceDown'
        //slices:15, // For slice animations
        //boxCols: 8, // For box animations
        //boxRows: 4, // For box animations
        animSpeed:800, // Slide transition speed
        pauseTime:8000, // How long each slide will show
        startSlide:0, // Set starting Slide (0 index)
        directionNav:true, // Next & Prev navigation
        directionNavHide:true, // Only show on hover
        controlNav:true, // 1,2,3... navigation
        controlNavThumbs:false, // Use thumbnails for Control Nav
        controlNavThumbsFromRel:false, // Use image rel for thumbs
        controlNavThumbsSearch: '.jpg', // Replace this with...
        controlNavThumbsReplace: '_thumb.jpg', // ...this in thumb Image src
        keyboardNav:true, // Use left & right arrows
        pauseOnHover:true, // Stop animation while hovering
        manualAdvance:false, // Force manual transitions
        captionOpacity:0.8, // Universal caption opacity
        prevText: 'Prev', // Prev directionNav text
        nextText: 'Next', // Next directionNav text
        beforeChange: function(){}, // Triggers before a slide transition
        afterChange: function(){}, // Triggers after a slide transition
        slideshowEnd: function(){}, // Triggers after all slides have been shown
        lastSlide: function(){}, // Triggers when last slide is shown
        afterLoad: function(){} // Triggers when slider has loaded
    });
	
	// Block-level links
	$('.block-link').click(function(){
		link = $(this).find('a').eq(0).attr('href');
		if ( link != undefined )
		{
			window.location = link;
		}
	});
	
	// Breadcrumbs: remove link from last crumb
	if ( $('#breadcrumbs').length )
	{
		$('#breadcrumbs a:last').replaceWith( function() {
		   return '<span>'+$(this).text()+'</span>';
		});		
	}
	
	// Toggle ellipsed text	
	$('.ellipse.toggle.more').click(function(e){
		e.preventDefault();
		// Hide the ellipse element
		$(this).parent('.ellipse').find('.ellipsis_text:first').slideToggle('fast');
		// Toggle the main text element
		//$(this).parent('.ellipse').find('.full_text').eq(0).slideToggle('fast');
		$(this).parent('.ellipse').find('.full_text:first').slideToggle('fast');
		//$(this).prev('.full_text').slideToggle('fast');		
		// Toggle link text
		var text = $(this).text();
		$(this).text( text == 'Show More' ? 'Show Less' : 'Show More' );
	});

	// Input Hints
	//$('input[title]').inputHints();
	
	// Add .last to all Paragraphs. Oh, IE
	$('#page-content p:last-child').addClass('last');
	
	// Encoded email address to help prevent spam
	//$('.mailto').deobfuscate();
	
	// Live Form Validation
	$('.validate-text').validate({
		expression: 'if (VAL) return true; else return false;',
		message: 'Required field'
	});
	$('.validate-email').validate({
		expression: 'if (VAL.match(/^[^\\W][a-zA-Z0-9\\_\\-\\.]+([a-zA-Z0-9\\_\\-\\.]+)*\\@[a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+)*\\.[a-zA-Z]{2,4}$/)) return true; else return false;',
		message: 'Valid email required'
	});	
	
})(jQuery);
