jQuery(document).ready(function($) {

	$("blockquote:not([class])").each(function( index ) {
		$(this).wrapAll('<div class="quote"></div>');
		$(this).wrapAll('<div class="row"><div class="quote-middle col-md-8"></div></div>');
		$(this).closest(".row").prepend('<div class="col-md-2"><div class="quote-left"></div></div>').append('<div class="col-md-2"><div class="quote-right"></div></div>');
	});

	$(document).mouseup(function (e){
		var container = $("#navigation");
		if (!container.is(e.target)
		  && container.has(e.target).length === 0)
		{
			container.hide();
		}
	});

	$(".feature-story").matchHeight();

	$(".toggle-menu").click(function(e) {
		e.preventDefault();
		$("#navigation").show();
	});

	$("#close-modal").click(function(e) {
		e.preventDefault();
		$("#modal-contents").hide();
	});

	$("#toggle-todays-news").click(function(e) {
		e.preventDefault();
		$("#most-popular").hide();
		$("#todays-news").show()
		$("#toggle-most-popular").removeClass('selected');
		$(this).addClass('selected');
	});

	$("#toggle-most-popular").click(function(e) {
		e.preventDefault();
		$("#todays-news").hide();
		$("#most-popular").show()
		$("#toggle-todays-news").removeClass('selected');
		$(this).addClass('selected');
	});

	$(".section").click(function(e) {
		e.preventDefault();
		$("#network").hide();
		$("#section").show()
		$(".network").removeClass('selected');
		$(this).addClass('selected');
	});

	$(".network").click(function(e) {
		e.preventDefault();
		$("#section").hide();
		$("#network").show();
		$(".section").removeClass('selected');
		$(this).addClass('selected');
	});

	$(".nav-close").click(function(e) {
		e.preventDefault();
		$("#navigation").hide();
		$("#network").hide();
		$("#section").show();
	});

	$(".show-phone").click(function(e) {
		e.preventDefault();
		$(this).next($(".hide-phone")).toggle();
	});

	$(".menu-item-has-children > a").append('<i class="fa fa-angle-down"></i>');

	$("#nav-switcher-area i").click(function(e){
		e.preventDefault();
	    $(this).parent().next('.sub-menu').toggle();      
	});

	var $affectedElements = $("p, .feature-story h4 a, .article h4, .article-category a, .home-category h4 a, .home-category h3, h1, .article-category a, body, #footer h4");
	// Storing the original size in a data attribute so size can be reset
	$affectedElements.each( function(){
	  var $this = $(this);
	  $this.data("orig-size", $this.css("font-size") );
	});

	$("#btn-increase").click(function(){
	  changeFontSize(1);
	})

	$("#btn-decrease").click(function(){
	  changeFontSize(-1);
	})

	$("#btn-orig").click(function(){
	  $affectedElements.each( function(){
	        var $this = $(this);
	        $this.css( "font-size" , $this.data("orig-size") );
	   });
	})
	
	$(window).scroll(function() {

		if($("#desktop_ad_4").length > 0) {
			
			var hT = $('#desktop_ad_4').offset().top,
				hH = $('#desktop_ad_4').outerHeight(),
				wH = $(window).height(),
				wW = $(window).width(),
				wS = $(this).scrollTop();
		
			if (wS > (hT+hH-wH) && wW > 1199) {
				$("#modal-contents").show();
				$(window).off('scroll');
			}
		}
	});	 
	/*
	$(window).on("load resize",function() {

		if($(window).width() < 1198) {

			$("#mobile_ad_1").html($("#dfp_medium_rectangle_ad_1").html());
			$("#mobile_ad_2").html($("#dfp_medium_rectangle_ad_2").html());
			$("#mobile_ad_3").html($("#dfp_medium_rectangle_ad_3").html());
			$("#mobile_ad_4").html($("#dfp_medium_rectangle_ad_4").html());
			$("#mobile_ad_5").html($("#dfp_medium_rectangle_ad_5").html());
			$("#mobile_ad_6").html($("#dfp_medium_rectangle_ad_6").html());
			$("#mobile_ad_7").html($("#dfp_medium_rectangle_ad_7").html());
			$("#mobile_ad_8").html($("#dfp_medium_rectangle_ad_8").html());
			$("#mobile_ad_grid").html($("#grid_pricing").html());


			$("#desktop_ad_1").html('');
			$("#desktop_ad_2").html('');
			$("#desktop_ad_3").html('');
			$("#desktop_ad_4").html('');
			$("#desktop_ad_5").html('');
			$("#desktop_ad_6").html('');
			$("#desktop_ad_7").html('');
			$("#desktop_ad_8").html('');
			$("#desktop_ad_grid").html('');
			
			googletag.pubads().refresh();
		} 

	});
	*/


	$(document).on('scroll', function() {

		$(".adwing-left").height($("#content-area").height());
		$(".adwing-right").height($("#content-area").height());

		$("#header").waypoint({
		  handler: function(){
		  	 $(".sticky_wing").css('top','320px');
			 $(".sticky_wing").removeClass('sticky_wing_absolute');  
		},
		  offset: '100%'
		});

		$("#content-area").waypoint({
		  handler: function(){
		  	 $(".sticky_wing").css('top','0px');
		},
		  offset: '100%'
		});
	
		$("#footer").waypoint({
		  handler: function() {
		  	$(".sticky_wing").css('top','auto');
		  	$(".sticky_wing").addClass('sticky_wing_absolute');  
		  },
		  offset: '100%'
		});

		$("#desktop_ad_6").waypoint({
			handler: function() {
				$(".sticky").removeClass('sticky_absolute');
			},
			offset: '100%'
		});

		$("#email-headlines, #email-headlines-property").waypoint({
			handler: function() {
				$(".sticky").addClass('sticky_absolute');
			},
			offset: '75%'
		});
		
	});

	function changeFontSize(direction){
	    $affectedElements.each( function(){
	        var $this = $(this);
	        $this.css("font-size", parseInt($this.css("font-size"))+direction );
	    });
	}
});


