/**
 * search.js
 * 
 */

var myPioneerSearch;

/**
 * Pioneer search options
 * 
 */
PioneerSearchOptions = Object.extend({
	ajaxUrl: {
		search: '/hu/ajax/alapkereso/'
		},
	ajaxMethod: 'post',
	ajaxLoadingImg: '/images/search_ajax_loading.gif',
	elements: {
		searchForm: 'alapkeresoSearchForm',
		searchResult: 'alapkeresoSearchResult',
		informationPanel: 'informationPanel',
		informationPanelContent: 'informationPanelContent',
		disableClickPanel: 'disableClickPanel'
		},
	messages: {
		},
	defaultChecked: [
		'classCheckboxC'
		]
	}, window.PioneerSearchOptions || {});

/**
 * Pioneer search class
 *
 */
var PioneerSearch = Class.create();

PioneerSearch.prototype = Object.extend(new PioneerCommon(), {
	/**
	 * Parameters
	 */
	actions: {},
	overlay: null,
	searchForm: null,
	searchResult: null,
	informationPanel: null,
	informationPanelContent: null,
	informationISIN: null,
	defaultChecked: [],
	searchInProgress: false,
	disableClickPanel: null,

	/**
	 * Methods
	 *
	 * Class constructor
	 * 
	 * @return PioneerOrder
	 */
	initialize: function() {
		this.ajaxUrl = PioneerSearchOptions.ajaxUrl;
		this.ajaxMethod = PioneerSearchOptions.ajaxMethod;
		this.ajaxLoadingImg = Builder.node('img', {'src': PioneerSearchOptions.ajaxLoadingImg, 'alt': '', 'class': 'searchAjaxLoadingImg'});
		this.elements = PioneerSearchOptions.elements;
		this.messages = PioneerSearchOptions.messages;

		this.overlay = new PioneerOverlay();

		this.searchForm = $(this.elements.searchForm);
		this.searchResult = $(this.elements.searchResult);
		this.informationPanel = $(this.elements.informationPanel);
		this.informationPanelContent = $(this.elements.informationPanelContent);
		
		this.defaultChecked = PioneerSearchOptions.defaultChecked;

		this.disableClickPanel = $(this.elements.disableClickPanel);

		if (this.disableClickPanel) {
			this.disableClickPanel.setOpacity(0.01);
			this.disableClickPanel.observe('click', function(){return false;});
		}

		this.setCheckboxes();

		this.search();
	},

	search: function() {
		if (this.searchForm) {
			var params = Form.serialize(this.searchForm, true);

			new Ajax.Request(
				this.ajaxUrl.search,
				{
					method: this.ajaxMethod,
					parameters: params,
					onCreate: (
						function() {
							this.searchInProgress = true;

							if (this.disableClickPanel) {
								this.disableClickPanel.show();
							}

							this.updateElement(this.searchResult, this.ajaxLoadingImg);
						}).bind(this),
					onComplete: (
						function(response) {
							this.getAjaxResult(response);

							if (this.ajaxResult.design) {
								this.updateElement(this.searchResult, this.ajaxResult.design);
							}
							
							if (this.disableClickPanel) {
								this.disableClickPanel.hide();
							}

							this.searchInProgress = false;
						}).bind(this),
					onFailure: (this.handleAjaxFailure).bind(this)
				});
		}
	},

	setCheckboxes: function() {
		if (this.searchForm) {
			var objs = Form.getInputs(this.searchForm, 'checkbox');
			
			if (objs && objs.length) {
				objs.each(
					function(obj) {
						obj.checked = false;
					});
			}
			
			if (this.defaultChecked && this.defaultChecked.length) {
				this.defaultChecked.each(
					function(objName) {
						var obj = $(objName);
						
						if (obj) {
							obj.checked = true;
							this.setActiveItem(obj);
						}
					}.bind(this));
			}
		}
	},

	setActiveItem: function(obj, startSearch) {
		if (this.searchInProgress !== false) {
			return;
		}

		if (obj && obj.id) {
			var itemObj = $(obj.id.replace(/Checkbox/, 'Item'));

			if (itemObj) {
				if (obj.checked) {
					itemObj.addClassName('groupItemActive');
				} else {
					itemObj.removeClassName('groupItemActive');
				}
			}

			if (typeof(startSearch) != 'undefined' && startSearch) {
				this.search();
			}
		}
	},

	showInformationPanel: function(isin) {
		var iObj = $(isin+'Information');

		if (iObj && this.informationPanel && this.informationPanelContent && typeof(mousePosition) == 'object') {
			if (this.informationISIN != isin) {
				this.updateElement(this.informationPanelContent, iObj.innerHTML);
				this.informationISIN = isin;
			}
			var panelDimensions = this.informationPanel.getDimensions();
			
			this.informationPanel.setStyle({
				top: (mousePosition.y - (panelDimensions.height + 8)) + 'px',
				left: (mousePosition.x - 165) + 'px'
				});

			this.informationPanel.show();
		}
	},

	hideInformationPanel: function() {
		if (this.informationPanel && this.informationPanelContent) {
			this.informationISIN = null;
			this.updateElement(this.informationPanelContent, this.emptyDivHTML);
			this.informationPanel.hide();
		}
	}
});

/**
 * Create search object
 *
 * @return void
 */
function createPioneerSearch() {
	myPioneerSearch = new PioneerSearch();

	document.onmousemove = myPioneerSearch.getMousePosition;
}
