(function() {

    // set default value click
    var clickwindow = false;

    function headImageUpload() {
        $('#head-image-upload-thumbnail').fileupload({
            autoUpload: true,
            dataType: 'json',
            formData: { 'upload-head-image': '1' },
            done: function(e, data) {
                if (data.result.code == 0 && data.result.imagInfo != null) {
                    $("#avatar-photo-nav").attr('src', data.result.imagInfo + "&" + Math.random());
                    $("#avatar-photo-computer").attr('src', data.result.imagInfo + "&" + Math.random());
                    $("#avatar-photo-phone").attr('src', data.result.imagInfo + "&" + Math.random());

                    if ($('#upload-head-info-error').hasClass('head-info-error')) {
                        $('.head-info-error').remove();
                    }

                }

                if (data.result.code != null && data.result.msg != null) {
                    if ($('#upload-head-info-error').hasClass('head-info-error')) {
                        $('.head-info-error').remove();
                    }
                    $('#head-info').after(data.result.msg);

                }


            }
        });

        // for upload head image in phone
        $('#head-image-upload-thumbnail-phone').fileupload({
            autoUpload: true,
            dataType: 'json',
            formData: { 'upload-head-image': '1' },
            done: function(e, data) {
                if (data.result.code == 0 && data.result.imagInfo != null) {

                    $("#avatar-photo-nav").attr('src', data.result.imagInfo + "&" + Math.random());
                    $("#avatar-photo-computer").attr('src', data.result.imagInfo + "&" + Math.random());
                    $("#avatar-photo-phone").attr('src', data.result.imagInfo + "&" + Math.random());
                    if ($('div').hasClass('head-info-error')) {
                        $('.head-info-error').remove();
                    }

                }


                if (data.result.code != null && data.result.msg != null) {
                    if ($('#upload-head-info-error').hasClass('head-info-error')) {
                        $('.head-info-error').remove();
                    }
                    $('#head-info-phone').after(data.result.msg);

                }


            }
        });


    }

    function clickIconFunction() {

        $('.fa-envelope, .fa-bell').click(function() {

            var url = $(this).parents().attr('href');
            var hasStyle = $(this).hasClass('navbar-msg-read');
            if (url != undefined && url != '') {
                if (hasStyle) {
                    window.location.href = url;
                } else {
                    var testMove = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
                    if (!testMove) {
                        window.location.href = url;
                    }
                }


            }

        });


    }

    function hoverAction() {

        $('#head-image-upload-thumbnail').mouseover(function() {

            $('#user_dropdown_avatar_uploader_button').addClass('photo-opacity');
        });


        $('#head-image-upload-thumbnail').mouseout(function() {

            $('#user_dropdown_avatar_uploader_button').removeClass('photo-opacity');
        });


        $('#head-image-upload-thumbnail').mouseup(function() {
            clickwindow = true;
            $('#user_dropdown_avatar_uploader_button').removeClass('photo-opacity');
        });


        $('#head-image-upload-thumbnail-phone').mouseup(function() {
            clickwindow = true;
        });
    }





    $(window).resize(function() {
        // for image style
        $('img').css({ "image-rendering": "auto" });

    });



    ;
    (function($, window, document, undefined) {


        var pluginName = 'bootstrapDropdownHover',
            defaults = {
                clickBehavior: 'default',
                hideTimeout: 200
            },
            _hideTimeoutHandler,
            _hardOpened = false,
            _touchstartDetected = false,
            _mouseDetected = false;

        function BootstrapDropdownHover(element, options) {
            this.element = $(element);
            this.settings = $.extend({}, defaults, options);
            this._defaults = defaults;
            this._name = pluginName;
            this.init();
        }

        function bindEvents(dropdown) {
            $('body').one('touchstart.dropdownhover', function() {
                _touchstartDetected = true;
            });

            $('body').one('mouseenter.dropdownhover', function() {
                if (!_touchstartDetected) {
                    _mouseDetected = true;
                }
            });



            $('.dropdown-toggle, .dropdown-menu', dropdown.element.parent()).on('mouseenter.dropdownhover', function() {
                //for small windows don't  mouseenter
                var detectedvalue = dropdown.element.find('span').hasClass('fa-item-right');
                if (!detectedvalue) {
                    detectedvalue = dropdown.element.next().next().hasClass('fa-item-right');
                }

                if (detectedvalue) {
                    if ($(window).width() < 767) {
                        return;
                    }

                }

                //touch device
                if (_mouseDetected && !$(this).is(':hover')) {
                    _mouseDetected = false;
                }

                if (!_mouseDetected) {
                    return;
                }

                clearTimeout(_hideTimeoutHandler);
                if (!dropdown.element.parent().hasClass('open')) {
                    _hardOpened = false;
                    dropdown.element.dropdown('toggle');
                }
            });

            $('.dropdown-toggle, .dropdown-menu', dropdown.element.parent()).on('mouseleave.dropdownhover', function() {

                var detectedvalue = dropdown.element.find('span').hasClass('fa-item-right');
                if (!detectedvalue) {
                    detectedvalue = dropdown.element.next().next().hasClass('fa-item-right');
                }
                if (detectedvalue) {
                    if ($(window).width() < 767) {
                        return;
                    }
                }

                if (clickwindow) {
                    setTimeout(function() {
                        clickwindow = false;
                    }, 1000);

                    return;

                }
                if (!_mouseDetected) {
                    return;
                }

                if (_hardOpened) {
                    return;
                }
                _hideTimeoutHandler = setTimeout(function() {
                    if (dropdown.element.parent().hasClass('open')) {
                        dropdown.element.dropdown('toggle');
                    }
                }, dropdown.settings.hideTimeout);
            });

            dropdown.element.on('click.dropdownhover', function(e) {
                if (!_mouseDetected) {
                    return;
                }

                switch (dropdown.settings.clickBehavior) {
                    case 'default':
                        return;
                    case 'disable':
                        e.preventDefault();
                        e.stopImmediatePropagation();
                        break;
                    case 'sticky':
                        if (_hardOpened) {
                            _hardOpened = false;
                        } else {
                            _hardOpened = true;
                            if (dropdown.element.parent().hasClass('open')) {
                                e.stopImmediatePropagation();
                                e.preventDefault();
                            }
                        }
                        return;
                }
            });
        }

        function removeEvents(dropdown) {
            $('.dropdown-toggle, .dropdown-menu', dropdown.element.parent()).off('.dropdownhover');
            $('.dropdown-toggle, .dropdown-menu', dropdown.element.parent()).off('.dropdown');
            dropdown.element.off('.dropdownhover');
            $('body').off('.dropdownhover');
        }

        BootstrapDropdownHover.prototype = {

            init: function() {
                this.setClickBehavior(this.settings.clickBehavior);
                this.setHideTimeout(this.settings.hideTimeout);
                bindEvents(this);
                return this.element;
            },
            setClickBehavior: function(value) {
                this.settings.clickBehavior = value;
                return this.element;
            },
            setHideTimeout: function(value) {
                this.settings.hideTimeout = value;
                return this.element;
            },
            destroy: function() {
                clearTimeout(_hideTimeoutHandler);
                removeEvents(this);
                this.element.data('plugin_' + pluginName, null);
                return this.element;
            }
        };


        $.fn[pluginName] = function(options) {
            var args = arguments;

            if (options === undefined || typeof options === 'object') {
                if (!$.contains(document, $(this)[0])) {
                    $('[data-toggle="dropdown"]').each(function(index, item) {
                        $(item).bootstrapDropdownHover(options);
                    });
                }
                return this.each(function() {
                    // not a select
                    if (!$(this).hasClass('dropdown-toggle') || $(this).data('toggle') !== 'dropdown') {
                        $('[data-toggle="dropdown"]', this).each(function(index, item) {
                            // For each nested select
                            $(item).bootstrapDropdownHover(options);
                        });
                    } else if (!$.data(this, 'plugin_' + pluginName)) {
                        $.data(this, 'plugin_' + pluginName, new BootstrapDropdownHover(this, options));
                    }
                });


            } else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') {

                var returns;

                this.each(function() {
                    var instance = $.data(this, 'plugin_' + pluginName);
                    if (instance instanceof BootstrapDropdownHover && typeof instance[options] === 'function') {
                        returns = instance[options].apply(instance, Array.prototype.slice.call(args, 1));
                    }
                });

                return returns !== undefined ? returns : this;
            }

        };

    })(jQuery, window, document);

    function open_or_download_app() {
        $('.open_button, .open_app').click(function() {
            var lang = navigator.language || navigator.userLanguage;
            var loadDateTime = new Date();
            window.setTimeout(function() {
                var timeOutDateTime = new Date();
                if (timeOutDateTime - loadDateTime < 2200) {
                     if (lang.startsWith('zh')) {
                        window.location = $("#app_link").attr("cn_link");
                    } else {
                        window.location = $("#app_link").attr("us_link");
                    }
                } else { window.close(); }
            }, 2000);
            window.location = $("#app_link").attr("open_link");
        });     
    };

    function downpop() {
        $('.close_button').click(function() {
            $('.down').hide();
        });
    };

    function init() {
        $('.navbar [data-toggle="dropdown"]').bootstrapDropdownHover();
        hoverAction();
        clickIconFunction();
        headImageUpload();
        open_or_download_app();
        downpop();
    };

    $(document).ready(function() {
        init();
    });

})();