if(!NikeFootball) {
	var NikeFootball = {};
}

NikeFootball.Homepage = function() {

	_initTopLockFlash = function() {
		
var deeplink = document.location.hash;
    if (deeplink) {
    		deeplink = deeplink.substr(1);
    } else {
    		deeplink = document.location.search;
    		if (deeplink) deeplink = deeplink.substr(6);
    }
        
    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),
      link: deeplink
		};
		
		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: "mercurial_superfly"
		};
		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);
		$('#nav').attr({'style': 'visibility: visible'});
	}
	
	_initLeftNavNoFlash = function() {
		$('#nav').attr({'style': 'visibility: visible'});
	};
	
	_initFeaturesNonFlash = function() {
		var featuresHTML = '';
		var rssParams = {
			path: '',
			toutHeader: ''
		}
		$.get(GLOBAL_VARIABLES.features_configPath, function (xml) {
			$(xml).find('rssPath').each(function() {
				var rssPath = $(this).text();
				rssPath = rssPath.replace('{lang_locale}', GLOBAL_VARIABLES.locale);
				rssParams.path = rssPath;
			});
			if (rssParams.path != '') {
				$.get(rssParams.path, function(data) {
					$('item', data).each(function() {
						itemTitle = $(this).find('title').text();
						itemLink = $(this).find('link')[0].nextSibling.nodeValue;
						itemDescription = $(this).find('description').text();
						itemImage = $(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>';
					});
					$('#featuresFeed').html(featuresHTML);
					if ($('item', data).length > 1) {
						$('#featuresTout').prepend('<div class="carouselCtrl"><a href="#" class="newsControl ctrlPrev" id="featurePrev">prev</a><a href="#" class="newsControl ctrlNext" id="featureNext">next</a></div>');
						$('#featuresFeed').cycle({
							fx: 'scrollLeft', 
							timeout: 6000, 
							speed: 600, 
							pause: true, 
							prev: '#featurePrev', 
							next: '#featureNext'
						});
					}
				});
			}
		});
	};
		
	_initIdNonFlash = function() {
		$('.carouselTout').cycle({
			fx: 'scrollLeft', 
			timeout: 7000, 
			speed: 700, 
			pause: true, 
			prev: '#carouselPrev', 
			next: '#carouselNext'
		});
		$('#idTout').attr({'style': 'visibility: visible'});
	};
		
	_initHeroFlash = function() {
	
    var deeplink = document.location.hash;
    if (deeplink) {
    		deeplink = deeplink.substr(1);
    } else {
    		deeplink = document.location.search;
    		if (deeplink) deeplink = deeplink.substr(6);
    }
        
		var flashVars = {
			locale: GLOBAL_VARIABLES.locale,
			deepLink: deeplink
		};
		
		var flashParams = {
			wmode: "transparent",
			scale: "noscale",
			quality: "best",
			allowDomain: "always",
			allowScriptAccess: "always",
			wmode: "transparent",
			BASE: '/nikefootball/global/swf/campaigns/mercurialsuperfly/home/'
		};
		var flashAttributes = {};
		swfobject.embedSWF("/nikefootball/global/swf/campaigns/mercurialsuperfly/home/SuperflyHomeApp.swf", "heroFlash", "908", "929", "9", "/nikefootball/global/swf/nike5/home/expressInstall.swf", flashVars, flashParams, flashAttributes);
		$('#heroFlash').attr({'style': 'visibility: visible'});
	};

	_initHeroFade = function () {
		$('#heroSwitch').css({'display': 'block'});
		$('#heroFlash').cycle({
			fx: 'fade',
			sync: 0,
			timeout: 7000,
			speedIn: 3000,
			speedOut: 600,
			pager: '#heroSwitch'
		});
		$('#heroFlash').attr({'style': 'visibility: visible'});
	};
	
	_initFeaturesFlash = function() {
		if (GLOBAL_VARIABLES.flash_tout_left == '') {
			return;
		} else {
			var flashVars = {
				platypus_region: nsw_data.platypus_region,
				trackerObj: tracking.flash(nav_tracker_obj),
				commerce_mode: nsw_data.commerce_mode,
				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);
			$('#leftNav').css('left','-102px');
		}
	};
	
	_initFeaturesNoFlash = function() {
		$('#featuresTout').attr({'style': 'visibility: visible'});
	};
	
	_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 = {};
			$('#idToutWrapper').empty().prepend('<div id="idTout"></div>');
			swfobject.embedSWF(GLOBAL_VARIABLES.flash_tout_right, "idTout", "285", "185", "9", "", flashVars, flashParams, flashAttributes);
			$('#idFlash').attr({'style': 'margin-left: -5px;'});
			$('#idToutWrapper').prepend('<h4>NIKEiD</h4>');
			$('#idTout').attr({'style': 'visibility: visible'});
		}
	};

	_initMenuAccordion = function () {
		$('.secondaryButton').slideUp();
		$('.primaryButton a').each(function () {
			if ($(this).next().hasClass('secondaryButton')) {
				$(this).addClass('folding');
			}
		});
		$(".primaryButton a").click(function() {
			if ($(this).hasClass('folding')) {
				if (($(this).next()).css('display') == 'block') {
					if (!$(this).parent().find('.secondaryButton').is(':animated')) {
						$('.secondaryButton').slideUp();
					}
					return false;
				} else {
					if (!$(this).parent().find('.secondaryButton').is(':animated')) {
						$('.secondaryButton').slideUp();
						$(this).nextAll().slideToggle("slow");
					}
					return false;
				}
			} else {
				$('.secondaryButton').slideUp();
				$(this).nextAll().slideToggle("slow");
			}
		});
	};
	
	_initNewsScrollControls = function () {
		$('.subscribe').after('<a class="newsControl ctrlUp" href="#">up</a><a class="newsControl ctrlDown" href="#">down</a>');
	};
	
	_initNewsScroll = function () {
		$('.ctrlUp').fadeTo('slow', 0.33).addClass('btnDisabled');
	};
	
	_initNewsScrollClick = function (direction, e) {
		e.preventDefault();
		var newsFeedList = $('#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 ($('.ctrlUp').hasClass('btnDisabled')) {
					$('.ctrlUp').fadeTo('slow', 1).removeClass('btnDisabled')
				}
				if ((newsFeedListPos * -1) > (newsFeedListHeight - 325)) { // fade out button and add class to make it 'inactive'
					$('.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 ($('.ctrlDown').hasClass('btnDisabled')) {
						$('.ctrlDown').fadeTo('slow', 1).removeClass('btnDisabled')
					}
					if ((newsFeedListPos * -1) <= 162) { // fade out button and add class to make it 'inactive'
						$('.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();
				_initNikeIdFlash();
			} else {
				_initHeroFade();
				_initFeaturesNoFlash();
				_initIdNonFlash();
				_initLeftNavNoFlash();
			}
		}
	};
}

$(document).ready(function() {
	var homepage = new NikeFootball.Homepage();
	homepage.init();
	
	$('.ctrlUp').click(function (e) {
		if (!$(this).parent().find('.feedList').is(':animated')) {
			_initNewsScrollClick('down', e);
		}
		e.preventDefault();
	});
	$('.ctrlDown').click(function (e) {
		if (!$(this).parent().find('.feedList').is(':animated')) {
			_initNewsScrollClick('up', e);
		}
		e.preventDefault();
	});
	
	// set up click tracking
	$('#featuresTout .feedList a').click(function () {
		tracking.click({
			prop17: 'tout',
			prop18: 'home',
			pageName: track_site + '>home>features>article'
		});
	});
	$('#idTout a').click(function() {
		tracking.click({
			prop17: 'tout',
			prop18: 'home',
			pageName: track_site + '>home>idTout>article'
		});
	});
	$('#newsTout a').click(function() {
		tracking.click({
			prop17: 'tout',
			prop18: 'home',
			pageName: track_site + '>home>newsfeed>article'
		});
	});
	
	$('.carouselPageNumber').click(function(e) {
		viewPage(e.target.id);
		e.preventDefault();
	}); 
});

function setMenuArea(id, width, height) {
	$("#" + id).width(width+'px');
	$("#" + 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) {
	$('#' + pageNumber).toggleClass('activeSlide');
	$('#' + currentPageNumber).toggleClass('activeSlide');
	currentPageNumber = pageNumber;
 }
