jQuery(document).ready(
    function($) {
        "use strict";
        $('#main').fadeTo(1000, 1);
        $('.widget-title').addClass(title_picker);
        $('.slide-title, .slide-excerpt, .slide-date').addClass(widget_fx);
        $('.blog-post-image img, .img-featured-posts-image img, .featured-posts-image img, .small-image img, .tv-big-image img, .tv-small-posts-image img, .multi-category-image img').wrap("<div class='" + image_effect + "'></div>");
        $('#secondary .home-widget').each(function() {
            $(this).removeClass('two-parts three-parts four-parts').addClass('one-part')
        });

        //wp-widgets width
        $('.widget_recent_comments, .widget_recent_entries, .widget_calendar, .widget_nav_menu, .widget_pages, .widget_archive, .widget_links, .widget_meta, .widget_tag_cloud, .widget_rss, .widget_search, .widget_categories, .woocommerce, .widget[id*=bbp_], .widget[id*=bp_]').each(function() {
            $(this).closest('.home-widget').addClass('one-part');
        });


        $('.style_title2, .style_title6, .style_title7, .style_title8').wrapInner("<div class='widget-style-wrap'></div>");
        //widget-title	

        $('.style_title5, .style_title4, .style_title3, .style_title1, .comment-count').each(function() {
            var html = $(this).html();
			var text = $(this).text();
            var split = html.split(" ");
			var splittext = text.split(" ");
            if (split.length > 1 && splittext.length > 1) {
                split[split.length - 1] = "<span class='last-word'>" + split[split.length - 1] + "</span>";
                $(this).html(split.join(" "));
            } else {}
        });

        $('.slide-title h2 a, .tv-featured-title').each(function() {
            var html = $(this).html();
            var split = html.split(" ");
            if (split.length > 3) {
                split[split.length - 1] = "<span class='last-word'>" + split[split.length - 1] + "</span>";
                split[split.length - 2] = "<span class='last-word'>" + split[split.length - 2] + "</span>";
                $(this).html(split.join(" "));
            } else {}
        });



        if ($('.tabber-container').length) {
            //tabs
            $('.tabber-container').each(function() {
                $(this).find(".tabber-content").hide();
                $(this).find("ul.tabs li:first").addClass("active").show();
                $(this).find(".tabber-content:first").show();
            });
            $("ul.tabs li").click(
                function(e) {
                    $(this).parents('.tabber-container').find("ul.tabs li")
                        .removeClass("active");
                    $(this).addClass("active");
                    $(this).parents('.tabber-container').find(
                        ".tabber-content").hide();
                    var activeTab = $(this).find("a").attr("href");
                    $(this).parents('.tabber-container').find(activeTab)
                        .fadeIn();
                    e.preventDefault();
                });
            $("ul.tabs li a").click(function(e) {
                e.preventDefault();
            });
        }
        //ticker

        function tick() {
            var first_tick = $("ul.ticker-list li:first").width();
            $("ul.ticker-list li:first").animate({
                marginLeft: -first_tick
            }, 800, function() {
                $(this).detach().appendTo("ul.ticker-list").removeAttr("style");
                var first_tick = $("ul.ticker-list li:first").width();
            });
        }

        function tak() {
            var last_tick = $("ul.ticker-list li:last-child").width();
            $("ul.ticker-list li:first").animate({
                marginLeft: last_tick
            }, 800, function() {
                $("ul.ticker-list li:last-child").detach().prependTo("ul.ticker-list");
                $("ul.ticker-list li").removeAttr("style");
                var last_tick = $("ul.ticker-list li:last-child").width();
            });
        }
        $('.ticker-right').click(function() {
            tick();
        });
        $('.ticker-left').click(function() {
            tak();
        });

        var interval = setInterval(tick, 5000);

        $('ul.ticker-list li, .ticker-left, .ticker-right').hover(function() {
            clearInterval(interval);
        }, function() {
            interval = setInterval(tick, 5000);
        });
		
		
        function ticket_width() {
            $('#ticker-list-box').css('width', $('#ticker').width() - $('.ticker-heading').outerWidth() - 20);
        }
        ticket_width();
        $(window).resize(ticket_width);
	$(window).load(function() { ticket_width();});	

        //share-icons
		
        function sharewidthresponsive() {
            var sharewidthfull = $('.share-post').outerWidth(true);
            var sharewidth = $('.share-title').outerWidth(true);
            $('.share-post ul').css('width', sharewidthfull - sharewidth - 2);

        };
        sharewidthresponsive();
        $(window).resize(sharewidthresponsive);



        //fixed-menu	
        var aboveHeight = $('#nav-wrapper').offset().top;
        $(window).scroll(function() {
            if ($(window).scrollTop() > aboveHeight) {
                $('.show-menu').addClass('fixed-menu');
            } else {
                $('.show-menu').removeClass('fixed-menu');
            }

        });

        $(window).load(function() {
            //Masonry script
            if ($('#fullwidth').length) {
                var fullwidthmas = $('#fullwidth').masonry({
                    columnWidth: function(containerWidth) {
                        return containerWidth / 4;
                    },
                    itemSelector: '.home-widget',
                    animationOptions: {
                        duration: 200
                    }
                });
                fullwidthmas.imagesLoaded(function() {
                    setTimeout(function() {
                        fullwidthmas.masonry();
                    }, 500);
                });
                $(window).resize(function() {
                    setTimeout(function() {
                        fullwidthmas.masonry();
                    }, 500);
                });
            }
        });

        //sidebar height

        function sidebarheight() {
            $('#main').imagesLoaded(function() {
                var fullsize = $('#wrapper').width();
                var primaryheight = $('#primary').height();
                if (fullsize > 1024) {
                    $('#secondary').css('min-height', primaryheight);
                } else {
                    $('#secondary').css('min-height', 0);
                }

            });
        }
		$(window).load(function() {
			setTimeout(function() {
                         sidebarheight();
                    }, 500);
       
		});
        $(window).resize(sidebarheight);

        //fixed-sidebar-last widget

        $('#main').imagesLoaded(function() {
            if ($('#secondary .home-widget:last-child').length) {

                var widgetaboveHeight = $('#secondary .home-widget:last-child ').offset().top - 34;
                if ($('#navigation').hasClass('show-menu')) {
                    $('#secondary .home-widget:last-child').addClass('navigation-has-menu');
                }

                var primaryheight = $('#primary').height();
                var secondaryheight = 0;
                $('#secondary .home-widget').each(function() {
                    secondaryheight = $(this).height();
                });


                //fixed widget in #secondary area			
                $(window).scroll(function() {

                    if (primaryheight - secondaryheight < 0) {
                        $('#secondary .fixed-widget').css('position', 'relative');
                        $('#secondary .home-widget:last-child ').css('top', '0');

                    } else {
                        if ($(window).scrollTop() > widgetaboveHeight) {
                            $('#secondary .home-widget:last-child').addClass('fixed-widget');
                        } else {
                            $('#secondary .home-widget:last-child ').removeClass('fixed-widget');
                        }

                        if ($('.footer-wrap').offset().top - ($(window).scrollTop() + $('#fullwidth ').height() + $('#secondary .home-widget:last-child').height() + $('#navigation.fixed-menu').height() + 60) < 0) {
                            $('#secondary .home-widget:last-child ').css('top', $('.footer-wrap').offset().top - ($(window).scrollTop() + $('#fullwidth ').height() + $('#secondary .home-widget:last-child').height() + 60));

                            //alert($('#fullwidth ').height());
                        } else {
                            $('#secondary .home-widget:last-child ').css('top', '')
                        }
                    }
                });
            };
        });

        function fixedwidgetwidth() {
            var secondarywidth = $('#secondary').width();
            $('#secondary .home-widget:last-child ').css('max-width', secondarywidth);
        }
        fixedwidgetwidth();
        $(window).resize(fixedwidgetwidth);




        //scroll effects

        $(window).scroll(function() {
            var t1ttop = $(this).scrollTop();
            var t1theight = $(this).height();
            var t1tbottom = t1ttop + t1theight;


            $('.home-widget').each(function() {

                var geget = $(this).offset().top;
                if (t1tbottom > geget && geget > t1theight && $(window).width() > 1024) {
                    $(this).find('.widget').addClass(widget_fx);
                }

            });

            $('.score-line').each(function() {

                var geget = $(this).offset().top;
                if (t1tbottom > geget && geget > t1theight) {
                    $(this).find('.score-width').addClass('active');
                }
            });
        });

        //carousel
        if ($('.carousel').length) {
            $(window).load(function() {
                $('.carousel').each(function() {
                    function normalcarousel_grid() {
                        return (window.innerWidth < 600) ? 2 :
                            (window.innerWidth < 1024) ? 3 : carouselitems;
                    }
                    $(window).resize(function() {
                        var gridSize = normalcarousel_grid();
						$('.carousel').each(function() {
							$(this).data('flexslider').vars.minItems = gridSize;	
						});
						$('.carousel').each(function() {
							$(this).data('flexslider').vars.maxItems = gridSize;	
						});
						$('.carousel').each(function() {
							$(this).data('flexslider').vars.move = gridSize;	
						});
                    });
                    var widget_size = $(this).closest('.home-widget');

                    if ($(widget_size).hasClass('one-part')) {
                        var carouselitems = 2;
                    } else if ($(widget_size).hasClass('two-parts')) {
                        var carouselitems = 3;
                    } else if ($(widget_size).hasClass('three-parts')) {
                        var carouselitems = 4;
                    } else if ($(widget_size).hasClass('four-parts')) {
                        var carouselitems = 6;
                    }


                    $(this).flexslider({

                        animation: 'slide',
                        itemWidth: 210,
                        itemMargin: 10,
                        minItems: normalcarousel_grid(),
                        maxItems: normalcarousel_grid(),
                        move: normalcarousel_grid(),
                        slideshow: false,
                        controlNav: false,
                        directionNav: true,
                        start: function(slider) {
                            slider.fadeTo(1000, 1);
                            slider.removeClass('loading');
                        },
                        after: function(slider) {
                            slider.resize();
                        }
                    });
                });
            });
        }


        //tv-widget carousel
        if ($('.tv-ajax-carousel').length) {
            $(window).load(function() {
                $('.tv-ajax-carousel').each(function() {
                    function ajaxcarousel_grid() {
                        return (window.innerWidth < 600) ? 2 :
                            (window.innerWidth < 1024) ? 3 : carouselitems;
                    }
                    $(window).resize(function() {
                        var gridSize = ajaxcarousel_grid();
						$('.tv-ajax-carousel').each(function() {
							$(this).data('flexslider').vars.minItems = gridSize;	
						});
						$('.tv-ajax-carousel').each(function() {
							$(this).data('flexslider').vars.maxItems = gridSize;	
						});
						$('.tv-ajax-carousel').each(function() {
							$(this).data('flexslider').vars.move = gridSize;	
						});
                    });
                    var widget_size = $(this).closest('.home-widget');

                    if ($(widget_size).hasClass('one-part')) {
                        var carouselitems = 2;
                    } else if ($(widget_size).hasClass('two-parts')) {
                        var carouselitems = 3;
                    } else if ($(widget_size).hasClass('three-parts')) {
                        var carouselitems = 4;
                    } else if ($(widget_size).hasClass('four-parts')) {
                        var carouselitems = 6;
                    }


                    $(this).flexslider({

                        animation: 'slide',
                        itemWidth: 410,
                        itemMargin: 10,
                        minItems: ajaxcarousel_grid(),
                        maxItems: ajaxcarousel_grid(),
                        move: ajaxcarousel_grid(),
                        slideshow: false,
                        controlNav: false,
                        directionNav: true,
                        start: function(slider) {

                        },
                        after: function(slider) {
                            slider.resize();
                        }
                    });
                });
            });
        }


        //video type page carousel
        if ($('.tv-carousel').length) {
            var tv_carousel;



            $(window).load(function() {
                function tv_carousel_grid() {
                    return (window.innerWidth < 600) ? 2 :
                        (window.innerWidth < 1024) ? 3 : 5;
                }
                $('.tv-carousel').flexslider({
                    animation: 'slide',
                    itemWidth: 210,
                    itemMargin: 10,
                    minItems: tv_carousel_grid(),
                    maxItems: tv_carousel_grid(),
                    move: tv_carousel_grid(),
                    slideshow: false,
                    controlNav: false,
                    directionNav: true,
                    start: function(slider) {
                        tv_carousel = slider;
                    },
                    after: function(slider) {
                        tv_carousel.resize();
                    }
                });

                $(window).resize(function() {
                    var gridSize = tv_carousel_grid();
                    tv_carousel.vars.minItems = gridSize;
                    tv_carousel.vars.maxItems = gridSize;
                });


            });
        }


        //type pages ajax
        $('.term-post-format-video').on('click', '.ajax', function(e) {
            e.preventDefault();
            var post_id = $(this).attr("data-number");

            jQuery.ajax({
                post: post_id,
                type: "POST",
                data: {
                    id: post_id
                },
                success: function(output) {
                    $(".tv-video-wrapper").replaceWith($('.tv-video-wrapper', output));
                    $(".tv-format-title").replaceWith($('.tv-format-title', output));
                    $(".tv-format-subtitle").replaceWith($('.tv-format-subtitle', output));
                }
            });
        });



        $(window).load(function() {
            $('.term-post-format-gallery').on('click', '.ajax', function(e) {
                e.preventDefault();
                var post_id = $(this).attr("data-number");

                jQuery.ajax({
                    post: post_id,
                    type: "POST",
                    data: {
                        id: post_id
                    },
                    success: function(output) {
                        $(".tv-video-wrapper").replaceWith($('.tv-video-wrapper', output));
                        $(".tv-format-title").replaceWith($('.tv-format-title', output));
                        $(".tv-format-subtitle").replaceWith($('.tv-format-subtitle', output));
                        $('.post-page-gallery-thumbnails').flexslider({
                            animation: 'slide',
                            controlNav: false,
                            animationLoop: false,
                            slideshow: false,
                            itemWidth: 166,
                            itemMargin: 0,
                            minItems: 4,
                            maxItems: 4,
                            move: 4,
                            directionNav: true,
                            asNavFor: '.post-page-gallery-slider',
                            start: function(slider) {
                                slider.fadeTo(1000, 1);
                                slider.removeClass('loading');
                            }
                        });

                        $('.post-page-gallery-slider').flexslider({
                            animation: slide_picker,
                            controlNav: false,
                            animationLoop: false,
                            slideshow: false,
                            sync: '.post-page-gallery-thumbnails',
                            start: function(slider) {
                                slider.fadeTo(1000, 1);
                                slider.removeClass('loading');
                            }
                        });
                    }
                });
            });
        });

        $('.term-post-format-video, .term-post-format-gallery').on('click', '.pagination a', function(e) {
            e.preventDefault();
            var link = $(this).attr('href');

            $('.tv-page-widget').append('<div class="more-posts"></div>');
            $('.pagination').replaceWith('<div class="load-content"><div class="load-circle"></div></div>');
            $('.more-posts').load(link + ' .tv-page-widget li, .pagination', function() {
                $('.more-posts li').hide().detach().appendTo('.tv-page-widget ul').fadeIn(500);
                $('.more-posts .pagination').detach().appendTo('.tv-page-widget');
                $('.more-posts').remove();
                $('.load-content').remove();

            });
        });




        //keyboard navigation next prev 		
        $(document).keydown(function(e) {
            var url = false;
            if (e.which == 37) { // Left arrow key code
                url = $('.previous-title a').attr('href');
            } else if (e.which == 39) { // Right arrow key code
                url = $('.next-title a').attr('href');
            }
            if (url) {
                window.location = url;
            }
        });

        //menu button for responsive mobile


        $("#mob-menu").click(function() {
            $("#main-nav ul, #navigation").toggleClass("active");
            $(this).toggleClass("active");

        });


        //super slider widget



        function superslidermargin() {
            $('.fullwidth-super-slider, .fullwidth-ticker').css('max-width', $(window).width());
            $('.fullwidth-super-slider, .fullwidth-ticker').css('margin-left', -($('#main').offset().left + 15));
        };
        superslidermargin();
        $(window).resize(superslidermargin);

        if ($('.super-slider').length) {
            $(window).load(function() {
                function getGridSize() {
                    return (window.innerWidth < 600) ? 1 :
                        (window.innerWidth < 1024) ? 2 : 3;
                }
					$('.super-slider').flexslider({
                    animation: "slide",
                    animationLoop: false,
                    itemWidth: 210,
                    itemMargin: 0,
                    move: 1,
                    minItems: getGridSize(), // use function to pull in initial value
                    maxItems: getGridSize(), // use function to pull in initial value
                    start: function(slider) {                   
                        $('.loading').fadeTo(1000, 1);
                        slider.removeClass('loading');
                    },
                    after: function(slider) {
                        slider.resize();
                    }
                });
			
                // check grid size on resize event
                $(window).resize(function() {
                    var gridSize = getGridSize();
                    $('.super-slider').each(function() {
						$(this).data('flexslider').vars.minItems = gridSize;	
					});
					$('.super-slider').each(function() {
						$(this).data('flexslider').vars.maxItems = gridSize;	
					});
                 
                });

            });
        }
        //wide slider	


        $(window).load(function() {
            $('.wide-slider').each(function() {


                var widget_id = $(this).closest('.widget').attr('id');
                var widget_id_p = '#' + widget_id;
                var widget_id_x = '.' + widget_id;
                $(widget_id_p).find('.wide-slider-control li').addClass(widget_id);

                $(this).flexslider({
                    animation: slide_picker,
                    slideshowSpeed: 8000,
                    manualControls: $(widget_id_x),
                    controlNav: true,
                    directionNav: true,
                    pauseOnHover: true,
                    start: function(slider) {
                        $('.slider-container').fadeTo("fast", 1);
                        $('.wide-slider-control').fadeTo("fast", 1);
                    }
                });
            });
        });

        //Gallery slider
        $(window).load(function() {
            $('.post-page-gallery-thumbnails').flexslider({
                animation: 'slide',
                controlNav: false,
                animationLoop: false,
                slideshow: false,
                itemWidth: 166,
                itemMargin: 0,
                minItems: 4,
                maxItems: 4,
                move: 4,
                directionNav: true,
                asNavFor: '.post-page-gallery-slider',
                start: function(slider) {
                    slider.fadeTo(1000, 1);
                    slider.removeClass('loading');
                }
            });

            $('.post-page-gallery-slider').flexslider({
                animation: slide_picker,
                controlNav: false,
                animationLoop: false,
                slideshow: false,
                sync: '.post-page-gallery-thumbnails',
                start: function(slider) {
                    slider.fadeTo(1000, 1);
                    slider.removeClass('loading');
                }

            });
        });

        //super-menu scripts

        $(".menu-link, .menu-item-has-children").mouseenter(function() {
            $(".menu-item").removeClass("active");
            if ($(this).hasClass("menu-item-has-children")) {
                $(this).addClass("active");
            } else {
                $(this).parent().addClass("active");
            }
        });

    });