/* 
 * Global JS for General Dynamics
 * Phillip Salfity (psalfity@matrixgroup.net) 
 * 	-Equal Height Cloumns/Elements
 * 	-Random Image Generation
 *  -Auto Scrolling
 *  -Other Generic Methods
 */
(function($) {
	/***************************************************************  
	 * Equal Heights
 	 ****************************************************************/
	$.fn.equalHeights = function(minHeight, maxHeight) {
		tallest = (minHeight) ? minHeight : 0;
		this.each(function() {
			if($(this).height() > tallest) {
				tallest = $(this).height();
			}
		});
		if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
		return this.each(function() {
			$(this).height(tallest); //.css("overflow","auto")
		});
	}

	/***************************************************************
	* Random Image Generation
	****************************************************************/
	
	//EB Business Template Home Branding Area
	$.EB_HomeBrandingDealer = {
		//Set the plugin default values
		defaults: {
			//Set path to the images for a cleaner array
			path: '/images/template/',
			//Set up the array of images (this would be a good spot for a RESTful database to pull via javascript)
			myEB_HomeImages: [
				'eb_branding_home_a.jpg',
				'eb_branding_home_b.jpg',
				'eb_branding_home_c.jpg',
				'eb_branding_home_d.jpg',
				'eb_branding_home_e.jpg'
			],
			myEB_HomeAlt: [
				'Flooding the drydock',
				'Flooding the drydock at night',
				'Submarine and Tug',
				'Submarine under construction',
				'Submarine passing New London Lighthouse'
			]
		}
	}
	
	//EB Business Template About Branding Area
	$.EB_AboutBrandingDealer = {
		//Set the plugin default values
		defaults: {
			//Set path to the images for a cleaner array
			path: '/images/template/',
			//Set up the array of images (this would be a good spot for a RESTful database to pull via javascript)
			myEB_AboutImages: [
				'eb_branding_about_a.jpg',
				'eb_branding_about_b.jpg',
				'eb_branding_about_c.jpg',
				'eb_branding_about_d.jpg'
			],
			myEB_AboutAlt: [
				'World Class Welders',
				'USS Hartford Maintenance and Modernization',
				'Block III Payload Tube',
				'Design Build Process'
			]
		}
	}
	
	//EB Business Template Third Level Branding Area
	$.EB_ThirdLevelBrandingDealer = {
		//Set the plugin default values
		defaults: {
			//Set path to the images for a cleaner array
			path: '/images/template/',
			//Set up the array of images (this would be a good spot for a RESTful database to pull via javascript)
			myEB_ThirdLImages: [
				'eb_branding_l3_a.jpg',
				'eb_branding_l3_b.jpg',
				'eb_branding_l3_c.jpg',
				'eb_branding_l3_d.jpg'
			],
			myEB_ThirdLAlt: [
				'Submarine passing New London Lighthouse',
				'Electric Boat Quonset Point Facility',
				'Block III Payload Tube',
				'Electric Boat New London'
			]
		}
	}
	


	//Homepage Branding Area
	$.homeBrandingDealer = {
		//Set the plugin default values
		defaults: {
			//Set path to the images for a cleaner array
			path: '/images/template/',
			//Set up the array of images (this would be a good spot for a RESTful database to pull via javascript)
			myHomeImages: [
				'branding_home_a_c.jpg',
				'branding_home_a_d.jpg',
				'branding_home_c_a.jpg',
				'branding_home_c_c.jpg',
				'branding_home_c_f.jpg',
				'branding_home_c_g.jpg',
				'branding_home_i_c.jpg',
				'branding_home_m_c.jpg'
			],
			myHomeAlt: [
				'Aero C',
				'Aero D',
				'Combat A',
				'Combat B',
				'Combat C',
				'Combat F',
				'Combat G',
				'Axsys camera systems',
				'USS New Hampshire, Virginia-class submarine'
			]
		}
	}
	


/*
 *         EXTEND RANDOM IMAGE
 */	
	
	//EB Business Template Home Branding Area
	$.fn.extend({
		EB_HomeBrandingDealer:function(config) {
			var config = $.extend({}, $.EB_HomeBrandingDealer.defaults, config);
				return this.each(function() {
						var iNames = config.myEB_HomeImages;
						var iAlts = config.myEB_HomeAlt;
						var iNamesSize = iNames.length;
						var iDealer = Math.floor(Math.random()*iNamesSize);
						var iWinner = iNames[iDealer];
						var iAlt = iAlts[iDealer];
						var fullPath = config.path + iWinner;
						$(this).attr( {
							src: fullPath,
							alt: iAlt
						});
				});
		}
	});
	
	//EB Business Template About Branding Area
	$.fn.extend({
		EB_AboutBrandingDealer:function(config) {
			var config = $.extend({}, $.EB_AboutBrandingDealer.defaults, config);
				return this.each(function() {
						var iNames = config.myEB_AboutImages;
						var iAlts = config.myEB_AboutAlt;
						var iNamesSize = iNames.length;
						var iDealer = Math.floor(Math.random()*iNamesSize);
						var iWinner = iNames[iDealer];
						var iAlt = iAlts[iDealer];
						var fullPath = config.path + iWinner;
						$(this).attr( {
							src: fullPath,
							alt: iAlt
						});
				});
		}
	});
	
	//EB Business Template Third Level Branding Area
	$.fn.extend({
		EB_ThirdLevelBrandingDealer:function(config) {
			var config = $.extend({}, $.EB_ThirdLevelBrandingDealer.defaults, config);
				return this.each(function() {
						var iNames = config.myEB_ThirdLImages;
						var iAlts = config.myEB_ThirdLAlt;
						var iNamesSize = iNames.length;
						var iDealer = Math.floor(Math.random()*iNamesSize);
						var iWinner = iNames[iDealer];
						var iAlt = iAlts[iDealer];
						var fullPath = config.path + iWinner;
						$(this).attr( {
							src: fullPath,
							alt: iAlt
						});
				});
		}
	});
	


	//Homepage Branding Area
	$.fn.extend({
		homeBrandingDealer:function(config) {
			var config = $.extend({}, $.homeBrandingDealer.defaults, config);
				return this.each(function() {
						var iNames = config.myHomeImages;
						var iAlts = config.myHomeAlt;
						var iNamesSize = iNames.length;
						var iDealer = Math.floor(Math.random()*iNamesSize);
						var iWinner = iNames[iDealer];
						var iAlt = iAlts[iDealer];
						var fullPath = config.path + iWinner;
						$(this).attr( {
							src: fullPath,
							alt: iAlt
						});
				});
		}
	});


	/** Automatic Scrolling **/
	$.fn.doScroll = function() {
		$('html,body').animate({
			scrollTop: $(this).offset().top
		}, 150);	
	}
	/** Add class to second item **/
	$.fn.addClassSecond = function() {
		this.each(function () {
			$(this).children(':eq(1)').addClass('second');
		});
	}
	
	/** Add class to even item **/
	$.fn.addClassEven = function() {
		this.each(function () {
			$(this).children(':even').addClass('even');
		});
	}	
	
})(jQuery);

/***************************************************************
 * Runon Ready
 ***************************************************************/
$(document).ready(function() { 
	var thePath = location.pathname;
/************************************************
* Run Random Image Generator
*************************************************/

	$('#EBhome').hide().fadeIn('slow').EB_HomeBrandingDealer();
	$('#EBabout').hide().fadeIn('slow').EB_AboutBrandingDealer();
	$('#EB_ThirdL').hide().fadeIn('slow').EB_ThirdLevelBrandingDealer();
	$('#brandingHome').homeBrandingDealer().hide().fadeIn('slow');
	
	/** Make Sure Page Is At Least As Tall As SubNav **/
	var subnavheight = 10 + $('.subnav').height();
	var mainheight = $('#main').height();
	var anchorsheight = $('.anchors').height();
	var leftSide = subnavheight + anchorsheight;
	if (subnavheight>mainheight) {$(document).ready(function() {$('#main').css("height", subnavheight);});}
	if (leftSide>mainheight) {$(document).ready(function() {$('#main').css("height", leftSide);});}
	
	
/************************************************
* Run Equal Heights
*************************************************/

	$('#main .block').equalHeights();
	$('#main .four-col h5, .four-block h5').equalHeights();
	$('#contentinfo .footer-block').equalHeights();
	$('#main .four-block').equalHeights();

/************************************************
* Automatic Scrolling
*************************************************/
	/** Automatic Scrolling **/
	//To Top
	$('.backTop').click(function(event){event.preventDefault();$('html, body').animate({scrollTop: 0}, 150);});
	//SkipLinks
	$('#skipLinks a').click(function(event){event.preventDefault();var theLink = $(this).attr('href');$(theLink).doScroll();});
	

/** Push anchors below subnav **/
	var subH = $(".subnav").height();+"px";
	$('#main .anchors').css('top',subH);	
/************************************************
* Add Classes
*************************************************/

	//External Links
	$("#main a[href*='http://']:not([href*='"+window.location.hostname+"'])").addClass("ExternalLink");
	//Internal Links
  	$("#main a[href*='/.*'], #main a[href*='"+window.location.hostname+"']").addClass("InternalLink");
	
	//Tables
	$('table').each(function(){$('tbody tr:odd').addClass('alt');});
	
	//Last
	$('.intro ul li:last, .subnav ul li:last, #main div.four-block:last, #contentinfo div.footer-block:last').addClass('last');
	
/************************************************
* Form Validation- client side
*************************************************/	

	//Clear on first focus
	$('input[type=text]').one("focus", function(){$(this).attr('value','');});
	$('textarea').one("focus", function(){$(this).empty();});


/************************************************
* Push anchors under Subnavigation
*************************************************/	
	
	var subH = $(".subnav").height();+"px";
	$('#main .anchors').css('top',subH);
	
/************************************************
* Turn up fps for cleaner animations
*************************************************/

	jQuery.fx.interval = 100;

});

	function validateForm() {
		if( (document.getElementById('phone').value == '') && (document.getElementById('email').value == '') ){
			var theW = $('#error').width();
			$('#error').css({'display' : 'block', 'width' : theW});
			return false;
		}
		return true;
	}
