var ResultListClass = Class.create();
ResultListClass.prototype = {
	//Properties
	items_array: null,
	el: null,
	id: null,
	vars: null,
	result_items: null,
	pagination: null,
	current_page: null,
	current_selected: null,
	ajax: null,
	ajax_url: 'ajax/',
	
	//Constructor
	initialize: function(el) {
		this.el = el;
		this.id = el.id;
		this.current_page = 0;
		this.items_array = new Array(),
		this.items_array[this.current_page] = this.el.innerHTML;
		this.findPagination();
		this.findItems();
	},
	
	//Methods
	selectItem: function(e) {
		el = e.currentTarget || Event.findElement(e, 'a');
		if (!Element.hasClassName(el, 'selected')) {
			this.removeSelected();
			Element.addClassName(el, 'selected');
		}
		this.current_selected = el.href.getUrlArgument();
		
		this.vars = 'id=' + this.current_selected + '&table=' + this.id + '&type=record';
		this.incomingType = 'record';
		this.outgoingAJAX();
			
		if (this.id == 'photo_list' && articles.selected())
			articles.removeSelected();
		if (this.id == 'article_list' && photos.selected())
			photos.removeSelected();
		
	},
	prevPage: function() {
		if (this.current_page > 0)
		{
			this.current_page = this.current_page - 1;
			this.vars = 'page=' + this.current_page + '&table=' + this.id + '&type=list';
			this.incomingType = 'list';
			this.outgoingAJAX();
		}
	},
	nextPage: function() {
		this.current_page = this.current_page + 1;
		this.vars = 'page=' + this.current_page + '&table=' + this.id + '&type=list';
		this.incomingType = 'list';
		this.outgoingAJAX();
	},
	incomingAJAX: function(t) {
		//alert(t.responseText);
		this.n_content = DOM.getElementTextNS('', 'result', t.responseXML, 0);
		if (this.n_content && this.incomingType == 'list')
		{
			this.items_array[this.current_page] = this.n_content;
			Element.update(this.el, this.n_content);
			this.findItems();
		}
		else if (this.n_content && this.incomingType == 'record')
		{
			Element.update('result', this.n_content);
		}
	},
	outgoingAJAX: function() {
		if (this.incomingType == 'record' || !this.items_array[this.current_page])
		{
			var vars = this.vars;
			this.vars = '';
			this.ajax = new Ajax.Request(this.ajax_url,
				{
					parameters: vars,
					onSuccess: this.incomingAJAX.bind(this)
				}
			);
		}
		else
		{
			this.cachedAjax();
		}
	},
	cachedAjax: function() {
		Element.update(this.el, this.items_array[this.current_page]);
		this.findItems();
	},
	findPagination: function() {
		if (!this.pagination) {
			var context = this.el.nextSibling;
			while (context.nodeType == 3) {
				context = context.nextSibling;
			}
			if (context.tagName == 'P' && Element.hasClassName(context, 'pagination')) {
				this.pagination = context;
			}
		}
		var pagination_links = this.pagination.getElementsByTagName('a');
		if (pagination_links.length) {
			for (var i = 0; i < pagination_links.length; i++) {
				if (Element.hasClassName(pagination_links[i], 'prev'))
					Event.observe(pagination_links[i], 'click', this.prevPage.bind(this), false);
				if (Element.hasClassName(pagination_links[i], 'next'))
					Event.observe(pagination_links[i], 'click', this.nextPage.bind(this), false);
			}
		}
	},
	findItems: function() {
		this.result_items = this.el.getElementsByTagName('a');
		for (var i = 0; i < this.result_items.length; i++) {
			Event.observe(this.result_items[i], 'click', this.selectItem.bind(this), false);
			if (this.result_items[i].href.getUrlArgument() == this.current_selected)
				Element.addClassName(this.result_items[i], 'selected');
		}
		if (!this.current_selected)
		{
			if (this.id == 'article_list' && (typeof(photos) == 'undefined' || !photos.selected()))
				this.selectFirst();
			else if(this.id == 'photo_list' && (typeof(articles) == 'undefined' || !articles.selected()))
				this.selectFirst();
		}
	},
	selectFirst: function() {
		this.removeSelected();
		if (!Element.hasClassName(this.result_items[0], 'selected'))
		{
			Element.addClassName(this.result_items[0], 'selected');
			this.current_selected = this.result_items[0].href.getUrlArgument();
			this.vars = 'id=' + this.current_selected + '&table=' + this.id + '&type=record';
			this.incomingType = 'record';
			this.outgoingAJAX();
		}
	},
	removeSelected: function() {
		for (var i = 0; i < this.result_items.length; i++) {
			if (Element.hasClassName(this.result_items[i], 'selected'))
				Element.removeClassName(this.result_items[i], 'selected');
		}
		this.current_selected = null;
	},
	selected: function ()
	{
		return this.current_selected;
	}
};

//instantiate and use the object 
var ResultListClass_Rules = {
	'#article_list' : function(el) {
		articles = new ResultListClass (el);
	},
	'#photo_list' : function(el) {
		photos = new ResultListClass (el);
	}
}
Behaviour.register(ResultListClass_Rules);
