//create cross-browser indexOf
/*Array.prototype.indexOf = function (obj, start) {
	for (var i = (start || 0); i < this.length; i++) {
		if (this[i] == obj) {
			return i;
		}
	}
}*/

$.fn.sSelect = function(options) {
	$(this).each(function(){
		var defaults = {
			defaultText: 'Please select'
		};

		//initial variables
		var opts = $.extend(defaults, options),
			input = $(this),
			containerDivText = $('<div class="selectedTxt"></div>'),
			newUl = $('<ul class="newList"></ul>'),
			containerDiv = $('<div class="newListSelected" tabindex="0"></div>'),
			itemIndex = -1,
			currentIndex = -1,
			keys = [],
			//prevKey = false,
			newListItems = '',
			disabledInput = $(this).attr('disabled');

			//build new list
			containerDiv.insertAfter(input);
			containerDivText.prependTo(containerDiv);
			newUl.appendTo('body');
			input.hide();

		//test for optgroup
		if (input.children('optgroup').length == 0)
		{
			input.children().each(function(i){
				var option = $(this).text();
				//add first letter of each word to array
				keys.push(option.charAt(0).toLowerCase());
				if ($(this).attr('selected') == true){
					opts.defaultText = option;
					currentIndex = i;
				}
				newListItems += '<li>'+option+'</li>';
			});
			//add new list items to ul
			newUl.html(newListItems);
			//cache list items object
			var newLi = newUl.children();

		}
		else
		{
			//optgroup
			input.children('optgroup').each(function(i){

				var optionTitle = $(this).attr('label'),
					optGroup = $('<li class="newListOptionTitle">'+optionTitle+'</li>');

				optGroup.appendTo(newUl);

				var optGroupList = $('<ul></ul>');

				optGroupList.appendTo(optGroup);

				$(this).children().each(function(){
					++itemIndex;
					var option = $(this).text();
					//add first letter of each word to array
					keys.push(option.charAt(0).toLowerCase());
					if ($(this).attr('selected') == true){
						opts.defaultText = option;
						currentIndex = itemIndex;
					}
					newListItems += '<li>'+option+'</li>';
				})
				//add new list items to ul
				optGroupList.html(newListItems);
				newListItems = '';
			});

			//cache list items object
			var newLi = newUl.find('ul li');
		}

		//check if a value is selected
		if (currentIndex != -1){
			i = 0;
			setSelected(input);
		} else {
			//set placeholder text
			containerDivText.text(opts.defaultText);
		}

		var newLiLength = newLi.length;

		function setSelected(input)
		{
			newLi.eq(currentIndex).addClass('hiLite');
			containerDivText.text(input.children().filter(':selected').text());
		}

		//decide if to place the new list above or below the drop-down
		function newUlPos(){
			var docHeight = $(window).height(),
			    halfDocHeight = Math.ceil(docHeight/2);

			if(halfDocHeight <= newUl.height())
			{
				newUl.height(halfDocHeight);
				newUl.width(newUl.width()+15);
			}

			var containerPosY = containerDiv.offset().top,
				containerHeight = containerDiv.height()+3,
				scrollTop = $(window).scrollTop(),
				newUlHeight = newUl.height()+3;

			containerPosY = containerPosY-scrollTop;
			if (containerPosY+newUlHeight >= docHeight){
				newUl.css({
					top : (containerDiv.offset().top - newUlHeight) + 'px',
					left : (containerDiv.offset().left) + 'px'
				});

			} else {
				newUl.css({
					top : (containerDiv.offset().top + containerHeight) + 'px',
					left : (containerDiv.offset().left) + 'px'
				});
			}
		}

		//run function on page load
		newUlPos();

		if(!disabledInput)
		{
			//run function on browser window resize
			$(window).resize(function(e){
				newUlPos(e);
			});

			$(window).scroll(function(e){
				newUlPos(e);
			});

			containerDivText.click(function(){
				if (newUl.is(':visible')){
					newUl.hide();
					unbindKeyDown();
					return false;
				}

				//show list
				newUl.slideDown('normal');
				bindKeyDown();
			});

			function unbindKeyDown()
			{
				containerDiv.unbind('keydown');
			}

			function bindKeyDown()
			{
				containerDiv.keydown(function(e){
					switch (e.keyCode)
					{
						case 40: //down
							incActiveSelect();
							break;
						case 39: //right
						case 38: //up
						case 37: //left
						case 33: //page up
						case 36: //home
						case 34: //page down
						case 35: //end
						case 13: //enter
						case 27:
					}
				});
			}

			function incActiveSelect()
			{
				var newLink = $('.newList').filter(':visible');
				//alert('test');

			}

			$(document).click(function(){
				newUl.hide();
			});

			newLi.click(function(){
				var text = $(this).text();

				$(this).addClass('hiLite');

				currentIndex = $(this).parent().children().index($(this));
				setSelectText(text);
				newUl.hide();
				containerDiv.css('position','static');//ie
			});

			containerDivText.hover(
				function() {
					$(this).addClass('newListSelHover');
				},
				function () {
					$(this).removeClass('newListSelHover');
				}
			);

			newLi.hover(
				function () {
					newLi.removeClass('hiLite');
					$(this).addClass('newListHover');
				},
				function () {
					$(this).removeClass('newListHover');
				}
			);
		}
		else
		{
			containerDiv.addClass('SELECT_DISABLED');
		}

		function setSelectText(text){
			//set text of select box
			input.val(text).change();
			containerDivText.text(text);
		}
	});
};
