jQuery(document).ready(function() {
	$('form#invite ul li div.invite_div').mouseover(function(){
		$(this).addClass('checkboxselected');
		}).mouseleave(function(){
		$(this).removeClass('checkboxselected');	
		}).click(function(event){
			var $checkbox = $(this).find(':checkbox');
		    $checkbox.attr('checked', !$checkbox.attr('checked'));
		});
	//Clicking on checkbox does nothing since clicking on div checks it
	$('form#invite ul li div.invite_div :checkbox').click(function(e){
		e.stopPropagation();
	});
	$('a#select_all').click(function(){
		$('form#invite ul li :checkbox').attr('checked', true);
	});
	$('a#clear_all').click(function(){
		$('form#invite ul li :checkbox').attr('checked', false);
	});
	
	jQuery('a#main_nav_topics').hover(function (){
		$('#nav_topics').fadeIn('fast');
		$('#user_drop').fadeOut();
	}, function(){
	});
	jQuery('#nav_topics').mouseleave(function() { jQuery(this).fadeOut(); });
	jQuery('a#main_nav_user_action').click(function(event){
		event.preventDefault(); 
	});
	jQuery('a#main_nav_user_action').hover(function (){
		$('#user_drop').fadeIn('fast');
		$('#nav_topics').fadeOut();
	}, function(){
	});
	jQuery('#user_drop').mouseleave(function() { jQuery(this).fadeOut(); });
	jQuery('.login_button').click(function() {
		$('#login_tooltip').show();
	});
	jQuery('.label_overlay').inputLabel();
	$('#table_filter_export').click(function(event) { 
		event.preventDefault(); 
		$('#table_export').submit();
	});
	$('#p_menu ul li').each(function(i, e){
		if ($(this).children().attr('href')) {
			$(this).click(function(event) { event.preventDefault(); location.href = $(this).children().attr('href'); });
		}
	});
	jQuery('.toggle-description').click(function(event){
		event.preventDefault(); 
		if ($('#description-na').is(":hidden")) { $('#description-na').slideDown('slow');
		} else { $('#description-na').slideUp('fast'); }
	});
	jQuery('[rel=tooltip]').tipsy({fade: true, gravity: 's' });
	if ($('#main_top_tw').attr('title')) {
		if ($('#message').html()) {
		setTimeout(function() { $('#main_top_tw').tipsy({fade: true, gravity: 'ne'}).tipsy('show'); }, 4750); 
		} else { $('#main_top_tw').tipsy({fade: true, gravity: 'ne'}).tipsy('show'); }
	}
	jQuery('#message').delay(4000).slideUp(1000, "easeOutExpo");
	jQuery('input.autosuggest').autocomplete('/process/autosuggest', {
		minChars: 3, max: 5, json: true, highlight: false, scroll: false, width: 300, delay: 600,
		formatItem: function(data, pos, max, item) {
			output = '<div>';
				output += '<img src="' + item.split(",")[2] + '" width="25" height="25" />';
				output += '<span>';
					output += item.split(",")[3];
					if (item.split(",")[1] != '') {
						output += '<em>' + item.split(",")[1] + '</em>';
					}
				output += '</span>';
			output += '</div>';
			return output;
		},
		formatResult: function(data, item) {
			return item.split(",")[1];
		}
	}).result(function(event, item, formatted) {
		if ($(this).val().substring(0,1) == '@') {
		} else { $(this).val(''); }
  	location.href = '/' + formatted.split(",")[0];
	});
});
(function () { PeerIndex = {
synchronize: function () {
	$.ajax({ url: '/process/synchronize'});
},
follow: function (e, account, tweet) {
	// e.preventDefault(); 
	$.ajax({ 
		data: {account: account, tweet: tweet},
		url: '/process/follow-twitterer', 
		beforeSend: function() { 
			$.fancybox.close(); 
		} 
	});
},
list: function (e, slug, element) {
	e.preventDefault(); 
	$.ajax({ 
		data: {topic: slug},
		url: '/process/create-list', 
		beforeSend: function() { 
			$(element).html('Creating Twitter list, please wait (up to 20s)...');
		},
		success: function() {
	  	$(element).fadeOut('slow');
	  } 
	});
},
tweet: function (e, refresh) {
	// e.preventDefault();
	$.ajax({ 
		url: '/process/tweet',
		type: 'post',
		data: {tweet: $('#tweet_content').val(), update: '1'},
		beforeSend: function() { 
			$.fancybox.close();
			window.open('http://twitter.com/?status=' + $('#tweet_content').val(),'sharer','toolbar=0,status=0,width=600,height=300');
		},
		success: function() {
	  	if (refresh) { setTimeout(function(){ location.reload(); }, 1200); }
	  }
	});
},
direct_message: function (e, refresh) {
	e.preventDefault();
	$.ajax({ 
		url: '/process/direct-message',
		type: 'post',
		beforeSend: function() { 
			$.fancybox.close(); 
		},
		success: function() {
	  	if (refresh) { setTimeout(function(){ location.reload(); }, 1200); }
	  }
	});
},
suggest: function (e) {
	e.preventDefault();
	if (!$('#topic_content').val()) { return false; }
	$.ajax({ 
		url: '/process/suggest-topic',
		type: 'post',
		data: {topic: $('#topic_content').val(), email: $('#topic_email').val()},
		beforeSend: function() { 
			$('#suggest_return').html('Thank you, we\'ll contact you soon!').css({'font-weight': 'bold', 'padding-top': '4px'});
			setTimeout(function(){ $.fancybox.close(); $('#topic_content').val(''); }, 4000); 
		}
	 })
},
email: function (email) {
	$.ajax({ url: '/process/save-email', type: 'post', data: {email: email} });
},
urls: function (e) {
	e.preventDefault();
	$.ajax({ 
		url: '/process/suggest-urls',
		type: 'post',
		data: { url_1: $('#url_1').val(), url_2: $('#url_2').val(), url_3: $('#url_3').val() },
		beforeSend: function() { 
			$('#suggest_url_return').html('Thank you, we\'ll contact you soon!').css({'font-weight': 'bold', 'padding-top': '4px'});
			setTimeout(function(){ $.fancybox.close(); }, 4000); 
		}
 })
},
number_format: function (number, decimals, dec_point, thousands_sep) {
	var n = !isFinite(+number) ? 0 : +number, 
	prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
	sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
	dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
	s = '',
	toFixedFix = function (n, prec) {
		var k = Math.pow(10, prec);
		return '' + Math.round(n * k) / k;
	};
	s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
	if (s[0].length > 3) {
		s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
	}
	if ((s[1] || '').length < prec) {
		s[1] = s[1] || '';
		s[1] += new Array(prec - s[1].length + 1).join('0');
	}
	return s.join(dec);
}
} }) ();
/* other */
var uservoiceOptions = {
	key: 'peerindex', host: 'peerindex.uservoice.com', forum: '64635', showTab: true,
	alignment: 'left', background_color:'#C3C3C3', text_color: 'white', hover_color: '#636363', lang: 'en'
};
function _loadUserVoice() {
	var s = document.createElement('script');
	s.setAttribute('type', 'text/javascript');
	s.setAttribute('src', ("https:" == document.location.protocol ? "https://" : "http://") + "cdn.uservoice.com/javascripts/widgets/tab.js");
	document.getElementsByTagName('head')[0].appendChild(s);
}
