/**
 * @author scottkosman
 */

var adidasPop = {	
		isLocaleData: function(data,page) { //Check for locale data portlets
			if(data.locale) {
				if(data.locale[page]) {
					return true;
				}
			}
			return false;
		},
		isLocalOverwrite: function(data,page,portlet) {	// Check for overwrite data
			// Have to check each parent separately or IE will explode. BOOOM!!
			if(data.locale) {
				if(data.locale[page]) {
					if(data.locale[page][portlet]) {
						return true;
					}
				}
			}
			return false;
		},
		create: {
			indexPortlet: function(item,target) {
				//Check for Local Data to Overwrite portlet. 						
				var w = item.width;
				switch(w) {
					case "0.25":
						var moduleWidth = "240px";
						break;
					case "0.3":
						var moduleWidth = "320px";
						break;
					case "0.5":
						var moduleWidth = "480px";
						break;
					case "1":
						var moduleWidth = "100%";
						break;
					default:
						var moduleWidth = "100%";
						break;
				}		
				
					

				if (item.type == "flash") {	

					var div = $("<div>").attr("id",item.title).attr("class","portletDiv").attr("style","width:"+moduleWidth+";height:"+item.height+";");
					
					div.html("<a href='"+item.noflashUrl+"'><img src='"+item.noflash+"' alt='"+item.noflashAlt+"' /></a>");
					
					div.appendTo(target);
			
					if (item.launcher == "self") {
						//var flashvars = {locale: locale, country: adidas_cc, configDirectory:"content/assets/xml/lang/"+locale+"/" };
						var flashvars = createFlashVars(item);
						var launcher = item.media;
					} else {
						//var flashvars = {application: item.media, locale: locale, country: adidas_cc, configDirectory:"content/assets/xml/lang/"+locale+"/"};
						var flashvars = createFlashVars(item);
						flashvars.application = item.media;
						var launcher = item.launcher;
					};
					
					var params = {allowFullScreen: "true", allowScriptAccess: "always", wmode:"transparent"};
					swfobject.embedSWF(launcher, item.title, moduleWidth, item.height, "9.0.0", false,flashvars,params);
					
					if (item.textContent) {
						$("<div>").addClass(item.textContent[0].type).append("<a href="+item.textContent[0].linkurl+">"+item.textContent[0].link+"</a>").insertAfter($("#"+item.title+""));
					};

				} else if (item.type == "img")  {

					var div = $("<div>").attr("id",item.title).attr("class","portletDiv").attr("style","width:"+moduleWidth+";height:"+item.height+";");
					var a = $("<a>").attr("href",item.url);
					if(item.target) a.attr("target",item.target);
					$("<img>").attr("src",item.media).attr("alt",item.alt).appendTo(a);
					a.appendTo(div);
					div.appendTo(target);
					
				} else {
					if(target == "#locale") var divClass = "localePortletDiv";
					else var divClass = "portletDiv";
					
					item.backgroundoffset ? os=item.backgroundoffset : os="left";
					
					var div = $("<div>").attr("id",item.title).attr("class",divClass).attr("style","width:"+moduleWidth+";height:"+item.height+"; background: url("+item.media+") "+os+" top no-repeat;");
					
					if (item.offset) {
						var offset = item.offset;
						if(item.headline) $("<h2>").text(item.headline).css('margin-left', offset).appendTo(div);
						if(item.subhead) $("<h3>").text(item.subhead).css('margin-left', offset).appendTo(div);
						if(item.copy) $("<p>").html(item.copy).css('margin-left', offset).appendTo(div);
					} else {					
						if(item.headline) $("<h2>").text(item.headline).appendTo(div);
						if(item.subhead) $("<h3>").text(item.subhead).appendTo(div);
						if(item.copy) $("<p>").html(item.copy).appendTo(div);
					}
					
					if (item.button) {
						$("<div>").addClass("buttonHolder").html("<div class=\"buttonArrow\"></div><div class=\"buttonBody\"><a class=\"buttonLink\" href=\""+item.url+"\">"+item.button+"</a></div>").appendTo(div);
					};

					if (item.dualbutton) {
						var dbh = $("<div>").addClass("dualbuttonHolder").appendTo(div)
						$("<div>").addClass("leftbutton").html("<a href=\""+item.dualbutton[0].lefturl+"\">"+item.dualbutton[0].leftcopy+"</a>").appendTo(dbh);
						$("<div>").addClass("rightbutton").html("<a href=\""+item.dualbutton[0].righturl+"\">"+item.dualbutton[0].rightcopy+"</a>").appendTo(dbh);
					};
					
					if (item.link) {
						offset ? left=offset : left="20px";
						$("<a>").attr("href",item.linkurl).addClass("collPortletLink").attr("style","margin-left: "+left).html(item.link).appendTo(div);
					}
					
					div.appendTo(target);
				}
			},
			collectionPortlet: function(item,target) {
			
				var w = item.width;
				switch(w) {
					case "0.25":
						var moduleWidth = "25%";
						break;
					case "0.5":
						var moduleWidth = "50%";
						var paragraphWidth = "240px";
						break;
					case "1":
						var moduleWidth = "100%";
						var paragraphWidth = "390px";
						break;
					default:
						var moduleWidth = "100%";
						break;
				}
				
				if (item.backgroundoffset) {
					var bgoffset = item.backgroundoffset;
				} else {
					var bgoffset = "left";
				};
				
				if (item.topoffset) {
					var topoffset = item.topoffset;
				} else {
					var topoffset = "top";
				};

				var div = $("<div>").addClass("collPortlet").attr("id",item.title).attr("style","width:"+moduleWidth+"; height:"+item.height+"; background: url("+item.media+") "+bgoffset+" "+topoffset+" no-repeat;");

				if (item.offset) {
					var offset = item.offset;
				} else {
					var offset = "20px;";
				};
				
				$("<h2>").text(item.headline).appendTo(div).attr("style","margin-left:"+offset);
				$("<div>").html(item.copy).attr("style","width:"+paragraphWidth+"; float: none; margin-left:"+offset).appendTo(div);

				if (item.button) {
					$("<div>").addClass("buttonHolder").html("<div class=\"buttonArrow\"></div><div class=\"buttonBody\"><a class=\"buttonLink\" href=\""+item.buttonUrl+"\">"+item.button+"</a></div>").appendTo(div);
				};

				if (item.link) {
					var link = item.link;
					var newLink = link.replace("{0}",item.headline);
					if (item.collection) {
						if (fv !=false) {
							$("<a>").addClass("collPortletLink collGalleryPop").attr("style","margin-left:"+offset).attr("rel",item.collection).html(newLink).appendTo(div);
						}
					} else if (item.linkUrl || item.linkurl) {
						if(item.linkUrl) {
							$("<a>").attr("href",item.linkUrl).addClass("collPortletLink").attr("style","margin-left:"+offset).html(newLink).appendTo(div);
						} else {
							$("<a>").attr("href",item.linkurl).addClass("collPortletLink").attr("style","margin-left:"+offset).html(newLink).appendTo(div);
						};
					};
				};

				div.appendTo(target);
				
				if (item.gallery) {
					
					var galleryDiv = $("<div>").addClass("galleryDiv");
					galleryDiv.appendTo(div);
					for (x in item.gallery) {
						var img = $("<img />").attr("src",item.gallery[x].thumb). attr("alt",item.gallery[x].altText).attr("rel",item.gallery[x].imgId).attr("width",item.gallery[x].twidth).attr("height",item.gallery[x].theight).addClass("collThumbs");
						img.appendTo(galleryDiv);
					}
					galleryDiv.supersleight();
				};
			
			}
		},
		getPageName: function(data) { // gets the id of the page and loads common elements (header, navbar, footer)
			fv = getFlashVersion();
			var pagename = $("html").data("pagename");
			var navbarParams = {};
			if($("body").attr("id") != "shortBody") {
				$("<div id='bgCap'></div>").prependTo("body");
				$("<div id='topgradContainer'><div id='topgrad'></div></div>").prependTo("body");
				$("<div id='bottomgrad'></div>").appendTo("#header");
		 		$("#topgradContainer").supersleight();
				$("#header").supersleight();
				var navbarParams = {wmode:"transparent"};
			}
			
			if (pagename == "rainbeau" || pagename == "violet" || pagename == "marta" ) {
				pagename = "workouts";
			};
			
			$.each(data, function(i,item) {
				for (x in item) {
					if (x == "pageid" && item.pageid == $("html").data("pagename")) {
						if (this.header[0].type == "single")  {
							$("<img />").attr("src", this.header[0].media).appendTo("#header");
						} else if (this.header[0].type == "flash") {
							$("<div>").attr("id",this.header[0].mediaID).appendTo("#header");
							
							//var flashvars = {application: this.header[0].media, locale: locale, country: adidas_cc, configDirectory:"content/assets/xml/lang/"+locale+"/"};
							//var params = {wmode:"transparent"};
							//swfobject.embedSWF("swf/launchers/StandardLauncher.swf", "mastheadcontainer", "975", "361", "9.0.0", false, flashvars,params);
							
							var flashvars = createFlashVars(this.header[0]);
							var params = {wmode:"transparent", scale:"noScale"};
							swfobject.embedSWF(this.header[0].media, this.header[0].mediaID, this.header[0].width, this.header[0].height, "9.0.0", false, flashvars,params);
							
							
						} else if (this.header[0].type == "crossfade") {
							for (x in this.header[0].media) {
								var eachImg = $("<img />").attr("src",this.header[0].media[x]);
								var div = $("<div>").attr("id","cf"+x);
								eachImg.appendTo(div);
								div.appendTo("#header");
							}
							var cf = new Crossfader( new Array('cf0','cf1'), 1000, 4000 );
						}
					}
				}
			});
			
			
			//build navbar <ul> which will then be replaced by Flash navbar. This is a fallback in case no flash plugin is available (iPhones, etc.)
			$.each(data.navigation, function(i, item) {	
				var anchor = $("<a>").attr("href",item.url).html(item.title);
				var listItem = $("<li>");
				anchor.appendTo(listItem);
				listItem.appendTo("#navbar");
			});			
			
			var flashvars = {locale: locale, section: pagename, country: adidas_cc, configDirectory:"content/assets/xml/lang/"+locale+"/"};
			var navbarParams = {wmode:"transparent"}
			// params set at beginning of this function
			swfobject.embedSWF("swf/navigation/Navigation.swf", "navbar", "976", "44", "9.0.0", false, flashvars,navbarParams);
		},
		
		equalHeights: function(group) {
			tallest = 0;
		    group.each(function() {
		        thisHeight = $(this).height();
		        if(thisHeight > tallest) {
		            tallest = thisHeight;
		        }
		    });
		    group.height(tallest);
		},
		
		applyFb: function() {
			$("a.thumbs").fancybox({
				'zoomOpacity' : true,
				'overlayShow' : true,
				'zoomSpeedIn' : 500,
				'zoomSpeedOut' : 500
			});

			$("a.adivid").fancybox({
				'overlayShow': true
			});
			
		},
		
		populateIndex: function(){  	
			$.getJSON("content/assets/xml/lang/"+locale+"/data.js",
				function(data){
					$("html").data("data",data);
					adidasPop.getPageName(data);				
					
					//populate body content
					$.each(data.index.portlets, function(i,item){
		          		//Check for local overwrites
						if(adidasPop.isLocalOverwrite(data,"index",item.title)) item = data.locale.index[item.title];
						adidasPop.create.indexPortlet(item,"#bodycontent");
			        });
					
					// If locale data then add locale bar and portlets.
					if (adidasPop.isLocaleData(data,"index")) {
						if (data.locale.index.portlets) {
							$("<div>").attr("id","locale").appendTo("#bodycontent"); //Create Locale Bar
											
							$.each(data.locale.index.portlets, function(i,item){ //Loop locale portlets and add
								adidasPop.create.indexPortlet(item,"#locale");
							});
						}
					}
		   	});
		},
			
		populateCollections: function() {
			$.getJSON("content/assets/xml/lang/"+locale+"/data.js",
				function(data){
					$("html").data("data",data);
					adidasPop.getPageName(data);
					
					$("img.collThumbs").live("click", function() {
						if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7) {
							if (!galleryViewed) {
								var colltop = ($(document).scrollTop())+"px";
							} else {
								var collTop = "0"
							}
						} else {
							var colltop = "0";
						}
						
						$("<div id='collGallery'></div><div id='collPop'></div>").prependTo("body");
						//$("").supersleight();
						var imgId = $(this).attr("rel");
						
						var flashvars = {
								startupImageName: imgId, 
								collection: "adilibria", 
								countryCode:adidas_cc, 
								pidWsdl: pidWsdl, 
								locale: locale, 
								productsUrl: "content/assets/xml/lang/"+locale+"/CollectionsViewerData.xml", 
								langUrl:"content/assets/xml/lang/"+locale+"/CollectionsViewerLang.xml", 
								stylesUrl: "content/assets/styles/global/CollectionsViewerStyles.xml", 
								imagePath:"swf/collectionsFPV/assets/images/"
							};
						var params = {wmode:"transparent", salign:"lt"};
						swfobject.embedSWF("swf/collectionsFPV/swf/Main.swf", "collPop", "100%", "100%", "9.0.0", false, flashvars,params);		
						$("#collPop").css("top",colltop);
						var galleryViewed = "1";
					}).live("mouseover",function() {$(this).css("cursor","pointer")});
					
					$("a.collGalleryPop").live("click", function() {
						if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7) {
							if (!galleryViewed) {
								var colltop = ($(document).scrollTop())+"px";
							} else {
								var collTop = "0"
							}
						} else {
							var colltop = "0";
						}
						
						$("<div id='collGallery'></div><div id='collPop'></div>").prependTo("body");
						//$("").supersleight();
						var coll = $(this).attr("rel"); 
						if (coll == "clima365") {
							var imgId = "Q4_clima365_sophie_05";
						} else if (coll == "yatra") {
							var imgId = "Q4_yatra_rosa_05";
						};
						var flashvars = {
								startupImageName: imgId, 
								collection: coll, 
								countryCode:adidas_cc, 
								pidWsdl: pidWsdl, 
								locale: locale, 
								productsUrl: "content/assets/xml/lang/"+locale+"/CollectionsViewerData.xml", 
								langUrl:"content/assets/xml/lang/"+locale+"/CollectionsViewerLang.xml",
								stylesUrl: "content/assets/styles/global/CollectionsViewerStyles.xml", 
								imagePath:"swf/collectionsFPV/assets/images/"
						};
						var params = {wmode:"transparent", salign:"lt"};
						swfobject.embedSWF("swf/collectionsFPV/swf/Main.swf", "collPop", "100%", "100%", "9.0.0", false, flashvars,params);	
						$("#collPop").css("top",colltop);
						var galleryViewed = "1";
					}).live("mouseover",function() {$(this).css("cursor","pointer")});
					
					$.each(data.collections.portlets, function(i,item){
						adidasPop.create.collectionPortlet(item,"#bodycontent");
					});
					
					// If locale data then add locale bar and portlets.
					if (adidasPop.isLocaleData(data,"collections")) {
						$("<div>").attr("id","locale").insertAfter("#clima365"); //Create Locale Bar
						
						$.each(data.locale.collections.portlets, function(i,item){ //Loop locale portlets and add
							adidasPop.create.collectionPortlet(item,"#locale");
						});
					}
				});
		},
	
		populateWorkouts: function(who){
			$("body").addClass("bodyworkout");
			
			var who = getQs("athlete") ? getQs("athlete") : "rainbeau";
			
			$.getJSON("content/assets/xml/lang/"+locale+"/data.js",
					function(data){	
					$("html").data("data",data);
					adidasPop.getPageName(data);		
					//populate left-hand column
					$.each(data[who].sidebar, function(i, item) {	
						for (x in item) {
							if (item.title == $("html").data("pagename")) {
								var bkgd = item.active;
							} else {
								var bkgd = item.media;
							};
						};
						//var pads = "padding-left:"+item.alignleft+"; padding-top:"+item.aligntop+";";
						var a = $("<a>").addClass("ps_"+item.title).attr("href",item.url).html(item.subhead+"<br /><span>"+item.athlete+"</span>");
						var li = $("<li>").attr("id",item.title).attr("style","background: url("+bkgd+") left top no-repeat;");



						a.appendTo(li);
						li.appendTo("#personalitiesSubnav");
						a.hover(
							function() {
								a.attr("style","background: url("+item.active+") left top no-repeat;");
							},
							function() {
								a.attr("style","background: none;");
						});
					});

					
					
					//populate main column content
					$.each(data[who].centerContent,function(i,item) {
						$("#rightContent").html(item.copy);
					});
					
					//populate social media links
					if(data[who].social) {
						$("<h2>").html("Links").addClass("socialHeader").appendTo("#rightContent");
						var ul = $("<ul>").addClass("social");						
						$.each(data[who].social,function(i,item) {
							var li = $("<li>");
							$("<a>").addClass(item.type).html(item.copy).attr("href",item.href).attr("target","_blank").appendTo(li);
							li.appendTo(ul);
						});
						ul.appendTo("#rightContent");
					}

					$.each(data[who].rightContent,function(i,item) {
					
						var w = item.width;
						switch(w) {
							case "0.25":
								var moduleWidth = "225px";
								break;
							case "0.5":
								var moduleWidth = "50%";
								var paragraphWidth = "240px";
								break;
							case "1":
								var moduleWidth = "100%";
								var paragraphWidth = "390px";
								break;
							default:
								var moduleWidth = "100%";
								break;
						}
					
						var galleries = $("<div>").attr("id","galleries").appendTo("#sidebar");
						
						//Video Gallery
						if(item.videoGallery) {
						
							$("<h2>").html("Video Gallery").appendTo("#galleries");
							
							$("<div>").attr("id","videoGallery").appendTo("#galleries");
							
							var flashvars = createFlashVars(item.videoGallery[0]);
							flashvars.swf = "swf/com.adidas.core.Application.swf";
							
					
							
							var params = {allowFullScreen: "true", allowScriptAccess: "always", wmode:"transparent", scale:"noScale", align:"lT" };
							swfobject.embedSWF(item.videoGallery[0].media, "videoGallery", item.videoGallery[0].width, item.videoGallery[0].height, "9.0.0", false, flashvars,params);
							
							
							/**
							var ul = $("<ul>").attr("id","videoGallery");
							for (x in item.videoGallery) {
								var li = $("<li>");
								var content = $("<img />").attr("src", item.videoGallery[x].media);
//								var div = $("<div>").attr("id",item.videoGallery[x].title);
								
								var title=(item.videoGallery[x].title);
								var content = $("<img />").attr("src",item.videoGallery[x].media);
								var anchor = $("<a>").attr("href","#"+title).addClass("adivid");
								content.appendTo(anchor); 
								anchor.appendTo(li);
								li.appendTo(ul);
								$("<div>").attr("style","display: none").attr("id",title).html(item.videoGallery[x].content).appendTo("#galleries");
							};
							ul.appendTo("#galleries");
							
							$("#videoGallery").paginate({
								perPage: 4,
								tiled: true
							});
							**/
						}							
						
						//Photo Gallery
						if(item.photoGallery) 
						{
							$("<h2>").html("Photo Gallery").appendTo("#galleries");
							var ul = $("<ul>").attr("id","photoGallery");
							for (x in item.photoGallery) {
								var li = $("<li>");
								var content = $("<img />").attr("src", item.photoGallery[x].media);
								//var div = $("<div>").attr("id",item.photoGallery[x].title);
								//console.log(item.photoGallery[x].title)
								if (item.photoGallery[x].fancybox) {
									if (item.photoGallery[x].fancybox == "image") {
										var anchor = $("<a>").attr({"href":item.photoGallery[x].url,"rel":"group"}).addClass("thumbs");
										content.appendTo(anchor);
										anchor.appendTo(li);
										li.appendTo(ul);
										
									} else {
										content.appendTo(div);
									};
								} else {
									content.appendTo(div);
								};
							};
							ul.appendTo("#galleries");
						$("#photoGallery").paginate();
						}
						$("a.thumbs").fancybox({
							'zoomOpacity' : true,
							'overlayShow' : true,
							'zoomSpeedIn' : 500,
							'zoomSpeedOut' : 500
						});
						
						$("<div>").attr("id","sideBarTout").appendTo("#sidebar");
						
						
						var flashvars = createFlashVars(data[who].sidebarFlash[0]);
						//flashvars.swf = "swf/com.adidas.core.Application.swf";
							
						var params = {wmode:"transparent", scale:"noScale", salign:"lT" };
						swfobject.embedSWF(data[who].sidebarFlash[0].media, "sideBarTout", data[who].sidebarFlash[0].width, data[who].sidebarFlash[0].height, "9.0.0", false, flashvars,params);
							
						/**
						var a = $("<a>").attr("href","memyself.aspx");
						$("<img>").attr("src","img/sidebar_memyself_ad.jpg").attr("alt","Me, Myself - Rosa, Jessica, Ana, Sophie - Discover Move").addClass("sidebar-ad").appendTo(a);
						a.appendTo("#sidebar");
						**/
						
						if(item.featuredProduct) {
							var fpdiv = $("<div>").attr({'id': 'featured-product'});
							$("<h3>").html(item.featuredProduct.title).appendTo(fpdiv);
							$("<img />").attr({'src': item.featuredProduct.media, 'alt': item.featuredProduct.title}).appendTo(fpdiv);
							$("<div>").addClass("buttonHolder").css('margin-left', '25px').html("<div class=\"buttonArrow\"></div><div class=\"buttonBody\"><a class=\"buttonLink\" href=\""+item.featuredProduct.buttonUrl+"\">"+item.featuredProduct.button+"</a></div>").appendTo(fpdiv);
							
							fpdiv.appendTo("#sidebar");
						}
						
						adidasPop.applyFb();
					});
				});
		},		
		
		populateEvents: function(){
//			var locale = getQs("locale");
//			if (locale == "") {locale = "en-en"};
			
			$.getJSON("content/assets/xml/lang/"+locale+"/data.js",
				function(data){
					$("html").data("data",data);
					adidasPop.getPageName(data);				
					
					$("<span>").css("float","left").text(data.events.headline).prependTo("#eventsHeadline");
					$("<h2>").text("Description").prependTo("#eventsLeft");
					$("#eventsLeft h2").html('dasdasdasdasd');
					$("#eventsLeft").html(data.events.description);
					$("<h2>").text(data.events.titleDescription).prependTo("#eventsLeft");					
					//Events Details					
					$("#eventDetails").html('<h2>'+ data.events.details[0].headline+'</h2>');					
					$("<div>").attr("id","deadline").appendTo("#eventDetails");
					$("<h3>").text(data.events.details[0].deadline).appendTo("#deadline");
					$("<p>").text(data.events.details[0].time).appendTo("#deadline");
					$("<div>").addClass("buttonHolder").css('margin-left', '25px').css('width','100%').html("<div class=\"buttonArrow\"></div><div class=\"buttonBody\"><a class=\"buttonLink\" href=\""+data.events.details[0].buttonUrl+"\">"+data.events.details[0].button+"</a></div>").appendTo("#eventDetails");			
				
										
					if (data.events.details[0].whoText){					
						$("<hr>").appendTo("#eventDetails");
						$("<ul>").html("<li><span>"+data.events.details[0].who+"</span>"+ data.events.details[0].whoText+"</li>"+
								   "<li><span>"+data.events.details[0].when+"</span>"+ data.events.details[0].whenText+"</li>"+
								   "<li><span>"+data.events.details[0].where+"</span>"+ data.events.details[0].whereText+"</li>"+
								   "<li><span>"+data.events.details[0].cost+"</span>"+ data.events.details[0].costText+"</li>"+
								   "<li class=\"listLink\"><a href="+data.events.details[0].link+">"+data.events.details[0].linkText+"</li>").appendTo("#eventDetails");	
					}			
					
					//load schedule
					/*$.each(data.events.schedule, function(i,item) {
						var div = $("<div>").addClass("singleEvent");
						$("<h3>").html(item.date).prependTo(div);
						$("<p>").addClass("eventTime").text(item.time).appendTo(div);
						$("<p>").text(item.text).appendTo(div);
						div.appendTo("#eventsLeft");
					});*/
					adidasPop.equalHeights($('.singleEvent'));
		   		});
		},
		
		populateProducts: function(){
//			var locale = getQs("locale");
//			if (locale == "") {locale = "en-en"};
			
			
/*			$.getJSON("content/assets/xml/lang/"+locale+"/data.js",
				function(data){
					
					
		   		});*/
		},
		
		populateProductsDetail: function(){
//			var locale = getQs("locale");
//			if (locale == "") {locale = "en-en"};
			var fv = getFlashVersion();
			
		},
	
		populateStyle: function(){
//			var locale = getQs("locale");
//			if (locale == "") {locale = "en-en"};
			
			$.getJSON("content/assets/xml/lang/"+locale+"/data.js",
				function(data){
					$("html").data("data",data);
					adidasPop.getPageName(data);
					//$("<img src='img/style_dummy.jpg'>").appendTo("#bodycontent");
					var copy = $("<div />").attr("id","eventsLeft");
					var sidebar = $("<div />").attr("id","styleSide");
					
					$("<h2></h2>").attr("style","font-size:14px").html(data.style.headline).appendTo(copy);
					
					var ents = data.style.entries; //or, you know, the shepherds of the trees
					var len = ents.length;
					
					var tmp = "";
					for (x=0; x<len; x++) {
						tmp += "<div class='newEntry'>";
						tmp += "<img src='"+ents[x].img+"' class='entryImg' />";
						tmp += "<div class='entryBody'>";
						tmp += "<h4>"+ents[x].date+"</h4><h2>"+ents[x].headline+"</h2>";
						
						var copylen = ents[x].copy.length;
						for(y=0; y<copylen; y++) {
					    	var cop = ents[x].copy[y];
							if (cop.type == "para") {
								if (cop.subhead) {
									tmp += "<h3>"+cop.subhead+"</h3>";
								}
								tmp += "<p>"+cop.para+"</p>";
							} else if (cop.type == "callout") {
								tmp += "<div class='callout'><p>"+cop.para+"</p><p class='attrib'>"+cop.attrib+"</p></div>";
							} else if (cop.type == "ol") {
								tmp += "<h3>"+cop.subhead+"</h3>";
								var list = cop.para.length;
								tmp += "";
								for (j=0; j<list; j++) {
									tmp += ""+cop.para[j]+"<br/>";
								}
								tmp += "<br/>";
							}
						}
						
						if (ents[x].buttonCopy) {
							tmp += "<div class='buttonHolder'><div class='buttonArrow'></div><div class='buttonBody'><a class='buttonLink' href="+ents[x].buttonUrl+">"+ents[x].buttonCopy+"</a></div></div>";
						};
						
						if (ents[x].link) {
							tmp += "<a class='collPortletLink' href="+ents[x].linkUrl+">"+ents[x].link+"</a>";
						};
						
						tmp += "</div></div>";
					}
					
					
					var sbents = data.style.sidebar;
					var sblen = sbents.length;
					var sbtmp = "";
					
					for (j=0; j<sblen; j++) {
						sbtmp += "<div class='styleSidebar'>"
									+ "<h2>"+ sbents[j].title +"</h2>"
									+ (sbents[j].img || "")
									+ "<p>"+ (sbents[j].copy || "") +"</p>"
									+ ((sbents[j].link) ? "<a class=\"collPortletLink\" href=\""+ sbents[j].linkUrl +"\">"+ sbents[j].link +"<\/a>" : "")
									+ "</div>";
					}
					
					
					copy.appendTo("#bodycontent");
					sidebar.appendTo("#bodycontent");
					
					$("#eventsLeft").append(tmp);
					$("#styleSide").append(sbtmp);
					$(".newEntry:last").css("background","none");
		   		});
		},
		
		populateStyleYourself: function(){
//			var locale = getQs("locale");
//			if (locale == "") {locale = "en-en"};
			
			var st = getQs("st");
			var sm = getQs("sm");
			var sb = getQs("sb");
			var sa = getQs("sa");
			
			if (st == "") {st = "P09617"};
			if (sm == "") {sm = "P09602"};

			// AMS removed if (sb == "") {sb = "G01710"};
			
			if (sb == "") {sb = "G01690"};
			if (sa == "") {sa = "E43616"};
			
			$.getJSON("content/assets/xml/lang/"+locale+"/data.js",
				function(data){
					$("html").data("data",data);
					adidasPop.getPageName(data);

					var flashvars = {country: adidas_cc, startupTop:st, startupMid:sm, startupBot:sb, startupAcc:sa, lang: locale, pidUrl: pidWsdl};
					var params = {base:"swf/styleyourself", bgcolor:"#FFFFFF", wmode:"transparent"};
					swfobject.embedSWF("swf/styleyourself/MixAndMatchModule08.swf", "bodycontent", "975", "502", "9.0.0", false, flashvars,params);

					//$("#shortBody").supersleight();	
		   		});
		},
		
		populateMemyself: function() {
//			var locale = getQs("locale");
//			if (locale == "") {locale = "en-en"};
			
			$.getJSON("content/assets/xml/lang/"+locale+"/data.js",
				function(data){
					$("html").data("data",data);
					adidasPop.getPageName(data);
					
					var flashvars = {country: adidas_cc, lang: locale, pidUrl: pidWsdl};
					var params = {base:"meMyself/build/", wmode:"transparent"};
					swfobject.embedSWF("meMyself/build/Main.swf", "bodycontent", "975", "502", "9.0.0", false, flashvars,params);
			
					//$("#shortBody").supersleight();
				});
		},
		
		populatePersonalities: function(){
//			var locale = getQs("locale");
//			if (locale == "") {locale = "en-en"};
			
			$.getJSON("content/assets/xml/lang/"+locale+"/data.js",
				function(data){
					$("html").data("data",data);
					adidasPop.getPageName(data);		
					
					//populate left-hand column
					$.each(data.personalities.sidebar, function(i, item) {	
						for (x in item) {
							if (item.title == $("html").data("pagename")) {
								var bkgd = item.active;
							} else {
								var bkgd = item.media;
							};
						};
						var pads = "padding-left:"+item.alignleft+"; padding-top:"+item.aligntop+";";
						var a = $("<a>").addClass("ps_"+item.title).attr("href",item.url).attr("style",pads).html(item.subhead+"<br /><span>"+item.athlete+"</span>");
						var li = $("<li>").attr("id",item.title).attr("style","background: url("+bkgd+") left top no-repeat;");

						

						a.appendTo(li);
						li.appendTo("#personalitiesSubnav");
					 	a.hover(
							function() {
								a.attr("style","background: url("+item.active+") left top no-repeat;"+pads+";");
							},
							function() {
								a.attr("style","background: none;"+pads+";");
						});
					});
					
					//populate main column content
					$.each(data.personalities.centerContent,function(i,item) {
						$("#rightContent").html(item.copy);
					});
					
					//populate right-hand column
					$.each(data.personalities.rightContent,function(i,item) {
						
						for (x in item.featuredProduct) {
							var content = $("<img />").attr("src", item.featuredProduct[x].media);
							var div = $("<div>").attr("id",item.featuredProduct[x].title);
							content.appendTo(div);
							$("<h2>").html("Featured Product").appendTo("#sidebar");
							div.appendTo("#sidebar");
						};
						adidasPop.applyFb();
					});
					
					//populate adiTV player
					$.each(data.personalities.adiTv, function(i,item) {
						var title=(item.title);
						var content = $("<img />").attr("src",item.media);
						var anchor = $("<a>").attr("href","#"+title).addClass("adivid");
						content.appendTo(anchor); anchor.appendTo("#aditvPlayer");
						$("<div>").attr("style","display: none").attr("id",title).html(item.content).appendTo("body");
						adidasPop.applyFb();
					});
		   		});
		},

		populateAmbassadors: function(who){
			$("body").addClass("bodyambassador");
			//var who = getQs("ambassador");
			$.getJSON("content/assets/xml/lang/"+locale+"/data.js",
					function(data){	
					$("html").data("data",data);
					adidasPop.getPageName(data);		
					//populate left-hand column
					$.each(data[who].sidebar, function(i, item) {	
						for (x in item) {
							if (item.title == $("html").data("pagename")) {
								var bkgd = item.active;
							} else {
								var bkgd = item.media;
							};
						};
						var pads = "padding-left:"+item.alignleft+"; padding-top:"+item.aligntop+";";
						var a = $("<a>").addClass("ps_"+item.title).attr("href",item.url).attr("style",pads).html(item.subhead+"<br /><span>"+item.athlete+"</span>");
						var li = $("<li>").attr("id",item.title).attr("style","background: url("+bkgd+") left top no-repeat;");



						a.appendTo(li);
						li.appendTo("#personalitiesSubnav");
						a.hover(
							function() {
								a.attr("style","background: url("+item.active+") left top no-repeat;"+pads+";");
							},
							function() {
								a.attr("style","background: none;"+pads+";");
						});
					});

					
					
					//populate main column content
					$.each(data[who].centerContent,function(i,item) {
						$("#rightContent").html(item.copy);
					});

					//populate social media links
					if (data[who].social) { 
							$("<h2>").html(data[who].social.title).addClass("socialHeader").appendTo("#rightContent");
							
							var ul = $("<ul>").addClass("social");						
							$.each(data[who].social.links,function(i,item) {
								var li = $("<li>");
								$("<a>").addClass(item.type).html(item.copy).attr("href",item.href).attr("target","_blank").appendTo(li);
								li.appendTo(ul);
							});
							ul.appendTo("#rightContent");					
					
					}
					
					
					
					
					$.each(data[who].rightContent,function(i,item) {
					
						var w = item.width;
						switch(w) {
							case "0.25":
								var moduleWidth = "225px";
								break;
							case "0.5":
								var moduleWidth = "50%";
								var paragraphWidth = "240px";
								break;
							case "1":
								var moduleWidth = "100%";
								var paragraphWidth = "390px";
								break;
							default:
								var moduleWidth = "100%";
								break;
						}
					
					
						if(item.videoGallery || item.photoGallery || item.relEvent || item.relInfo) {
							var galleries = $("<div>").attr("id","galleries").appendTo("#sidebar");
							
						//Video Gallery
						if(item.videoGallery) {
						
							$("<h2>").html(item.videoGallery[0].title).appendTo("#galleries");
							
							$("<div>").attr("id","videoGallery").appendTo("#galleries");
							
							var flashvars = createFlashVars(item.videoGallery[0]);
							flashvars.swf = "swf/com.adidas.core.Application.swf";
							
							var params = {wmode:"transparent", scale:"noScale", align:"lT" };
							swfobject.embedSWF(item.videoGallery[0].media, "videoGallery", item.videoGallery[0].width, item.videoGallery[0].height, "9.0.0", false, flashvars,params);
							
							

						}							
						
							
							//Photo Gallery
							if(item.photoGallery) {
								$("<h2>").html(item.photoGallery.title).appendTo("#galleries");
								var ul = $("<ul>").attr("id","photoGallery");
								for (x in item.photoGallery.images) {
									var li = $("<li>");
									var content = $("<img />").attr("src", item.photoGallery.images[x].media);
									//var div = $("<div>").attr("id",item.photoGallery[x].title);
									//console.log(item.photoGallery[x].title)
									if (item.photoGallery.images[x].fancybox) {
										if (item.photoGallery.images[x].fancybox == "image") {
											var anchor = $("<a>").attr({"href":item.photoGallery.images[x].url,"rel":"group"}).addClass("thumbs");
											content.appendTo(anchor);
											anchor.appendTo(li);
											li.appendTo(ul);
											
										} else {
											content.appendTo(div);
										};
									} else {
										content.appendTo(div);
									};
								};							
								ul.appendTo("#galleries");
								$("#photoGallery").paginate();
							}
						
							if(item.relEvent) {
								$("<h2>").html("Related Event").appendTo(galleries);
								for (x in item.relEvent) {
									var div = $("<div>").attr("class",item.relEvent[x].title);
									var img = $("<img>").attr("src",item.relEvent[x].media);
									var title = $("<h3>").html(item.relEvent[x].headline);
									var copy = $("<p>").html(item.relEvent[x].copy);
									var link = $("<a>").attr("href",item.relEvent[x].linkUrl).addClass("collPortletLink").html(item.relEvent[x].link);								
									img.appendTo(div);title.appendTo(div);copy.appendTo(div);link.appendTo(div); div.appendTo(galleries); 
								};
							}
							
							if(item.relInfo) {							
								$("<h2>").html("Related Information").appendTo(galleries);
								for (x in item.relInfo) {
									var div = $("<div>").attr("class",item.relInfo[x].title);
									var link = $("<a>").attr("href",item.relInfo[x].linkUrl).addClass("collPortletLink").html(item.relInfo[x].link);
									var copy = $("<p>").html(item.relInfo[x].copy);
																	
									link.appendTo(div);copy.appendTo(div); div.appendTo(galleries); 
								};							
							}
						}
						/* FLASH METHOD 
						for (x in item.featuredProduct) {
							var div = $("<div>").attr("id",item.featuredProduct[x].title).attr("class","portletDiv").attr("style","width:"+moduleWidth+";height:"+item.featuredProduct[x].height+";");
							div.appendTo("#sidebar");
						
							if (item.launcher == "self") {
								var flashvars = {locale: locale, country: adidas_cc, configDirectory:"content/assets/xml/lang/"+locale+"/" };
								var launcher = item.featuredProduct[x].media;
							} else {
								var flashvars = {application: item.featuredProduct[x].media, locale: locale, country: adidas_cc, configDirectory:"content/assets/xml/lang/"+locale+"/"};
								var launcher = item.featuredProduct[x].launcher;
							};
							var params = {allowFullScreen: "true", allowScriptAccess: "always", wmode:"transparent"};
							
							swfobject.embedSWF(launcher, item.featuredProduct[x].title, moduleWidth, item.featuredProduct[x].height, "9.0.0", false,flashvars,params);
						};
						*/
						/* Temp Image replacement */
						if(item.featuredProduct) {
							if(item.featuredProduct.type=="twoButtons"){
								var fpdiv = $("<div id=\"featured-product\">");
								$("<h3>").html(item.featuredProduct.name).appendTo(fpdiv);
								$("<img />").attr({'src': item.featuredProduct.media, 'alt': item.featuredProduct.name}).appendTo(fpdiv);
								$("<div>").addClass("dualbuttonHolder").html("<div class=\"leftbutton\"><a href=\""+item.featuredProduct.button1Url+"\">"+item.featuredProduct.button1Copy+"</a></div><div class=\"rightbutton\"><a href=\""+item.featuredProduct.button2Url+"\">"+item.featuredProduct.button2Copy+"</a></div>").appendTo(fpdiv);
								fpdiv.appendTo("#sidebar");
							} else {
								var a = $("<a>").attr("href",item.featuredProduct.href).attr("target","_blank");
								$("<img>").attr("src",item.featuredProduct.media).attr("alt",item.featuredProduct.name).appendTo(a);
								a.appendTo("#sidebar");
								$("<a>").attr("href",item.featuredProduct.href).addClass("collPortletLink").css("margin-left","15px").appendTo("#sidebar");
							}							
						}
						adidasPop.applyFb();
					});
				});
		},
		populateMemyselfHK: function() {
//			var locale = getQs("locale");
//			if (locale == "") {locale = "en-en"};
			
			$.getJSON("content/assets/xml/lang/"+locale+"/data.js",
				function(data){
					$("html").data("data",data);
					adidasPop.getPageName(data);
					var flashvars = {country: adidas_cc, lang: locale, pidUrl: pidWsdl};
					var params = {base:"meMyself/build/modules/", wmode:"transparent"};
					swfobject.embedSWF("meMyself/build/modules/local-promo-HK.swf", "bodycontent", "945", "450", "9.0.0", false, flashvars,params);
					$("#bodycontent").attr("style","margin-top:15px;");
					//$("#shortBody").supersleight();
				});
		},
		populateMemyselfTW: function() {
//			var locale = getQs("locale");
//			if (locale == "") {locale = "en-en"};
			
			$.getJSON("content/assets/xml/lang/"+locale+"/data.js",
				function(data){
					$("html").data("data",data);
					adidasPop.getPageName(data);
					var flashvars = {country: adidas_cc, lang: locale};
					var params = {base:"meMyself/build/modules/", wmode:"transparent", scale:"noscale"};
					swfobject.embedSWF("meMyself/build/modules/local-promo-TW.swf", "bodycontent", "945", "635", "9.0.0", false, flashvars,params);
					$("#bodycontent").attr("style","width:945px; margin-top:15px;");
					$("body").addClass("localpromotw");
					//$("#shortBody").supersleight();
				});
		}				
		
};