jQuery(document).ready( function() {
	jQuery("a.form-control").livequery('click',
		function() {
			
			// Get current ativity ID.
			var aid = cpt_bp_get_element_id(this);
			
			// Get container ID for current activity comment form.
			var container = '#wire-post-new-'+aid;
			
			jQuery(container).toggle("normal");
			jQuery(container+" textarea").val('');
			jQuery(container+" textarea").focus();
			
			jQuery(container+' div#message').removeClass();
			jQuery(container+' div#message').html('');
			
			
			
			return false;
		}
	);
	
	
	$("a.cool, a.uncool").live('click', function() {
		
		var container = '#' + $(this).parent().parent().find('div.activity-comment').attr('id');
		
// 		console.log($(this).parent());
		
		jQuery(this).attr("href", "#");
		
// 		if($(container).parent().find(".rated").length == 0) {
		

		
			if(jQuery(this).hasClass('uncool')) {
				jQuery(container + " input[name='coolness']").val('0');
				what = 'floppt';
			}
			else {
				jQuery(container + " input[name='coolness']").val('1');
				what = 'FLIPPT';
			}
			
// 			jQuery(container).toggle("normal");
			jQuery(container+" textarea").val('&nbsp;');
// 			jQuery(container+" textarea").focus();
			
			jQuery(container+' div#message').removeClass();
			jQuery(container+' div#message').html('');
			
			sbm($(container + " form"), false);
			
// 			console.log(" a[rel='"+my_name+"']");
			
			$(container).parent().find(" a[rel='"+my_name+"']").remove();
			$(container).parent().find( " #my_decision span").text("Du findest es " + what);
			$(container).parent().find("#my_decision").removeClass().addClass(what.toLowerCase())
			
			$(container).parent().find("#comment_form_flipflop").show();
			
// 			$(container).append('<div style="padding-left:10px; display:none" class="rated" >Du hast bereits einen Kommentar hinterlassen!</div>');
		
// 		} else {
// 			jQuery(container).parent().find(".rated").show("normal");
// 		}
		
		
		return false;
	});
	
	$("#comment_form_flipflop input").live('click', function() {
		$(this).parent().hide();
		$(this).parent().parent().parent().parent().find('a.new-comment').click();
	});
	
	$("a.new-comment").live('click', function() {
		
		var container = '#' + $(this).parent().parent().find('div.activity-comment').attr('id');
		
		jQuery(this).attr("href", "#");
		
		jQuery(container+" textarea").val('');
		
		jQuery(container).show("normal");
		
// 		console.log(container);
		jQuery(container+" textarea").focus().bind('keypress', function(e) {
			var code = (e.keyCode ? e.keyCode : e.which);
			if(code == 13) {
				$(this).parent().find("input[type='submit']").click();
			}
		});
		
		jQuery(container+' div#message').removeClass();
		jQuery(container+' div#message').html('');
		
		jQuery(container + " input[name='coolness']").val('2');
		
		
		
		return false;
	});

	jQuery("form.wire-post-new-form").submit(
		function() {
			return sbm(this, true);
		}
	);
	
	jQuery("a.cpt-activity-comments-delete").livequery('click',
		function() {
		
			// Get current comment ID.
			var cid = cpt_bp_get_element_id(this);
			
			var nonce = jQuery(this).attr('href');
			nonce = nonce.split('?_wpnonce=');
			nonce = nonce[1].split('&');
			nonce = nonce[0];
			
			jQuery.post( ajaxurl, {
				'action': 'bp_cpt_activity_comments_delete',
				'comment-id': cid,
				'_wpnonce': nonce,
				'cookie': encodeURIComponent(document.cookie)
				},
				function(response) {
					if ( !response ) {
						jQuery('li#cpt-activity-comment-'+cid).hide("normal");
					}
					else {
						jQuery('li#cpt-activity-comment-'+cid+' div#message').html(response).show();
					}
				}
			);
			return false;
		}
	);
});

function sbm(frm, rel) {
	// Get current ativity ID.
	var aid = cpt_bp_get_element_id(frm);
	
	jQuery('#ajax-loader-activity-comment-'+aid).toggle();
	
	jQuery.post( ajaxurl, {
		'action': 'bp_cpt_activity_comments_post',
				 'activity-id': aid,
				 'wire-post-textarea': jQuery('#wire-post-new-'+aid+' textarea#wire-post-textarea-'+aid).val(),
				 '_wpnonce': jQuery("input#_wpnonce_activity_comments_"+aid).val(),
				 'cookie': encodeURIComponent(document.cookie),
				 'coolness': jQuery("#wire-post-new-"+aid+" input[name='coolness']").val()
	},
				 function(response) {
					 response = response.split('[[SPLIT]]');
					 
					 var aid = response[0];
					 jQuery('#ajax-loader-activity-comment-'+aid).toggle();
					 
					 jQuery('div#wire-post-new-'+aid+' div#message').addClass(response[1]);
					 jQuery('div#wire-post-new-'+aid+' div#message').html(response[2]);
					 
					 var newli = response[3].replace(/^\s+|\s+$/g, '');
					 if (newli.length>0 && rel){
						 jQuery('div#wire-post-new-'+aid).toggle().find('textarea').unbind('keypress');
						 jQuery('ul#wire-post-list-'+aid).append(response[3]).children(':last').hide().fadeIn(2000);
						 // Close comment box
						 //jQuery('#wire-post-new-'+aid).hide("normal").html("<div style='padding:10px'>Du hast bereits einen Kommentar hinterlassen!</div>");

	
// 						window.location = "http://" + loc + "?" + (new Date()).getTime() + "#act-"+aid;
					 }
					 
				 }
				 );
				 return false;
}


// Get activity id from ID of an element.
function cpt_bp_get_element_id(element) {
	var id = jQuery(element).attr('id');
	id = id.split('-');
	id = id[id.length-1];
	return id;
}