/* ----------------------------------------
 * -- NAMESPACES --------------------------
 * ---------------------------------------- */
nf.hp = nf.hp || {};	// Home Panel Namespace
	
/* ----------------------------------------
 * -- HOME PANEL --------------------------
 * ---------------------------------------- */
nf.hp.init = function(){
	
	nf.hp.carousel.init();
	nf.hp.touts.init();
	
	/**
	Disabling tout swf embed
	nf.hp.toutswf.init();
	*/
	
	//nf.hp.nikeplus.init();
	
}
/* ----------------------------------------
 * -- TRACKING ----------------------------
 * ---------------------------------------- */
nf.hp.tracking = {
	
	tmp: null,
	
	init: function(){
	
		// Set up tracking for carousel and mini PDPs CTA and initial view
		// tracker_obj.pageName taken from global variable set for dalewarth tracking script
		$.each($("div.slide .slide-content"), function(index, item){
			
			$(item).find("div.btn-region a").click(function(){
			
				stringToTrack = tracker_obj.pageName + ">P1>carousel>FP" + (index+1) + ">" + $(this).attr("data-tracking");
				
				s.eVar13 = "Homepage carousel>" + (index+1);
				s.prop3 = "Homepage carousel>" + (index+1);
				s.s_objectID = stringToTrack;
				
				// Call tracking object
				nf.fw.tracking.click(stringToTrack);
				
				// If link doesn't go anywhere abort
				if($(this).attr("href") == "#"){ return false; }
			
			});
			
			$(item).find("span.miniPDPmarker img").click(function(){
			
				stringToTrack = tracker_obj.pageName + ">P1>carousel>FP" + (index+1) + ">pdp>" + $(this).attr("data-tracking");
				
				s.eVar13 = "Homepage carousel>" + (index+1);
				s.prop3 = "Homepage carousel>" + (index+1);
				s.s_objectID = stringToTrack;
				
				// Call tracking object
				nf.fw.tracking.click(stringToTrack);
				
				// If link doesn't go anywhere abort
				if($(this).attr("href") == "#"){ return false; }
			
			});
			
			// Track which carousel items are to be displayed on load ?
		
		});
		
		// Set up tracking for carousel navigation
		// tracker_obj.pageName taken from global variable set for dalewarth tracking script
		$.each($("div#carouselnav li"), function(index, item){
		
			$(item).find("a").click(function(){
			
				stringToTrack = tracker_obj.pageName + ">P1>carousel>FP" + (index+1);
				
				s.eVar13 = "Carousel navigation";
				s.prop3 = "Carousel navigation";
				s.s_objectID = stringToTrack;
				
				// Call tracking object
				nf.fw.tracking.click(stringToTrack);
				
				// If link doesn't go anywhere abort
				if($(this).attr("href") == "#"){ return false; }
				
			});
		
		});
		
		// Set up tracking for marketing and video touts CTA and initial view
		// tracker_obj.pageName taken from global variable set for dalewarth tracking script
		$.each($("div.touts .tout"), function(index, item){
			
			$(item).find("div.btn-region a").click(function(evt){
				evt.stopImmediatePropagation();
				
				stringToTrack = tracker_obj.pageName + ">P" + (index+2) + ">tout>" + $(this).attr("data-tracking");
				
				s.eVar13 = "Homepage tout>P" + (index+1);
				s.prop3 = "Homepage tout>P" + (index+1);
				s.s_objectID = stringToTrack;
				
				// Call tracking object
				nf.fw.tracking.click(stringToTrack);
				
				// If link doesn't go anywhere abort
				if($(this).attr("href") == "#"){ return false; }
			
			});
			
			// Track which touts are to be displayed on load ?
		
		});
		
		// Set up tracking for news feed
		// tracker_obj.pageName taken from global variable set for dalewarth tracking script
		$("div.follow-links dd a").click(function(){
		
			stringToTrack = tracker_obj.pageName + ">tout>news_feed" + $(this).attr("data-tracking");
			
			s.eVar13 = "Homepage tout>News feed";
			s.prop3 = "Homepage tout>News feed";
			s.s_objectID = stringToTrack;
			
			// Call tracking object
			nf.fw.tracking.click(stringToTrack);
			
			// If link doesn't go anywhere abort
			if($(this).attr("href") == "#"){ return false; }
			
			// Track whether news tout is to be displayed on load ?
			// Track individual news feed items
		
		});
		
		
		if(debugMode){
			console.log("-- MSG -- Tracking added to carousel & touts");
		}
	}
	
}
/* ----------------------------------------
 * -- CAROUSEL FUNCTIONALITY --------------
 * ---------------------------------------- */
nf.hp.carousel = {
	init: function(){
		// DOM MANIPULATION LARGE BG IMGS
		// create div to hold copied slides (will enable view of full width bg images)
		$('body').prepend($('<div id="moved-slides"/>'));

		$('#hero-section .slide').each(function(i, slide){
			// clone slide and remove slide-content before sticking the copy in to moved-slides location
			$(slide).clone().empty().appendTo('#moved-slides');
			$(slide).css('background-image','none');
		});
		
		// CAROUSELS
		// instantiate the 2 carousels: background images and content if there is carousel content in the page
		if($('#hero-section').length > 0){
			
			$('#hero-section').crossFadeCarousel({
				slides:$('#hero-section .slide'), 
				navlinks:true, 
				navigation: {
					injectlocation: 'prepend',
					pausethenreplay: false
				},
				fadebody: {
					set: true,
					cssInfo: nf.gv.fadeBodyColors
				} 
			});
				
			$('#moved-slides').crossFadeCarousel({
				slides:$('#moved-slides .slide'), 
				navlinks:true, 
				navigation: {
					id: 'carouselnav2',
					injectlocation: 'prepend',
					pausethenreplay: false
				}
			});
		
			// hide the background carousel's navigation
			$('#carouselnav2').hide();
			
			// pass the click event through to the equivalent hidden nav link
			$hiddennavlinks = $('#carouselnav2 a');
			$('#carouselnav a').each(function(i, a){
				$(a).click(function(){
					$($hiddennavlinks[i]).click();
					// close any open pdps
					$(".miniPDP").remove();
				});
			});
			
		}
	}
}
/* ----------------------------------------
 * -- TOUT FUNCTIONALITY ------------------
 * ---------------------------------------- */
nf.hp.touts = {
	init: function(){
		// Tout effects (whole tout clickable and shimmer effect)
		$.each($("div.touteffect"), function(index, item){
			var links = $(item).find('h3 a, p a');
			
			var $fader = $('<div class="fader"/>');
			$fader.hide().appendTo($(item).find('div.img'));
			
			links.click(function(event){
				event.stopPropagation();
				(this.target != '') ? window.open(this.href) : window.location.href = this.href;
				return false;
			});
			$(item)
				.css("cursor", "pointer")
				.mouseenter(function(){
					$fader.fadeTo(100, 0.3).fadeTo(200, 0);
				})
				.click(function(){
					$(links[0]).click();
					if($(links[0]).attr("href").indexOf("script") != -1){ eval($(links[0]).attr("href")) }
				});
		});
	}
}
/* ----------------------------------------
 * -- TOUT SWF FUNCTIONALITY --------------
 * ---------------------------------------- */
 /**
 nf.hp.toutswf = {
	init: function(){
		//If a div with class 'swfcontent' exists, embed swf referenced in id attribute
		$.each($("div.swfcontent"), function(index, item) {
			var siteName = window.location.protocol + '//' + window.location.host;
			var targetElementID = $(item).attr('id'); // cache swf src
			$(item).attr('id', 'targetSWF'); // change to shortened id for swfObject
			
			var swfSRC = siteName + '/nikefootball/content' + targetElementID,
				toutWidth = '453',
				toutHeight = '405',
				flashVars = {
					'stageIndex' : 's',
					'debug' : 'true',
					'locale' : nf.gv.locale,
					'dataPath' : siteName + '/nikefootball/assets/homepage/swf/lifebuildingticker/data'
				},
				flashParams = {
					'allowScriptAccess' : 'true',
					'scaleMode' : 'noScale',
					'BGCOLOR' : '#000000'
				},
				flashAttributes = false;
			
			swfobject.embedSWF(swfSRC, 'targetSWF', toutWidth, toutHeight, '10', false, flashVars, flashParams, flashAttributes);
		});
	}
 }
 */
/* ----------------------------------------
 * -- FEED FUNCTIONALITY ------------------
 * ---------------------------------------- */
nf.hp.feeds = {
	
	twitterPosition: null,
	twitterFeedLoaded: false,
	twitterFeedContent: "",
	feedsLoaded: false,
	feedsContent: "",
	cufonLocales: "de_DE|en__EMEA|en_AU|en_GB|en_ID|en_IN|en_MY|en_NZ|en_SG|en_US|es__LATM|es_AR|es_ES|es_MX|fr_FR|it_IT|pt_BR|pt_PT",

	init: function(){
	
		$.each($("#newsfeed li"), function(index, item){
		
			var url = $(item).attr("data-url");
			
			if(url.indexOf("twitter") !== -1){
				nf.hp.feeds.twitterPosition = index;
			}
		
		});
		
		nf.hp.feeds.initialLoad();
		
		// if feed url contains twitter set up repeating call to updateFeed
		if(this.twitterPosition != null){
			
			var intervalKey = window.setInterval(function(){
				nf.hp.feeds.loadTwitterFeed(true);
			}, 60000);
			
		}
	
	},
	
	initialLoad: function(){
		var self = this;
		
		if(this.twitterPosition != null){
			this.twitterFeed = this.loadTwitterFeed(false);
		} else {
			nf.hp.feeds.twitterFeedLoaded = true;
		}
		this.feeds = this.loadFeeds();
		
		var intervalKey = window.setInterval(function(){
			if((nf.hp.feeds.feedsLoaded) && (nf.hp.feeds.twitterFeedLoaded)){
				window.clearInterval(intervalKey);
				nf.hp.feeds.displayFeed();
			}
		}, 3000);
		
	},
	
	loadFeeds: function(){
		var self = this;
		
		$.get("/nikefootball/home/socialfeeds?locale=" + nf.gv.locale, function(data) {
			//console.log(data);
			nf.hp.feeds.feedsContent = data;
			nf.hp.feeds.feedsLoaded = true;
			
		});
		
	},
	
	loadTwitterFeed: function(isRefresh){
		var self = this;

		$.get("/nikefootball/home/twitterfeed?locale=" + nf.gv.locale, function(data) {
			//console.log(data);
			nf.hp.feeds.twitterFeedContent = data;
			nf.hp.feeds.twitterFeedLoaded = true;
			if(isRefresh){
				nf.hp.feeds.updateTwitterFeed();
			}
		});
		
	},
	
	displayFeed: function(){
		
		$.each($("#newsfeed li"), function(index, item){
			$(item).fadeOut("slow", function(){
				$(item).remove();
			});
		});
		
		$("#newsfeed ul").html(nf.hp.feeds.feedsContent + nf.hp.feeds.twitterFeedContent).hide();
		if(nf.hp.feeds.cufonLocales.indexOf(nf.gv.locale) != -1){
			nf.fw.cufon.refresh();
		}
		$("#newsfeed ul").fadeIn("slow");
		
	},
	
	updateTwitterFeed: function(){
	
		$("#newsfeed ul li:last").fadeOut("slow", function(item){
			$(item).replaceWith(nf.hp.feeds.twitterFeedContent);
			if(nf.hp.feeds.cufonLocales.indexOf(nf.gv.locale) != -1){
				nf.fw.cufon.refresh();
			}
			$("#newsfeed ul li:last").fadeIn("slow");
		});
	
	}
}
/* ----------------------------------------
 * -- NF+ FUNCTIONALITY -------------------
 * ---------------------------------------- */
nf.hp.nikeplus = {
	
	userInteraction: false,
	submitForm: false,
	inputActive: false,
	intervalKey: null,
	
	init: function(){
	
		$("#nikefootballplusform input[type=text]").focus(function(evt){
			//console.log(evt.currentTarget);
			$(evt.currentTarget).addClass("isactive");
			nf.hp.nikeplus.inputActive = true;
		});
		
		$("#nikefootballplusform input[type=text]").blur(function(evt){
			//console.log(evt.currentTarget);
			if($(evt.currentTarget).val() == ""){
				$(evt.currentTarget).removeClass("isactive");
				nf.hp.nikeplus.inputActive = false;
			}
		});
		
		$("#nikefootballplusform").submit(function(evt){
			var subValue = $("#nikefootballplusform").find("input[type=text]").attr("value");
			
			if((subValue == "") || (subValue.length < 12)){
				nf.hp.nikeplus.hasError();
				return false;
			} else {
				if(nf.hp.nikeplus.submitForm){
					//console.log($(evt.currentTarget).attr("action"));
					s.eVar13 = "Homepage Nikefootballplus";
					s.prop3 = "Homepage Nikefootballplus";
					// Call tracking object
					nf.fw.tracking.view(tracker_obj.pageName + ">nikefootballplus>submit_code");
					return true;
				} else {
					nf.hp.nikeplus.checkCode($("#elite_code").val());
					return false;
				}
			}
		});
		
		$("#p_commerce .nikeplusinner").mouseenter(function(evt){
			nf.hp.nikeplus.openExtra();
		});
		
		$("#p_commerce .nikeplusinner").mouseleave(function(evt){
			if(!$("#nikefootballplusform input[type=text]").hasClass("isactive")){
				nf.hp.nikeplus.closeExtra();
			}
		});
		
		$("#nikefootballplusform input[type=text]").trigger("blur");
		$("#nikeplusextra").hide();
	},
	
	openExtra: function(){
		$("#nikeplusextra").show();
	},
	
	closeExtra: function(){
		$("#nikeplusextra").hide();
	},
	
	hasError: function(){
		$("#nikefootballplusform .error-label").show();
		$("#nikefootballplusform .error-label").fadeOut(3000);
	},
	
	checkCode: function(code){
		
		nf.fw.services.checkEliteCode({
			code: code,
			callback: function(json) {
				//var data = '(' + json.data.responseText.replace(/\\n|\\r/g, '') + ')';
				//data = eval(data);
				if(json.data.eliteCodeValidity.toUpperCase() != "VALID"){
					nf.hp.nikeplus.hasError();
					s.eVar13 = "Homepage Nikefootballplus";
					s.prop3 = "Homepage Nikefootballplus";
					// Call tracking object
					nf.fw.tracking.view(tracker_obj.pageName + ">nikefootballplus>invalid_code");
				} else {
					nf.hp.nikeplus.submitForm = true;
					$("#nikefootballplusform").attr("action", $("#nikefootballplusform").attr("action").replace(/#CODE#/, code))
					$("#nikefootballplusform").trigger("submit");
				}
				
			}
		});
	}
}

/* ----------------------------------------
 * -- DOM READY ---------------------------
 * ---------------------------------------- */
$(function() {

	// Initialise the home panel
	nf.hp.init();

	// CUFON
	nf.fw.cufon.modHeader = "#newsfeed h2 a, .nikeplusinner form, #nikeplusextra";
	//nf.fw.cufon.modHeaderLinks = "";
	nf.fw.cufon.modBody = "";
	nf.fw.cufon.modBrand = "";
	nf.fw.cufon.modButtons = ".btns a";
	
	$(nf.fw.cufon.modHeader).addClass("cfHeader");
	//$(nf.fw.cufon.modHeaderLinks).addClass("cfHeaderLinks");
	$(nf.fw.cufon.modBody).addClass("cfBody");
	$(nf.fw.cufon.modBrand).addClass("cfBrand");
	$(nf.fw.cufon.modButtons).addClass("cfButtons");
	
	nf.fw.cufon.replace("header", "cfHeader");
	//nf.fw.cufon.replace("header", "cfHeaderLinks", {hover:true});
	nf.fw.cufon.replace("body", "cfBody");
	nf.fw.cufon.replace("brand", "cfBrand");
	nf.fw.cufon.replace("buttons", "cfButtons");
	
	
});

/* ----------------------------------------
 * -- PAGE LOAD ---------------------------
 * ---------------------------------------- */
$(window).load(function() {

	/* ----------------------------------------
	 * -- MODULE TRACKING ---------------------
	 * ---------------------------------------- */
	nf.hp.tracking.init();
	
	/* ----------------------------------------
	 * -- ADD MINI PDPS -----------------------
	 * ---------------------------------------- */
	if(nf.gv.commerceEnabled) nf.fw.minipdp.init($("div#hero-section div.btn-region"));
	
	/* ----------------------------------------
	 * -- LOAD ACTUAL FEEDS -------------------
	 * ---------------------------------------- */
	if(($("#newsfeed").length > 0)){
		nf.hp.feeds.init();
	}
	
});
