// Базовый класс объектов.
var CObj = Class.create();
CObj.prototype = {

	// Конструктор.
	initialize: function() {},

	// Устанавливает/возвращает имя текущего экземпляра класса.
	set_name: function(name) {
		if ( typeof(name) != 'undefined' ) {
			this.name = name;
		}
	},

	get_name: function() {
		return this.name;
	}

};

// Базовый класс списков.
var CList = Class.create();
CList.prototype = Object.extend( new CObj(), {

	// Конструктор.
	initialize: function(name, store_uri) {
		this.set_name (name);
		this.store_uri = store_uri;
		this.sort_by = 'ord_created';
		this.sort_order = 'DESC';
		this.by_page = '';
		this.page = 1;
		this.caching = false;
	},

	// Устанавливает/возвращает URI бэкенда для работы со списком.
	set_store: function(store_uri) {
		this.store_uri = store_uri;
	},

	get_store: function() {
		return this.store_uri;
	},
	
	// Устанавливает/возвращает кол-во записей на странице.
	set_by_page: function(by_page) {
		this.by_page = by_page;
	},

	get_by_page: function() {
		return this.by_page;
	},

	// Устанавливает/возвращает поле для сортировки.
	set_sort_by: function(sort_by) {
		if ( this.sort_by == sort_by ) {
			this.sort_order = (this.sort_order == 'DESC') ? 'ASC' : 'DESC';
		}
		this.sort_by = sort_by;
		this.reload();
		return false;
	},

	get_sort_by: function() {
		return this.sort_by;
	},

	// Устанавливает/возвращает порядок сортировки (ASC/DESC).
	set_sort_order: function(sort_order) {
		this.sort_order = sort_order;
		this.reload();
		return false;
	},

	get_sort_order: function() {
		return this.sort_order;
	},

	// Устанавливает/возвращает страницу списка.
	set_page: function(page) {
		this.page = page;
		this.reload();
		return false;
	},

	get_page: function() {
		return this.page;
	},

	// Разрешить/запретить кеширование ответов сервера.
	cache: function(do_cache) {
		this.caching = do_cache;
	},

	// Перезагружает список.
	reload: function() {
		params =
			'sb=' + this.sort_by +
			'&so=' + this.sort_order +
			'&by_page=' + this.by_page +
			'&p=' + this.page;
		if ( !this.caching ) {
			rnd = Math.round(Math.random() * 1000000000);
			params += '&rnd=' + rnd;
		}
		ajx = new Ajax.Request(
			this.store_uri, 
			{method: 'get', parameters: params, onSuccess: this.show}
		);
	},

	// Функция отображения списка - перекрывается.
	show: function(transport) {
		//var response = transport.responseText.evalJSON();
		//alert( response );
		;
	},
	
	pagenavigator: function(count, num_on_page) {
		/** Создаем постраничный навигатор **/
		bPage = $('pager-container');
		if (bPage) 
		{
			/** Удаляем постраничный вывод **/
			bPage.innerHTML='';
			if(Math.ceil(count/num_on_page)>1)
			{
				spn=document.createElement('span');
				spn.setAttribute('class','pages-label');
				spn.setAttribute('className','pages-label');
				spn.appendChild( document.createTextNode('уФТБОЙГЩ:') );
				bPage.appendChild( spn );
				for(i=0; i<Math.ceil(count/num_on_page); i++)
				{
					spn = document.createElement( 'span' );
					p=(i+1);
					if (p==(this.get_page()>0?this.get_page():1))
					{
						spn.setAttribute('class','selected');
						spn.setAttribute('className','selected');
						spn.innerHTML=p;
					}
					else
					{
						m='<a href="javascript:void(0);" onClick="'+this.get_name()+'.pagenav('+p+')">'+p+'</a>';
						spn.innerHTML=m;
					}
					bPage.appendChild( spn );
					bPage.appendChild( document.createTextNode(' ') );
				}
				div = document.createElement( 'div' );
				
				page_prev=this.get_page()-1;
				page_next=this.get_page()+1;

				div.innerHTML='<span><a href="javascript:void(0)" onClick="'+this.get_name()+'.pagenav('+page_prev+')">&larr; РТЕДЩДХЭБС</a></span> <span><a href="javascript:void(0)" onClick="'+this.get_name()+'.pagenav('+page_next+')">УМЕДХАЭБС &rarr;</a></span>';
				bPage.appendChild( div );
			}
		}
	}

});
