/*
Guest Chat component
*/
var GuestChatComp = {
  Init : function(){
    /* Early exit if we don't find any guest chat on the page */
    if(!$('guest-chat-component')) {
      return;
    }

    /* Exit if we dont get an comp-id */
    if(!GuestChatComp.getCompId()) {
      return;
    }

    /* We check if there is a input div, if there is none
       we presume the chat is closed and do not continue */
    if(!$('guest-chat-input-textarea')) {
      return;
    }
    
    /* Clear placeholding nbsp in textarea */
    if(!$('guest-chat-input-textarea').empty()) {    
      $('guest-chat-input-textarea').clear();
    }

    /* Start the periodic update of the chat */   
    GuestChatComp.updateChat(5, 1); 
    /* Start the periodic update of the moderator chat
       if there is such a div */
    if($('guest-chat-pane-moderator')) {
      GuestChatComp.updateModeratorChat(5, 1);
    }
  },

  getCompId : function() {
	if(!$('guest-chat-component').getAttribute('name')) {
      return;
    }
    
    return $('guest-chat-component').getAttribute('name');
  },

  updateModeratorChat : function(interval) {
    /* FIXME: WHAT TO DO ON ERRORS? */
    new PeriodicalExecuter(function(pe) {
      new Ajax.Updater('guest-chat-content-moderator',
		       '/templates/shared/guest-chat/action.xml',
	  {
	    method: 'post',
	    parameters: {__toolbar: 0, action: 'update_moderator',
			__xsl : '/templates/temp-guest-chat/actions.xsl',
			 comp_id: GuestChatComp.getCompId()},
	    /* insertion: Insertion.Bottom, */
	    onComplete: function(transport) {
	      /* Scroll down */
	      /* FIXME: dont scroll if user have moved the scrollbar */
	      with ($('guest-chat-pane-moderator')){scrollTop = scrollHeight;}
	    }
	  });
      /* pe.stop(); */
    }, interval);
  },

  updateChat : function(interval) {
   /* FIXME: WHAT TO DO ON ERRORS? */	
	new PeriodicalExecuter(function(pe) {
       new Ajax.Updater('guest-chat-content',
		       '/templates/shared/guest-chat/action.xml',
	  {
	    method: 'post',
	    parameters: {__toolbar: 0, action: 'update',
			 __xsl : '/templates/temp-guest-chat/actions.xsl',
			 comp_id: GuestChatComp.getCompId()},
	    /* insertion: Insertion.Bottom, */
	    onComplete: function(transport) {
	      /* Scroll down */
	      /* FIXME: dont scroll if user have moved the scrollbar */
	      with ($('guest-chat-content')) {scrollTop = scrollHeight;}
	    }
	  });
      /* pe.stop(); */
    }, interval);
  },

  writeChatMsg : function() {
    new Ajax.Request('/templates/shared/guest-chat/action.xml',
	{
	  method:'post',
	  parameters: {__toolbar: 0, action: 'write',
	   		   __xsl : '/templates/temp-guest-chat/actions.xsl',
		       comp_id: GuestChatComp.getCompId(),
		       text: $F('guest-chat-input-textarea'),
                       magic_roxen_automatic_charset_variable: GuestChatComp.getRoxenAutoCharset()},
	  onSuccess: function(transport) {
	    /* Clear input field */
	    $('guest-chat-input-textarea').clear();
	  },
	  onFailure: function(){
	    alert('Misslyckades att skicka meddelandet.')
	  }
	});
  },

  writeChatMsgModerator : function() {
    // Get writer
    var writer = '';
    if($('guest-chat-moderator-write-as')) {
      writer = $F('guest-chat-moderator-write-as');
    }
    
    new Ajax.Request('/templates/shared/guest-chat/action.xml',
	{
	  method:'post',
	  parameters: {__toolbar: 0, action: 'write',
	  		   __xsl : '/templates/temp-guest-chat/actions.xsl',
		       comp_id: GuestChatComp.getCompId(),
		       text: $F('guest-chat-input-textarea'),
	               nick: writer,
	               selected: GuestChatComp.selectedChatEntry,
	               magic_roxen_automatic_charset_variable: GuestChatComp.getRoxenAutoCharset()},
	  onSuccess: function(transport) {
	    /* Clear input field and selected chat entry */
	    $('guest-chat-input-textarea').clear();
	    GuestChatComp.clearSelectedChatEntry();
	  },
	  onFailure: function(){
	    alert('Misslyckades att skicka meddelandet.')
	  }
	});
  },
  
  highlightChatEntry : function(chat_entry_div) {
    if(!chat_entry_div) {
      return;
    }

    chat_entry_div.addClassName('guest-chat-entry-highlight');
  },

  deHighlightChatEntry : function(chat_entry_div) {
    if(!chat_entry_div) {
      return;
    }

    chat_entry_div.removeClassName('guest-chat-entry-highlight');
  },

  selectChatEntry : function(chat_entry_div) {
    if(!chat_entry_div) {
      return;
    }

    var selected_content = $('guest-chat-selected-chat-entry-content');
    
    GuestChatComp.selectedChatEntry = chat_entry_div.getAttribute('id');
    selected_content.update(chat_entry_div.innerHTML);
    selected_content.ondblclick = function () {
      GuestChatComp.clearSelectedChatEntry();
    }
      
    $('guest-chat-selected-chat-entry').show();    
  },

  clearSelectedChatEntry : function () {
    GuestChatComp.selectedChatEntry = null;
    $('guest-chat-selected-chat-entry-content').update();
    $('guest-chat-selected-chat-entry').hide();
  },

  getRoxenAutoCharset : function () {
    var text_input_form = $('guest-chat-input-text-form');
    var roxen_auto_charset = text_input_form['magic_roxen_automatic_charset_variable']
    return $F(roxen_auto_charset);
  }
}

SafeOnload.AddFunction(GuestChatComp.Init);
