jQuery.noConflict();

jQuery(document).ready(function ($) {

	// close the .commenter-feed-list when click out of list. 
	$('body').click(function(event) {
	    if (!$(event.target).closest('.commenter-feed-list:visible').length) {
	        $('.commenter-feed-list').slideUp();
	    };
	});

	// for ajax callback
	var queue = new Array();

	$('.commenter-feed').click(function() {

		var feed = $(this);
		var cite = feed.parent();
		// clicked before
		if(feed.hasClass('success')) {
			//feed and author link's parent
			var cite = feed.parent();
			if(cite.find('.commenter-feed-list').is(':hidden')) {
				// close others
				$('.commenter-feed-list').slideUp();
				// show this
				cite.find('.commenter-feed-list').slideDown();
			} else {
				// if this list show, hide it.
				cite.find('.commenter-feed-list').slideUp();
			}
		  // click first time
		} else {
			//start animation, define in css
			feed.addClass('commenter-feed-click');
			// get author url
			commenter_url = cite.find('.commenter-feed-author-url').attr('href');

			//alert('x:' + author.offset().left + 'y:' + author.offset().top);
			//add query string, for get it back			
			id = randomString();
			// add id to queue, index it in ajax callback
			queue[id] = feed;
			
			//ajax, commenter_feed.ajax_url generate by wp_localize_script
			$.get(commenter_feed.ajax_url, {url:commenter_url, id: id}, function(data) {
				
				// get id
				id = data.substr(0, 8);
				// get data
				data = data.substr(8, data.length);
				
				// find the target
				target = queue[id];
				
				target.addClass('success');
				
				// close other
				$('.commenter-feed-list:visible').slideUp();

				// for refresh, remove the result before
				target.next('.commenter-feed-list').remove();
				// add data
				target.after(data);
				
				feed_list = target.next('.commenter-feed-list');

				//$container.css({'left':author.offset.left, 'top':author.offset().top});
				// show result
				feed_list.hide().slideDown();
				
				//close button
				feed_list.find('.close').click(function() {
					$(this).parent().parent().slideUp();
					return false;
				});
				
				// refresh button
				feed_list.find('.refresh').click(function() {
					$(this).parent().parent().slideUp();
					$(this).parent().parent().parent().find('.commenter-feed').removeClass('success').click();
					return false;

				});	
				// remove animation
				target.removeClass('commenter-feed-click');

			}); // end of $.get
		}		

		return false;

	});
	
// return random string
function randomString() {

	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";

	var string_length = 8;

	var randomstring = '';

	for (var i=0; i<string_length; i++) {

		var rnum = Math.floor(Math.random() * chars.length);

		randomstring += chars.substring(rnum,rnum+1);

	}

	return randomstring;

}

});

