var load_more_click_count = 0;

$(document).ready(function() {
	'use strict';
	
	/* script to hack CSS for safari ("Safari" is part of Chrome's userAgent) */
	var has_version	= navigator.userAgent.indexOf("Version/") > -1;
	var has_safari	= navigator.userAgent.indexOf("Safari") > -1;
	if (has_version && has_safari) {
		/* reposition nav copy */
		//alert('safari');
		$('.navbar.navbar-default ul.nav li a span.copy').css('top', '-10px');
	}
	/**/
	if(( $('#swf-container').length > 0 || $('.blog-main.watch').length > 0 ) && $('iframe').length > 0) {
		$('iframe').each(function() {
			$(this).ready(function() {checkForBorderFix();});
		});
	}
	
	$('#mobile-nav-back-button').click(function(e){
		e.preventDefault();
		$('button.navbar-toggle').click();
	});
	
	var resizeFrontpageFeature = new Imager('.delayed-fpf-load', {
		availableWidths: [188, 243, 293, 375, 485, 585],
	});

	var resizeNewsFeed = new Imager('.delayed-image-load', {
		availableWidths: [220, 263, 848],
		onImagesReplaced: function() {
			$('.news-feed').imagesLoaded(function() {
				$('.news-feed').isotope({
					itemSelector: '.post',
					masonry: {
						columnWidth: '.post'
					}
				});
			});
		}
    });

	$('.carousel').carousel({ 
		interval: 9000
  	});

	// http://www.janinedalton.com/blog/archives/2012/infinite-scrolling-with-manual-trigger-in-expressionengine/
	$('.news-feed').infinitescroll({
		behavior: 'twitter',
		extractLink: true,
		navSelector: '.navigation',    // selector for the paged navigation
		nextSelector: '.navigation a.next',  // selector for the NEXT link (to page 2)
		itemSelector: '.post',     // selector for all items you'll retrieve
		loading: {
			finishedMsg: '',
			msgText: 'Loading...',
			img: '/kids/content/images/loading.gif'
		}
	},
		// call Isotope as a callback
		function( newElements ) {
			var $newElems = $( newElements ).css({ opacity: 0 });
			
			load_more_click_count++;
			ga('send', 'event', 'Load more stuff', 'click', 'click count: '+ load_more_click_count);
			
			var resizeNewsFeedScroll = new Imager('.delayed-image-load', {
				availableWidths: [220, 263, 848],
				onImagesReplaced: function() {
					$newElems.imagesLoaded(function() {
						$newElems.animate({ opacity: 1 });
						$('.news-feed').isotope( 'appended', $( newElements ) );

						if($('.news-feed .end').length) {
							$('.navigation a.next').remove();
						}
					});
				}
			});
		}
	);

	$('.scoreboard').click(function(e) { 
		e.preventDefault();
		$('.gamescores').load($(this).attr('href'), checkForBorderFix);
	});

	$('.gamescores').load($('.scoreboard-default').attr('href'), checkForBorderFix);
	
	initNowLeaving();
});

function initNowLeaving() {
	preloadLeavingAssets();
	var home = window.location.hostname + "/kidscbc2";
	
	$('a').each(function(){
		if(($(this).attr('href').indexOf(home) == -1 && $(this).attr('href').charAt(0) != '/')) {
			if($(this).hasClass('carousel-control')) {return;}
			$(this).on('click', function(e){
				e.preventDefault();
				// alert('external link');
				
				$.fancybox.open(' \
					<div class="leaving_box"> \
						<img class="" width="100%" alt="You are now leaving Kids\' CBC" src="'+ preloaded_leaving_images[0].src +'" /> \
						<div class="buttons"> \
							<a href="#" class="cancel">CANCEL</a> \
							<a href="'+ $(this).attr("href") +'" target="_blank" class="okay">OKAY</a> \
						</div> \
					</div>'
				);
				
				
				
				$('.leaving_box .buttons .cancel').click(function(e){
					e.preventDefault();
					forceLightboxClose();
				});
				
			});
		}
	});
	
}

var preloaded_leaving_images = new Array()
function preloadLeavingAssets() {
	var preload = ["http://cbc.ca/kidscbc2/content/images/now-leaving.png"]
	for (i = 0; i < preload.length; i++) {
		preloaded_leaving_images[i] = new Image()
		preloaded_leaving_images[i].src = preload[i]
	}
}

function forceLightboxClose() {
	// console.log('close');
	$.fancybox.close();
}

function checkForBorderFix() {
	console.log('checkForBorderFix()');
	if($('.blog-main').length > 0){
		if($('.blog-main').height() < $('.blog-main').next().height()) {
		   $('.blog-main').attr('style', 'height:'+$('.blog-main').next().height()+'px');
		}
	}
	
	if($('#swf-container').length > 0) {
		if($('#swf-container').height() < $('#swf-container').next().height()) {
		   $('#swf-container').attr('style', 'height:'+$('#swf-container').next().height()+'px');
		}
	}
	
	return false;
}
