if(!NikeFootball) {
	var NikeFootball = {};
}

//fix homepage specific issues inherited from fragments/tracking.html
//fix an extra trailing > inside the prefix, if it's there; 
if (nav_tracker_obj.prefix.indexOf('>')==nav_tracker_obj.prefix.length-1){
	nav_tracker_obj.prefix = track_site;
}


NikeFootball.Homepage = function() {

	_initTopLockFlash = function() {
		var flashVars = {
			id: "topLockNav",
			stylePath: "/nikefootball/global/xml/style.xml",
			fontPath: "/nikeos/global/modules/nav/v1/font/fontlibrary.swf",
			configPath: "/nikefootball/" + GLOBAL_VARIABLES.localeDir + "/xml/site.xml",
			siteHost: GLOBAL_VARIABLES.siteHost,
			lang_locale: GLOBAL_VARIABLES.locale,
			current_url: escape(location.href),
			trackerObject: tracking.flash(nav_tracker_obj)
		};
		
		var flashParams = {
			quality: "best",
			wmode: "transparent",
			scale: "noscale",
			menu: 'false',
			allowScriptAccess: "always",
			salign: "tl",
			name: "topLockNav"
		};

		var flashAttributes = {};
		swfobject.embedSWF("/nikeos/global/modules/nav/v1/swf/nav-module-top-1-2.swf", "topLockNav", "300", "65", "9", "", flashVars, flashParams, flashAttributes);	     
	};
	
	_initLeftNavFlash = function() {
		var flashVars = {
			id: "nav",
			trackerObject: tracking.flash(nav_tracker_obj),
			stylePath: "/nikefootball/global/xml/style.xml",
			fontPath: "/nikeos/global/modules/nav/v1/font/fontlibrary.swf",
			configPath: GLOBAL_VARIABLES.assetBasePath + GLOBAL_VARIABLES.localeDir + '/xml/nav.xml',
    		navXML: innerXHTML('nav', true),
            currentUrl: escape(document.location.href),
			currentNav: "index"
		};
		var flashParams = {
			quality: "best",
			wmode: "transparent",
			scale: "noscale",
			salign: "tl",
			menu: 'false',
			allowScriptAccess: "always",
			name: "nav"
		};
		var flashAttributes = {};
		//swfobject.embedSWF("/nikeos/global/modules/nav/v1/swf/nav-module-menu-1-2.swf", "nav", "102", "616", "9", "", flashVars, flashParams, flashAttributes);
	}
	
	_initFeaturesNonFlash = function() {
		var featuresHTML = '';
		var rssParams = {
			path: '',
			toutHeader: ''
		}
		JQ.get(GLOBAL_VARIABLES.features_configPath, function (xml) {
			JQ(xml).find('rssPath').each(function() {
				var rssPath = JQ(this).text();
				rssPath = rssPath.replace('{lang_locale}', GLOBAL_VARIABLES.locale);
				rssParams.path = rssPath;
			});
			if (rssParams.path != '') {
				JQ.get(rssParams.path, function(data) {
					JQ('item', data).each(function() {
						itemTitle = JQ(this).find('title').text();
						itemLink = JQ(this).find('link')[0].nextSibling.nodeValue;
						itemDescription = JQ(this).find('description').text();
						itemImage = JQ(this).find('media\\:content').attr('url');
						if (itemTitle.length >= 24) {
							itemTitle = itemTitle.substring(0,24) + '...';
						}
						if (itemDescription.length >= 46) {
							itemDescription = itemDescription.substring(0,46) + '...';
						}
						featuresHTML += '<li><div class="featureImageWrapper"><img alt="'+itemTitle+'" src="'+itemImage+'" /></div><div class="featureDescription"><a href="'+itemLink+'">'+itemTitle+'</a>'+itemDescription+'</div></li>';
					});
					JQ('#featuresFeed').html(featuresHTML);
					if (JQ('item', data).length > 1) {
						JQ('#featuresTout').prepend('<div class="carouselCtrl"><a href="#" class="newsControl ctrlPrev" id="featurePrev">prev</a><a href="#" class="newsControl ctrlNext" id="featureNext">next</a></div>');
						JQ('#featuresFeed').cycle({
							fx: 'scrollLeft', 
							timeout: 6000, 
							speed: 600, 
							pause: true, 
							prev: '#featurePrev', 
							next: '#featureNext'
						});
					}
				});
			}
		});
	};
		
	_initIdNonFlash = function() {
		JQ('.carouselTout').cycle({
			fx: 'scrollLeft', 
			timeout: 7000, 
			speed: 700, 
			pause: true, 
			prev: '#carouselPrev', 
			next: '#carouselNext'
		});
	};
		
	_initHeroFlash = function() {
		JQ('#heroSwitch').append('<ul><li><a href="#" id="1" class="carouselPageNumber">1</a></li><li><a href="#" id="2" class="carouselPageNumber">2</a></li><li><a href="#" id="3" class="carouselPageNumber">3</a></li><li><a href="#" id="4" class="carouselPageNumber">4</a></li><li><a href="#" id="5" class="carouselPageNumber">5</a></li><li><a href="#" id="6" class="carouselPageNumber">6</a></li><li><a href="#" id="7" class="carouselPageNumber">7</a></li><li><a href="#" id="8" class="carouselPageNumber">8</a></li></ul>').css("display","block");

		var flashVars = {
			locale: GLOBAL_VARIABLES.locale,
			deepLink: GLOBAL_VARIABLES.deepLink
		};
		
		var flashParams = {
			wmode: "transparent",
			scale: "noscale",
			quality: "best",
			allowDomain: "always",
			allowScriptAccess: "always",
			wmode: "transparent",
			BASE: '/nikefootball/global/swf/modules/homepage_carousel/'
		};
		var flashAttributes = {};
		swfobject.embedSWF("/nikefootball/global/swf/modules/homepage_carousel/HomePageCarousel.swf", "heroFlash", "910", "1018", "9", "", flashVars, flashParams, flashAttributes);
	};

	_initHeroFade = function () {
		JQ('#heroSwitch').css({'display': 'block'});
		JQ('#heroFlash').cycle({
			fx: 'fade',
			sync: 0,
			timeout: 7000,
			speedIn: 3000,
			speedOut: 600,
			pager: '#heroSwitch'
		});
	};
	
	_initFeaturesFlash = function() {
		if (GLOBAL_VARIABLES.flash_tout_left == '') {
			return;
		} else {
/*			
			//fix homepage specific issues inherited from fragments/tracking.html
			nav_tracker_obj.persistent.pageName = track_site + '>home>features>article';
			nav_tracker_obj.persistent.prop3 = track_site + '>home>features>article';
*/
		var flashVars = {
				platypus_region: GLOBAL_VARIABLES.locale,
				trackerObj: tracking.flash(nav_tracker_obj),
				commerce_mode: 'emea',
				configPath: GLOBAL_VARIABLES.features_configPath,
				lang_locale: GLOBAL_VARIABLES.locale
			};
			var flashParams = {
				wmode : "transparent",
				allowDomain: "always",
				allowScriptAccess: "always",
				scale: "noscale"
			};
			var flashAttributes = {};
			swfobject.embedSWF(GLOBAL_VARIABLES.flash_tout_left, "featuresTout", "280", "225", "8", "", flashVars, flashParams, flashAttributes);
			JQ('#leftNav').css('left','-102px');
		}
	};
	
	_initNikeIdFlash = function() {
		if (GLOBAL_VARIABLES.flash_tout_right == '') {
			return;
		} else {
		var encodedLinkSuffix = escape('');
			var flashVars = {
				platypus_region: 'emea',
				trackerObj: tracking.flash(nav_tracker_obj),
				commerce_mode: 'emea',
				configPath: GLOBAL_VARIABLES.nikeID_configPath,
				lang_locale: GLOBAL_VARIABLES.locale,
				link_suffix: encodedLinkSuffix
			};
			var flashParams = {
				wmode : "transparent",
				allowScriptAccess: "always",
				allowDomain: "always",
				scale: "noscale"
			};
			
			var flashAttributes = {};
			JQ('#idToutWrapper').empty().prepend('<div id="idTout"></div>');
			swfobject.embedSWF(GLOBAL_VARIABLES.flash_tout_right, "idTout", "285", "185", "9", "", flashVars, flashParams, flashAttributes);
			JQ('#idFlash').attr({'style': 'margin-left: -5px;'});
			JQ('#idToutWrapper').prepend('<h4>NIKEiD</h4>');
		}
	};
	
	_initBootroomTout = function() {
		if (useSiteFont == 1) {
			Cufon.set('fontFamily', 'Rhode-SemiboldCondensed');
		}
		else if (useSiteFont == 2) {
			Cufon.set('fontFamily', 'Arial');
		}
		Cufon.replace('span.homeToutTitle');
	}

	_initMenuAccordion = function () {
		JQ('.secondaryButton').slideUp();
		JQ('.primaryButton a').each(function () {
			if (JQ(this).next().hasClass('secondaryButton')) {
				JQ(this).addClass('folding');
			}
		});
		JQ(".primaryButton a").click(function() {
			if (JQ(this).hasClass('folding')) {
				if ((JQ(this).next()).css('display') == 'block') {
					if (!JQ(this).parent().find('.secondaryButton').is(':animated')) {
						JQ('.secondaryButton').slideUp();
					}
					return false;
				} else {
					if (!JQ(this).parent().find('.secondaryButton').is(':animated')) {
						JQ('.secondaryButton').slideUp();
						JQ(this).nextAll().slideToggle("slow");
					}
					return false;
				}
			} else {
				JQ('.secondaryButton').slideUp();
				JQ(this).nextAll().slideToggle("slow");
			}
		});
	};
	
	_initNewsScrollControls = function () {
		JQ('.subscribe').after('<a class="newsControl ctrlUp" href="#">up</a><a class="newsControl ctrlDown" href="#">down</a>');
	};
	
	_initNewsScroll = function () {
		JQ('.ctrlUp').fadeTo('slow', 0.33).addClass('btnDisabled');
	};
	
	_initNewsScrollClick = function (direction, e) {
		e.preventDefault();
		var newsFeedList = JQ('#newsTout').find('.feedList');
		var newsFeedListPos = newsFeedList.position().top;
		var newsFeedListHeight = newsFeedList.innerHeight();
		var newsFeedContainerHeight = 162;
		if (direction === 'up') {
			var newsFeedHeightLimit = newsFeedListHeight - (newsFeedListPos * -1);
			if (newsFeedHeightLimit > 162) {
				var newPosForNewsFeedList = ((newsFeedListPos - 162) - (newsFeedListPos % 162)) + 'px'; // make list snap to display 3 list items
				if (JQ('.ctrlUp').hasClass('btnDisabled')) {
					JQ('.ctrlUp').fadeTo('slow', 1).removeClass('btnDisabled')
				}
				if ((newsFeedListPos * -1) > (newsFeedListHeight - 325)) { // fade out button and add class to make it 'inactive'
					JQ('.ctrlDown').fadeTo('slow', 0.33).addClass('btnDisabled');
				}
			} else {
				var newPosForNewsFeedList = newsFeedListPos + 'px';
				if ((newsFeedListPos * -1) < (newsFeedListHeight)) {
					newPosForNewsFeedList == (newsFeedListHeight - 162) + 'px';
				}
			}
		} else if (direction === 'down') {
			var newsFeedHeightLimit = newsFeedListHeight - newsFeedListPos;
			if (newsFeedHeightLimit >= (newsFeedListPos * -1)) {
				if (newsFeedListPos < 0) {
					var newPosForNewsFeedList = ((newsFeedListPos + 162) - ((newsFeedListPos) % 162)) + 'px'; // snap as above
					if (JQ('.ctrlDown').hasClass('btnDisabled')) {
						JQ('.ctrlDown').fadeTo('slow', 1).removeClass('btnDisabled')
					}
					if ((newsFeedListPos * -1) <= 162) { // fade out button and add class to make it 'inactive'
						JQ('.ctrlUp').fadeTo('slow', 0.33).addClass('btnDisabled');
					}
				} else {
					var newPosForNewsFeedList = newsFeedListPos + 'px';
					if (newsFeedListPos > 0) {
						newPosForNewsFeedList == '0px';
					}
				}
			} else {
				newPosForNewsFeedList == '0px';
			}
		}
		newsFeedList.animate({
			top: newPosForNewsFeedList
		}, 450);
	};

	return {
		init : function() {
			var minimumFlashVersion = 9;
			if(swfobject.getFlashPlayerVersion().major >= minimumFlashVersion && location.search.indexOf('noFlash=true') == -1) {
				_initLeftNavFlash();
				_initTopLockFlash();
				_initFeaturesFlash();
				_initHeroFlash();
				_initNewsScrollControls();
				_initNewsScroll();
				//_initMenuAccordion();
				if (useFlashTout) {
					_initNikeIdFlash();
				}
				else {
					_initBootroomTout();
				}
			} else {
				_initHeroFade();
				_initFeaturesNonFlash();
				_initIdNonFlash();
			}
		}
	};
}

JQ(document).ready(function() {
	var homepage = new NikeFootball.Homepage();
	homepage.init();
	
	JQ('.ctrlUp').click(function (e) {
		if (!JQ(this).parent().find('.feedList').is(':animated')) {
			_initNewsScrollClick('down', e);
		}
		e.preventDefault();
	});
	JQ('.ctrlDown').click(function (e) {
		if (!JQ(this).parent().find('.feedList').is(':animated')) {
			_initNewsScrollClick('up', e);
		}
		e.preventDefault();
	});
	
	// set up click tracking
	
	JQ('#featuresTout .feedList a').click(function () { //this first one is done inside the flash so look out for the _initFeaturesFlash function and the trackerObj
		tracking.click({
			prop17: 'tout',
			prop18: 'home',
			pageName: track_site + '>home>features>article',
			prop3: track_site + '>home>features>article'
		});
	});
	JQ('#idTout a').click(function() {
		tracking.click({
			prop17: 'tout',
			prop18: 'home',
			pageName: track_site + '>home>P3>bootroom>article',
			prop3: track_site + '>home>P3>bootroom>article'
		});
	});
	JQ('#newsTout a').click(function() {
		tracking.click({
			prop17: 'tout',
			prop18: 'home',
			pageName: track_site + '>home>P4>newsfeed>article',
			prop3: track_site + '>home>P4>newsfeed>article'
		});
	});
	
	JQ('.carouselPageNumber').click(function(e) {
		viewPage(e.target.id);
		e.preventDefault();
	}); 
});

function setMenuArea(id, width, height) {
	JQ("#" + id).width(width+'px');
	JQ("#" + id).height(height+'px');
}

// These functions are required by the various flash elements within the homepage.

function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}
		 
function viewPage(value) {
	thisMovie("heroFlash").viewPage(value);
}

var currentPageNumber;
function pageChanged(pageNumber) {
	JQ('#' + pageNumber).toggleClass('activeSlide');
	JQ('#' + currentPageNumber).toggleClass('activeSlide');
	currentPageNumber = pageNumber;
 }

