/**
 * PraceSelect object
 * For support diacritics must be available stripDiac function which strip diacritics from text (used by keyboard shortcuts)
 * 
 * @param DomElement toggleEl toggle element which open/close selectbox
 * @return void
 */
var PraceSelect = function(toggleEl){
		this.init(toggleEl);
}

/**
 * PraceSelect constructor
 * 
 * @param DomElement toggleEl toggle element which open/close selectbox
 * @return void
 */
PraceSelect.prototype.init = function(toggleEl) {
	var self = this;
	self.toggleEl = toggleEl;
	self.hiddenContainer = true;
	self.lastKey = null;
	self.keyTimeout = null;
	self.highlightPos = null;
	self.mouseOver = false;
	self.itemsCount = null;
	self.containerEl = null;
	self.items = null;
	
	YAHOO.util.Event.addListener(toggleEl, 'keydown', function(event) { self.keydownToggleEl(event); } );
	YAHOO.util.Event.addListener(toggleEl, 'click', function(event) { self.clickToggleEl(event); } );
	YAHOO.util.Event.addListener(toggleEl, 'mouseover', function(event) { self.mouseOverToggleEl(event); } );
	YAHOO.util.Event.addListener(toggleEl, 'mouseout', function(event) { self.mouseOutToggleEl(event); } );
	
	this.showContainerEvent = new YAHOO.util.CustomEvent("showContainer", this);
	this.hideContainerEvent = new YAHOO.util.CustomEvent("hideContainer", this);
	this.highlightEvent = new YAHOO.util.CustomEvent("highlight", this);
	this.chooseEvent = new YAHOO.util.CustomEvent("choose", this);
}

/**
 * Event which will be triggered when selectbox is opened
 */
PraceSelect.prototype.showContainerEvent = null;

/**
 * Event which will be triggered when selectbox is closed
 */
PraceSelect.prototype.hideContainerEvent = null;

/**
 * Event which will be triggered when item from selectbox is highlighted
 */
PraceSelect.prototype.highlightEvent = null;

/**
 * Event which will be triggered when item from selectbox is choosed
 */
PraceSelect.prototype.chooseEvent = null;

/**
 * Set container element
 * 
 * @param DomElement containerEl
 * @return void
 */
PraceSelect.prototype.setContainerEl = function(containerEl) {
	var self = this;
	self.containerEl = containerEl;
	YAHOO.util.Event.addListener(self.containerEl, 'keydown', function(event) { self.keydownContainer(event); } );
	YAHOO.util.Event.addListener(self.containerEl, 'mouseout', function(event) { self.mouseOutContainer(event); } );
	YAHOO.util.Event.addListener(self.containerEl, 'mouseover', function(event) { self.mouseOverContainer(event); } );
}

/**
 * Set container items - add listeners for selectbox items
 * 
 * @param DomNodeList items
 * @return void
 */
PraceSelect.prototype.setContainerItems = function(listItems) {
	var self = this;
	for (var i = 0; i < listItems.length; i++ ) {
		YAHOO.util.Event.addListener(listItems[i], 'blur', function(event) { self.blurContainerItem(event); });
		YAHOO.util.Event.addListener(listItems[i], 'click', function(event) { self.select(event); });
		YAHOO.util.Event.addListener(listItems[i], 'mouseover', function(event) { self.mouseOverContainerItem(event); });				
	}
	this.setItemsCount(listItems.length);
	this.items = [];
	for (var i = 0; i < listItems.length; i++) {
		this.items[i] = listItems[i];
	}
}

/**
 * Set count of items
 * 
 * @param integer count
 * @return void
 */
PraceSelect.prototype.setItemsCount = function(count) {
	this.itemsCount = count >= 0 ? count : null;
}

/**
 * Determine if the container is closed or opened
 * 
 * @return boolean
 */
PraceSelect.prototype.isHiddenContainer = function() {
	return this.hiddenContainer == true ? true : false;
}

/**
 * Show container
 * 
 * @return void
 */
PraceSelect.prototype.showContainer = function() {
	if (this.containerEl !== null) {
		this.containerEl.style.display = 'block';
	}
	this.showContainerEvent.fire(this);			
	this.hiddenContainer = false;						
}

/**
 * Hide container
 * 
 * @return void
 */
PraceSelect.prototype.hideContainer = function() {
	this.hideContainerEvent.fire(this);
	if (this.containerEl !== null) {
		this.containerEl.style.display = 'none';
	}
	this.hiddenContainer = true;
}

/**
 * Choose is called when selectbox item is choosed
 * 
 * @return void
 */
PraceSelect.prototype.choose = function() {
	this.chooseEvent.fire(this, this.highlightPos);
}

/**
 * Highlight previous item
 * 
 * @return void
 */
PraceSelect.prototype.highlightPrev = function() {
	this.highlight(this.highlightPos == null ? 0 : this.highlightPos-1);
}

/**
 * Highlight next item
 * 
 * @return void
 */
PraceSelect.prototype.highlightNext = function() {
	this.highlight(this.highlightPos == null ? 0 : this.highlightPos+1);
}

/**
 * Highlight specified item
 * Check if position must be greater than 0 or equal to 0 and smaller than total items count
 * 
 * @param integer position
 * @param bool hideContainer
 * @return void
 */
PraceSelect.prototype.highlight = function(position, hideContainer) {
	if (position < 0 || (this.itemsCount != null && position >= this.itemsCount)) {
		return;
	}
	if (!hideContainer && this.isHiddenContainer()) {
		this.showContainer();
	}
	this.highlightPos = position;
	this.highlightEvent.fire(this, this.highlightPos);
	
}

/**
 * Default handler when key is pressed over toggle element
 * 
 * @param Event event
 * @return void
 */
PraceSelect.prototype.keydownToggleEl = function(event) {
	this.keydownContainer(event);
}

/**
 * Default handler when key is pressed over container
 * 
 * @param Event event
 * @return void
 */
PraceSelect.prototype.keydownContainer = function(event) {
	var self = this;	
	if (event.keyCode != 9 && this.isHiddenContainer()) {
		self.showContainer();
	}
	self.lastKey = event.keyCode;
	self.keyTimeout = setTimeout(function() { clearTimeout(self.keyTimeout); self.lastKey = null; }, 200);
	
	switch (event.keyCode) {
		case 39: // right
		case 13: //enter
			self.choose();
			self.hideContainer();
			YAHOO.util.Event.stopEvent(event);		
			break;
		case 9:
			if (!self.isHiddenContainer()) {
				self.hideContainer();
				YAHOO.util.Event.stopEvent(event);
			}
			break;
		case 27: //esc
			self.hideContainer();
			YAHOO.util.Event.stopEvent(event);
			break;
		case 38: //up
			self.highlightPrev();
			YAHOO.util.Event.stopEvent(event);
			break;
		case 40: // down
			self.highlightNext();
			YAHOO.util.Event.stopEvent(event);
			break;
	}
	
	if (event.keyCode >= 65 && event.keyCode <= 90) { // [A-Z]
		var findNext = false;
		var start = self.highlightPos + 1;
		var stop = self.items.length;
		while (!findNext) {
			for (var i = start; i < stop; i++) {
				var content = self.items[i].textContent ? self.items[i].textContent : self.items[i].innerText;
				if (typeof stripDiac != 'undefined') {
					content = stripDiac(content);
				}
				if (content.charCodeAt(0) == event.keyCode) {
					self.highlight(i);
					findNext = true;
					break;
				}
			}
			if (start == 0 && stop == self.highlightPos) {
				findNext = true;
			}
			start = 0;
			stop = self.highlightPos;
		}
	}
}

/**
 * Default handler when click is registered over toggle element
 * 
 * @param Event event
 * @return void
 */
PraceSelect.prototype.clickToggleEl = function(event) {		
	if (!this.isHiddenContainer()) {
		this.hideContainer();
	} else {
		this.showContainer();
		this.highlight(this.highlightPos == null ? 0 : this.highlightPos);
	}
	YAHOO.util.Event.preventDefault(event);			
}

/**
 * Default handler when mouse is over toggle element
 * 
 * @param Event event
 * @return void
 */
PraceSelect.prototype.mouseOverToggleEl = function(event) {
	this.mouseOver = true;
}

/**
 * Default handler when mouse is over container
 * 
 * @param Event event
 * @return void
 */
PraceSelect.prototype.mouseOverContainerItem = function(event) {
	var self = this;
	self.mouseOver = true;
	var items = self.items;
	var target = YAHOO.util.Event.getTarget(event);
	for (var i = 0; i < items.length; i++) {
		if (target == items[i]) {
			self.highlight(i);
		}
	}
}

/**
 * Default handler when mouse is out of container
 * 
 * @param Event event
 * @return void
 */
PraceSelect.prototype.mouseOutContainer = function(event) {
	this.mouseOver = false;
}

/**
 * Default handler when mouse is over the container
 * 
 * @param Event event
 * @return void
 */
PraceSelect.prototype.mouseOverContainer = function(event) {
	this.mouseOver = true;
}

/**
 * Default handler when mouse is out of toggle element
 * 
 * @param Event event
 * @return void
 */
PraceSelect.prototype.mouseOutToggleEl = function(event) {
	this.mouseOver = false;
}

/**
 * Default handler when container lost focus
 * 
 * @param Event event
 * @return void
 */
PraceSelect.prototype.blurContainerItem = function(event) {
	if (!this.mouseOver && this.lastKey != 38 && this.lastKey != 40) {
		this.hideContainer();
	}
}

/**
 * Default handler when item is selected by mouse click
 */
PraceSelect.prototype.select = function(event) {
	this.choose();
	this.hideContainer();
	YAHOO.util.Event.preventDefault(event);
}


/**
 * Handler for locality map modal - choose locality on flash map
 * 
 * @param id of region
 * @param name of region
 * @return void
 */
function selectMapLocality(id, name) {
	 //localityChooseByCode(id);
	var items = document.getElementById('localityList').getElementsByTagName('a');
		for (var itemIndex in items) {
			if (parseInt(itemIndex) != itemIndex) continue;
			if (items[itemIndex].getAttribute('rel') == id) {
				localitySelect.highlight(itemIndex);
  				localitySelect.choose();
  				localitySelect.hideContainer();  				
			}
		}
	 
	 Modal.hide();
}
 
/**
* Handler for locality map modal - click on close button on flash map
* 
* @return void
*/
function closeMap() {
	Modal.hide();
}

/**
 * Helper method for strip diacritics (just for Czech & Slovak)
 *
 * @param string s
 * @return string
 */
function stripDiac(s) {
	var nodiac = {
			'á': 'a',
			'č': 'c',
			'ď': 'd',
			'é': 'e',
			'ě': 'e',
			'í': 'i',
			'ň': 'n',
			'ó': 'o',
			'ř': 'r',
			'š': 's',
			'ť': 't',
			'ú': 'u',
			'ů': 'u',
			'ž': 'z',
			'ý': 'y',
			'ŕ': 'r',
			'ľ': 'l',
			'ĺ': 'l',
			'ä': 'a',
			'ô': 'o',
			'Á': 'A',
			'Č': 'C',
			'Ď': 'D',
			'É': 'E',
			'Ě': 'E',
			'Í': 'I',
			'Ň': 'N',
			'Ó': 'O',
			'Ř': 'R',
			'Š': 'S',
			'Ť': 'T',
			'Ú': 'U',
			'Ů': 'U',
			'Ž': 'Z',
			'Ŕ': 'R',
			'Ľ': 'L',
			'Ĺ': 'L',
			'Ä': 'A',
			'Ô': 'O',
			'Ý': 'Y'
		     };
	var result = [];
	for(var i=0; i<s.length; i++) {
	    result[result.length] = typeof nodiac[s.charAt(i)] != 'undefined' ? nodiac[s.charAt(i)] : s.charAt(i);
	}
	return result.join('');
}

/**
 * Toggle extended search
 *
 * @return void
 */
function toggleExtendedSearch(event) {
	var container = YAHOO.util.Dom.get('extendedSearchContainer');
	var promo = YAHOO.util.Dom.get('promo-290x100');
	if (container.style.display == 'none') {
		var exInput = document.createElement('input');
		var isMSIE = /*cc_on*/false;
		if (isMSIE) {
			exInput.type = 'hidden';
		} else {
			exInput.setAttribute('type', 'hidden');
		}
		exInput.setAttribute('name', 'showExtendedSearch');
		exInput.setAttribute('value', '1');
		exInput.setAttribute('id', 'showExtendedSearch');
		YAHOO.util.Dom.get('extendedSearchContainer').appendChild(exInput);
		container.style.display = 'block';
		YAHOO.util.Dom.get('extendedSearch').className = 'arrow-active';
		if (promo) {
			promo.style.display = 'block';
		}
	} else {
		var exInput = YAHOO.util.Dom.get('showExtendedSearch');
		if (exInput) {
			exInput.parentNode.removeChild(exInput);
		}
		container.style.display = 'none';
		YAHOO.util.Dom.get('extendedSearch').className = 'arrow';
		if (promo) {
			promo.style.display = 'none';
		}
	}
	if (event) {
		YAHOO.util.Event.preventDefault(event);
	}
}

