$(document).ready(function() {	

	$('#hpslider').cycle();
	
	$('.sb').click( function() {
		
		var thisID = $(this).attr('id');
	
		if (thisID == 'appointment'){
			$('.modalTop h2').empty().append('Book An appointment');
			$('.modal-content').load('./inc/appointmentContent.php', function(){
				$('#modalPopup #appReqFrm #app_date').datePicker();
				  
			});		
		}
		else if (thisID == 'newsletter'){
			$('.modalTop h2').empty().append('Subscribe to our Newsletter');
			$('.modal-content').load('./inc/newsContent.php');
		}
		else if (thisID == 'locate'){
			$('.modalTop h2').empty().append('How to find Vaughan Spine + Sport Therapy');
			$('.modal-content').load('./inc/locateContent.php');
		}
		
		$('#modalPopup').modal();	
	});
	
	$('ul.serviceList li a').click( function() {		
		var thisID = $(this).attr('id');		
		dataString = "action=getService&id=" + thisID; 
		
		$.ajax({
		type: "POST", 
			url: "forms_cb.php",
			data: dataString,
			dataType: "json",
			success: function(data) {
				$('#servicePopup .smodalTop h2').empty().html(data.info['name']);	
				
				$('#servicePopup .smodal-content').empty();
				
				if (data.image != null) {
					var servicePic =  '<img src="./images/services/' + data.image + '" alt="image" />';
				}
				else {
					var servicePic = '';	
				}
				
				$('#servicePopup .smodal-content').html(servicePic  + '<p>' + data.info['overview'] + data.info['description'] + '</p>');

				$('#servicePopup').modal();		
			}
		});
	
		return false;
	});
	
	$("#mailFrm").submit(function() {
		dataString = $("#mailFrm").serialize(); 
		$.ajax({
		type: "POST", 
			url: "forms_cb.php",
			data: dataString,
			dataType: "json",
			success: function(data) {
			 if (data.success == 'FAIL')
					{
						doErrors('statusboxPopup', data);
					} 
					else  if (data.success == 'PASS') 
					{
						doSuccess('statusboxPopup', data);
						resetForm('mailFrm');
					}								
				}
		});
		return false; 
	});
	
	$('a#popupLoc').click( function() {
		$('.modal-title h2').empty().append('How to find Vaughan Spine + Sport Therapy');
		$('.modal-content').load('./inc/locateContent_v2.php');
		$('#modalPopup').modal();	
		return false; 
	});

	if ($('ul.quotes').length) {	
		$('ul.quotes').quote_rotator({
			rotation_speed: 10000	
		});
	}
	
	$("#nlSignupFrm").live("submit", function() {
		dataString = $("#nlSignupFrm").serialize(); 
		$.ajax({
			type: "POST", 
			url: "forms_cb.php",
			data: dataString,
			dataType: "json",
			success: function(data) {
			 if (data.success == 'FAIL')
					{
						$("#statusbox").html('<ul></ul>');
						$("#statusbox" + ' ul').empty().addClass('frmErrors');
						$.each(data.errors, function(index, value) {
						$("#statusbox" + ' ul').append("<li>" + value.replace('_', ' ') + "</li>");
						});
					} 
					else  if (data.success == 'PASS') 
					{
						$("#statusbox").html('<ul></ul>');
						$("#statusbox" + ' ul').empty().addClass('frmSuccess');
						$.each(data.pass, function(index, value) {
						$("#statusbox" + ' ul').append("<li>" + value.replace('_', ' ') + "</li>");
						});
						resetForm('nlSignupFrm');
					}								
				}
		});
		return false; 
	});
	
	$("#nlUnsubscribeFrm").submit(function() {
		dataString = $("#nlUnsubscribeFrm").serialize(); 
		$.ajax({
			type: "POST", 
			url: "forms_cb.php",
			data: dataString,
			dataType: "json",
			success: function(data) {
			 if (data.success == 'FAIL')
					{
						$("#statusbox").html('<ul></ul>');
						$("#statusbox" + ' ul').empty().addClass('frmErrors');
						$.each(data.errors, function(index, value) {
						$("#statusbox" + ' ul').append("<li>" + value.replace('_', ' ') + "</li>");
						});
					} 
					else  if (data.success == 'PASS') 
					{
						$("#statusbox").html('<ul></ul>');
						$("#statusbox" + ' ul').empty().addClass('frmSuccess');
						$.each(data.pass, function(index, value) {
						$("#statusbox" + ' ul').append("<li>" + value.replace('_', ' ') + "</li>");
						});
						resetForm('nlUnsubscribeFrm');
					}								
				}
		});
		return false; 
	});
	
	$("#appReqFrm").live("submit", function() {
		dataString = $("#appReqFrm").serialize(); 
		$.ajax({
			type: "POST", 
			url: "forms_cb.php",
			data: dataString,
			dataType: "json",
			success: function(data) {
			 if (data.success == 'FAIL')
					{
						$("#statusbox").html('<ul></ul>');
						$("#statusbox" + ' ul').empty().addClass('frmErrors');
						$.each(data.errors, function(index, value) {
						$("#statusbox" + ' ul').append("<li>" + value.replace('_', ' ') + "</li>");
						});
					} 
					else  if (data.success == 'PASS') 
					{
						$("#statusbox").html('<ul></ul>');
						$("#statusbox" + ' ul').empty().addClass('frmSuccess');
						$.each(data.pass, function(index, value) {
						$("#statusbox" + ' ul').append("<li>" + value.replace('_', ' ') + "</li>");
						});
						resetForm('appReqFrm');
					}								
				}
		});
		return false; 
	});	

//end of doc ready events
});

	// FORM RESET - CLEARS FORM OF PREVIOUSLY ENTERED DATA
	function resetForm(id) {
		$('#'+id).each(function(){
				this.reset();
		});
	}

	// ERROR MESSAGE DISPLAY
	function doErrors(elementID,data)
	{
			$("#statusboxPopup").modal();
			$('#' + elementID).html('<ul></ul>');
			$('#' + elementID + ' ul').empty().addClass('errors');
			$.each(data.errors, function(index, value) {
			$('#' + elementID + ' ul').append("<li>" + value.replace('_', ' ') + "</li>");
			});	
	}

	// SUCCESS MESSAGE DISPLAY
	function doSuccess(elementID,data)
	{
		$("#statusboxPopup").modal();
		$('#' + elementID).html('<ul></ul>');
		$('#' + elementID + ' ul').empty().addClass('success');
		$.each(data.pass, function(index, value) {
		$('#' + elementID + ' ul').append("<li>" + value.replace('_', ' ') + "</li>");
		});	
	}	

