$(document).ready(function() {
	
	// Main Nav
	$('#nav ul').hide();
	$('#nav li').hoverIntent(function() {
		$(this).addClass('hovered');
		$('ul', this).animate({
			height: ['toggle', 'swing']
		}, 300);
	}, function() {
		$(this).removeClass('hovered');
		$('ul', this).animate({
			height: ['toggle', 'swing']
		}, 100);
	});
	
	// Homepage Tabs
	$('#tabs-home .tabs').tabs('.panes > div', {
		effect: 'fade'
	});
			
	// Overlay
	$('div.overlay').appendTo('body');
	$("a.launchoverlay[rel]").overlay({
		expose: 'black'
	});
	 $('a.prev, a.next').click(function() {
	 	return false;
	 });

	
	//Scrollable
	
	$('#flowpanes').scrollable({size: 1, clickable: false}).navigator({
		navi: '#flowtabs',
		naviItem: 'a',
		activeClass: 'current'
	});
	
	$('#flowpanes2').scrollable({size: 1, clickable: false}).navigator({
		navi: '#flowtabs2',
		naviItem: 'a',
		activeClass: 'current'
	});
	
	//Initialize forms accordion
	$('div#forms_accordion').accordion({ active: false });
	//Check URI for #success hash
	var theHash = location.hash;
	if ( theHash == '#success' )
	{
		//Show success message
		$('#msg_thanks').fadeIn('slow');
	}
	//Close on click
	$('a.closeme').click(function() {
		$(this).parent().fadeOut('normal');
	});
	
	
	
	// open pdf files in new window
	$("a[href$=pdf]").each(function() {
		$(this).attr('target', '_blank');
	});
	
	jQuery.fn.fadeIn = function(speed, callback) { 
	    return this.animate({opacity: 'show'}, speed, function() { 
	        if (jQuery.browser.msie)  
	            this.style.removeAttribute('filter');  
	        if (jQuery.isFunction(callback)) 
	            callback();  
	    }); 
	}; 
	 
	jQuery.fn.fadeOut = function(speed, callback) { 
	    return this.animate({opacity: 'hide'}, speed, function() { 
	        if (jQuery.browser.msie)  
	            this.style.removeAttribute('filter');  
	        if (jQuery.isFunction(callback)) 
	            callback();  
	    }); 
	}; 
	 
	jQuery.fn.fadeTo = function(speed,to,callback) { 
	    return this.animate({opacity: to}, speed, function() { 
	        if (to == 1 && jQuery.browser.msie)  
	            this.style.removeAttribute('filter');  
	        if (jQuery.isFunction(callback)) 
	            callback();  
	    }); 
	};
	
	
	// smooth scroll
	function filterPath(string) {
	return string
	.replace(/^\//,'')
	.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
	.replace(/\/$/,'');
	}
	var locationPath = filterPath(location.pathname);
	var scrollElem = scrollableElement('html', 'body');
	
	$('a[href*=#]').each(function() {
	var thisPath = filterPath(this.pathname) || locationPath;
	if (  locationPath == thisPath
	&& (location.hostname == this.hostname || !this.hostname)
	&& this.hash.replace(/#/,'') ) {
	  var $target = $(this.hash), target = this.hash;
	  if (target) {
	    var targetOffset = $target.offset().top;
	    $(this).click(function(event) {
	      event.preventDefault();
	      $(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
	        location.hash = target;
	      });
	    });
	  }
	}
	});
	
	// use the first element that is "scrollable"
	function scrollableElement(els) {
	for (var i = 0, argLength = arguments.length; i <argLength; i++) {
	  var el = arguments[i],
	      $scrollElement = $(el);
	  if ($scrollElement.scrollTop()> 0) {
	    return el;
	  } else {
	    $scrollElement.scrollTop(1);
	    var isScrollable = $scrollElement.scrollTop()> 0;
	    $scrollElement.scrollTop(0);
	    if (isScrollable) {
	      return el;
	    }
	  }
	}
	return [];
	}
	
});
