/* --> Escorts */
Cubix.Escorts = {};

Cubix.Escorts.url = '/escorts'; // Must be set from php

Cubix.Escorts.Load = function (url, callback) {
	this.container = $('container_escorts_in');
	
	var overlay = new Cubix.Overlay(this.container, {});
	overlay.disable();
	
	var uri = new URI(document.location.href);
	// uri = uri.get('directory') + uri.get('file');
	uri = Cubix.Escorts.url;
	uri = uri.replace(/\/$/, '');
	
	url = uri + '/' + url;
	
	if ( -1 == url.indexOf('?') ) {
		url += '?';
	}
	else {
		url += '&';
	}
	
	url += 'ajax';
	
	new Request({
		url: url,
		method: 'get',
		/*evalScripts:true,*/
		onSuccess: function (resp) {
			this.container.set('html', resp);
			
			if ( $defined(Cubix.PhotoRoller) ) {
				Cubix.PhotoRoller.Init();
			}
			
			Cubix.Tip.Init($$('.availability img'));
			
			overlay.enable();
			
			if ( $defined(callback) ) {
				callback.call(this, resp);
			}
		}.bind(this)
	}).send();
	
	return false;
}
/* <-- */

/* --> Tool Tip */
Cubix.Tip = {};

Cubix.Tip.Init = function (elements) {
	var els = [];
	elements.each(function(element) {
		if ( element.get('title') )
		{
			var content = element.get('title').split('::');
			element.store('tip:title', content[0]);
			element.store('tip:text', content[1]);
			els.include(element);
		}
	});
	
	var tip = new Tips($$(els));
};
/* <-- */

/* --> Filter */
Cubix.Filter = {};

Cubix.Filter.Change = function (filter) {
	var els = [];
	
	if ( $defined(filter.filter) ) {
		els.include({ el: $('filter-options'), value: filter.filter });
	}
	
	if ( $defined(filter.sort) ) {
		els.include({ el: $('sort-options'), value: filter.sort });
	}
	
	if ( ! els.length ) return;
	
	els.each(function (el) {
		var value = el.value;
		el = el.el;
		
		var selected = el.getPrevious('a.first');
		var options = el.getElements('a').filter(function (el) { return ! el.hasClass('sub') });
		var sel = el.getElements('a').filter(function (el) { return el.getStyle('display') == 'none' })[0];
		
		options.each(function (opt) {
			if ( value == opt.get('rel') ) {
				var title = opt.get('html');
				
				if ( ! opt.getParent('li').getParent('li').getParent('ul').hasClass('nav') ) {
					title = opt.getParent('li').getParent('li').getElement('a').get('html') + '-' + title;
				}
				
				opt.clone().setStyle('display', 'block').set('html', title).addClass('first').replaces(selected);
				sel.setStyle('display', null);
				opt.setStyle('display', 'none');
				
				options.empty();
			}
		});
	});
}

Cubix.Filter.InitSearchInput = function (input) {
	Cubix.Filter.InitSearchInput.input = $(input);
	
	this.timer = null;
	
	input.addEvents({
		keyup: function () {
			$clear(this.timer);
			this.timer = setTimeout('Cubix.Filter.InitSearchInput.KeyUp(Cubix.Filter.InitSearchInput.input)', 500);
		}.bind(this)
	});
}

Cubix.Filter.InitSearchInput.KeyUp = function (input) {
	Cubix.Filter.Set({ name: input.get('value').length ? input.get('value') : null });
}

Cubix.Filter.Set = function (filter, clear) {
	if ( ! clear ) {
		this.filter = $merge(this.filter, filter);
	}
	else {
		this.filter = filter;
	}
	
	/*Cubix.Filter.Change(this.filter);*/
	if ( ! clear ) {
		Cubix.LocationHash.Set(Cubix.LocationHash.Make(this.filter));
	}
	
	return false;
}
/* <-- */

/* --> LocationHash */
Cubix.LocationHash = {};

Cubix.LocationHash.Set = function (hash) {
	if ( hash.length ) {
		document.location.hash = '#' + hash;
	}
	else {
		document.location.hash = '';
	}
	
	return false;
}

Cubix.LocationHash.Parse = function () {
	var hash = document.location.hash.substring(1);
	
	if ( ! hash.length ) {
		return {};
	}
	
	var params = hash.split('/');
	
	var filter = {};
	
	params.each(function (param) {
		param = param.split('_');
		var param_name = param[0];
		param.erase(param_name);
		
		if ( param.length ) {
			value = param.join('_');
		}
		else {
			value = '';
		}
		
		eval('filter.' + param_name + ' = value');
	});
	
	return filter;
}

Cubix.LocationHash.Make = function (filter) {
	var hash = '';
	
	for ( var key in filter ) {
		var value;
		eval('value = filter.' + key + ';');
		
		if ( value == null ) continue;
		
		hash += '/' + key;
		
		if ( value.length ) {
			hash +=  '_' + value;
		}
	}
	
	hash = hash.substring(1);
	
	return hash;
}
/* <-- */

/* --> HashController */
Cubix.HashController = {
	_current: '',
	
	init: function () {
		setInterval('Cubix.HashController.check()', 300);
	},
	
	check: function () {
		var hash = document.location.hash.substring(1);
		
		if (hash != this._current) {
			this._current = hash;
			
			Cubix.Escorts.Load(hash, Cubix.HashController.Callback);
		}
	}
};

Cubix.HashController.Callback = function () {
	Cubix.Filter.Change(Cubix.LocationHash.Parse());
	Cubix.Filter.Set(Cubix.LocationHash.Parse(), true);
}
/* <-- */
