function customSelect (settings) {
	var t = this;
	var s = settings;

	t.container	 = s.container;
	t.scontainer = s.container.children('div.sh');
	t.svalue	 = s.container.children(s.value);
	t.list		 = s.container.find(s.list);
	t.option	 = s.option;

	t.scontainer.hide();

	t.useBubbles = s.useBubbles || false;

	t.fn = {};
	$.extend(t.fn, s.fn);

	t.svalue.bind('click keydown', t, t.show);
	t.list.bind('click', t, t.select);
	t.container.bind('mousewheel', t, t.roll);

	t.timeStamp = new Date();

	t.value = 0;

	t.list.html($('#' + settings.data_id).children().html());
//
//	if (s.fixWidth) {
//		t.container.width(Math.round(t.scontainer.width() / 14) + 0.2 + 'em');
//		t.list.children().each(function () {
//			var item = $(this);
//			item.children().text(this.title)
//			this.title = '';
//		});
//	}

	t.size = t.list.children().length;

	if (t.useBubbles) {
		t.helperContainer = $('#' + s.helper_id);
	}

	/*$(window).resize(function(){
		t.fixPageHeight();
	});*/
	
	$('BODY').click(function(){ //fix height when customselect menu showed
		t.fixPageHeight();
	});
	t.scontainer.click(function() {
		if (t.container.children('a').hasClass('unlink'))		//IE needs this
			t.container.children('a').removeClass('unlink');
		t.scontainer.hide();
		t.visible = false;
		t.fixPageHeight();	
	});
	t.visible = false;
}

customSelect.prototype = {
	addOption: function (value, title, selected) {
		this.list.append('<' + this.option + '><a href="#' + value + '">' + title + '</a></' + this.option + '>');
		this.size++;

		if (selected) {
			this.val(selected);
		}
	},

	fixPageHeight: function () {
		//*
		var footer = $('#footer');
		
		var footerOffset = footer.offset().top;
		var sContainerHeight = this.visible ? this.scontainer.height() : 0;
		//this.scontainer.show();
		var sContainerOffset = this.scontainer.offset().top;
		//this.scontainer.hide();	
		
		var content = $('#content');
		if(content.height()+content.offset().top < sContainerHeight+sContainerOffset) {
			content.height(sContainerHeight+sContainerOffset-content.offset().top);
		} else {
			content.css('height', 'auto');
		}
		/*
		if (sContainerHeight + sContainerOffset > footerOffset) {
			//footer[0].style.marginTop = -165 + sContainerHeight + sContainerOffset - footerOffset + 15 + 'px';
			footer[0].style.marginTop = sContainerHeight + sContainerOffset - footerOffset + 15 + 'px';
		} /*else {
			footer[0].style.marginTop = -165 + 15 + 'px';
		}*/
	},

	select: function (e) {
		e.preventDefault();
		var t = e.data;

		if (t.useBubbles) {
			if (e.target.tagName == 'I') {
				t.showBubbleHelp(e);
				return;
			}
		}

		if (e.target.tagName != 'A') { return; }
		t.val(t.list.children().index($(e.target).parent()[0]) + 1);

		if (t.fn.change) {
			t.fn.change.call(t.fn.that);
		}
	},

	showBubbleHelp: function (e) {
		e.preventDefault();
		e.stopPropagation();
		var t = this;
		var helper = t.helperContainer;
	
		var id = e.target.parentNode.parentNode.href.replace(/^.+#/, '');

		if (t.lastHelper == id) {
			t.lastHelper = '';
			helper.addClass('invisible');
			return;
		}

		var offset = t.fn.that.container.offset();
		
		t.lastHelper = id;
		
		var windowPos = {
			x: $(window).width() + $(window).scrollLeft(),
			y: $(window).height() + $(window).scrollTop()
		};

		helper.children().addClass('invisible');
		// slava: lowercase id, because i need to specify
		$('#bb-' + id.toLowerCase()).removeClass('invisible');
		//$('#bb-' + id).removeClass('invisible');
		helper.removeClass('invisible');
		//e.target.visible = true;
		helper.css({
			top: Math.round(e.pageY - offset.top) - 5,
			left: Math.round(e.pageX - offset.left) + 20
		});
		
		var helperPos = {
			x: helper.offset().left + helper.outerWidth(),
			y: helper.offset().top + helper.outerHeight()
		};
		
		if(helperPos.x > windowPos.x) {
			if(e.pageX  - 20 - helper.outerWidth() < $(window).scrollLeft()) {
				helper.css('left', $(window).scrollLeft() + 5 - offset.left);
			} else {
				helper.css('left', Math.round(e.pageX - offset.left) - 25 - helper.outerWidth());
			}
		}
		
		if(helperPos.y > windowPos.y) {
			helper.css('top', Math.round(e.pageY - offset.top) - 5 - helper.outerHeight());
		}
		//helper.html(t.converter.strings.formats[this.parentNode.id].description);

		var f = function (e) {
			if (e.target.parentNode.id == 'bubble-help' || (e.type == 'keydown' && e.keyCode != 27)) {
				return;
			}
			
			t.lastHelper = '';
			helper.addClass('invisible');
			$(this).unbind('click keydown', f);
		};

		$(document).bind('click keydown', f);
	},

	val: function (value, change) {
		if (!isNaN(value)) {
			value--;
			var curr = this.list.children(':eq(' + value + ')');
			this.svalue.html(curr.children(this.svalue).text());
			this.value = value;
			curr.addClass('curr bg bg-h16 dc-bg');
			curr.siblings().removeClass('curr bg bg-h16 dc-bg');

			if (change) {
				this.fn.change.call(this.fn.that);
			}
		} else {
			return this.value + 1;
		}
	},

	show: function (e) {
		if (e.keyCode && e.keyCode != 13) { return };

		var t = e.data;
		e.preventDefault();

		//console.info('hello');
		if (t.visible) {
			if (t.useBubbles) {
				t.helperContainer.addClass('invisible');				
			}
			t.scontainer.hide();
			t.visible = false;
			t.fixPageHeight();																									//fixPageHeight
			//$('.mainmenu ul > li').addClass('not').removeClass('h');
			return;
		}

		$(document).click();
		
		e.stopPropagation();

		/* Hide current link */
		if  (!t.container.children('a').hasClass('unlink'))
			t.container.children('a').addClass('unlink');
		else 
			t.container.children('a').removeClass('unlink');
		//t.container.children('a').addClass('hidden');
		
//		t.container.children('a').css('text-indent', '-1000');
		//t.scontainer.hide();
		t.scontainer.show();
		//if(!($.browser.msie && parseInt($.browser.version) == 6)) t.scontainer.css('left', t.container.children('a').position().left-18);
		//alert($.browser.version);
		t.visible = true;
		t.fixPageHeight();
//		/* page content height adjustment */
//		var footer = $('#footer');
//		footer[0].style.marginTop = '-165px';
//
//		var footerOffset = footer.offset().top;
//		var sContainerHeight = t.scontainer.height();
//		var sContainerOffset = t.scontainer.offset().top;
//
//		if (sContainerHeight + sContainerOffset > footerOffset) {
//			footer[0].style.marginTop = -165 + sContainerHeight + sContainerOffset - footerOffset + 15 + 'px';
//		} 
//		/* //page content height adjustment */

		var f = function (e) {
			if (e.target.tagName == 'I' && $(e.target).parents('.select')[0]) {
				return;
			}
			
			var t = e.data;
			if (e.keyCode && (e.keyCode == 38 || e.keyCode == 40)) { 
				e.preventDefault();
				t.roll(e);
				return;
			}

			if (e.keyCode && (e.keyCode != 27 && e.keyCode != 13)) { return; }
			
			t.container.children('a').removeClass('hidden');
			t.container.children('a').removeClass('unlink');
//			t.container.children('a').css('text-indent', '0');

			/* timeout hack for proper item select in FF2 */
			setTimeout(function () {
					t.scontainer.hide();
//					footer[0].style.marginTop = '-165px';
				},
				100
			);
			t.visible = false;
			
			$(this).unbind('click focus keydown', f);
		};

		$(document).bind('click focus keydown', t, f);
	},

	fixContainerWidth: function () {
		//this.container.width(Math.round(this.scontainer.width() / 12) - 1.5 + 'em');
	},

	roll: function (e, delta) {
		var that = e.data;

		if (e.which == 38 || e.which == 40 || delta) {
			e.preventDefault();

			var src = delta ? (delta > 0 ? 1 : -1) : e.which;

			var iVal = that.calcValue(parseInt(that.val()), src);

			if (iVal < 1 || iVal > that.size) {
				return;
			}

			that.val(iVal, 1);
		}
	},

	calcValue: function (val, src) {
		return (src == 38 || src == 1) ? val - 1 : val + 1;
	}
};