var pseudoSelect; 
$(document).ready(function(){
	pseudoSelect = function(obj){
		if (obj) {
			if (typeof obj == 'object') { var sel = obj } else { var sel = $(obj) }
			if (!sel[0]) { return false }
				if (sel[0].nodeName.toLowerCase()=='select' || sel[0].tagName.toLowerCase()=='select') {
					sel.each(function(e){
						var thSel = $(this);
						var val = '';
						var classes = ' '+thSel.attr('class');
						var wdth = parseInt($(this).width())+parseInt($(this).css('padding-right')) || $(this).css('width') || 'auto';
						var wdth2 = parseInt($(this).width())+parseInt($(this).css('padding-left')) || $(this).css('width') || 'auto';
						var lPadd = parseInt($(this).css('padding-left'));
						var rPadd = parseInt($(this).css('padding-right'));
						var hght2 = parseInt($(this).css('height'))-(parseInt($(this).css('border-bottom-width'))*2) || $(this).height() || 'auto';
						var hght3 = parseInt($(this).css('height')) || $(this).height() || 'auto';
						if ($.browser.mozilla) {
							var padd = parseInt($(this).css('padding-top'))+parseInt($(this).css('padding-bottom'));
							hght2=hght2+padd+(parseInt($(this).css('border-bottom-width'))*2); 
							if (navigator.userAgent.search(/[Ll]inux/)!=-1 || navigator.userAgent.search(/[Mm]acintosh/)!=-1) { 
								hght2=hght2+(parseInt($(this).css('border-bottom-width'))*2)
								wdth = wdth+parseInt(thSel.css('border-left-width'))+parseInt(thSel.css('border-right-width'));
							}
							hght3=hght3+padd+(parseInt($(this).css('border-bottom-width'))*2);
						}
						var lBord = parseInt($(this).css('border-left-width'));
						var txt = thSel.find('option:selected').text() || thSel.find('option').eq(0).text();
						var newEl = $(this).hide().wrap('<div class="pseudo_sl'+classes+'" />').parent().attr({title:val}).width(wdth).css({paddingLeft:lPadd});
						var wrap = newEl.append('<div class="pseudo_wrap"></div>').find('.pseudo_wrap');
						var toTxt = wrap.append('<span></span>').find('span').css({lineHeight:hght2+'px', height:hght2+'px'}).text(txt);
						
						var btn = wrap.append('<em></em>').find('em').height(hght2-2).width(hght2-2).css({right:-parseInt($(this).css('padding-right'))});
						
						var dropCont = newEl.append('<div class="pseudo_opt_cont"><div>').find('.pseudo_opt_cont').width(wdth2+rPadd).css({top:hght3-parseInt($(this).css('border-bottom-width'))+'px'});
						var scrll = dropCont.append('<div class="pointsbox"><span class="pointtotop"></span><span class="pointtobot"></span></div>').find('.pointsbox');
						var drop = dropCont.append('<div class="pseudo_opt"></div>').find('.pseudo_opt');
						//drop.width(wdth-parseInt(drop.css('border-right-width'))-parseInt(drop.css('border-left-width')));
						var elemH = 0;
						for (i=1;i<=$(this).find('option').length;i++) {
							elemH += drop.append('<a rel="'+$(this).find('option').eq(i-1).val()+'" href="#">'+$(this).find('option').eq(i-1).text()+'</a>').find('a:last').height();
						}
						
						if (elemH<=drop.height()) {
							dropCont.addClass('no_scroll');
							scrll.hide();
						}
						
						dropCont.hide();
						
						scrll.each(function(e){
							var thi = $(this);
							thi.mousedown(function(){
								thi[0].sc = 1;
								$(document).mousemove(function(e){
									if (thi[0].sc == 1) {
										var toY = e.pageY-dropCont.offset().top-5;
										if (toY > dropCont.height()-thi.height()) { toY = dropCont.height()-thi.height() } else if (toY < thi.height()) { toY = thi.height() }
										thi.css({top:toY-(thi.height()/2)+'px'});
										var hght = 0;
										for (var i=0; i < drop.find('a').size(); i++) {
											hght += drop.find('a').eq(i).height()+parseInt(drop.find('a').eq(i).css('padding-top'))+parseInt(drop.find('a').eq(i).css('padding-bottom'))
										}
										var roll = (hght-drop.height())/(dropCont.height()-(thi.height()*2));
										drop.scrollTop((toY-thi.height())*roll);
											
									}
								});
								return false;
							});
							
							$(document).mousedown(function(){
								thi[0].sc = 0;
							});
							
							$(document).mouseup(function(){
								thi[0].sc = 0;
							});
							
							dropCont.hover(function(){
								if (window.addEventListener) { window.addEventListener('DOMMouseScroll', wheel, false); }
								window.onmousewheel = wheel;
								document.onmousewheel = wheel;
							}, function(){if (window.removeEventListener) { window.removeEventListener('DOMMouseScroll', wheel, false); } window.onmousewheel = ''; document.onmousewheel = ''});
							function wheel(event) {
								if(!event) {event = window.event}
								if (event.wheelDelta) {	wheelDelta = -event.wheelDelta/120; } 
								else if (event.detail) { wheelDelta = event.detail/3; }
								var top = parseInt(thi.css('top'));
								if(isNaN(top)) { top = 0; }
								var toY = top+(wheelDelta*3);
								if (toY > dropCont.height()-(thi.height()*1.5)) { toY = dropCont.height()-(thi.height()*1.5) } else if (toY < thi.height()/2) { toY = thi.height()/2 }
								thi.css({top:toY+'px'});
								var hght = 0;
								for (var i=0; i < drop.find('a').size(); i++) {
									hght += drop.find('a').eq(i).height()+parseInt(drop.find('a').eq(i).css('padding-top'))+parseInt(drop.find('a').eq(i).css('padding-bottom'))
								}
								var roll = (hght-drop.height())/(dropCont.height()-(thi.height()*2));
								drop.scrollTop((toY-(thi.height()/2))*roll);
								if (event.preventDefault)
								{
									event.preventDefault();
								}
								event.returnValue = false;
							}
						});
						
						scrll.click(function(){return false});
						
						newEl.click(function(){
							if (!$(this).hasClass('select') && !$(this).find('select').attr('disabled')) {
								$(this).addClass('select').addClass('st').css({zIndex: 10000});
								dropCont.show();
								scrll.css({top:scrll.height()/2});
							}
						});
						
						drop.find('a').click(function(){
							txt = $(this).text();
							val = $(this).attr('rel');
							toTxt.text(txt);
							var th = $(this);
							thSel.find(':selected').removeAttr('selected');
							thSel.find('option').eq(drop.find('a').index(th)).attr({selected: 'selected'});
							if(thSel.valid) {
								thSel.valid();
								thSel.valid();
							}
							thSel.change();
							$('body').click();
							return false;
						});
						
						$('body').click(function(){
							if (newEl.hasClass('select') && !newEl.hasClass('st')) {
								newEl.removeClass('select');
								newEl.css({zIndex: 0});
								dropCont.hide();
							} else {
								newEl.removeClass('st');
							}
						});
						
						
						
						
						
						
						
					});
				}
		}
	}
	
	
	pseudoSelect($('select'));
	
	
	
});
