if('undefined' != typeof $)
{
	$(document).ready(function(){
	
		setupFormValidation();
		officesGMap();
		setupHomeBanner();
		setupLandingBoxHovers();
		setupGeneralContactForm();
		setupSidebarContactForm();
		setupFooterContactForm();
		setupNavMenuDropdowns();

	});
	
	function setupFormValidation()
	{
		mainContactFormValidation();
		miniContactFormValidation();
	}
	
	function mainContactFormValidation()
	{
		if( $('#contactPageContactForm').length > 0 )
		{
			$('#contactPageContactForm').submit(function(){
				
				var errors = 0;
				
				//name field error
				var nameField = $('#name', this);
				if( '' == nameField.val() || nameField.attr('data-default') == nameField.val() || nameField.attr('data-error') == nameField.val() )
				{
					nameField.css('color', '#f00').val(nameField.attr('data-error'));
					errors++;
				}
				
				//email field error
				var emailField = $('#email', this);
				if( '' == emailField.val() || emailField.attr('data-default') == emailField.val() || emailField.attr('data-error') == emailField.val() )
				{
					emailField.css('color', '#f00').val(emailField.attr('data-error'));
					errors++;
				}
				
				//message field error
				var messageField = $('#msg', this);
				if( '' == messageField.val() || messageField.attr('data-default') == messageField.val() || messageField.attr('data-error') == messageField.val() )
				{
					messageField.css('color', '#f00').val(messageField.attr('data-error'));
					errors++;
				}
				
				if(errors)
				{
					var errorMsg = errors == 1 ? 'There was an error with your submission.<br />Please see above for details.' : 'There were errors with your submission.<br />Please see the individual fields for details.' ;
					$('.errorRow', this).remove();
					$('.submitRow', this).before('<li class="formRow errorRow"><p>' + errorMsg + '</p></li>');
					
					return false;
				}
			});
		}
	}
	
	function miniContactFormValidation()
	{
		if( $('#footerContactForm').length > 0 )
		{
			$('#footerContactForm').submit(function(){
				
				var errors = 0;
				
				//name field error
				var nameField = $('#mini_name', this);
				if( '' == nameField.val() || nameField.attr('data-default') == nameField.val() || nameField.attr('data-error') == nameField.val() )
				{
					nameField.css('color', '#f00').val(nameField.attr('data-error'));
					errors++;
				}
				
				//email field error
				var emailField = $('#mini_email', this);
				if( '' == emailField.val() || emailField.attr('data-default') == emailField.val() || emailField.attr('data-error') == emailField.val() )
				{
					emailField.css('color', '#f00').val(emailField.attr('data-error'));
					errors++;
				}
				
				//message field error
				var messageField = $('#mini_msg', this);
				if( '' == messageField.val() || messageField.attr('data-default') == messageField.val() || messageField.attr('data-error') == messageField.val() )
				{
					messageField.css('color', '#f00').val(messageField.attr('data-error'));
					errors++;
				}
				
				if(errors)
				{
					return false;
				}
			});
		}
	}
	
	function officesGMap()
    {
	    if($('#gMap').length > 0)
	    {
			var map = new GMap2(document.getElementById("gMap"));
			
			map.setCenter(new GLatLng(42, 25), 3);
			map.setCenter(new GLatLng(50, 12), 4);
			map.setUIToDefault();
			
			var blueIcon = new GIcon(G_DEFAULT_ICON);
        	blueIcon.image = "/images/office-marker.png";
        	blueIcon.shadow = "";
        	blueIcon.iconSize = new GSize(48, 48);
        	blueIcon.iconAnchor = new GPoint(16, 48);
        	blueIcon.infoWindowAnchor = new GPoint(16, 48);
        	blueIcon.imageMap=[0,0, 47,0, 47,47, 0,47]; //imagemap style polygon coords, making a square from top/left to top/right to bottom/right to bottom/left

        	markerOptions = { icon:blueIcon };
        	
        	var ukMarker = new GMarker(new GLatLng(51.572868782073606, -0.7607345943450928), markerOptions);
        	GEvent.addListener(ukMarker, "click", function() { map.setCenter(new GLatLng(51.572868782073606, -0.7607345943450928), 14); });
			map.addOverlay(ukMarker); 	
			
			var roMarker = new GMarker(new GLatLng(44.451037605072244, 26.11404597759247), markerOptions);
			GEvent.addListener(roMarker, "click", function() { map.setCenter(new GLatLng(44.451037605072244, 26.11404597759247), 14); });
        	map.addOverlay(roMarker);
        	
        	var plMarker = new GMarker(new GLatLng(52.10440305754613, 20.621895790100098), markerOptions);
        	GEvent.addListener(plMarker, "click", function() { map.setCenter(new GLatLng(52.10440305754613, 20.621895790100098), 14); });
        	map.addOverlay(plMarker);

	    }
    }

	
	function setupHomeBanner()
	{
		if(0 < $('.homeBannerInner').length)
		{
			$('.homeBannerInner ul li div').show();
			$('.homeBannerInner img').show();
			$('.homeBannerOverlay').show();
			
			$('.homeBannerInner').infiniteCarousel({
				displayTime: 6000,
				textholderHeight : .2,	
				displayProgressBar : 0,
				displayThumbnailBackground: 0
		
			});
		}
	}
	
	function setupLandingBoxHovers()
	{
		$('.landingBox').hover(
			
			function(){
				var thisImage = $('img', this);
				thisImage.attr('src', thisImage.attr('src').replace('-off', '-on'));	
			},
			function(){
				var thisImage = $('img', this);
				thisImage.attr('src', thisImage.attr('src').replace('-on', '-off'));	
			}
			
		);
	}
	
	function setupGeneralContactForm()
	{
		$('.textInput').each(function(){
		
			$(this).val($(this).attr('data-default'));
		});
		
		$('.textInput').focus(function(){
			
			var thisDefault = $(this).attr('data-default');
			var thisValue = $(this).val();
			
			if(thisDefault == thisValue)
			{
				$(this).val('');
			}
			
			$(this).attr('style', '');
		});
		
		$('.textInput').blur(function(){
			
			var thisDefault = $(this).attr('data-default');
			var thisValue = $(this).val();
			
			if('' == thisValue)
			{
				$(this).val(thisDefault);
			}
			
			$(this).attr('style', '');
		});
	}
	
	function setupSidebarContactForm()
	{	
		/*$('.contactSidebar .imageButton').hover(
		
			function(){
				$(this).attr('src', '/images/button-sidebarContact-hover.png');	
			},
			function(){
				$(this).attr('src', '/images/button-sidebarContact.png');	
			}
		);
		
		$('.contactSidebar .imageButton').focus(function(){
			$(this).attr('src', '/images/button-sidebarContact-hover.png');	
		});
		$('.contactSidebar .imageButton').blur(function(){
			$(this).attr('src', '/images/button-sidebarContact.png');	
		});*/
	}
	
	function setupFooterContactForm()
	{	
		/*$('.footerContactForm .imageButton').hover(
		
			function(){
				$(this).attr('src', '/images/button-footerContact-hover.png');	
			},
			function(){
				$(this).attr('src', '/images/button-footerContact.png');	
			}
		);
		
		$('.footerContactForm .imageButton').focus(function(){
			$(this).attr('src', '/images/button-footerContact-hover.png');	
		});
		$('.footerContactForm .imageButton').blur(function(){
			$(this).attr('src', '/images/button-footerContact.png');	
		});*/
	}
	
	function setupNavMenuDropdowns()
	{
		$('.hasDropdown').hover(
		
			function(){
				$('.navbarDropdown', this).show();	
			},
			function (){
				$('.navbarDropdown', this).hide();	
			}
		
		);
		
		$('.navbarDropdown').hover(
		
			function(){
				$(this).parent().addClass('hovered');	
			},
			function(){
				$(this).parent().removeClass('hovered');	
			}
			
		);
	
	}
}

