function callback (post_id, exception) { 
	if(post_id) { 
		
	} 
} 

function fb_send(message, link) {
	var actionLinks = [{"text": "Tell me!", "href": link}];
	message = message+"\n\n"+link;
	var attachment = {'media':[{'type':'image','src':'http://sayat.me/images/logo.png','href':link}]}; 
	FB.Connect.streamPublish(message, attachment, actionLinks, null, 'Tell me what you think about me!', callback);
}

var checked = '';
var timeoutID;
function check_url(url) {
	$.get(root_uri + 'ajax.php', {url: url}, function(data) {
	  	if(data == '0') {	  	
	  		$('.check_url.red').addClass('hidden');
	  		$('.check_url.green').removeClass('hidden');
	  	} else if(data == '1') {	  		
	  		$('.check_url.green').addClass('hidden');
	  		$('.check_url.red').removeClass('hidden');
	  	} else {	  		
	  		$('.check_url.green').addClass('hidden');
	  		$('.check_url.red').addClass('hidden');
	  	}
	});
}

/**
 * document ready function
 */
$(document).ready(function() {
	$('a.login').click(function(e) {
		$('form, .login').toggleClass('hidden');
		
		return false;
	});
	
	$('.hint').hint();
	
	$('.fb_send').click(function(e) {
		var link = $('.fb_url').html();
		var message = $('.fb_message').html();
		fb_send(message, link);
		return false;
	});
	
	$('.fb_send_published').click(function(e) {
		//var link = $('.fb_url_published').html();
		//var message = $('.fb_message_published').html();
		var link = $(this).siblings('.fb_url_published').html();
		var message = $(this).siblings('.fb_message_published').html();
		fb_send(message, link);
		return false;
	});
	
	$('a.toggle_sidebar_box').click(function(e) {
		$(this).siblings('div.slide').slideToggle('normal');
		return false;
	});

	$('a.fb_tab_done').click(function(e) {
		$.post(root_uri + 'ajax.php', {fb_tab_done: ''}, function(data) {
		});
		$(this).parent().parent().slideToggle('normal');
		return false;
	});
		
	$('input.reply').click(function(e) {
		$(this).parent().hide().siblings('form').removeClass('hidden');
		return false;
	});
	
	$('#enable_reply').click(function(e) {
		if($(this).is(':checked')) {
			$('div.signup').removeClass('hidden');
		} else {
			$('div.signup').addClass('hidden');
		}
		return true;
	});
	
	
	$('input.show_comments').click(function(e) {
		$(this).hide().siblings('div.comments').children().removeClass('hidden');
		return false;
	});
	
	$('#passwd').keyup(function(e) {
		if($(this).val() != '' && $('#passwd_confirm').hasClass('inactive')) {
			$('#passwd_confirm').removeClass('inactive').fadeIn(1000);
		}
	});
	
	$('#feedback_url').keyup(function(e) {
		var url = $('#feedback_url').val();
		//check via ajax
		if(url != checked) {
			checked = url;
			window.clearTimeout(timeoutID);
			timeoutID = window.setTimeout(check_url, 500, url);			
		}
	});
	
	$("textarea.embed_code").focus(function() {
  		// only select if the text has not changed
  		if(this.value == this.defaultValue) {
   			this.select();
  		}
 	});
 	
 	$('a.remove_item').click(function(e) {
 		var item = $(this).attr('name');
		$.post(root_uri + 'ajax.php', {delete_item: item}, function(data) {
			if(data == '1') {
				$('#item_'+item).hide('slow');
			}
		});
		return false;
	});
 	
 	$('a.toggle_next').click(function(e) {
 		$(this).children('span').toggle();
		$(this).next().toggle();
		return false;
	});
 	

	// =======================
	// Edit in place functions 
	// =======================
 	
 	$('.edit_in_place').click(function(e) {
		// set text
 		$(this).siblings().find('input[name=text]').val($(this).text());
		// show form
		$(this).hide();
 		$(this).siblings().show();
 		return false;
	});

	// add the modification functions to the buttons (needed here for cancel)
	$('.in_place_save').click(function(){save_in_place_changes(this, false);});
	$('.in_place_cancel').click(function(){save_in_place_changes(this, true);});
	
	// post submit or cancel function
	function save_in_place_changes(obj, cancel) {
		if (cancel) {
			// hide from show text
			$(obj).parent().hide();
			$(obj).parent().siblings().show();
		} else {
			// submit
			var text = $(obj).parent().find('input[name=text]').val(); 
			$.post(root_uri + 'ajax.php', {feedback_copyedit: text}, function(data){
				if (data == '') {
					data = '(Click to add a call for feedback)';
				}
				// set the new text
				$(obj).parent().siblings('h1').text(data);
				// hide from show text
				$(obj).parent().hide();
				$(obj).parent().siblings().show();
			});
		}
		return false;
	};
 	
});
