
// Make sure basic namespace is defined
if (typeof(betahaus) == 'undefined') 
    var betahaus = {};

// Create our own namespace
betahaus.discussion = {

	show: function(){
		//This function should be called with jq(selector).live()
		var topic = jq(this).parents('.discussion_topic');
		var topic_id = topic.attr('id');
		var load_url = topic_id + '/full_post_listing';
		
		//Start spinner
        topic.children('.spinner img').show();
				
		if (topic.hasClass('unloaded')){
			// Remove the post listing and replace with full listing.
			topic.children('.post_listing').load(load_url, function() {
					topic.addClass('loaded').removeClass('unloaded');
			});
		}else if (topic.hasClass('loaded')){
			// Here we need to collapse the list - we load it with just one post.
            topic.children('.post_listing').load(load_url, {'limit':1}, function() {
                    topic.addClass('unloaded').removeClass('loaded');
            });
		}
		//Hide spinner
		topic.children('.spinner img').hide();
		return false;
	},
	
    post_submit: function(){

	    var button = jq(this);
        button.attr('disabled', 'disabled');
        var topic = jq(this).parents('.discussion_topic');
        var form = jq(this).parents('form');


		var text = form.serializeArray()[0]['value'];
        
		action = jq(form).attr('action');

        jq.post(
          action,
          data = {'form.text': text,
                  'form.actions.save': 'True',
				  'ajaxload': 'True'},
          function(data){
		  	topic.removeClass('loaded').addClass('unloaded');
			jq('textarea', topic).each(function(){jq(this).val('')});
			jq('.discussion_toggle', topic).click();
			button.attr('disabled', '').removeClass('submitting');
          }
        );
        
        return false;
		
	},
		
	enable_ajax_submit: function(){
		// Click action for the submit post button
		jq('#discussion .discussion_topic #form\\.actions\\.save').live('click', betahaus.discussion.post_submit);
		
		// The delete button on the delete confirm page.
		jq('#discussion #form\\.actions\\.delete').live('click', function(){

            jq(this).attr('disabled', 'disabled');
            var topic = jq(this).parents('.discussion_topic');
            var form = jq(this).parents('form');
            
            jq.post(
              jq(form).attr('action'),
              data = {'form.actions.delete':'True', 'ajaxload':'True'},
              function(data){
			  	topic_id = topic.attr('id');
                topic.addClass('unloaded').removeClass('loaded');
                jq('.discussion_toggle', topic).click();
              }
            );
            
            return false;
          });
			
		  // the cancel button
		  jq('#discussion #form\\.actions\\.cancel').live('click', function(){
		  	var topic = jq(this).parents('.discussion_topic'); 
			topic.addClass('unloaded').removeClass('loaded');
            jq('.discussion_toggle', topic).click();
			return false;
		  });

	    jq('#discussion .delete_link').live('click', function(){
	        post = jq(this).parents('.discussion_post');
	        post.load(jq(this).attr('href'));
	        return false;
	    });		  
		  
    },

/*
	hotkeys: function (e) {
		switch (e.keyCode){
			// 13 is enter key
			case 13:
			     if (e.altKey){
				 	// This causes the both the submit click and reload to happen.
				 	//jq('#form\\.actions\\.save', jq(this).parents('form')).click();
				    //return false;
				 }
			 break;
		}
	},
*/
	
	expand_topic: function(){
		jq('a.discussion_toggle', this).click();
	},

    expand_post: function(){
		if (!jq(this).hasClass('scrolledTo')){
			// we have not scrolled to this before.
	        self.scrollTo(0,this.offsetTop);
			jq(this).addClass('scrolledTo');			
		}
    },
    
    submit_new_topic: function(){
    	topic = jq('#discussion #new_topic_form textarea.topic_textarea').val();
    	text = jq('#discussion #new_topic_form textarea.post_textarea').val();
    	if(jQuery.trim(topic) != '' && jQuery.trim(text) != '')
    		return true;
    	else
    		return false;    	
    },
    
    // Attach event handlers on load
    init: function(){
    	betahaus.discussion.enable_ajax_submit();

        jq('.toggle_posts').live('click', betahaus.discussion.show);

        //FIXME: This doesn't work- see error message
        //jq('#discussion textarea').bind('keydown', betahaus.discussion.hotkeys);
		
		var topic = jq.url.param('t');
		
		if (topic != ''){
			//Escape +-signs with \\+ otherwise jQuery won't work
			var topic = topic.replace('++topic++','\\+\\+topic\\+\\+');
            jq('#'+topic).each(betahaus.discussion.expand_topic);
		}
		
		jq('#discussion #new_topic_form #form\\.actions\\.save').bind('click', betahaus.discussion.submit_new_topic);
    }
}


jq(document).ready(betahaus.discussion.init);

