/*

[Main Script]

Project: EcoHosting - Responsive Hosting and WHMCS WordPress Theme
Version: 1.8
Author : themelooks.com

*/

(function ($) {
    'use strict';
    
    $(function () {
        /* ------------------------------------------------------------------------- *
         * CUSTOM BACKGROUND IMAGE
         * ------------------------------------------------------------------------- */
        var bgSrc = $('[data-bg-src]');
        
        bgSrc.each(function () {
            var $this = $(this),
                imgValue = $this.data('bg-src');
            $this.css('background-image', 'url(' + imgValue + ')');
        });
        
        /* ------------------------------------------------------------------------- *
         * BACKGROUND VIDEO
         * ------------------------------------------------------------------------- */
        var $bgVideoEl = $('[data-bg-video]');
        
        if ( $bgVideoEl.length ) {
            $bgVideoEl.tubular({videoId: $bgVideoEl.data('bg-video'), wrapperZIndex: 0});
        }
        
        /* ------------------------------------------------------------------------- *
         * FIXED MENU
         * ------------------------------------------------------------------------- */
        var wn = $(window)
        ,   $menu = $('#menu')
        ,   $menuTopbar = $('.menu--topbar')
        ,   primaryMenu = $('#primaryMenu')
        ,   secondaryMenu = $('#secondaryMenu')
        ,   menuHalfHeight = $menuTopbar.outerHeight() + primaryMenu.outerHeight()
        ,   menuTotalHeight = menuHalfHeight + secondaryMenu.outerHeight()
        ,   secondaryMenuToggle = function () {
                return wn.scrollTop() > menuHalfHeight ? secondaryMenu.addClass('sticky') : secondaryMenu.removeClass('sticky');
            };
        
        if ( secondaryMenu.length ) {
            secondaryMenuToggle();
        }

        wn.on('load resize', function () {
            menuTotalHeight = menuHalfHeight + secondaryMenu.outerHeight();
            $menu.next('.HeaderAdjust').css('margin-top', menuTotalHeight);
        });
		
        var $dropdownMenuToggle = $('.dropdown-menu > li > .dropdown-toggle');
		
		$dropdownMenuToggle.on('click', function (e) {
			$(this).parent().toggleClass('open');
			return false;
		});
		
        /* ------------------------------------------------------------------------- *
         * BACK TO TOP BUTTON
         * ------------------------------------------------------------------------- */
        var $backToTopBtn = $('#backToTop'),
            $domainOffer = $('.domain--offer'),
            showBackToTopBtn = function () {
                return wn.scrollTop() > 1 ? $backToTopBtn.add($domainOffer).addClass('show') : $backToTopBtn.add($domainOffer).removeClass('show');
            };
        
        $backToTopBtn.on('click', function () {
            $('html, body').animate({
                scrollTop: 0
            }, 500);
            
            return false;
        });
        
        if ( $backToTopBtn.length ) {
            showBackToTopBtn();
        }
        
        /* ------------------------------------------------------------------------- *
         * ON SCROLL
         * ------------------------------------------------------------------------- */
        wn.on('scroll', function () {
            /* TOGGLE SECONDARY MENU */
            if ( secondaryMenu.length ) {
                secondaryMenuToggle();
            }
            
            /* SHOW BACK TO TOP BUTTON */
            if ( $backToTopBtn.length ) {
                showBackToTopBtn();
            }
        });
        
        /* -------------------------------------------------------------------------*
         * TOOLTIP
         * -------------------------------------------------------------------------*/
        var $tooltip = $('[data-toggle="tooltip"]');
        
        if ( $tooltip.length ) {
            $tooltip.tooltip();
        }
        
        /* -------------------------------------------------------------------------*
         * OWL CAROUSEL
         * -------------------------------------------------------------------------*/
        var headerSlider = $('.header-slider'),
            headerSliderNav = $('.header--slider-nav'),
			$speed = $( '#header-slider' ).data('spped');
        
        if ( headerSlider.length ) {
            headerSlider.owlCarousel({
                singleItem: true,
                slideSpeed: 800,
                autoPlay: $speed,
                addClassActive: true,
                pagination: false,
                navigation: true,
                navigationText: ['<i class="fa fa-long-arrow-left"></i>', '<i class="fa fa-long-arrow-right"></i>'],
                afterInit: function () {
                    headerSliderNav.find('li').removeClass('active').eq(this.currentItem).addClass('active');
                },
                afterMove: function () {
                    headerSliderNav.find('li').removeClass('active').eq(this.currentItem).addClass('active');
                }
            });
        }
        
        headerSliderNav.on('click', 'li', function () {
            headerSlider.trigger('owl.goTo', $(this).index());
        });
        
        var testimonialSlider = $('.testimonial-slider')
        ,   testimonialCustomPagination = function () {
                $.each(this.owl.userItems, function (i) {
                    var recommenderThumb = jQuery(this).data('recommender-thumb');
                    var paginationLinks = jQuery('.testimonial-slider .owl-page span');

                    $(paginationLinks[i]).html('<img src="'+ recommenderThumb +'" alt="" class="img-responsive" />');
                });
            };
            
        if ( testimonialSlider.length ) {
            if (testimonialSlider.children('.testimonial-item').length > 3) {
                testimonialSlider.addClass('overload');
            }
            testimonialSlider.owlCarousel({
                slideSpeed: 700,
                paginationSpeed: 700,
                singleItem: true,
                autoPlay: true,
                addClassActive: true,
                afterInit: testimonialCustomPagination,
                afterUpdate: testimonialCustomPagination
            });
        }
        
        var brandsSlider = $('.brands-slider');
            
        if ( brandsSlider.length ) {
            brandsSlider.owlCarousel({
                slideSpeed: 700,
                paginationSpeed: 700,
                items: 5,
                autoPlay: true,
                pagination: false
            });
        }

        /* ------------------------------------------------------------------------- *
         * VPS SLIDER
         * ------------------------------------------------------------------------- */
        var $headerVPS = $('.vps-pricing--slider-holder')
        ,   vpsSlider = $('.VPSPricingSlider')
        ,   vpsItemCPUel = $('.vps-pricing--cpu')
        ,   vpsItemRAMel = $('.vps-pricing--ram')
        ,   vpsItemSPACEel = $('.vps-pricing--space')
        ,   vpsItemBANDWIDTHel = $('.vps-pricing--bandwidth')
        ,   vpsItemPriceEl = $('.vps-pricing--total-price span')
        ,   vpsPricingBtn = $('.vps-pricing--action-btn .btn');
        
        var vpsSliderInit = function (vpsSliderOpts) {
                        
            var $countSlid = vpsSliderOpts.length - 1;
           
            // VPS slider variables
            var $uiSliderHandle;
            
            // Add slider pips
            for ( var i = 0; i < $countSlid; i++ ) {
                $('<div class="pip"></div>')
                    .css('left', ((100 / $countSlid) * i) + '%')
                    .appendTo( $headerVPS.children('.pips') );
            }
            
            // Initialize slider
            vpsSlider.slider({
                animate: "fast",
                range: "min",
                min: 0,
                max: $countSlid,
                value: 1,
                step: 1,
                create: function () {
                    
                   
                    
                    $(vpsItemCPUel[0]).text(vpsSliderOpts[1]._eco_vps_plan_feature[0]);
                    $(vpsItemCPUel[1]).text(vpsSliderOpts[1]._eco_vps_plan_feature[1]);
                    $(vpsItemCPUel[2]).text(vpsSliderOpts[1]._eco_vps_plan_feature[2]);
                    $(vpsItemCPUel[3]).text(vpsSliderOpts[1]._eco_vps_plan_feature[3]);
                    
                    vpsPricingBtn.attr('href', vpsSliderOpts[1]._eco_vps_plan_btnurl);
                    
                    $uiSliderHandle = vpsSlider.children('.ui-slider-handle');
                    $('<i class="fa fa-map-marker"></i><em></em>').appendTo($uiSliderHandle);
                    $uiSliderHandle.children('em').html(vpsSliderOpts[1]._eco_vps_plan_name);
                },
                slide: function (event, ui) {
                    
                    
                    
                    $(vpsItemCPUel[0]).text(vpsSliderOpts[ui.value]._eco_vps_plan_feature[0]);
                    $(vpsItemCPUel[1]).text(vpsSliderOpts[ui.value]._eco_vps_plan_feature[1]);
                    $(vpsItemCPUel[2]).text(vpsSliderOpts[ui.value]._eco_vps_plan_feature[2]);
                    $(vpsItemCPUel[3]).text(vpsSliderOpts[ui.value]._eco_vps_plan_feature[3]);
                    $(vpsItemCPUel[4]).text(vpsSliderOpts[ui.value]._eco_vps_plan_feature[4]);
                    
                    vpsPricingBtn.attr('href', vpsSliderOpts[ui.value]._eco_vps_plan_btnurl);
                    
                    $uiSliderHandle.children('em').html(vpsSliderOpts[ui.value]._eco_vps_plan_name);
                }
            });
        };

        if ( vpsSlider.length ) {
            
            $.ajax({
                type: 'POST',
                url: ajaxvps.ajaxurl,
                data: {
                    action: 'vpspricing_ajax',
                    vpscontent: ajaxvps.vpscontent,
                    vps_security: ajaxvps.vps_security
                },
                
                dataType: 'json',
                success: vpsSliderInit
                
            });
        }
        
        /* -------------------------------------------------------------------------*
         * COUNTER
         * -------------------------------------------------------------------------*/
        var counterNum = $('.counter-number');
            
        if ( $(counterNum).length ) {
            $(counterNum).counterUp({
                delay: 10,
                time: 1000
            });
        }
        
        /* -------------------------------------------------------------------------*
         * PRICING TABLE LABEL
         * -------------------------------------------------------------------------*/
        if ( wn.width() < 992 ) {
            $('#compare table td, #pricingTable2 table td').each(function () {
                $(this).prepend('<span class="labelText">'+ $(this).data('label') + '</span>');
            });
        }
        
        /* -------------------------------------------------------------------------*
         * MAP
         * -------------------------------------------------------------------------*/
  
        function map_marker( gtlat, gtlng, gtzoom ){
            
            var $map = $('#map'),
            map, marker, myLatLng;
            
            if ( $map.length ) {
                myLatLng = {
                    lat: parseFloat( gtlat ),
                    lng: parseFloat( gtlng )
                };

                map = new google.maps.Map(document.getElementById('map'), {
                    center: myLatLng,
                    zoom: parseFloat( gtzoom ),
                    scrollwheel: false,
                    disableDefaultUI: true,
                    zoomControl: true
                });
                
                marker = new google.maps.Marker({
                    position: myLatLng,
                    map: map,
                    animation: google.maps.Animation.DROP,
                    draggable: true
                });
                
                if ( wn.width() < 767 ) {
                    map.setOptions({draggable: false});
                }
            } 
        }
        
        
        $.ajax({
        
            type:'POST',
            url:ajaxvps.ajaxurl,
            data:{
                action: 'contactpage_map',
                contact_security: ajaxvps.contact_security
            },
            dataType: 'json',
            success: function( data ){ 
                map_marker( data.lat, data.lng, data.zoom );
            }
     
        });
        
        
        
    });
    
    $(window).on('load', function () {
        /* ------------------------------------------------------------------------- *
         * PRELOADER
         * ------------------------------------------------------------------------- */
        var $preloader = $('#preloader');
        
        if ( $preloader.length ) {
            $preloader.fadeOut('slow');
        }
    });
})(jQuery);
