/* ---------------------------------------------------------------------------------------------------- */
/* =sospo.js */
/* ---------------------------------------------------------------------------------------------------- */

var sospo = {};


/* -------------------------------------------------- */
/* =search */

var search = new Class({
	
	initialize: function (container) {
		this.container = $(container);
		this.results = new this.results(this);
		this.data = new this.data(this);
		this.region = new this.region(this);
		this.bathtype = new this.bathtype(this);
		this.order = new this.order(this);
		this.quick = new this.quick(this);
		this.map = new this.map(this);
		this.meta = new this.meta(this);
		pageTracker._trackPageview();
		localTracker._trackPageview();
	},
	
	region: new Class({
		initialize: function (_root) {
			this._root = _root;
			this.selection = [];
			this.items = this._root.container.getElements('.region input');
			this.items.each(function(item){
				if (item.checked) this.add(item);
				if (item.hasClass('all')) item.addEvent('click',this.all.pass(item,this));
				else item.addEvent('click',this.toggle.bind(this,item));
			},this);
			if (this.selection.length > 0) window.addEvent('load',this._root.data.countdown.bind(this._root.data));
		},
		toggle: function (item) {
			(this.selection.test(item.getProperty('name'))) ? this.remove(item) : this.add(item);
			this._root.data.countdown();
		},
		add: function (item) {
			this.selection.push(item.getProperty('name'));
			item.checked = true;
		},
		remove: function (item) {
			this.selection.remove(item.getProperty('name'));
			item.checked = false;
		},
		all: function (all) {
			var bln = all.checked;
			this.selection = [];
			this.items.each(function(item){
				if (bln && item != all) this.add(item);
				item.checked = bln;
			},this);
			this._root.data.countdown();
		},
		clear: function () {
			this.items.each(function(item){	item.checked = false },this);
			this.selection = [];
		}
	}),
	
	bathtype: new Class({
		initialize: function (_root) {
			this._root = _root;
			this.selection = [];
			this.items = this._root.container.getElements('.bathtype input');
			this.items.each(function(item){
				if (item.checked) this.add(item);
				if (item.hasClass('all')) item.addEvent('click',this.all.pass(item,this));
				else item.addEvent('click',this.toggle.bind(this,item));
			},this);
			if (this.selection.length > 0) window.addEvent('load',this._root.data.countdown.bind(this._root.data));
		},
		toggle: function (item) {
			(this.selection.test(item.getProperty('name'))) ? this.remove(item) : this.add(item);
			this._root.data.countdown();
		},
		add: function (item) {
			this.selection.push(item.getProperty('name'));
			item.checked = true;
		},
		remove: function (item) {
			this.selection.remove(item.getProperty('name'));
			item.checked = false;
		},
		all: function (all) {
			var bln = all.checked;
			this.selection = [];
			this.items.each(function(item){
				if (bln && item != all) this.add(item);
				item.checked = bln;
			},this);
			this._root.data.countdown();
		},
		clear: function () {
			this.items.each(function(item){	item.checked = false },this);
			this.selection = [];
		}
	}),
	
	order: new Class({
		initialize: function (_root) {
			this._root = _root;
			this.active = 0;
			this.mode = 0;
		},
		setup: function () {
			this.items = $ES('#results th a');
			var i=0;
			this.items.each(function(item){
				item.addEvent('click',this.set.bindAsEventListener(this,i));
				(this.active == i && this.mode == 1) ? item.addClass('desc') : item.removeClass('desc');
				i++;
			},this);
		},
		set: function (event, index) {
			var event = new Event(event); event.preventDefault();
			if (this.active == index) {
				(this.mode == 0) ? this.mode = 1 : this.mode = 0;
			}
			this.active = index;
			this._root.data.send();
		}
	}),
	
	data: new Class({
		initialize: function (_root) {
			this._root = _root;
			this.timer = null;
			this.params = '';
		},
		countdown: function () {
			this.timer = $clear(this.timer);
			this.timer = this.send.delay(650,this);
			this._root.quick.clear();
		},
		send: function () {
			this._root.results.fx.hide();
			this.params = '';
			if (this._root.region.selection.length > 0) this.params += '&regions=;' + this._root.region.selection.join(';') + ';';
			if (this._root.bathtype.selection.length > 0) this.params += '&bathtypes=;' + this._root.bathtype.selection.join(';') + ';';
			if (this._root.quick.keyword.length > 0 ) this.params += '&search=' + this._root.quick.keyword;
			if (this.params.length == 0) {
				$('results-inject').setHTML('');
				this._root.results.container.setStyle('display','none');
				$('sms-promo-container').setStyle('display','block');
				$('images-container').setStyle('display','block');
				return false;
			}
			if (days != null) this.params += '&days=' + days;
			if (overload != null) this.params += overload;
			this.params += '&orderby=' + this._root.order.active + '&mode=' + this._root.order.mode;
			this.params = this.params.substring(1);
			this.ajax = new Ajax('/_service/search.asp', {
				postBody: this.params,
				update: $('results-inject'),
				onComplete: this._root.results.show.bind(this._root.results)
			});
			this.ajax.setHeader('Content-type', 'application/x-www-form-urlencoded; charset=utf-8');
			this.ajax.request();
		}
	}),
	
	results: new Class({
		initialize: function (_root) {
			this._root = _root;
			this.container = $('results-container');
			this.fx = new Fx.Style(this.container, 'opacity').hide();
		},
		show: function () {
			$('sms-promo-container').setStyle('display','none');
			$('images-container').setStyle('display','none');
			this.container.setStyle('display','block');
			this._root.order.setup();
			this._root.meta.set_print();
			this._root.meta.set_reader();
			this._root.meta.set_lang();
			this.fx.start(1);
		},
		hide: function () {
			this.fx.start(0);
		}
	}),
	
	meta: new Class({
		initialize: function (_root) {
			this._root = _root;
			this.print = $E('a.pdf');
			this.printer_url = this.print.getProperty('href');
			this.reader = $E('a.readspeaker');
			this.reader_url = this.reader.getProperty('href');
			this.lang_switches = $ES('div#lang a');
		},
		set_print: function () {
			this.print.setProperty('href',encodeURI(this.printer_url + this._root.data.params));
		},
		set_reader: function () {
			this.reader.setProperty('href',encodeURI(this.reader_url + this._root.data.params));
		},
		set_lang: function () {
			this.lang_switches.each(function(anchor){
				anchor.setProperty('href','?lang=' + anchor.getProperty('title') + '&' + this._root.data.params);
			},this);
		}
	}),
	
	quick: new Class({
		initialize: function (_root) {
			this._root = _root;
			this.frm = $('frm-quicksearch').addEvent('submit',this.send.bindAsEventListener(this));
			this.input = $('frm-quicksearch-keyword');
			this.btn = $('frm-quicksearch-btn-submit').addEvent('click',this.send.bindAsEventListener(this));
			this.keyword = '';
			//if (this.input.getValue().length > 0) window.addEvent('load',this.send.bind(this));
			if (this.input.getValue().length > 0) this.send.delay(500,this);
		},
		send: function (event) {
			if (event && event.type == 'click') {
				var event = new Event(event); event.preventDefault();
			}
			this.keyword = ''; this.keyword = this.input.getValue();
			if (this.keyword.length == 0) return false;
			this._root.region.clear(); this._root.bathtype.clear();
			this._root.data.send();
		},
		clear: function () {
			this.input.value = '';
			this.keyword = '';
		}
	}),
	
	map: new Class({
		initialize: function (_root) {
			this._root = _root;
			this.img = $('map');
			this.areas = $('map-regions').getElements('area');
			this.areas.each(function(area){
				var item = this._root.region.items.filter(function(item){
					return item.getProperty('name') == area.getProperty('id');
				},this);
				area.addEvent('mouseover',this.over.bind(this,area));
				area.addEvent('mouseout',this.out.bind(this,area));
				area.addEvent('click',this.set.bindAsEventListener(this,item));
			},this);
		},
		set: function (event, item) {
			var event = new Event(event); event.preventDefault();
			this._root.region.toggle(item);
		},
		over: function (area) {
			this.img.setProperty('src','/_gfx/map-'+ area.getProperty('id').substring(7) +'.gif');
		},
		out: function (area) {
			this.img.setProperty('src','/_gfx/map.gif');
		}
	})
	
});
if (window.location.pathname == '/home/') window.addEvent('domready',function () { sospo.search = new search('frm-search') });


/* -------------------------------------------------- */
/* =details */

var acco = Accordion.extend({
	display: function(index){
		if ((this.timer && this.options.wait) || (index === this.previous && !this.options.alwaysHide)) return this;
		this.previous = index;
		var obj = {};
		this.elements.each(function(el, i){
			
			if (i == index) {
				obj[i] = {};
				if (el.offsetHeight > 0) {
					this.fireEvent('onBackground', [this.togglers[i], el]);
					for (var fx in this.effects) obj[i][fx] = 0;
				} else {
					this.fireEvent('onActive', [this.togglers[i], el]);
					for (var fx in this.effects) obj[i][fx] = el[this.effects[fx]];
				}
			}
		}, this);
		return this.start(obj);
	}
});

var details = new Class({
	
	initialize: function () {
		this.infos = new this.infos(this);
		pageTracker._trackPageview();
		localTracker._trackPageview();
	},
	
	infos: new Class({
		initialize: function (_root) {
			this._root = _root;
			this.container = $('info-container');
			this.accordion = new acco(this.container.getElements('h3.toggler'),this.container.getElements('div.chapter'),{ alwaysHide: true });
		}
	})
	
});
if (window.location.pathname == '/details/') window.addEvent('domready',function () { sospo.details = new details() });
