(function($){
	var gjSelDivs = new Array();
    var gjSelUls = new Array();
    var gjSelNms = new Array();
    	
	$('html').addClass('stylish-select');

	Array.prototype.indexOf = function (obj, start) {
		for (var i = (start || 0); i < this.length; i++) {
			if (this[i] == obj) {
				return i;
			}
		}
	}

	$.fn.extend({
		getSetSSValue: function(value){
			if (value){
				$(this).val(value).change();
				return this;
			} else {
				return $(this).find(':selected').val();
			}
		},
		setToDefault: function(){
			$(this).val(0).change();
			return this;
		},
		resetSS: function(){
			var oldOpts = $(this).data('ssOpts');
			$this = $(this);
			$this.next().remove();
			$this.unbind('.jSel').jSel(oldOpts);
		}
	});

	$.fn.jSel = function(options) {
		return this.each(function() {
			var defaults = {
				defaultText: 'Please select',
				animationSpeed: 0, 	//set speed of dropdown
				ddMaxHeight: '', 	//set css max-height value of dropdown
				ddMaxLen: 30,
				cssTag: '',
				containerClass: '' 	//additional classes for container div
			};

			var opts = $.extend(defaults, options),
			$input = $(this),
			$containerDivText = $('<div class="jSel_selTxt'+opts.cssTag+'"></div>'),
			$containerDiv = $('<div class="jSel_newLstSel'+opts.cssTag+' '+opts.containerClass+'"></div>'),
			$newUl = $('<ul class="jSel_newLstUl'+opts.cssTag+'" style="visibility:hidden;"></ul>'),
			itemIndex = -1,
			currentIndex = -1,
			keys = [],
			prevKey = false,
			prevented = false,
			$newLi;

			$(this).data('ssOpts',options);

			$containerDiv.insertAfter($input);
			$containerDiv.attr("tabindex", $input.attr("tabindex") || "0");
			$containerDivText.prependTo($containerDiv);
			$newUl.appendTo($containerDiv);
			$input.hide();

			$containerDivText.data('ssReRender',!$containerDivText.is(':visible'));

			gjSelDivs.push($containerDiv);
			gjSelUls.push($newUl);	
			gjSelNms.push(opts.cssTag);

            if ($input.children('optgroup').length == 0){
            	var t = '';
                $input.children().each(function(i){
                    var option = $(this).html();
                    var key = $(this).val();
                    keys.push(option.charAt(0).toLowerCase());
                    if ($(this).attr('selected') == true){
                        opts.defaultText = option;
                        currentIndex = i;
                    }
                    t += '<li><a href="JavaScript:void(0);" title="'+key+'">'+option+'</a></li>';
                });
				$newUl.append(t);
                $newLi = $newUl.children().children();
            } else { 
                $input.children('optgroup').each(function(){
                    var optionTitle = $(this).attr('label'),
                    $optGroup = $('<li class="jSel_optTtl'+opts.cssTag+'">'+optionTitle+'</li>');
                    $optGroup.appendTo($newUl);
                    var $optGroupList = $('<ul></ul>');
                    $optGroupList.appendTo($optGroup);

                    $(this).children().each(function(){
                        ++itemIndex;
                        var option = $(this).html();
                        var key = $(this).val();
                        keys.push(option.charAt(0).toLowerCase());
                        if ($(this).attr('selected') == true){
                            opts.defaultText = option;
                            currentIndex = itemIndex;
                        }
                        $optGroupList.append($('<li><a href="JavaScript:void(0);">'+option+'</a></li>').data('key',key));
                    })
                });
                $newLi = $newUl.find('ul li a');
            }

            var newUlHeight = $newUl.height(),
            containerHeight = $containerDiv.height(),
            newLiLength = $newLi.length;

            if (currentIndex != -1){
                navigateList(currentIndex, true);
            } else {
                $containerDivText.text(opts.defaultText);
            }

            function newUlPos(){
                var $w = jQuery(window), containerPosY = $containerDiv.offset().top,
                docHeight = $w.height(),
                scrollTop = $w.scrollTop(), mh = parseInt(opts.ddMaxHeight);

                if (newUlHeight > mh) 
                    newUlHeight = mh;

                containerPosY = containerPosY-scrollTop;
                if (containerPosY+newUlHeight >= docHeight){
                    $newUl.css({
                        top: '-'+newUlHeight+'px',
                        height: newUlHeight
                    });
                    $input.onTop = true;
                } else {
                    $newUl.css({
                        top: containerHeight+'px',
                        height: newUlHeight
                    });
                    $input.onTop = false;
                }
            }

            newUlPos();

			$(window).bind('resize.jSel scroll.jSel', newUlPos);

            function positionFix(){
                $containerDiv.css('position','relative');
            }

            function positionHideFix(){
                $containerDiv.css('position','static');
            }

            $containerDivText.bind('click.jSel',function(event){
            	var i;
                event.stopPropagation();

				i = 0;                
                $.each(gjSelDivs, function(k,$v) {
                	$v.removeClass().addClass('jSel_newLstSel'+gjSelNms[i])
						.css('position','static')//ie
						.children('ul').hide();
					i++;
	            });
                
				if($(this).data('ssReRender')) {
					newUlHeight = $newUl.height('').height();
					containerHeight = $containerDiv.height();
					$(this).data('ssReRender',false);
					newUlPos();
				}

				i = 0;
				$.each(gjSelUls, function(k,$v) {
					if ($v.not($(this).next()))
					{
						$v.hide()
							.parent()
								.css('position','static')
								.removeClass('jSel_lstFocus'+gjSelNms[i]);
					}
					i++;
				});

               	$newUl.toggle();
               	positionFix();
				$newLi.eq(currentIndex).focus();
            });

            $newLi.bind('click.jSel',function(e){
                var $clickedLi = $(e.target);
                currentIndex = $newLi.index($clickedLi);
                prevented = true;
                navigateList(currentIndex);
                $newUl.hide();
                $containerDiv.css('position','static');//ie

            });

            $newLi.bind('mouseenter.jSel',
				function(e) {
					var $hoveredLi = $(e.target);
					$hoveredLi.addClass('jSel_itemHov'+opts.cssTag);
				}
			).bind('mouseleave.jSel',
				function(e) {
					var $hoveredLi = $(e.target);
					$hoveredLi.removeClass('jSel_itemHov'+opts.cssTag);
				}
			);

            function navigateList(currentIndex, init){
                $newLi.removeClass('jSel_itemHilite'+opts.cssTag)
                .eq(currentIndex)
                .addClass('jSel_itemHilite'+opts.cssTag);

                if ($newUl.is(':visible')){
                    $newLi.eq(currentIndex).focus();
                }

                var text = $newLi.eq(currentIndex).html();
                var val = $newLi.eq(currentIndex).attr('title');

                if (init == true){
                    $input.val(val);
					var cut_text = (text.length <= opts.ddMaxLen) ? text : (text.substr(0, opts.ddMaxLen - 2) + '...');
					$containerDivText.attr('title', text);
                    $containerDivText.text(cut_text);
                    return false;
                }

				try {
				    $input.val(val)
				} catch(ex) {
				    // handle ie6 exception
				    $input[0].selectedIndex = currentIndex;
				}

                $input.change();
				var cut_text = (text.length <= opts.ddMaxLen) ? text : (text.substr(0, opts.ddMaxLen - 2) + '...');
				$containerDivText.attr('title', text);
                $containerDivText.text(cut_text);
            }

            $input.bind('change.jSel',function(event){
                $targetInput = $(event.target);
                if (prevented == true){
                    prevented = false;
                    return false;
                }
                $currentOpt = $targetInput.find(':selected');
                currentIndex = $targetInput.find('option').index($currentOpt);
                navigateList(currentIndex, true);
			});

            function keyPress(element) {
                $(element).unbind('keydown.jSel').bind('keydown.jSel',function(e){
                    var keycode = e.which;
                    prevented = true;
                    switch(keycode) {
                        case 40: //down
                        case 39: //right
                            incrementList();
                            return false;
                            break;
                        case 38: //up
                        case 37: //left
                            decrementList();
                            return false;
                            break;
                        case 33: //page up
                        case 36: //home
                            gotoFirst();
                            return false;
                            break;
                        case 34: //page down
                        case 35: //end
                            gotoLast();
                            return false;
                            break;
                        case 13:
                        case 27:
                            $newUl.hide();
                            positionHideFix();
                            return false;
                            break;
                    }

                    keyPressed = String.fromCharCode(keycode).toLowerCase();
                    var currentKeyIndex = keys.indexOf(keyPressed);
                    if (typeof currentKeyIndex != 'undefined') { 
                        ++currentIndex;
                        currentIndex = keys.indexOf(keyPressed, currentIndex); 
                        if (currentIndex == -1 || currentIndex == null || prevKey != keyPressed) currentIndex = keys.indexOf(keyPressed); 
                        navigateList(currentIndex);
                        prevKey = keyPressed;
                        return false;
                    }
                });
            }

            function incrementList(){
                if (currentIndex < (newLiLength-1)) {
                    ++currentIndex;
                    navigateList(currentIndex);
                }
            }

            function decrementList(){
                if (currentIndex > 0) {
                    --currentIndex;
                    navigateList(currentIndex);
                }
            }

            function gotoFirst(){
                currentIndex = 0;
                navigateList(currentIndex);
            }

            function gotoLast(){
                currentIndex = newLiLength-1;
                navigateList(currentIndex);
            }

            $containerDiv.bind('click.jSel',function(e){
                e.stopPropagation();
                keyPress(this);
 				if (!$.browser.msie)
					$(this).blur(); 
           });

            $containerDiv.bind('focus.jSel',function(){
                $(this).addClass('jSel_lstFocus'+opts.cssTag);
                keyPress(this);
            });

            $containerDiv.bind('blur.jSel',function(){
                $(this).removeClass('jSel_lstFocus'+opts.cssTag);
            });

            $(document).bind('click.jSel',function(){
                $containerDiv.removeClass('jSel_lstFocus'+opts.cssTag);
                $newUl.hide();
                positionHideFix();
            });

            $containerDivText.bind('mouseenter.jSel',
				function(e) {
					var $hoveredTxt = $(e.target);
					$hoveredTxt.parent().addClass('jSel_lstHov'+opts.cssTag);
				}
			).bind('mouseleave.jSel',
				function(e) {
					var $hoveredTxt = $(e.target);
					$hoveredTxt.parent().removeClass('jSel_lstHov'+opts.cssTag);
				}
            );

            $newUl.css({
                left: '0',
                display: 'none',
                visibility: 'visible'
            });
        });
    };
})(jQuery);