function callback (post_id, exception) { 
	if(post_id) { 
		
	} 
} 

function fb_send(message, link) {
	message = message+"\n\n"+link;
	FB.ui({
		method: 'stream.publish',
		caption: message,
		picture: 'http://sayat.me/images/logo.png',
		link: link,
		user_message_prompt: 'Tell me what you think about me!'
	}, function(response) {});
	/*FB.api({
		method: 'status.set',
		status: message
	}, function(response) {
	});*/
	//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');
	  	}
	});
}

function rate(n) {
	var user = $('#star_for_user').attr('value');
	$.post(root_uri + 'ajax.php', {star_user: user, star_rating: n},
			function(data) {
		$('#stars').hide('slow');
	});
}

function action_select(method) {
	$.post(root_uri + 'ajax.php', {action_select: method},
		function(data) {
			return;
	});
	return true;
};

function create_and_show_new_items(data) {
	if(data.length > 0) {
		
		var template_html = $('.activity_template').html();		
		template_html = template_html.replace(/xusernamex/g, data[0].username);
		template_html = template_html.replace(/xfullnamex/g, data[0].full_name);
		template_html = template_html.replace(/xhashx/g, data[0].hash);
		template_html = template_html.replace(/xidx/g, data[0].id);		
		var text = data[0].text.substring(0,20)+'...';
		
		template_html = template_html.replace(/xtextx/g, text);
				
		$('div.latest').prepend(template_html);
		
		$('#activity_'+data[0].id).slideDown(1000, function() {
			data.splice(0, 1);
			window.setTimeout(create_and_show_new_items, 3000, data);			
		});		
	} else {
		window.setTimeout(get_latest_activity, 2000);
	}
}

function get_latest_activity() {
	var t = new Date().getTime();
	var new_items = new Array();
	
	if(true) {
		$.get(root_uri + 'activity.json', { time: t }, function(data){
			var len = data.length;
			
			for(var i = 0; i < len; i++){
				//new item, append
				if($('#activity_'+data[i].id).length == 0) {					
					new_items.push(data[i]);
				}
			}	
			
			//append new items, then schedule the new update
			create_and_show_new_items(new_items);
		});
	} else {
		//no focus, wait and try again
		window.setTimeout(get_latest_activity, 2000);
	}
}

/**
 * document ready function
 */
$(document).ready(function() {
	
	if($('div.latest').length > 0) {
		window.setTimeout(get_latest_activity, 2000);
	}
	
	$('a.login').click(function(e) {
		$('.login').toggleClass('hidden');
		
		return false;
	});
	
	$('.hint').hint();
	
	$('.fb_send_icon').click(function(e) {
		action_select('fb post url to wall icon');
		var link = $('.fb_url').html();
		var message = $('.fb_message').html();
		fb_send(message, link);
		return false;
	});
	
	$('.fb_send').click(function(e) {
		action_select('fb post url to wall');
		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) {
		// normal slide
		$(this).siblings('div.slide').slideToggle('normal');
		// the inline case
		$(this).siblings('div.slide_inline').toggleClass('hidden');
		$(this).siblings('div.slide_inline').toggleClass('inline');
		return false;
	});
	
	$('input.reply').click(function(e) {
		$(this).parent().hide().siblings('form').removeClass('hidden');
		return false;
	});
	
	$('#enable_reply').click(function(e) {
		$('#right_fb_facepile').slideToggle('normal');
		$(this).siblings('div.slide').slideToggle('normal');
		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);
		}
	});
	
	$('#passwd2').keyup(function(e) {
		if($(this).val() != '' && $('#passwd_confirm').hasClass('inactive')) {
			$('#passwd_confirm2').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);			
		}
	});
	
	$('#feedback_url2').keyup(function(e) {
		var url = $('#feedback_url2').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);});

	$('#star_1').hover(function() {
		$('#star_desc').hide();
		$('#star_desc_1').show();
	}, function() {
		$('#star_desc_1').hide();
		$('#star_desc').show();
	});
	$('#star_2').hover(function() {
		$('#star_desc').hide();
		$('#star_desc_2').show();
	}, function() {
		$('#star_desc_2').hide();
		$('#star_desc').show();
	});
	$('#star_3').hover(function() {
		$('#star_desc').hide();
		$('#star_desc_3').show();
	}, function() {
		$('#star_desc_3').hide();
		$('#star_desc').show();
	});
	$('#star_4').hover(function() {
		$('#star_desc').hide();
		$('#star_desc_4').show();
	}, function() {
		$('#star_desc_4').hide();
		$('#star_desc').show();
	});
	$('#star_5').hover(function() {
		$('#star_desc').hide();
		$('#star_desc_5').show();
	}, function() {
		$('#star_desc_5').hide();
		$('#star_desc').show();
	});
	$('#star_1').click(function() {
		rate(1);
	});
	$('#star_2').click(function() {
		rate(2);
	});
	$('#star_3').click(function() {
		rate(3);
	});
	$('#star_4').click(function() {
		rate(4);
	});
	$('#star_5').click(function() {
		rate(5);
	});

	$('a.language').click(function() {
		$('div.language').toggle();
		return false;
	});
	
	// 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;
	};
 	
	$('a.feedback_map_link').qtip({
		content: {
            text: $('a.feedback_map_link').attr('title'), 
            title: {
               text:  $('a.feedback_map_link .link_title').text()
            }
        },
		style: {
			classes: 'ui-tooltip-sayatme ui-tooltip-shadow ui-tooltip-rounded'
	    },
	    position: {
	        my: 'top center', 
	        at: 'bottom center'
	    }, 
	    show: {
	         event: 'mouseenter',
	         ready: false
	    }
	});
	
	$('.fbb').click(function(e) {
		var link = $('.fb_url').html();
		var message = $('.fb_message').html();
		fb_send(message, link);
		return false;
		
	});
	
	$('a.open_dialog, input.close_dialog').live('click', function(e) {
		$('div.dialog, div.disable').toggle();
		return false;
	});
	
	$('a.twitter_select_url').click(function(e) {
		$(this).attr('href', $('div.twitter_url').html());
		
		return true;
	});
});

