$(function() { 
	hoverIntentConfig();
	bgImageSize();
	sectionTabs();
	bioPics();
	clearFormFields();
});

function dropDownsOver() {
	$(this).find(".sub").stop().show().fadeTo(300, 1); //Find sub and fade it in
	$(function() {
	   //Function to calculate total width of all ul's
	   function calcSubWidth() {
	       rowWidth = 0;
	       //Calculate row
	       $(this).find("ul").each(function() { //for each ul...
	           rowWidth += $(this).width(); //Add each ul's width together
	       });
	   };
	}); 
	
	if ( $(this).find(".row").length > 0 ) { //If row exists...
	
	   var biggestRow = 0;	
	
	   $(this).find(".row").each(function() {	//for each row...
	       $(this).calcSubWidth(); //Call function to calculate width of all ul's
	       //Find biggest row
	       if(rowWidth > biggestRow) {
	           biggestRow = rowWidth;
	       }
	   });
	
	   $(this).find(".sub").css({'width' :biggestRow}); //Set width
	   $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin
	
	} else { //If row does not exist...
	
	   //$(this).calcSubWidth();  //Call function to calculate width of all ul's
	   //$(this).find(".sub").css({'width' : rowWidth}); //Set Width
	
	}
}

function dropDownsOut(){
  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
      $(this).hide();  //after fading, hide it
  });
}
function hoverIntentConfig() {
	var config = {
	     sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)
	     interval: 50, // number = milliseconds for onMouseOver polling interval
	     over: dropDownsOver, // function = onMouseOver callback (REQUIRED)
	     timeout: 0, // number = milliseconds delay before onMouseOut
	     out: dropDownsOut // function = onMouseOut callback (REQUIRED)
	};
	$("ul.mainnav li").hoverIntent(config); //Trigger Hover intent with custom configurations
}

function windowScroll() {
	$(window).scroll(function() {
	  $('#bgImage').css('bottom', '0px');
	});
}
function bgImageSize() {
	if (self.innerWidth) {
	      myInnerWidth = self.innerWidth;
	      myInnerHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientWidth) {
	      myInnerWidth = document.documentElement.clientWidth;
	      myInnerHeight = document.documentElement.clientHeight;
	}
	else if (document.body) {
	      myInnerWidth = document.body.clientWidth;
	      myInnerHeight = document.body.clientHeight;
	}
	else {
	      return;
	}
	
	var scrollerWidth = getScrollerWidth();
	
	if($.browser.msie){

	}else{
		myInnerWidth = myInnerWidth - scrollerWidth;
	}
	// Check if window width is greater than the image width (1193) and resize if this is true
	if (myInnerWidth > 1280) {
		$('#bgImage').css('width', myInnerWidth);
	}else{
		$('#bgImage').css('width', 1280);
	}
	// Resize image when window is resized
	$(window).resize(function() {
		if (self.innerWidth) {
		        myInnerWidth = self.innerWidth;
		        myInnerHeight = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientWidth) {
		        myInnerWidth = document.documentElement.clientWidth;
		        myInnerHeight = document.documentElement.clientHeight;
		}
		else if (document.body) {
		        myInnerWidth = document.body.clientWidth;
		        myInnerHeight = document.body.clientHeight;
		}
		else {
		        return;
		}	
		// Check if window width is greater than the image width (1193) and resize if this is true		
		if (myInnerWidth > 1280) {
			$('#bgImage').css('width', myInnerWidth);
		}else{
			$('#bgImage').css('width', 1280);
		}
		$('#bgImageWrap').css({
			'width': myInnerWidth,
			'height': myInnerHeight
		});
	});
	
	if (myInnerWidth > 1000) {
		$('body').css('overflow-x', 'hidden');
	}
	
	$('#bgImageWrap').css({
		'width': myInnerWidth,
		'height': myInnerHeight
	});
	
	$(window).scroll(function() {
		var scrollTop = $(window).scrollTop();
		newHeight = myInnerHeight + scrollTop;
		$('#bgImageWrap').css({
			'height': newHeight
		});
	});
}

function sectionTabs() {
	$(".aboutNav").tabs(".aboutContent > div", {effect: 'fade', current: 'active', history:true}).history();
	$(".servicesNav").tabs(".servicesContent > div", {effect: 'fade', current: 'active', history:true}).history(); 
	$(".teamNav").tabs(".teamContent > div", {current: 'active', history:true}); 
	$(".legalNav").tabs(".legalContent > div", {effect: 'fade',current: 'active', history:true}); 
}

function bioPics() {
	function nameOver() {
		if ($('.bioPic').css('display') == 'none'){
			$('.bioPic').fadeIn(300);
		};
		var newSrc = $(this).attr('rel');
		$('.bioPic img').attr('src', newSrc);
		var newAlt = $(this).attr('title');
		$('.bioPic img').attr('alt', newAlt);
	}
	function nameOut() {
		$('.bioPic').fadeOut(300);
		$('.bioPic').css('display', 'none');
		$('.bioPic img').attr('src', '');
	}
	var hoverConfig = {    
	     sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
	     interval: 100, // number = milliseconds for onMouseOver polling interval    
	     over: nameOver, // function = onMouseOver callback (REQUIRED)    
	     timeout: 100, // number = milliseconds delay before onMouseOut    
	     out: nameOut // function = onMouseOut callback (REQUIRED)    
	};
	$('.bioPic').css('display', 'none');
	$('.teamList a').hoverIntent(hoverConfig);
}
function getScrollerWidth() {
    var scr = null;
    var inn = null;
    var wNoScroll = 0;
    var wScroll = 0;

    // Outer scrolling div
    scr = document.createElement('div');
    scr.style.position = 'absolute';
    scr.style.top = '-1000px';
    scr.style.left = '-1000px';
    scr.style.width = '100px';
    scr.style.height = '50px';
    // Start with no scrollbar
    scr.style.overflow = 'hidden';

    // Inner content div
    inn = document.createElement('div');
    inn.style.width = '100%';
    inn.style.height = '200px';

    // Put the inner div in the scrolling div
    scr.appendChild(inn);
    // Append the scrolling div to the doc
    document.body.appendChild(scr);

    // Width of the inner div sans scrollbar
    wNoScroll = inn.offsetWidth;
    // Add the scrollbar
    scr.style.overflow = 'auto';
    // Width of the inner div width scrollbar
    wScroll = inn.offsetWidth;

    // Remove the scrolling div from the doc
    document.body.removeChild(
        document.body.lastChild);

    // Pixel width of the scroller
    return (wNoScroll - wScroll);
}

function clearFormFields() {

	$('input').click(function() {
		inputValue = $(this).val();
		if (inputValue == 'Name' || inputValue == 'Email' || inputValue == 'Search') {
			$(this).val('');
		}else{
			$(this).select();
		}
	});
	$('input').blur(function() {
		if($(this).val() == '') {
			$(this).val(inputValue);
		}
	});
	$('textarea').click(function() {
		textAreaVal = $(this).val();
		if ($(this).val() == 'Message') {
			$(this).val('');
		}else{
			$(this).select();
		}
	});
	$('textarea').blur(function() {
		if($(this).val() == '') {
			$(this).val(textAreaVal);
		}
	});
}

