/* [ ---- Ebro Admin - common js ---- ] */

    $(function() {
        //* icon navigation
        ebro_navigation.icons();
        //* text navigation
        ebro_navigation.text();
        //* mobile navigation
        ebro_navigation.mobile();
        //* accordions
		ebro_accordions.init();
		//* tooltips_popovers
		ebro_tooltips_popovers.init();
		//* main search autocomplete
		ebro_autocomplete.init();
		
        //* ebro style switcher
        ebro_styleSwitcher.init();
        
        //* don't close dropdown on document click
        $('.notification_dropdown .dropdown-menu').click(function(e) {
            e.stopPropagation();
        });
		
    });

    //* get text without DOM element from node
    jQuery.fn.justtext=function(){return $.trim($(this).clone().children().remove().end().text())};

    //* avoid 'console' errors in browsers that lack a console
    if (!(window.console && console.log)) {
        (function() {
            var noop = function() {};
            var methods = ['assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error', 'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log', 'markTimeline', 'profile', 'profileEnd', 'markTimeline', 'table', 'time', 'timeEnd', 'timeStamp', 'trace', 'warn'];
            var length = methods.length;
            var console = window.console = {};
            while (length--) {
                console[methods[length]] = noop;
            }
        }());
    }

    //* detect touch devices 
    function is_touch_device() {
        return !!('ontouchstart' in window);
    };
	
	//* detect HiRes displays
    function isRetina(){
		var mediaQuery = "(-webkit-min-device-pixel-ratio: 1.5),\
				(min--moz-device-pixel-ratio: 1.5),\
				(-o-min-device-pixel-ratio: 3/2),\
				(min-resolution: 1.5dppx)";
		if (window.devicePixelRatio > 1)
			return true;
		if (window.matchMedia && window.matchMedia(mediaQuery).matches)
			return true;
		return false;
	};
    
    //* browser detect
    jQuery.browser = {};
    jQuery.browser.mozilla = /mozilla/.test(navigator.userAgent.toLowerCase()) && !/webkit/.test(navigator.userAgent.toLowerCase());
    jQuery.browser.webkit = /webkit/.test(navigator.userAgent.toLowerCase());
    jQuery.browser.opera = /opera/.test(navigator.userAgent.toLowerCase());
    jQuery.browser.msie = /msie/.test(navigator.userAgent.toLowerCase());
    
    
    //* navigation
    ebro_navigation = {
        //* icon navigation
        icons: function() {
            if($('#icon_nav_v').length && $('#icon_nav_v').is(':visible')) {
				//* sticky menu 
				$('#icon_nav_v').sticky('.main_section',{
					animate: false,
					useFixed: false,
					animTime: 300
				});
			}
        },
        //* text navigation
        text: function() {
            if($('#text_nav_v').length && $('#text_nav_v').is(':visible')) {
                
				// Initialize navgoco with default options
				$("#text_nav_v").navgoco({
					caret: '<i class="icon-angle-down"></i>',
					accordion: false,
					openClass: 'parent_active',
					save: true,
					cookie: {
						name: 'navgoco',
						expires: false,
						path: '/'
					},
					slide: {
						duration: 400,
						easing: 'swing'
					}
				});
				
				$("#text_nav_v_collapse").click(function(e) {
					e.preventDefault();
					$("#text_nav_v").navgoco('toggle', false);
				});
			
				$("#text_nav_v_expand").click(function(e) {
					e.preventDefault();
					$("#text_nav_v").navgoco('toggle', true);
				});
                
				//* sticky menu 
				$('#text_nav_v').sticky('.main_section',{
					animate: false,
					useFixed: false,
					animTime: 300
				});
				
            }
            if($('#text_nav_h').length && $('#text_nav_h').is(':visible')) {
				
				//* sub-level menu
                $("#text_nav_h").jMenu({
                    openClick : (is_touch_device()) ? true : false,
                    ulWidth : 200,
                    absoluteTop : 31,
                    absoluteLeft : 0,
                    effects : {
                        effectSpeedOpen : 100,
                        effectSpeedClose : 100,
                        effectTypeOpen : 'show',
                        effectTypeClose : 'hide',
                        effectOpen : 'linear',
                        effectClose : 'linear'
                    },
                    TimeBeforeOpening : 100,
                    TimeBeforeClosing : 100,
                    animatedText : false
				});
                //* add arrow to navigation parent
                $('#text_nav_h a.fNiv').each(function() {
                    if($(this).next('ul').length) {
						$(this).addClass('isTopParent').append('<i class="icon-angle-down"></i>')
					}
                });
                $('#text_nav_h ul a.isParent').each(function() {
                    $(this).append('<i class="icon-angle-right"></i>');
                });
                //* add "active" class on mouseenter
                $('#text_nav_h li').on('mouseenter',function() {
                    $(this).addClass('active');
                }).on('mouseleave',function() {
                    $(this).removeClass('active');
                });
            }
        },
        //* mobile navigation
        mobile: function() {
            if($('#text_nav_v').length) {
                $("#text_nav_v").tinyNav({
                    target: $('#mobile_navigation'),
                    active: 'link_active'
                });
				$('#mobile_navigation select').addClass('form-control').wrap('<div class="container" />');
            }
            if($('#text_nav_h').length) {
                $("#text_nav_h").tinyNav({
                    target: $('#mobile_navigation'),
					active: 'link_active'
                });
				$('#mobile_navigation select').addClass('form-control').wrap('<div class="container" />');
            }
        }
    }
    
	//* accordions
	ebro_accordions = {
		init: function() {
			$('.panel-group .panel-title a').each(function() {
				var $this = $(this);
				$this.append('<span class="icon-angle-left"></span>');
			})
			
			$('.panel-group .panel-collapse.in').each(function() {
				var $this = $(this);
				$this.closest('.panel').addClass('sect_active').find('.panel-title [class^="icon-"]').toggleClass('icon-angle-left icon-angle-up')
			})
			
			//* add active class (accorion show)
			$('.panel-group .panel-collapse').on('show.bs.collapse',function() {
				$(this).closest('.panel').addClass('sect_active').find('.panel-title [class^="icon-"]').toggleClass('icon-angle-left icon-angle-up');
			}).on('hide.bs.collapse',function() {
				$(this).closest('.panel').removeClass('sect_active').find('.panel-title [class^="icon-"]').toggleClass('icon-angle-left icon-angle-up');
			});
		}
	}
	
	//* tooltips, popovers
	ebro_tooltips_popovers = {
		init: function() {
			$('[data-toggle=tooltip]').tooltip();
			$('[data-toggle=popover]').popover();
		}
	}
	
	//* autocomplete
	ebro_autocomplete = {
		init: function() {
			$('.main_search .typeahead').typeahead({
				name: 'ebro-countries',
				valueKey: 'name',
				prefetch: 'js/lib/typeahead.js/example.json',
				template: [
					'<p class="sg_main"><b>{{name}}</b> <small class="text-muted">{{tld}}</small></p>',
					'<p class="sg_desc">{{subregion}}</p>'
				].join(''),
				engine: Hogan
			});
		}
	}
	
    //* style switcher
    ebro_styleSwitcher = {
        init: function() {
            $('#layout_style>option:eq(0)').prop('selected', true);
            $('#sidebar_left').prop('checked', true);
            
			//* layout
            $('#layout_style').on('change', function() {
                $this_val = $(this).val();
                
                $('body').removeClass('boxed full_width').removeClass('pattern_1 pattern_2 pattern_3 pattern_4 pattern_5 pattern_6 pattern_7 pattern_8  pattern_9  pattern_10');
                $('#style_pattern').hide();
                
                if($this_val == 'boxed') {
                    $('body').removeClass('full_width');
                    $('body').addClass($this_val+' pattern_1');
                    $('#style_pattern').show();
                }
                if($this_val == 'full_width') {
                    $('body').addClass('full_width');
                }
            })
            
			//* colors
            $('#theme_switch li').on('click',function() {
                var $color = $(this).attr('title');
                $('#theme_switch li').removeClass('style_active');
                $(this).addClass('style_active');
                $('#theme').attr('href','css/theme/'+$color+'.css');
            });
            
			//* patterns
            $('#style_pattern li').on('click',function() {
                var $pattern = $(this).attr('title');
                $('#style_pattern li').removeClass('pattern_active');
                $(this).addClass('pattern_active');
                $('body').removeClass('pattern_1 pattern_2 pattern_3 pattern_4 pattern_5 pattern_6 pattern_7 pattern_8  pattern_9  pattern_10').addClass($pattern);
            });
            
			//* show/hode style switcher
            $('.switcher_toggle').on('click', function(e) {
                if(!$('#style_switcher').hasClass('switcher_open')) {
                    $('#style_switcher').animate({ marginRight: 0 },200, function() {
                        $(this).addClass('switcher_open')  
                    })  
                } else {
                    $('#style_switcher').animate({ marginRight: '-191px'},200, function() {
                        $(this).removeClass('switcher_open')  
                    })  
                }
                e.preventDefault();
            })
            
			//* sidebar position
            $('#sidebar_switch input[name="sidebar_position"]').on('change', function() {
                $this_val = $(this).val();
                if($this_val == 'right') {
                    $('body').addClass('sidebar_right');
                } else {
                    $('body').removeClass('sidebar_right');
                }
            })
            
			//* reset styles
            $('#style_reset').click(function() {
                $('body').removeClass('pattern_1 pattern_2 pattern_3 pattern_4 pattern_5 pattern_6 pattern_7 pattern_8  pattern_9  pattern_10').addClass('pattern_1');
                $('#style_pattern li').removeClass('pattern_active').eq(0).addClass('pattern_active');
                $('#style_pattern').hide();
                
                $('#layout_style>option:eq(0)').prop('selected', true);
                $('body').removeClass('boxed');
                
                $('body').removeClass('full_width');
                
                $('#theme_switch li').removeClass('style_active').eq(0).addClass('style_active');
                $('#theme').attr('href','css/theme/color_1.css');
                
                $('body').removeClass('sidebar_right');
            })
			
			$('#how_to').click(function() {
					
			})
			
			setTimeout(function(){
				$('#style_switcher').animate({ marginRight: '-191px'},200, function() {
					$(this).removeClass('switcher_open')  
				})	
			},100);
			
        }
    }