// Focus first element
!function ($) {
    $.fn.focusFirstField = function(){
        $this = this;
        $this.find(":text:visible:enabled").filter(function(){
            return $(this).parents(":hidden").size() == 0;
        }).slice(0,1).focus();
        return this;
    }
}(window.jQuery);


// Search Clear
!function ($) {
    $.fn.searchClear = function (param){
        _defaultSettings = {
            inputName: '.search-query',
            inputIcon: 'inputIcon',
            clearBtnIcon: 'clearBtnIcon',
        };
        _set = $.extend(_defaultSettings, param);
        $this = this;
        $input = this.find(_set.inputName);
        $tmp = '<i class="'+_set.inputIcon+'"></i><i class="'+_set.clearBtnIcon+' search-clear"></i>';
        $input.wrap('<div class="sc-field" />');
        $this.find('.sc-field').prepend($tmp);
        $searchClear = $this.find(".search-clear");
        function run(e) {
            $searchClear.hide();
            if($input.val().length > 0) {
                $searchClear.show();
            }else {
                $searchClear.hide();
            }
            $input.on("blur keyup", function(){
                if($(this).val().length > 0) {
                    $searchClear.show();
                }else {
                    $searchClear.hide();
                }
            });
            $searchClear.on({
                click: function(){
                    $(this).hide();
                    $input.val("")
                },
            });
        }
        
        // Checking IE10
        // if Windows 8 and IE is ture. remove search clear buttom and fix text input padding-right
        if(/Windows NT 6.2/g.test(navigator.userAgent)){
            if(/MSIE/g.test(navigator.userAgent)){
                $searchClear.remove();
                $input.css({
                    'padding-right': '5px',
                });
            }else{run()}
        }else{run()}
    }
}(window.jQuery);

// Document Ready
$(function() {
    $('body').prepend("<div id='orbit_bar_temp'/>"); 
    $("#orbit_bar_temp").load('/load_orbit_bar',function(){
        $('body').prepend($(this).html());
        $(this).remove();
        $('#search').searchClear({
            inputName: '.search-query',
            inputIcon: 'icon-search',
            clearBtnIcon: 'icons-cross-3',
        });
        $('#login').on('shown', function () {
            $(document.body).addClass('modalBlur');
            $('#login').focusFirstField();
        }).on("hide", function() {
            $(document.body).removeClass('modalBlur');
        });
    });
});
