;(function($) {
	$.widget('ui.toursearch', {
		
		_init: function() {
			
			var _e = this.element, _o = this.options, self = this;
			
			_e.find('a.selected').click(function(_ev) { _ev.preventDefault() })
			
			$(document).click(function(_ev) {
				
				var _target = $(_ev.target);
				
				if (!_target.closest('div.select', $('#search')).length) {
					
					self._hideAllPopups();
				}
				/*
				if ($(_ev.target).closest('#' + _e.attr('id')).length == 0) {
				
					self._hideAllPopups();
				}
				*/
			});
			
			$.each(_o.elements, function(_id, _data) {
				
				var _link = $('#' + _id),
				_popup = (typeof(_data.getPopup) == 'function') ? _data.getPopup.call(_link) : self._getPopup.call(_link),
				_content = (typeof(_data.getContentHolder) == 'function') ? _data.getContentHolder.call(_popup) : self._getContentHolder.call(_popup);
				
				_link.click(function(_ev) {
					
					if (_data.refreshOnShow !== undefined && _data.refreshOnShow == true) {
					
						if (typeof(_data.getContent) == 'function') {
						
							_data.getContent.call(_content);
						}
					}
					
					self._hideAllPopups();
					
					if (typeof(_data.showPopup) == 'function') {
					
						_data.showPopup.call(_link);
					} else {
						
						self._showPopup.call(_link);
					}
				});
				
				var _setValue = function(_text, _value) {
					
					self._setValue.call(_link, _text, _value);
					
					if (typeof(_o.onChange) == 'function') {
					
						_o.onChange.call(self);
					}
				}
				
				if (typeof(_data.setValue) == 'function') {
					
					_setValue = function(_text, _value) {
						
						_data.setValue.call(_link, _text, _value, _o.onChange);
						
						if (typeof(_o.onChange) == 'function') {
							
							_o.onChange.call(self);
						}
					}
				}
				
				var _hide = function(){
					
					self._hideAllPopups.apply(self);
				}
				
				_content.click(function(_ev) {
					
					if (typeof(_data.onContentBodyClick) == 'function') {
						_data.onContentBodyClick.call(_content, _ev, _setValue, _hide);
					} else {
						self._onContentBodyClick.call(_content, _ev, _setValue, _hide);
					}
				});
				
				_popup.click(function(_ev) {
					
					if (typeof(_data.onPopupBodyClick) == 'function') {
						
						_data.onPopupBodyClick.call(_popup, _ev, _setValue, _hide);
					} else {
						
						self._onPopupBodyClick.call(_popup, _ev, _setValue, _hide);
					}
				})
			});
		},
		
		_setValue: function(_text, _value) {
			
			var _parent = this.parent(),
			_links = _parent.find('a.selected'),
			_input = _parent.find('input:hidden');
			
			_links.children('strong').html(_text);
			_input.val(_value);

		},
		
		_onContentBodyClick: function(_ev, _setValue, _hidePopup) {
			
			var _value_link = $(_ev.target).closest('a');
			
			if (_value_link.length == 0) {
			
				return;
			}
			
			var _text = _value_link.text(),
			_value = _value_link.attr('value');
			
			_setValue(_text, _value);
			
			_hidePopup();
		},
		
		_onPopupBodyClick: function(_ev, _setValue, _hide) {
			
			if ($(_ev.target).closest('a.selected').length) { _hide(); }
		},
		
		_hideAllPopups: function() {
			
			var self = this;
			
			$.each(this.options.elements, function(_id, _data) {
				
				var _link = $('#' + _id);
				
				if (typeof(_data.hidePopup) == 'function') {
				
					_data.hidePopup.call(_link);
				} else {
				
					self._hidePopup.call(_link);
				}
			})
		},
		
		_showPopup: function() {
			
			this.parent().addClass('active');
		},
		
		_hidePopup: function() {
			
			this.parent().removeClass('active');
		},
		
		_getPopup: function() {
		
			return this.parent().find('div.drop_down').eq(0);
		},
		
		_getContentHolder: function() {
		
			return this.find('ul.scroll').eq(0);
		},
		
		_getValue: function() {
			
			return this.parent().find('input:hidden').eq(0).val();
		},
		
		setValue: function(_id, _text, _value) {
			
			
			var _o = this.options, 
			_data = this.options.elements[_id],
			_link = $('#' + _id);
			
			if (_data === undefined) {
			
				return;
			}
			
			if (typeof(_data.setValue) == 'function') {
				
				_data.setValue.call(_link, _text, _value);
			} else {
				
				this._setValue.call(_link, _text, _value);
			}

			if (typeof(_o.onChange) == 'function') {

				_o.onChange.call(self);
			}

		},
		
		getValue: function(_id) {
			
			var _data = this.options.elements[_id],
			_link = $('#' + _id),
			self = this;
			
			if (typeof(_data.getValue) == 'function') {
			
				return _data.getValue.call(_link)
			} else {
			
				return self._getValue.call(_link);
			}
		},
		
		setContent: function(_id, _label, _value, _content) {
		
			if (typeof(this.options.elements[_id]) === undefined) {
				
				return;
			}
			
			var _link = $('#' + _id),
			self = this,
			_data = this.options.elements[_id],
			_popup = (typeof(_data.getPopup) == 'function') ? _data.getPopup.call(_link) : self._getPopup.call(_link),
			_content_holder = (typeof(_data.getContentHolder) == 'function') ? _data.getContentHolder.call(_popup) : self._getContentHolder.call(_popup);
			_content_holder.html(_content);
			
			if (typeof(_data.setValue) == 'function') {
			
				_data.setValue.call(_link, _label, _value);
			} else {
			
				self._setValue.call(_link, _label, _value);
			}
		},
		
		setLoadingState: function(_id, _label) {
			
			if (typeof(this.options.elements[_id]) === undefined) {
				
				return;
			}
			
			var _link = $('#' + _id),
			_fake_link = $('<a />').addClass('fake').attr('href', 'javascript:void(0);').html(_label).insertBefore(_link).show();
			
			_link.hide();
			
			return function() {
				
				_fake_link.remove();
				_link.show();
			}
		},
		
		onChange: function(_f) {
			if (typeof(_f) == 'function') {
			
				this.options.onChange = _f;
			}
		}
	});
	
})(jQuery);

$.extend($.ui.toursearch, {
	'defaults': {
		'elements': {
			'a': {
				'getContent': function(_c) {
					var _w = this;
					_w.title.html('dialog window');
					_w.content.html('window content');
					if (typeof(_c) == 'function') {
						_c.call(this);
					}
				},
				'onDisplay': function(_c) {
					_c.call(this);
				},
				'onClose': function(_c) {
					_c.call(this);
				},
				'permanent': true
			}
		},
		'title_selector': '.b-tex__title h3',
		'content_selector': 'div.l-popup__i_texover',
		'close_selector': 'a.st-close',
		'window_selector': 'div.l-popup_small',
		'onChange': null
	},
	'getter': 'getValue setLoadingState'
});
