/*
Javascript Document for Hydrok
Coded by Chris Stanyon - http://www.stanyon.co.uk - August 2011
=============================
Site Functions - jQuery 1.6.4
=============================
*/


$(document).ready(function(){
	externalLinks();
	newsLetter();
});




/**
 * All external links open in a new tab / window
 */
function externalLinks() {
	$('a[rel=external]').attr('target','_blank');
}



/**
 * Newsletter Subscription and Input Blur
 */
function newsLetter() {

	var $emailAddress = $('#newsletterSubscribe #emailAddress');
	var $title = $($emailAddress).attr('title');
	
	$($emailAddress).val($title);
	
	$($emailAddress).focus(function() { if ($(this).val() == $title) { $(this).val("") } });
	$($emailAddress).blur(function() { if ($(this).val() == '') { $(this).val($title); }});

	$('#newsletterSubscribe').submit(function(e) {
		e.preventDefault();
		var activeForm = $(this);
		$('#newsResponse').addClass('ajaxLoader').html('<span style="padding-left:20px;">Please wait...</span>');
		$.post(
			$(activeForm).attr('action'),
  			$(activeForm).serialize(),
  			function(data) {
  				$('#newsResponse').removeClass('ajaxLoader').html(data.Message);
				if (data.Result=="Success") { resetForm($(activeForm)); }
			},
			"json");
	});
}



/**
 * Case Studies for WTP
 */
function caseStudies() {
	
	$('.caseStudyIndex li a').click(function(e) {
		e.preventDefault();
		
		$('.caseStudyIndex li a').removeClass('active');
		$(this).addClass('active');
		
		$('#caseStudyContainer').addClass('ajaxLoader2').load($(this).attr('href'), function() {
			$(this).removeClass('ajaxLoader2');
		});
	});
}



/**
 * Ask The Experts form
 */
function askTheExperts() {
	$('#askTheExpert').submit(function(e) {
		e.preventDefault();
		$('#ajaxResponse').addClass('ajaxLoader').html('<span style="padding-left:20px;">Please wait...</span>');
		$.post(
			$(this).attr('action'),
  			$("#askTheExpert").serialize(),
  			function(data) {
  				$('#ajaxResponse').removeClass('ajaxLoader').html(data.Message);
				if (data.Result=="Success") { resetForm($("#askTheExpert")); }
			},
			"json");
	});
}



/**
 * The Contact Form
 */
function contactForm() {
	$('#contactForm').submit(function(e) {
		e.preventDefault();
		var activeForm = $(this);
		$('#ajaxResponse').addClass('ajaxLoader').html('<span style="padding-left:20px;">Please wait...</span>');
		$.post(
			$(activeForm).attr('action'),
  			$(activeForm).serialize(),
  			function(data) {
  				$('#ajaxResponse').removeClass('ajaxLoader').html(data.Message);
				if (data.Result=="Success") { resetForm($(activeForm)); }
			},
			"json");
	});
}



/**
 * Clear the values from a form
 */
function resetForm(ele) {

    $(ele).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });

}



/**
 * Load up the GoogleMap API
 */
function googleMap() {

	$("body").append('<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&callback=hydrokMap&region=GB">');

}


/**
 * Get the map for Hydrok
 */
function hydrokMap() {

	var myLatlng = new google.maps.LatLng(50.395870, -4.923614);
	var mapCenter = new google.maps.LatLng(50.458378, -4.842224);
	
	var myOptions = {
		zoom: 10,
		center: mapCenter,
		mapTypeId: google.maps.MapTypeId.HYBRID,
		mapTypeControl: true,
	    mapTypeControlOptions: {
	      style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
	    }
	};

	var marker = new google.maps.Marker({
		position: myLatlng,
		title:"Hydrok UK - Water Engineering Solutions"
	});
	
	var contentString = "<div id='googleMapOverlay'><img src='gfx/hydrokMapLogo.jpg' alt='Hydrok UK - Water Engineering Solutions' title='Hydrok UK - Water Engineering Solutions' /></div>";

	var infowindow = new google.maps.InfoWindow({
		content: contentString
	});

	var map = new google.maps.Map(document.getElementById("googleMap"), myOptions);

	marker.setMap(map);  	
	infowindow.open(map,marker);
	
	google.maps.event.addListener(marker, 'click', function() {
		infowindow.open(map,marker);
	});

}



/**
 * Image Fader
 */
function ImageFader() {
	setInterval(function () {
		var $active = $('#slideshowContainer img.active');

	    if ( $active.length == 0 ) $active = $('#slideshowContainer img:first');

	    var $next =  $active.next().length ? $active.next() : $('#slideshowContainer img:first');

	    $active.addClass('last-active');

	    $next.css({opacity: 0.0})
    	    .addClass('active')
        	.animate({opacity: 1.0}, 750, function() {
            	$active.removeClass('active last-active');
	        });
	}, 1500 );
}




/**
 * Slide the homepage features
 */
function features() {

	var wrapper = $('#homepageFeature');
	
	setInterval(function() {
		
		var $active = $('#homepageFeature .feature:first')
		$active.animate({
    		marginTop: -413
			}, 1000, function() {
				$active.appendTo($('#slider')).css('margin-top','0');
			});		
	},6000);
}

