﻿var DynamicList = new Class({
	Implements: Options,	
	options: {
				boardListId: '',
				categoryListId: '',
				forumListId: '',
				addBoardId: '',
				addCategoryId: '',
				addForumId: '',
				onClickBoard: $empty,
				onClickCategory: $empty,
				categoryContainerId: '',
				forumContainerId: '',
				onSubmitBoard: $empty,
				onSubmitCategory: $empty,
				onSubmitForum: $empty,
				onReloadBoardList: $empty,
				onReloadCategoryList: $empty,
				onReloadForumList: $empty,
				progressLayer: $empty
	},
		
	initialize: function(options) {
		this.setOptions(options);

		if ($(this.options.boardListId)) {
			$(this.options.boardListId).getChildren('li').each(function (item, index) {
				item.addEvent('click', this.BoardEntryClick.bind(this));
			}.bind(this));
		}
		this.HideCategoryList();
		this.HideForumList();
		if ($(this.options.addBoardId)) {
			$(this.options.addBoardId).addEvent('submit', this.BoardAdd.bind(this));
		}
		if ($(this.options.addCategoryId)) {
			$(this.options.addCategoryId).addEvent('submit', this.CategoryAdd.bind(this));
		}
		if ($(this.options.addForumId)) {
			$(this.options.addForumId).addEvent('submit', this.ForumAdd.bind(this));
		}
	},
	BoardListLoaded: function() {
		if ($(this.options.boardListId)) {
			$(this.options.boardListId).getChildren('li').each(function (item, index) {
				item.addEvent('click', this.BoardEntryClick.bind(this));
			}.bind(this));
		}
		this.options.progressLayer.End('Done');
	},
	CategoryListLoaded: function() {
		if ($(this.options.categoryListId)) {
			$(this.options.categoryListId).getChildren('li').each(function (item, index) {
				item.addEvent('click', this.CategoryEntryClick.bind(this));
			}.bind(this));
		}
		this.options.progressLayer.End('Done');
	},
	ForumListLoaded: function() {
		// no code needed here!
		this.options.progressLayer.End('Done');
	},
	BoardEntryClick: function(e) {
		this.options.progressLayer.Start('Loading...');
		this.options.onClickBoard(e);
		this.ShowCategoryList();
		$(this.options.addCategoryId).boardid.value = e.target.id;
	},
	CategoryEntryClick: function(e) {
		this.options.progressLayer.Start('Loading...');
		this.options.onClickCategory(e);
		this.ShowForumList();
		$(this.options.addForumId).categoryid.value = e.target.id;
	},
	BoardAdd: function(e) {
		this.options.progressLayer.Start('Creating...');
		this.options.onSubmitBoard(e);
		this.options.onReloadBoardList();
		return false;
	},
	CategoryAdd: function(e) {
		this.options.progressLayer.Start('Creating...');
		this.options.onSubmitCategory(e);
		this.options.onReloadCategoryList();
		return false;
	},
	ForumAdd: function(e) {
		this.options.progressLayer.Start('Creating...');
		this.options.onSubmitForum(e);
		this.options.onReloadForumList();
		return false;
	},
	DeleteBoard: function() {
	},
	ShowCategoryList: function() {
		if ($(this.options.categoryContainerId)) {
			$(this.options.categoryContainerId).set('style', 'display: block');
		}
		if ($(this.options.addCategoryId)) {
			$(this.options.addCategoryId).set('style', 'display: block');
		}
		this.HideForumList();
	},
	HideCategoryList: function() {
		if ($(this.options.categoryContainerId)) {
			$(this.options.categoryContainerId).set('style', 'display: none');
		}
		if ($(this.options.addCategoryId)) {
			$(this.options.addCategoryId).set('style', 'display: none');
		}
	},
	HideForumList: function() {
		if ($(this.options.forumContainerId)) {
			$(this.options.forumContainerId).set('style', 'display: none');
		}
		if ($(this.options.addForumId)) {
			$(this.options.addForumId).set('style', 'display: none');
		}
	},
	ShowForumList: function() {
		if ($(this.options.forumContainerId)) {
			$(this.options.forumContainerId).set('style', 'display: block');
		}
		if ($(this.options.addForumId)) {
			$(this.options.addForumId).set('style', 'display: block');
		}
	}
});
var dynList;

