// Some simple functions for the homepage

$(document).ready(function($){
	// Load banner slides and background fades
	homepageSlides();
	// YouTube overlay
	mediaOverlay();	

	// Attach video click events
	$(".watch-video-slide1").click(function(){
		loadVideo('http://www.youtube.com/v/wjTx4-KJoo8?version=3&f=user_uploads&app=youtube_gdata', true, 'We would like to reintroduce you to Encana.com','Take a closer look at our completely refreshed website and the enhanced features that will make it easier for you to navigate, engage and explore for whatever you are looking for. It is all for you!');

	});
		// Attach video click events
	$(".watch-video-slide2").click(function(){
		loadVideo('http://www.youtube.com/v/2qg8mrMqg9w?version=3&f=user_uploads&app=youtube_gdata', true, 'Take a closer look: Water protection during hydraulic fracturing','Take a closer look at how we responsibly use water in our natural gas hydraulic fracturing operations.');

	});		
	window.setTimeout(getBGs,10);
	window.setTimeout(getTweets,100);

    // Put background image in place if window is resized
	window.onresize = function(){
		checkBgWidth("1400");	
	}
});

function getBGs() {
	// Preload backgrounds
    var arrImages = ["/images/bg-clean-water.jpg","/images/bg-communities.jpg"];
    for (i in arrImages) {
        preloadImages(arrImages[i]);
    }
}

// Loads the Twitter feed. Requires jQuery Tweet plugin.
function getTweets() {
	$(".connect .tweet").tweet({
		username: "encanacorp",
		count: 1,
		fetch: 20,
		loading_text: "Loading tweets...",
		template: "{text}{time}",
		filter: function(t){ return ! /^@\w+/.test(t["tweet_raw_text"]); }			 
	}).bind("loaded",function(){$(this).find("a").attr("target","_blank");});
}

// Initialises the slides on the homepage, and handles the animation. This
// requires the Slides plugin
function homepageSlides() {
    checkBgWidth("1400");
	$("#slides").slides({
		animationStart: function() {},
		animationComplete: function() {			
			var thisIndex = $("#slides ul.slides_pagination li.current a").attr("id");

			if(thisIndex=='slide-page-1') $("#bg-image-1").stop().fadeIn(500, function() {
				$("#bg-image-2").fadeOut("fast");
				$("#bg-image-3").fadeOut("fast");
			});
			if(thisIndex=='slide-page-2') $("#bg-image-2").stop().fadeIn(500, function() {
				$("#bg-image-1").fadeOut("fast");
				$("#bg-image-3").fadeOut("fast");
			});
			if(thisIndex=='slide-page-3') $("#bg-image-3").stop().fadeIn(500, function() {
				$("#bg-image-1").fadeOut("fast");
				$("#bg-image-2").fadeOut("fast");
			});	
		},
		effect: 'slide', 
		generatePagination: false,
		generateNextPrev: true,
		hoverPause: true,
		paginationClass: 'slides_pagination',
		pause: 1000,
		play: 7000,
		slideSpeed: 450	
	});	
}

function checkBgWidth(imgWidth) {
   var windowWidth = $(window).width();
   if(windowWidth < imgWidth)
   {
       if (windowWidth <= 960) $(".bg-image").css({"background-position":"left top","width":"960px"});
       $("#bg-image-1").css("background-position","left top");
       $("#bg-image-2").css("background-position","left top");
       $("#bg-image-3").css("background-position","left top");
   }
   else
   {
       $(".bg-image").css({"background-position":"center top","width":"100%"});
       $("#bg-image-1").css("background-position","center top");
       $("#bg-image-2").css("background-position","center top");
       $("#bg-image-3").css("background-position","center top");        
   }    
}
