var Base = {
  toggle: function(id) {
    Element.toggle(id);
  },

  show: function(id) {
    Element.show(id);
  },
  
  hide: function(id) {
    Element.hide(id);
  },
  
  scrollTo: function(id) {
    var element = $(id);
    if (element != null)
      element.scrollIntoView();
  },

  scrollTop: function(id) {
    $(id).scrollTop = 0;
  },

  focus: function(id) {
    $(id).focus();
  },

  scrollBottom: function(id) {
    var element = $(id);
    element.scrollTop = element.scrollHeight;
  },
  
  wait: function(wait) {
    cursor = wait ? 'wait' : 'pointer';
    
    elements = document.getElementsByTagName('a');
    for (var i=0; i<elements.length; i++)
      elements[i].style.cursor=cursor;
  }
}

var User = {
  set: function(id, administrator, moderator) {
    User.id = id;
    User.administrator = administrator;
    User.moderator = moderator;
  },

  showIfAdministrator: function(id) {
    if (User.administrator)
      Base.show(id);
  }
}

var Comment = {
  actions: function(user_id, edit_id, separator_id, delete_id) {
    var current_user = (User.id == user_id);
    var moderator = User.moderator;
    
    if (current_user)
      Base.show(edit_id);

    if (current_user && moderator)
      Base.show(separator_id);

    if (moderator)
      Base.show(delete_id);
  }
}

var Search = {
	init : function(container, form, title, content, author, commentor, title_exact, content_exact, author_exact, commentor_exact) {
		Search.container = container;
		Search.form = form;
		Search.title = title;
  		Search.content = content;
  		Search.author = author;
  		Search.commentor = commentor;
		Search.title_exact = title_exact;
  		Search.content_exact = content_exact;
  		Search.author_exact = author_exact;
  		Search.commentor_exact = commentor_exact;
  		Search.reset();
	},

	reset: function() {
		Search.title_value     = '';
  		Search.content_value   = '';
  		Search.author_value    = '';
  		Search.commentor_value = '';
  		
		Search.title_exact_value     = false;
  		Search.content_exact_value   = false;
  		Search.author_exact_value    = false;
  		Search.commentor_exact_value = false;
		
		$(Search.form).reset(); 
		$(Search.container).hide();
	},

	store : function() {
  		Search.title_value     = $(Search.title).value;
  		Search.content_value   = $(Search.content).value;
  		Search.author_value    = $(Search.author).value;
  		Search.commentor_value = $(Search.commentor).value;

		Search.title_exact_value     = ( $(Search.title_exact).checked     == '1' ? '1' : '0' );
    	Search.content_exact_value   = ( $(Search.content_exact).checked   == '1' ? '1' : '0' );
    	Search.author_exact_value    = ( $(Search.author_exact).checked    == '1' ? '1' : '0' );
    	Search.commentor_exact_value = ( $(Search.commentor_exact).checked == '1' ? '1' : '0' );
  	},

	blank: function() {
		if (Search.title_value  == '' && Search.content_value == '' && Search.author_value == '' && Search.commentor_value == '')
			return true
		else
			return false
  	}
}

var Paginator = {
  init: function(url, update, first, previous, next, last, top, bottom, prefix, enable, disable, sort_id, sort_value_updated, sort_value_created) {
    Paginator.url = url;
    Paginator.update = update;
    Paginator.first  = first;
    Paginator.previous = previous;
    Paginator.next = next;
    Paginator.last = last;
    Paginator.top = top;
    Paginator.bottom = bottom;
    Paginator.prefix = prefix;
    Paginator.enable = enable;
    Paginator.disable = disable;
    Paginator.current_page = 0;
    Paginator.last_page = 0;
    Paginator.sort_id = sort_id;
    Paginator.sort_value_updated = sort_value_updated;
    Paginator.sort_value_created = sort_value_created;
  },
  
  link: function(page) {
    Base.show('side_loader');
    if (Search.blank())
	  new Ajax.Updater(Paginator.update, Paginator.url + Paginator.list_subject_postfix() + page, {asynchronous:true, evalScripts:true, method:'post', onComplete:function(request){Base.hide('side_loader'); Base.focus('main_c');}}); 
	else
	  new Ajax.Updater(Paginator.update, Paginator.url + Paginator.list_subject_postfix() + page, {parameters: { 
	  	page: page, 
		updated : ($('sort_order').value == Paginator.sort_value_updated ? '1' : '0'), 
	  	
		title_str:     Search.title_value,
	  	content_str:   Search.content_value,
		author_str:    Search.author_value, 
		commentor_str: Search.commentor_value, 

	  	title_exact:     Search.title_exact_value, 
		content_exact:   Search.content_exact_value, 
		author_exact:    Search.author_exact_value, 
		commentor_exact: Search.commentor_exact_value }, 

		asynchronous:true, evalScripts:true, method:'post', onComplete:function(request){Base.hide('side_loader'); Base.focus('main_c');}}); 
  },

  list_subject_postfix: function() {
    postfix = ''
	if (!Search.blank())
		postfix = 'search'
	else if ($('sort_order').value == Paginator.sort_value_updated)
		postfix = 'updated'	
	else
		postfix = 'created'
	return '_' + postfix + '/';
  },

  linkToCurrent: function() { Paginator.link(Paginator.current_page); },
  linkToFirst: function() { Paginator.link(1); },
  linkToPrevious: function() { Paginator.link(Math.max(1, Paginator.current_page-1)); },
  linkToNext: function() { Paginator.link(Math.min(Paginator.last_page, Paginator.current_page+1)); },
  linkToLast: function() { Paginator.link(Paginator.last_page); },

  firstPage: function() {
    return Paginator.current_page != 1;
  },
  
  lastPage: function() {
    return Paginator.current_page != Paginator.last_page;
  },

  showArrows: function(id, enable) {
    Paginator.showArrow(Paginator.top + id, enable);
    Paginator.showArrow(Paginator.bottom + id, enable);
  },
  
  showArrow: function(id, enable) {
    id_enable = Paginator.prefix + id + Paginator.enable;
    id_disable = Paginator.prefix + id + Paginator.disable;
    if (enable) {
      $(id_enable).show();
      $(id_disable).hide();
    }
    else {
      $(id_enable).hide();
      $(id_disable).show();
    }
  },
  
  setPages: function(current_page, last_page) {
    Paginator.current_page = current_page;
    Paginator.last_page = last_page;
    Paginator.showArrows(Paginator.first, Paginator.firstPage());
    Paginator.showArrows(Paginator.previous, Paginator.firstPage());
    Paginator.showArrows(Paginator.next, Paginator.lastPage());
    Paginator.showArrows(Paginator.last, Paginator.lastPage());
  }

}

var Subject = {
  init: function(prefix_id, url, update) {
    Subject.prefix_id = prefix_id;
    Subject.url = url;
    Subject.update = update;
    Subject.id = 0;
    Subject.scroll_pos = 0;
    Subject.sort = true;
  },
  
  setPrefixId: function(prefix_id) {
    Subject.prefix_id = prefix_id;
  },
  
  tr_id: function(id) {
    return Subject.prefix_id + id;
  },
    
  tr: function() {  
    return $(Subject.tr_id(Subject.id));
  },

  setTrClass: function(klass) {
    var tr = Subject.tr();
    if (tr != null)
      tr.className = klass;
  },

  unselect: function() {
    Subject.setTrClass('');
  },

  select: function() {
    Subject.setTrClass('selected_row');
  },

  unset: function() {
    Subject.unselect();
    Subject.id = 0;
  },

  reset: function() {
    Subject.id = -1;
  },

  set: function(id) {
    if (Subject.id == -1) {
      Subject.id = id;
      new Ajax.Updater(Subject.update, Subject.url + '/' + id, {asynchronous:true, evalScripts:true, method:'post'}); 
    }
    Subject.select();
  },
  
  change: function(id) {
    Subject.unselect();
    Subject.id = id;
    Subject.select();
  },

  showIfCurrent: function(id, subject_id) {
    if ($(id) && Subject.id == subject_id)
      Base.show(id);
  },
  
  storeScrollPos: function(scroll_pos) {
    Subject.scroll_pos = scroll_pos;
  },
  
  getScrollPos: function(scroll_pos) {
    return Subject.scroll_pos;
  }
}