jQuery(function(){

	//Home page slider
	if ($('#slider').length) {
		$('#slider').cycle();
	};
	
	//Mark active page
    var url = window.location.pathname, 
        urlRegExp = new RegExp(url.replace(/\/$/,'') + "$");
        $('#topNav a').each(function(){
            if(urlRegExp.test(this.href.replace(/\/$/,''))){
                $(this).parents('li').addClass('active');
            }
        });
		
	//Hover menu
	$('#topNav ul').css({opacity: '0.9'}); //sets opacity for older browsers
    var config = {    
         sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
         interval: 100,  // number = milliseconds for onMouseOver polling interval    
         over: doOpen,   // function = onMouseOver callback (REQUIRED)    
         timeout: 300,   // number = milliseconds delay before onMouseOut    
         out: doClose    // function = onMouseOut callback (REQUIRED)    
    };
    
    function doOpen() {
		$('#slider').cycle('pause'); //stops slider on home page changing while menu is showing
        $(this).addClass("hover");
		$('#contentArea, .banner').stop().animate({opacity:0.5},200);
        $('ul:first',this).css('visibility', 'visible');
    }
 
    function doClose() {
		$('#slider').cycle('resume');
        $(this).removeClass("hover");
        $('ul:first',this).css('visibility', 'hidden');
		$('#contentArea, .banner').stop().animate({opacity:1},500);
    }

    $("#topNav li").hoverIntent(config);
    $("#topNav li a").click(function(e){
		e.preventDefault;
	});
	
	//Make sidebar use page height.
	$('#pageCopy p:last').addClass('last');
	$('#contentArea td#sideBar p').css('minHeight', ($('#pageCopy').height() - 25) + 'px');
	
	var pageFile = document.location.href;
	var content, heading, lastHeading, nextHeading, divider;
	pageFile = pageFile.substring(pageFile.lastIndexOf('/') + 1, pageFile.length)
	
	//About Us Divider.
	if(pageFile.indexOf("about-us.asp") != -1){
		
		if($('#pageCopy hr').length > 0){
			
			//Set up anchors.
			$('h2').each(function(){
				heading = $(this).text();
				heading = heading.replace(/ /g, '-');//Replaces " " with "-".
				$(this).attr('id', heading);
			});
			
			$('#pageCopy hr').each(function(index){
				lastHeading = $('h2:eq(' + index + ')').attr('id');
				nextHeading = $('h2:eq(' + (index + 1) +')').attr('id');
				divider = '<div class="divider"><!-- <a href="#' + nextHeading + '" class="down"></a><a href="#' + lastHeading + '" class="up"></a> --></div>';
				$(this).replaceWith(divider);
			});
			
			$('.divider').localScroll();
			
			$('.divider a.up').click(function(){
				return false;
			});
			
			$('.divider a.down').click(function(){
				return false;
			});
		}
		
	}
		
	//Setup forms:
	if($('form').length > 0){
	
		var divWidth, labelWidth;
		var paddingWidth = 25;
	
		$('form fieldset > div').each(function(index){

			if($(this).children('input').is('checkbox') == false && $(this).hasClass('submitBox') == false && $(this).children('input').is('radio') == false || $(this).children('input[type="text"]').length > 0){	
				divWidth = $(this).width();
				labelWidth = $(this).children('label').width();
				$(this).children('input').width(divWidth - labelWidth - paddingWidth);				
			}
			
			if($(this).children('textarea').length > 0){
				divWidth = $(this).width();
				labelWidth = $(this).children('label').width();
				$(this).children('textarea').width(divWidth - labelWidth - paddingWidth);	
			}
			
		});
			
	}
	

});
