(function($)
{

    //add class of js to html tag
    if ($('html').not('.stylish-select'))
        $('html').addClass('stylish-select');

    //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;
//            }
//        }
//    }

    //utility methods
    $.fn.extend({
        getSetSSValue: function(value)
        {
            if (value || value == 0)
            {
                //set value and trigger change event
                $(this).val(value).change();

                return this;
            } else
            {
                return $(this).find(':selected').val();
            }
        },
        //added by Justin Beasley
        resetSS: function(options)
        {
            $(this).each(function()
            {
                var oldOpts = $(this).data('ssOpts');
                oldOpts = $.extend(oldOpts, options)
                $this = $(this);
                $this.next().remove();
                //unbind all events and redraw
                $this.unbind().sSelect(oldOpts);
            });
        }
    });

    $.fn.sSelect = function(options)
    {

        return this.each(function()
        {

            var defaults = {
                defaultText: 'Please select',
                animationSpeed: 0, //set speed of dropdown
                ddMaxHeight: '100px', //set css max-height value of dropdown
                allowDropUp: false
            };

            //var wrapper = $("<div>").css({ "position": "relative" });
            //var wrapper = $("<div class='newListWrapper'>");

            //initial variables
            var opts = $.extend(defaults, options),
            $input = $(this),
            $containerDivText = $('<div class="selectedTxtWrapper"><div class="selectedTxt"></div></div>'),
            $containerDiv = $('<div class="newListSelected" tabindex="0"></div>').css('zIndex', $(this).css("zIndex")),

            $newUl = $('<div class="newList"></div>'),
            itemIndex = -1,
            currentIndex = -1,
            keys = [],
            prevKey = false,
            prevented = false,
            $newLi;

            //added by Justin Beasley
            $(this).data('ssOpts', options);

            //build new list
            //$newUl.wrap(wrapper);
            $containerDiv.insertAfter($input);
            $containerDivText.prependTo($containerDiv);
            $newUl.appendTo($containerDiv);
            $input.hide();

            //test for optgroup
            if ($input.children('optgroup').length == 0)
            {
                $input.children().each(function(i)
                {
                    var option = $(this).text();
                    var key = $(this).val();
                    var cls = $(this).attr("class");

                    //add first letter of each word to array
                    keys.push(option.charAt(0).toLowerCase());
                    if ($(this).attr('selected') == true)
                    {
                        opts.defaultText = option;
                        currentIndex = i;
                    }
                    $newUl.append($('<div><a href="JavaScript:void(0);" class="' + cls + '">' + option + '</a></div>').data('key', key));

                });
                //cache list items object
                $newLi = $newUl.children().children();

            } else
            { //optgroup
                $input.children('optgroup').each(function()
                {

                    var optionTitle = $(this).attr('label'),
                    $optGroup = $('<div class="newListOptionTitle"><div style="text-align: center;">' + optionTitle + '</div></div>');

                    $optGroup.appendTo($newUl);

                    var $optGroupList = $('<div></div>');

                    $optGroupList.appendTo($optGroup);

                    $(this).children().each(function()
                    {
                        ++itemIndex;
                        var option = $(this).text();
                        var key = $(this).val();
                        var cls = $(this).attr("class");
                        
                        //add first letter of each word to array
                        keys.push(option.charAt(0).toLowerCase());
                        if ($(this).attr('selected') == true)
                        {
                            opts.defaultText = option;
                            currentIndex = itemIndex;
                        }
                        $optGroupList.append($('<div class="lis"><a href="JavaScript:void(0);" class="' + cls + '">' + option + '</a></div>').data('key', key));
                    })
                });
                //cache list items object
                $newLi = $newUl.find(".lis a");
            }

            //get heights of new elements for use later
            var newUlHeight = $newUl.height(),
            containerHeight = $containerDiv.height(),
            newLiLength = $newLi.length;

            //check if a value is selected
            if (currentIndex != -1)
            {
                navigateList(currentIndex, true);
            } else
            {
                //set placeholder text
                $containerDivText.find(".selectedTxt").text(opts.defaultText);
            }

            //decide if to place the new list above or below the drop-down
            function newUlPos()
            {
                var containerPosY = $containerDiv.offset().top,
                docHeight = jQuery(window).height(),
                scrollTop = jQuery(window).scrollTop();

                //if height of list is greater then max height, set list height to max height value
                if (newUlHeight > parseInt(opts.ddMaxHeight))
                {
                    newUlHeight = parseInt(opts.ddMaxHeight);
                }

                containerPosY = containerPosY - scrollTop;
                if (containerPosY + newUlHeight >= docHeight && opts.allowDropUp)
                {
                    $newUl.css({
                        top: '-' + newUlHeight + 'px',
                        height: newUlHeight
                    });
                    $input.onTop = true;
                } else
                {
                    $newUl.css({
                        top: containerHeight + 'px',
                        height: newUlHeight
                    });
                    $input.onTop = false;
                }
            }

            //run function on page load
            newUlPos();

            //run function on browser window resize
            $(window).resize(function()
            {
                newUlPos();
            });

            $(window).scroll(function()
            {
                newUlPos();
            });

            //positioning
            function positionFix()
            {
                $containerDiv.css('position', 'relative');
            }

            function positionHideFix()
            {
                $containerDiv.css('position', 'static');
            }

            $containerDivText.click(function(event)
            {

                event.stopPropagation();
                //if ($(this).attr("isDisabled") == false)
                //{
                //hide all menus apart from this one
                $('.newList').not($(this).next()).hide().parent().removeClass('newListSelFocus');

                //show/hide this menu
                $newUl.toggle();
                //$newUl.css('zIndex', '99999');
                positionFix();
                //scroll list to selected item
                $newLi.eq(currentIndex).focus();
                //}
            });

            $newLi.click(function(e)
            {

                var $clickedLi = $(e.target);

                //update counter
                currentIndex = $newLi.index($clickedLi);

                //remove all hilites, then add hilite to selected item
                prevented = true;
                navigateList(currentIndex);
                $newUl.hide();
                $containerDiv.css('position', 'static'); //ie

            });

            $newLi.hover(
                function(e)
                {
                    var $hoveredLi = $(e.target);
                    $hoveredLi.addClass('newListHover');
                },
                function(e)
                {
                    var $hoveredLi = $(e.target);
                    $hoveredLi.removeClass('newListHover');
                }
                );

            function navigateList(currentIndex, init)
            {
                $newLi.removeClass('hiLite')
                .eq(currentIndex)
                .addClass('hiLite');

                if ($newUl.is(':visible'))
                {
                    $newLi.eq(currentIndex).focus();

                    //if ($newUl.scrollTop() != 0)
                    //    $newUl.scrollTop($newUl.scrollTop() + $newLi.eq(currentIndex).position().top)
                }

                var text = $newLi.eq(currentIndex).text();
                var val = $newLi.eq(currentIndex).parent().data('key');

                //page load
                if (init == true)
                {
                    $input.val(val);
                    $containerDivText.find(".selectedTxt").text(text);
                    return false;
                }

                $input.val(val)
                $input.change();
                $containerDivText.find(".selectedTxt").text(text);
            };

            $input.change(function(event)
            {
                $targetInput = $(event.target);
                //stop change function from firing
                if (prevented == true)
                {
                    prevented = false;
                    return false;
                }
                $currentOpt = $targetInput.find(':selected');

                currentIndex = $targetInput.find('option').index($currentOpt);


                navigateList(currentIndex, true);
            }
            );

            //handle up and down keys
            function keyPress(element)
            {
                //when keys are pressed
                element.onkeydown = function(e)
                {
                    var keycode;
                    if (e == null)
                    { //ie
                        keycode = event.keyCode;
                    } else
                    { //everything else
                        keycode = e.which;
                    }

                    //prevent change function from firing
                    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;
                    }

                    //check for keyboard shortcuts
                    keyPressed = String.fromCharCode(keycode).toLowerCase();

                    var currentKeyIndex = keys.indexOf(keyPressed);

                    if (typeof currentKeyIndex != 'undefined')
                    { //if key code found in array
                        ++currentIndex;
                        currentIndex = keys.indexOf(keyPressed, currentIndex); //search array from current index
                        if (currentIndex == -1 || currentIndex == null || prevKey != keyPressed) currentIndex = keys.indexOf(keyPressed); //if no entry was found or new key pressed search from start of array


                        navigateList(currentIndex);
                        //store last key pressed
                        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.click(function()
            {
                keyPress(this);
            });

            $containerDiv.focus(function()
            {
                $(this).addClass('newListSelFocus');
                keyPress(this);
            });

            $containerDiv.blur(function()
            {
                $(this).removeClass('newListSelFocus');
            });

            //hide list on blur
            $('body').click(function()
            {
                $containerDiv.removeClass('newListSelFocus');
                $newUl.hide();
                positionHideFix();
            });

            //add classes on hover
            $containerDivText.hover(function(e)
            {
                var $hoveredTxt = $(e.target);

                if ($(e.target).is(".selectedTxt"))
                    $hoveredTxt = $(e.target).parent();

                $hoveredTxt.parent().addClass('newListSelHover');
            },
            function(e)
            {
                var $hoveredTxt = $(e.target);

                if ($(e.target).is(".selectedTxt"))
                    $hoveredTxt = $(e.target).parent();

                $hoveredTxt.parent().removeClass('newListSelHover');
            }
            );

            //reset left property and hide
            $newUl.css('left', '0').hide();

        });

    };

})(jQuery);
