//=======================================================================================================
//-------------------------------------------------------------------------------------------------------
//	Custom Javascript functions
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================
$(document).ready(function() {

//---------------------------------------------------------------------------------------------------------
//	Clear background Images from form inputs on focus
//---------------------------------------------------------------------------------------------------------
	$('.clearfield').focus(function() {
		$(this).css('background-position','0px -50px');
	});

	$('.clearfield').blur(function() {
		if (!$(this).val()) {
			$(this).css('background-position','center center');
		};
	});
	
//---------------------------------------------------------------------------------------------------------
//	CTA Toggle
//---------------------------------------------------------------------------------------------------------
	$(".toggler").toggle(
		function () {
			$(this).next().slideDown();
		},
		function () {
			$(this).next().slideUp();
		}
	);

//---------------------------------------------------------------------------------------------------------
//	CTA Next/Previous
//---------------------------------------------------------------------------------------------------------
	var t_current = 1;

	$(".t-next").click(function(event){
		event.preventDefault;
		
		$('#testimonial-'+t_current).hide();
		
		t_current = t_current < t_count ? t_current+1 : 1 ;
		
		$('#testimonial-'+t_current).show();
	});

	$(".t-prev").click(function(event){
		event.preventDefault;
		
		$('#testimonial-'+t_current).hide();
		
		t_current = t_current == 1 ? t_count : t_current-1;
		
		$('#testimonial-'+t_current).show();
	});
	
//---------------------------------------------------------------------------------------------------------
//	Load state based on City
//---------------------------------------------------------------------------------------------------------
	$('select.findbystate').change
	
	$('select.findbystate').change(function() {
		var state = $('select.findbystate option:selected').val();
		if (state) {
			$('select.cityinstate').load('/search/cities.php?state='+state);
		};
	});

//---------------------------------------------------------------------------------------------------------
//	Audio Player
//---------------------------------------------------------------------------------------------------------
	$(".sm2_link").toggle(
		function (event) {
			event.preventDefault();
			
			$(this).removeClass('sm2_paused');
			$(this).addClass('sm2_playing');
			
			var id = $(this).attr('id').substring(7);

			var selector_id = $(this).attr('id');

			soundManager.play(id,{
				onfinish:function(){
					resetlink(selector_id);
				}
			});
		},
		function (event) {
			event.preventDefault();
			$(this).removeClass('sm2_playing');
			$(this).addClass('sm2_paused');

			var id = $(this).attr('id').substring(7);
			soundManager.stop(id);
		}
	);
});

function resetlink(selector_id)
{
	$('#'+selector_id).click();

	$('#'+selector_id).removeClass('sm2_playing');
}

//-------------------------------------------------------------------------------------------------------
//	openEmailPopup
//-------------------------------------------------------------------------------------------------------
function openEmailPopup( id )
{
	sizeX = 500;
	sizeY = 400;
	
	//	Center the window
	leftpos	= (screen.width)  ? (screen.width-sizeX)/2 : 100;
	toppos	= (screen.height) ? (screen.height-sizeY)/2 : 100;

	//	Define the window size
	widthVar  = 'width=' + sizeX + ',';
	heightVar = 'height=' + sizeY + ',';
	
	//	Open the window
	contenturl				= '/contact/email.php/id/' + id;
	target					= 'email' + id;
	winobject				= window.open(contenturl,target,"menubar=0,statusbar=0,scrollbars=1,toolbar=0,location=0," + widthVar + heightVar + "left=" + leftpos + ",top=" + toppos + ",resizable=1" );
	winobject.focus();
}
