/* -----------------------------------------------------------------------------
This webapplication is powered by indual
Copyright (c) 2009 indual GmbH (www.indual.ch)                        
-------------------------------------------------------------------------------- */


/* + add status comment form --------------------------------------------------- */
function add_status_comment (status_id) {
  $('add_status_comment_'+status_id).request({
		method: 'post',
    encoding: 'ISO-8859-1',
		onSuccess: function(response) {
			$('comment_surrounder_'+status_id).update(response.responseText);
		}
	});
}

/* + add guestbook comment form ------------------------------------------------- */
function add_guestbook_comment (id) {
  $('add_guestbook_entry_comment_'+id).request({
    method: 'post',
    encoding: 'ISO-8859-1',
    onSuccess: function(response) {
      $('comment_surrounder_'+id).update(response.responseText);
    }
  });
}

/* + show next page of comments ------------------------------------------------ */
function next_comment_page (id, page, mode) {
  url = '/include/php/ajax_handler.php?action=next_comment_page&id='+id+'&page='+page+'&mode='+mode;
  new Ajax.Request(url, {
   method: 'get',
   encoding: 'ISO-8859-1',
   onComplete: function(response) {
     $('comment_surrounder_'+id).update(response.responseText);
   }
  });
}

/* + delete a state ------------------------------------------------------------ */
function delete_state (state_id) {
  url = '/include/php/ajax_handler.php?action=delete_state&id='+state_id;
  new Ajax.Request(url, {
   method: 'get',
   encoding: 'ISO-8859-1',
   onComplete: function(response) {
     $('state_surrounder_'+state_id).style.display = 'none';
   }
  });
}

/* + delete a comment ---------------------------------------------------------- */
function delete_comment (id, comment_id, mode) {
  url = '/include/php/ajax_handler.php?action=delete_comment&id='+id+'&comment_id='+comment_id+'&mode='+mode;
  new Ajax.Request(url, {
   method: 'get',
   encoding: 'ISO-8859-1',
   onComplete: function(response) {
     $('comment_surrounder_'+id).update(response.responseText);
   }
  });
}

/* + toggle status comments with icons ---------------------------------------- */
function toggle_comment_with_icon (status_id) {
  // toggle comment icon
  if ("minilink_kommentare" == $('comment_icon_'+status_id).className) {
    $('comment_icon_'+status_id).className = "minilink_kommentare background_bottom";
  } else {
    $('comment_icon_'+status_id).className = "minilink_kommentare";
  }
  // show comments
  $('comment_surrounder_'+status_id).toggle();
}

/* + toggle all status comments ----------------------------------------------- */
function toggle_all_status_comments () {
  var label_hide = 'Alle Kommentare einklappen';
  var label_show = 'Alle Kommentare ausklappen';
  var current_label = $('toggle_all_status_comments').innerHTML;
  // get all comment div surrounders
  if (current_label == label_hide) {
    $$('div.comment_surrounder').invoke('hide');
    $$('span.comment_icon a').each(function(element) {
      element.className = 'minilink_kommentare';
    });
    $('toggle_all_status_comments').innerHTML = label_show;
  } else {
    $$('div.comment_surrounder').invoke('show');
    $$('span.comment_icon a').each(function(element) {
      element.className = 'minilink_kommentare background_bottom';
    });
    $('toggle_all_status_comments').innerHTML = label_hide; 
  }
}

/* + add a funny smiley to an element ---------------------------------------- */
function add_a_smiley (element, smiley_number) {
  var smiley_text = '';
  switch (smiley_number) {
    case 1:
      smiley_text = ':-)';
      break;
    case 2:
      smiley_text = ':-1';
      break;
    case 3:
      smiley_text = ':-|';
      break;
    case 4:
      smiley_text = ':-(';
      break;
    case 5:
      smiley_text = ':-O';
      break;
    case 6:
      smiley_text = ':-D';
      break;
    default:
      break;
  }
  // Update element with the new smiley
  insert_at_cursor_position(element, '', ' '+smiley_text+' ');
}

/* + add text at the cursor position  -------------------------------------- */
function insert_at_cursor_position (element, aTag, eTag) {
  var input = $(element);
  input.focus();
  /* für Internet Explorer */
  if (typeof document.selection != 'undefined') {
    /* Einfügen des Formatierungscodes */
    var range = document.selection.createRange();
    var insText = range.text;
    range.text = aTag + insText + eTag;
    /* Anpassen der Cursorposition */
    range = document.selection.createRange();
    if (insText.length == 0) {
      range.move('character', -eTag.length);
    } else {
      range.moveStart('character', aTag.length + insText.length + eTag.length);      
    }
    range.select();
  }
  /* für neuere auf Gecko basierende Browser */
  else if (typeof input.selectionStart != 'undefined') {
    /* Einfügen des Formatierungscodes */
    var start = input.selectionStart;
    var end = input.selectionEnd;
    var insText = input.value.substring(start, end);
    input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
    /* Anpassen der Cursorposition */
    var pos;
    if (insText.length == 0) {
      pos = start + aTag.length;
    } else {
      pos = start + aTag.length + insText.length + eTag.length;
    }
    input.selectionStart = pos;
    input.selectionEnd = pos;
  }
}

/* + search member ---------------------------------------------------------- */
function search_member (search, mode) {
  url = '/include/php/ajax_handler.php?action=search_member&search='+search+'&mode='+mode;
  new Ajax.Request(url, {
   method: 'get',
   encoding: 'ISO-8859-1',
   onComplete: function(response) {
     $('search_member_results').update(response.responseText);
   }
  });
}