$(document).ready(function(){
	var basepath = '';
	/* scrolling news ticker for main page - ajax contentLoad */
	$(".newsfeed").load(basepath + '/SNTweet/ticker.php', function() {
		$(".newslist").jCarouselLite({  
        vertical: true,  
        hoverPause: true,
        visible: 1,  
        btnNext: ".btn-next",
        btnPrev: ".btn-prev",
        auto:5000,  
        speed:100
        
		});
	});

	/***************************************************************************************************************/
	/* Request Callback option */
	function processCallback(v,m,f) {
		// this function calls the callback script to sent an email
		if (v) {
			// only make post if 'ok' was pressed
			formdata = {
					phone: f.phone,
					name:  f.name,
					subject: f.subject,
					action: f.action
			};
			$.ajax({
				type: 'POST',
				url: basepath + '/callback.php',
				data: formdata,
				async: true,
				success: function(data) { 
					if (data == false) {
						$.prompt("<p>Thank you for requesting a call, we will phone you back within the next 30 minutes.</p>" 
								, { buttons: { Ok: false } }
								);
					} else {
						$.prompt("<p>There was a problem requesting a callback.</p>"
								 + "<p>Please email us at <a href='mailto:info@snt.uk.com'>info@snt.uk.com</a> and we "
								 + "will call you back within the next 30 minutes.</p>"
								 + "<p>" + data + "</p>"
								 );
					}
				}			
			});
		} 
		return true;
	}
	
	function validateCallback(v,m,f) {
		if (v == false) { return true; } // to close the popup on cancel
		if (f.phone == "") {
			m.children('#alertPhone').show();
			return false;
		}
		if (f.name == "") {
			m.children('#alertName').show();
			return false;
		}
	}

		
	$("a.callback").click(function(event) {
		title = $(this).attr('title');
		//console.log(title);
		$.prompt('<h1>Request Callback</h1>'
				+ '<div>Please enter your name &amp; phone number and we will call you back within the next 30 minutes.</div>'
				+ '<div class="first il"><b>Phone number</b></div>'
				+ '<div class="il"><input type="text" name="phone"></div>'
				+ '<div class="il" id="alertPhone">Please enter your phone number</div><br>'
				+ '<div class="first il"><b>Your name</b></div> '
				+ '<div class="il"><input type="text" name="name"></div> '
				+ '<div class="il" id="alertName">Please enter your name</div>'
				+ '<input type="hidden" name="subject" value="' + title + '">'
				+ '<input type="hidden" name="action" value="callback">'
 				,
				{ 
			buttons: { 
					Ok: true,
					Cancel: false
					},
			submit: validateCallback,
			callback: processCallback
			}
		); 
	});
	
	
	/***************************************************************************************************************/
	/* Request Emailback option */
	$("a.emailback").click(function(event) {
		title = $(this).attr('title');
		//console.log(title);
		$.prompt('<h1>Request Email Reply</h1>'
				+ '<div>Please enter your name &amp; email address and we will email you back within the next 30 minutes.</div>'
				+ '<div class="first il"><b>Email address</b></div>'
				+ '<div class="il"><input type="text" name="email"></div>'
				+ '<div class="il" id="alertEmail">Please enter your email address</div><br>'
				+ '<div class="il"><b>Comments</b></div>'
				+ '<div class="il"><textarea name="message" rows="5" cols="30"></textarea></div>'
				+ '<input type="hidden" name="subject" value="' + title + '">'
				+ '<input type="hidden" name="action" value="emailback">'
				,
				{ 
			buttons: { 
					Ok: true,
					Cancel: false
					},
			submit: validateEmailback,
			callback: processEmailback
			}
		); 
	});
	
	function validateEmailback(v,m,f) {
		if (v == false) { return true; } // to close the popup on cancel
		if (f.email == "") {
			m.children('#alertEmail').show();
			return false;
		}
	}
	
	function processEmailback(v,m,f) {
		// this function calls the callback script to sent an email
		if (v) {
			// only make post if 'ok' was pressed
			formdata = {
			email: f.email,
			subject: f.subject,
			message: f.message,
			action: f.action
			};

			$.ajax({
				type: 'POST',
				url: basepath + '/callback.php',
				data: formdata,
				async: true,
				success: function(data) { 
					if (data == false) {
						$.prompt("<p>Thank you for your email, we will send a reply within the next 30 minutes.</p>" 
								, { buttons: { Ok: false } }
								);
					} else {
						$.prompt("<p>There was a problem requesting an email.</p>"
								 + "<p>Please email us at <a href='mailto:info@snt.uk.com'>info@snt.uk.com</a> and we "
								 + "will send a reply within the next 30 minutes.</p>"
								 + "<p>" + data + "</p>"
								 );
					}
				}			
			});
		} 
		return true;
	}
});
