/* ******************************************************************************
		Created by GS on 12.02.2009, True Vision
****************************************************************************** */

var select = {
	s : {},
	
	set : function(id) {
		if (id && select.gebi(id)) {
			if (typeof this.s[id] != "undefined") {
				delete this.s[id];
			}
			
			this.s[id] = new this.fc(id, 1);
			return this.s[id];
		} else {
			var tmp = $('select.select').get();
			
			for (var i=0; i<tmp.length; i++) {
				tmp[i].id = tmp[i].id ? tmp[i].id : 'rndm_' + tmp[i].name;
				
				if (typeof this.s[tmp[i].id] != "undefined" && !this.s[tmp[i].id].custom) {
					delete this.s[tmp[i].id];
				}
				if (typeof this.s[tmp[i].id] == "undefined") {
					this.s[tmp[i].id] = new this.fc(tmp[i].id);
				}				
				if (!select.gebi(tmp[i].id + '_slctd')) {
					this.s[tmp[i].id].create();
				}
			}

			$(document)
				.unbind('click')
				.click(function(){
					select.hide();
				});
		}
	},
	
	get : function(id) {
		if (typeof this.s[id] != "undefined") return this.s[id];
	},
	
	hide : function() {
		$('div.select-optns').css({ display:'none' });
	},
	
	gebi: function(n) {
		if(!n) return false;
		if(!document.getElementById(n)) return false;
		return document.getElementById(n)
	},


	bubbling: function(e) {
		e = select.eie(e); 
		if (!e.cancelBubble) e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
		return false;
	},

	eie: function (e) {
		if (!e) var e = window.event;
		return e;
	}
}

select.fc = function(id, custom) {
	var obj = select.gebi(id);
	var len = obj.options.length;
	
	this.id			= obj.id;	
	this.name		= obj.name;
	this.index		= obj.selectedIndex;
	this.dwidth		= 0;
	this.owidth		= null;
	this.options	= [];
	this.custom		= custom ? 1 : 0;

	for (var o = 0; o < len; o++) {
		this.options[o] = obj.options[o].innerHTML;
	}
}

select.fc.prototype = {
	init : function(hash) {
		try {
			for (var i in hash) this[i] = hash[i];
		} catch(e) {
			alert(e);
		}
		$(document).click(function(){
			select.hide();
		});
	},
	
	replace : function(a, dw) {
		this.dwidth = isNaN(dw) ? this.dwidth : dw;
		if (typeof a == 'object' && this.options.length == a.length) { 
			for (var i = 0; i < this.options.length; i++) {
				this.options[i] = a[i];
			}
		}
	},
	
	create : function() {
		var obj = select.gebi(this.id);
		if (!obj) return;
		
		var len		=	this.options.length;
		var id		=	this.id;
		var check	=	obj.className.indexOf('check') != -1 ? ' select-check' : '';
		var slctd	=	$('<div></div>')
							.attr('id', id + '_slctd')
							.addClass('select-slctd' + check)
							.css({ width: (obj.offsetWidth + this.dwidth) + 'px' })
							.click(function(e){
								select.bubbling(e);
								select.gebi(id).focus();
								select.get(id).toggle();
							});
		var slctdw	=	$('<div></div>')
							.attr('id', id + '_fix')
							.addClass('select-fix')
							.css({ width: obj.offsetWidth + this.dwidth - 14 + 'px', overflow: 'hidden' })
							.html(this.options[obj.selectedIndex]);
		var optns	=	$('<div></div>')
							.attr('id', id + '_optns')
							.addClass('select-optns')
							
		slctd.append(slctdw);
		
		for (var i = 0; i < len; i++) {		
			var optn	=	$('<div></div>')
								.attr('id', id + '_optn' + i)
								.addClass('select-optn')
								.html(this.options[i] == '' ? '&nbsp;' : this.options[i])
								.click(function(idx){
									return function() {
										select.get(id).change(idx);
									}
								}(i));
			optns.append(optn);					
		}
		
		$(obj)
			.before(slctd)
			.before(optns)
			.css({ position:'absolute', left:'-100000px' })
			.keyup(function(){
				select.get(id, 1).change(obj.selectedIndex);
			})
			.focus(function(){
				$('#' + id + '_slctd').addClass('select-focus');
			}).blur(function(){
				$('#' + id + '_slctd').removeClass('select-focus');
			});
	},
	
	change : function(idx, toggle) {
		var obj = select.gebi(this.id);
		
		if (obj) {
			obj.selectedIndex = idx;
			this.index = idx;
			
			$('#' + this.id + '_fix').html(this.options[idx]);
			$('#' + this.id + '_optns div.select-optn').removeClass('select-optn-active');
			$('#' + this.id + '_optn' + idx).addClass('select-optn-active');
		}
		
		if (toggle) this.toggle();
		
		obj.focus();
	},
	
	toggle : function() {
		var slctd = select.gebi(this.id + '_slctd');
		var obj = select.gebi(this.id + '_optns');
		
		if (obj.style.display == 'block') {
			obj.style.display = 'none';
		} else {
			if (!this.owidth) {
				obj.style.visibility = 'hidden';
				obj.style.display = 'block';
				
				if (this.options.length > 10) {
					if (obj.offsetWidth + 18 < slctd.offsetWidth - 8) this.owidth = slctd.offsetWidth - 8;
						else this.owidth = obj.offsetWidth + 15;
					obj.className += ' select-scrl';
				} else {
					if (obj.offsetWidth < slctd.offsetWidth - 8) this.owidth = slctd.offsetWidth - 8;
						else this.owidth = obj.offsetWidth;
				}
				obj.style.display = 'none';
				obj.style.visibility = 'visible';
			}
			obj.style.width = this.owidth + 'px';
		
			select.hide();
			obj.style.display = 'block';
		}
		
		if (this.index) {
			select.gebi(this.id + '_optns').scrollTop = select.gebi(this.id + '_optn' + this.index).offsetTop;
		}
	}
}
