// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

/* Simple Lightbox */

function OpenLightbox(){
	new Effect.Appear('fader', {duration: 0.4, to: 0.6, queue: 'end'});
	new Effect.Appear('popup', {duration: 0.4, queue: 'end'}); 
}

function CloseLightbox(){
	new Effect.Fade('popup', {duration: 0.4});
  new Effect.Fade('fader', {duration: 0.4});
}

var PageForm = {
  editNewTitle: function(txtField) {
    $('new_page').innerHTML = (txtField.value.length > 5) ? txtField.value : 'New Page';
  }
}
var TicketForm = {
  editNewTitle: function(txtField) {
    $('new_ticket').innerHTML = (txtField.value.length > 5) ? txtField.value : 'New Ticket';
  }
}

var TopicForm = {
  editNewTitle: function(txtField) {
    $('new_topic').innerHTML = (txtField.value.length > 5) ? txtField.value : 'New Topic';
  }
}

var EditForm = {
  // show the form
  init: function(postId) {
    $('edit-post-' + postId + '_spinner').show();
    this.clearReplyId();
  },

  // sets the current post id we're editing
  setReplyId: function(postId) {
    $('edit').setAttribute('post_id', postId.toString());
    $('posts-' + postId + '-row').addClassName('editing');
    if($('reply')) $('reply').hide();
  },
  
  // clears the current post id
  clearReplyId: function() {
    var currentId = this.currentReplyId()
    if(!currentId || currentId == '') return;

    var row = $('posts-' + currentId + '-row');
    if(row) row.removeClassName('editing');
    $('edit').setAttribute('post_id', '');
  },
  
  // gets the current post id we're editing
  currentReplyId: function() {
    return $('edit').getAttribute('post_id');
  },
  
  // checks whether we're editing this post already
  isEditing: function(postId) {
    if (this.currentReplyId() == postId.toString())
    {
      $('edit').show();
      $('edit_post_body').focus();
      return true;
    }
    return false;
  },

  // close reply, clear current reply id
  cancel: function() {
    this.clearReplyId();
    $('edit').hide()
  }
}

var ReplyForm = {
  // yes, i use setTimeout for a reason
  init: function() {
    EditForm.cancel();
    $('reply').toggle();
    $('post_body').focus();
    // for Safari which is sometime weird
//    setTimeout('$(\"post_body\").focus();',50);
  }
}

/* Used by the page editor to toggle between html and wysiwyg */

function addEditor(id) {
  var html_tab = document.getElementById('html_view_tab');
  if (html_tab.className.match("selected"))
  {
	  toggle_tab_styles('html_view_tab', 'wysiwig_view_tab');
	  tinyMCE.execCommand('mceAddControl', false, id);
  }
}

function removeEditor(id) {
  var wysiwig_tab = document.getElementById('wysiwig_view_tab');
  if (wysiwig_tab.className.match("selected"))
  {
	  toggle_tab_styles('wysiwig_view_tab', 'html_view_tab');
	  tinyMCE.execCommand('mceRemoveControl', false, id);
  }
}

function toggle_tab_styles(selected, unselected) {
	var unselected_tab = document.getElementById(unselected);
	var selected_tab = document.getElementById(selected);
	unselected_tab.className += " selected"
	selected_tab.className = unselected_tab.className.replace("selected", "");
}


/* Used in registration to make the info boxes appear and disappear */

function display_note(display_id) {
	var display_note = document.getElementById(display_id);
	var notes = document.getElementsByName('note');
	for (var i = 0; i < notes.length; i++) {
		notes[i].style.display = 'none';
	}
	display_note.style.display = '';
}

function display_sub_register_type(reg_type){
	if ($("radio_"+reg_type).checked == true){
	  var sub_register_types = document.getElementsByName('sub_register_type');
	  for (var i = 0; i < sub_register_types.length; i++) {
		  sub_register_types[i].style.display = 'none';
	  }
	  if (reg_type != "Other") {
	    var sub_register_type = document.getElementById(reg_type);
	    sub_register_type.style.display = 'block';
	  }
	}
}

function display_phones() {  
  var phone_contact = document.getElementById("phone_contact")
  if ($("radio_phone").checked == true) {
    $('phone_contact').style.display = '';
  }
  else{
    $('phone_contact').style.display = 'none';
  }
}

/* Makes sub-pages appear in list */

function expand_pages(id){
	var rows = document.getElementsByName(id + '_child_page');
	for (var i = 0; i < rows.length; i++) {
		Element.addClassName(rows[i].id,'open');
		/* rows[i].style.display = ''; */
	}
	Element.hide(id + '_list_expand');
	Element.show(id + '_list_collapse');
}



function collapse_pages(id){
	var rows = document.getElementsByName(id + '_child_page');	
	if (rows.length > 0) { 
		collapse_child_pages(rows);
		document.getElementById(id + '_list_collapse').style.display = 'none';
		document.getElementById(id + '_list_expand').style.display = '';
		/* Element.hide(id + '_list_collapse');
			 Element.show(id + '_list_expand'); */
	}
}

function collapse_child_pages(rows) {
	for (var i = 0; i < rows.length; i++) {
		var row = rows[i]
		var id = row.id.gsub('parent_page_','')
		if (row.style.display == '' || row.style.display == 'block' ){
			row.style.display = 'none';
	 /* Effect.SlideUp(row.id); */
			collapse_pages(id);
			}
	}
}
	
/* Used to select all or none of the checkboxes in lists
/* The javascript given by PSD2HTML generates a div usually called MyCheckBox
/* to replace a checkbox. When clicking on the div, the div checkbox is reclassed and
/* the hidden checkbox is checked value is set to true. This script runs through the
/* checkboxes by using a count system. */

function select_all(count_string){
	var count_start = Element.childElements(Element.childElements('item-hover').first()).first().id.replace("myCheckbox",""); 
	var count_end 	= Element.childElements(Element.childElements('item-hover').last()).first().id.replace("myCheckbox","");
  toggle_checkboxes(true,"checkboxAreaChecked", count_start, count_end, count_string);
}
function select_none(count_string){
	var count_start = Element.childElements(Element.childElements('item-hover').first()).first().id.replace("myCheckbox",""); 
	var count_end 	= Element.childElements(Element.childElements('item-hover').last()).first().id.replace("myCheckbox","");
	toggle_checkboxes(false, "checkboxArea",  count_start, count_end, count_string);
}

function toggle_checkboxes(checked_value, class_name, count_start, count_end, count_string){
	var count = parseInt(count_string);
	var start = parseInt(count_start);
	var stop  = parseInt(count_end);
  for (var x = start; x <= stop; x++){
    $('myCheckbox'+x).className = class_name;
  }
  for (var y = 0; y <= (count - 1); y++){
    $('check'+y).checked = checked_value;
  }
}
