;(function($) {
$.widget("ui.searchTourList", $.extend({

	initParams: function() {
		var tsr = parseHash().tsr

		var values = tsr ? parseQuery(tsr) : {}

		if (!values.page) values.page = 1

		if (!values.rows) values.rows = 20
		else values.rows = Math.min(values.rows, 50)

		if (!values.orderBy) values.orderBy='price'

		//if (!values.orderBy) values.orderBy = 'price'
		//if (!values.orderTo) values.orderTo = 'asc'

		this.pagesPerList = 8 // 10 is too much width


		this.params = values
	},

	hideNav: function(){
		this.$navs.hide()
	},

	initShow: function() {
		this.$navs = $('.tsr-nav', this.element)
		this.$table = $('#tsr-table')
		this.$list = $('#tsr-list')
		this.$sort = $('#tsr-sort')

		this.navTmpl = tmpl("tsr-nav-pattern")
		var self = this

		$('a',this.$sort).autohelp('.autohelp')

		$('a',this.$sort).click(function(e) {
			this.blur()
			self.search({orderBy:this.getAttribute('sort')})
			e.stopPropagation()
		})

		this.$navs.click(function(e) {
			var o = e.target
			do {
				if (!o) break // happened once in testing (how?)
				if (o.getAttribute && o.getAttribute('page')) {
					self.search({page:o.getAttribute('page')})
					e.stopPropagation()
					break;
				}
				if (o.tagName=='UL')  break;
				o = o.parentNode
			} while(true)

		})

	},

	setTsfParams: function(tsf) {
		this.tsf = tsf
	},
	
	_init: function() {
		
		this.init.apply(this, arguments);
	},

	init: function() {
		this.ajaxController = new AjaxController(this, '/tour/', '.php')
		this.initParams()

		this.initShow()

	},

	getCount: function() {
		return this.count
	},

	showTours: function(data) {

		$('a.sel', this.$sort).removeClass('sel')

		if (this.params.orderBy) {
			$('.'+this.params.orderBy+' a', this.$sort).addClass('sel')
		}

		var pagesTotal = Math.ceil(data.count / this.params.rows)

		var first = data.page - this.pagesPerList/2
		data.first = Math.max(1, first)

		data.last = Math.min(data.first + this.pagesPerList, pagesTotal)

		if (data.last - data.first < this.pagesPerList) {
			data.first = Math.max(1, data.last - this.pagesPerList)
		}

		this.$navs.html(this.navTmpl(data))

		$(this.$navs[0]).append($('<div class="find">найдено <strong>'+data.count+'</strong> предложений</div>'))

		this.count = data.count

		this.$navs.show()


		this.$list.html(data.tours)
		this.$table.show()

	},

	onLoaded: function(data) {

		if (!data) {
			this.showError('.server-error')
			return
		}

		if (!data.count) {
			this.showError('.notfound')
			return
		}
		if (data.count && !data.page) {
			this.search({page:1})
			return
		}

		this.showTours(data)


	},


	showError: function(selector) {
		this.$table.hide()
		this.$navs.empty()
		this.$list.empty()
		$(selector, this.element).show();
	},

	hideError: function() {
		$('.error', this.element).hide()
	},

	onLoadError: function(errinfo) {

		if (errinfo.errcode == 'timeout') {
			this.showError('.timeout')
		} else {
			this.showError('.server-error')
		}

	},

	search: function(params) {
		this.hideError()

		if (window.opera) {
			var offset = $('#tsf-top').offset({ lite: true });
			window.scrollTo(offset.left, offset.top)
		} else {
			$('#tsf-top')[0].scrollIntoView(true)
		}


		for(var p in params) {
			this.params[p] = params[p]
		}

		var tsr_query = makeQuery(this.params)
		var tsf_query = makeQuery(this.tsf)
		ignoreNextHistoryChange = true

		location.hash = 'tsf='+tsf_query+';tsr='+tsr_query


		var self = this

		var parameters = 'tsf='+tsf_query+'&&tsr='+tsr_query

		$('#tour-announce').hide()
		$('.tour-announce').hide()
		this.$navs.hide()
		this.element.show() // show for loading indication

		this.count = null

		this.ajaxController.sendRequest({url:'tours', data: parameters})
	},

	deleteTour: function(id) {
		var ajaxer = {
			onLoaded: function() {
				$('#tour-list-'+id).remove()
			}
		}

		new AjaxController(ajaxer, '/tour/', '.php').sendRequest({url:'delete', data: 'tsf=id='+id+'&nocache='+Math.random()})
	}

}, classLoadingWidgetMixin))

	$.extend($.ui.searchTourList, { getter: "getCount"});
})(jQuery);