jQuery(document).ready(function($) {

	// Convert button, submit, and a.button to Jquery UI Button
	$('button, input[type=submit], input[type=button], a.button, p.edit_link a')
		.not('.login-button').not('.password-button').not('.back-button').not('.signup-button').not('.tag-button').not('.add-button').not('.alert-button')
		.not('.comment-reply-link').not('.top-button').not('.expand-button').not('.collapse-button').not('.typo-button').not('.request-button').not('.search-button')
		.not('.quick-find-button').not('.submit-fix-button')
		.button();
	$('p.edit_link a, .edit-button').button({icons: {primary: 'ui-icon-scissors'}});
	$('.login-button').button({icons: {primary: 'ui-icon-check'}});
	$('.back-button').button({icons: {primary: 'ui-icon-arrowreturnthick-1-w'}});
	$('.password-button').button({icons: {primary: 'ui-icon-key'}});
	$('.signup-button').button({icons: {primary: 'ui-icon-circle-plus'}});
	$('.tag-button').button({icons: {primary: 'ui-icon-tag'}});
	$('.add-button').button({icons: {primary: 'ui-icon-circle-plus'}});
	$('.alert-button').button({icons: {primary: 'ui-icon-alert'}});
	$('.comment-reply-link').button({icons: {primary: 'ui-icon-arrowreturnthick-1-w'}});
	$('.top-button').button({icons: {primary: 'ui-icon-circle-arrow-n'}});
	$('.expand-button').button({icons: {primary: 'ui-icon-plusthick'}});
	$('.collapse-button').button({icons: {primary: 'ui-icon-minusthick'}});
	$('.request-button').button({icons: {primary: 'ui-icon-star'}});
	$('.typo-button').button({icons: {primary: 'ui-icon-check'}});
	$('.search-button').button({icons: {primary: 'ui-icon-search'}});
	$('.quick-find-button').button({icons: {primary: 'ui-icon-search'}});
	$('.submit-fix-button').button({icons: {primary: 'ui-icon-check'}});



	// Top Button auto-scroll
	$("a.top-button").click(function(e){
		//get the top offset of the target anchor
		var target_offset = $("#header").offset();
		var target_top = target_offset.top;
		//goto that anchor by setting the body scroll top to anchor top
		$('html, body').animate({scrollTop:target_top}, 500);
		$(this).removeClass('ui-state-focus');
		e.preventDefault();
	});


 
	// Tooltips
	$('span[title]').qtip({
		text: false,
		position: {
			corner: {
				tooltip: 'bottomLeft', // tooltip initiator position
				target: 'topRight' // tooltip position
			}
		},
		style: {
			width: 200,
			padding: 5,
			background: '#fdf9e0',
			color: '#8b7435',
			textAlign: 'center',
			border: {
				width: 5,
				radius: 5,
				color: '#eadc87'
			},
			tip: 'bottomLeft'
		}
	});

	$('.jgrowl').each(function(){
		var message = $(this).html();
		var sticky = false;
		if ($(this).hasClass('sticky')) { sticky = true; }
		var theme = 'default';
		if ($(this).hasClass('error')) { theme = 'ui-state-error'; }
		$.jGrowl(message, {
			sticky: sticky,
			theme: theme
		});
	});
	
	// UserVoice Links
	var uvOptions = {};
	(function() {
		var uv = document.createElement('script'); uv.type = 'text/javascript'; uv.async = false;
		uv.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'widget.uservoice.com/MPlmgaIX0pSqxUPZfkExrQ.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(uv, s);
	})();
	$('.requests-link').click(function(e){
		UserVoice.showPopupWidget({widget_key: 'MPlmgaIX0pSqxUPZfkExrQ'});
		e.preventDefault();
	});
	$('.support-link').click(function(e){
		UserVoice.showPopupWidget({widget_key: 'OWAyeHUztWpyXHrzUGMLow'});
		e.preventDefault();
	});
	


	// Google Analytics Social Tracking
	if (typeof FB === 'object') {
		FB.Event.subscribe('edge.create', function(targetUrl) {
			_gaq.push(['_trackSocial', 'facebook', 'like', targetUrl]);
		});
		FB.Event.subscribe('edge.remove', function(targetUrl) {
			_gaq.push(['_trackSocial', 'facebook', 'unlike', targetUrl]);
		});
		FB.Event.subscribe('message.send', function(targetUrl) {
			_gaq.push(['_trackSocial', 'facebook', 'send', targetUrl]);
		});
		FB.Event.subscribe('comment.create', function(response) {
			_gaq.push(['_trackSocial', 'facebook', 'comment', response]);
			$.ajax({
				type: "POST",
				url: "http://www.learnwake.com/shared/php-scripts/fb-comment-email-notify.php",
				data: "url=" + response.href + "&encoded_emails=bGVhcm53YWtlQGdtYWlsLmNvbSxtY2xpbm1pa2VAZ21haWwuY29t"
			});
		});
		FB.Event.subscribe('comment.remove', function(response) {
			_gaq.push(['_trackSocial', 'facebook', 'uncomment', response]);
		});
	}

});

