

////////////////////////////
//UTIL
////////////////////////////
var Util = {
	domain: "http://e3.nintendo.com",
	videoItemsObject: undefined,
	current_video_title: "",
	current_vid_url: "",
	alertTest: function () {
		if(Util.videoItemsObject == undefined) {			
			$.getJSON("/_ui/data/videos.json", function(data) {
				var itemListArray = data.items;
				var newCount = itemListArray.length;
				Util.addAlert("videos", newCount);
			});
		} else {
			var newCount = Util.videoItemsObject.length;
			Util.addAlert("videos", newCount, true);
		}
		
	},
	addAlert: function (which, howMuch, atVidPage) {
		//check if cookie exist, if not create one
		var newVidCountCookie = $.cookie('NE32011_TOTAL_VIDCOUNT');
		var currentNew = howMuch;
		if(newVidCountCookie == null) {
			$.cookie('NE32011_TOTAL_VIDCOUNT', 0, { path: '/', expires: 30 });
		} else {
			if(!atVidPage) {
				currentNew = howMuch - parseInt(newVidCountCookie);
			} else {
				currentNew = 0;
				$.cookie('NE32011_TOTAL_VIDCOUNT', howMuch, { path: '/', expires: 30 });
			}
		}
		
		if(which == "videos" && currentNew > 0 ) {
			$("#main-nav #videos span").html(currentNew+" New").css("display", "block");
			$("#home-nav #videos span").html(currentNew+" New").css("display", "block");
		}
		
	},
	smoothScroll : function (elementID) {
		var targetOffset = $(elementID).offset();
		$('html, body').animate({scrollTop:targetOffset.top}, 500);
	},	
	includeNav: function () {
		var url = "/_ui/data/includes.html";
		var curSect = $('body').attr('id');
		$.get(url, function(data){
			$("#header").append($("#global-nav", data));
			$('#footer-wrapper').append($("#footer", data));
			$("#global-nav #"+ curSect+" a").removeClass('js-fade').addClass('selected-nav');
			Util.enableFader();
		});		
	},
	enableFader: function () {
		$(".js-fade").css({opacity: .6});

		$(".js-fade").mouseover(function () {
				$(this).animate({opacity: 1	}, 100);
		}).mouseout(function () {
				$(this).animate({opacity: .6}, 100);
		});		
	},
	cutString: function (stringToCut, limit) {
		//cuts a string at the last possible word
		var strSplit = stringToCut.substring(0,limit);
		return strSplit.substring(0,strSplit.lastIndexOf(" "));	
	},
	embedVideo: function (elementID, videoUrl, vidTitle, temp_autoplay) {
		var swfHeight = $("#"+elementID).height();
		var swfWidth = $("#"+elementID).width();
		var temp_autoplay = temp_autoplay || "true";
		var flashvars ={
			//url: "mp4:"+videoUrl+".mp4",
			url: "http://media.nintendo.com/e3/_ui/videos/"+videoUrl+".mp4",
			title: vidTitle,
			autoplay: temp_autoplay,
			loop: "false",
			omniture:"live", //off, live. Defaults to 'off'
			debug: "false" // turns on debug fields
		};
		var params = {allowFullScreen:"true", wmode: "transparent"};
		swfobject.embedSWF("/_ui/swf/player_vod.swf", elementID, swfWidth, swfHeight, "10.0.0", "", flashvars, params);
	},
	ageGateAdd: function (elementID) {
		$("#video-replace-container").html('<div id="'+elementID+'"><div id="replace-me-agegate"></div></div>');
		if (parseInt($.cookie("plufage")) < 13) {
				Util.ageGateFail();
		} else {
			var markup = '<div id="replace-me-agegate">\
			<strong id="agegate-header">Please enter your date of birth.</strong><br /><br />\
			<form name="agegate" ><input name="referer" type="hidden" value="" /><fieldset style="border:none;">Month:\
			<select id="month">\
			<option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option>\
			</select>\
			Year: <select id="year">\
			<option value="2010"  selected="selected">2010</option>\
			<option value="2009">2009</option>\
			<option value="2008">2008</option>\
			<option value="2007">2007</option>\
			<option value="2006">2006</option>\
			<option value="2005">2005</option>\
			<option value="2004">2004</option>\
			<option value="2003">2003</option>\
			<option value="2002">2002</option>\
			<option value="2001">2001</option>\
			<option value="2000">2000</option>\
			<option value="1999">1999</option>\
			<option value="1998">1998</option>\
			<option value="1997">1997</option>\
			<option value="1996">1996</option>\
			<option value="1995">1995</option>\
			<option value="1994">1994</option>\
			<option value="1993">1993</option>\
			<option value="1992">1992</option>\
			<option value="1991">1991</option>\
			<option value="1990">1990</option>\
			<option value="1989">1989</option>\
			<option value="1988">1988</option>\
			<option value="1987">1987</option>\
			<option value="1986">1986</option>\
			<option value="1985">1985</option>\
			<option value="1984">1984</option>\
			<option value="1983">1983</option>\
			<option value="1982">1982</option>\
			<option value="1981">1981</option>\
			<option value="1980">1980</option>\
			<option value="1979">1979</option>\
			<option value="1978">1978</option>\
			<option value="1977">1977</option>\
			<option value="1976">1976</option>\
			<option value="1975">1975</option>\
			<option value="1974">1974</option>\
			<option value="1973">1973</option>\
			<option value="1972">1972</option>\
			<option value="1971">1971</option>\
			<option value="1970">1970</option>\
			<option value="1969">1969</option>\
			<option value="1968">1968</option>\
			<option value="1967">1967</option>\
			<option value="1966">1966</option>\
			<option value="1965">1965</option>\
			<option value="1964">1964</option>\
			<option value="1963">1963</option>\
			<option value="1962">1962</option>\
			<option value="1961">1961</option>\
			<option value="1960">1960</option>\
			<option value="1959">1959</option>\
			<option value="1958">1958</option>\
			<option value="1957">1957</option>\
			<option value="1956">1956</option>\
			<option value="1955">1955</option>\
			<option value="1954">1954</option>\
			<option value="1953">1953</option>\
			<option value="1952">1952</option>\
			<option value="1951">1951</option>\
			<option value="1950">1950</option>\
			<option value="1949">1949</option>\
			<option value="1948">1948</option>\
			<option value="1947">1947</option>\
			<option value="1946">1946</option>\
			<option value="1945">1945</option>\
			<option value="1944">1944</option>\
			<option value="1943">1943</option>\
			<option value="1942">1942</option>\
			<option value="1941">1941</option>\
			<option value="1940">1940</option>\
			<option value="1939">1939</option>\
			<option value="1938">1938</option>\
			<option value="1937">1937</option>\
			<option value="1936">1936</option>\
			<option value="1935">1935</option>\
			<option value="1934">1934</option>\
			<option value="1933">1933</option>\
			<option value="1932">1932</option>\
			<option value="1931">1931</option>\
			<option value="1930">1930</option>\
			<option value="1929">1929</option>\
			<option value="1928">1928</option>\
			<option value="1927">1927</option>\
			<option value="1926">1926</option>\
			<option value="1925">1925</option>\
			<option value="1924">1924</option>\
			<option value="1923">1923</option>\
			<option value="1922">1922</option>\
			<option value="1921">1921</option>\
			<option value="1920">1920</option>\
			</select><br />	<br />\
			<input type="button" value="Continue" id = "continue" onClick="Util.ageGateValidate(this)" />\
			</fieldset>\
			</form>\
		</div>';
		$("#"+elementID).html(markup);	
		}
	},
	ageGateValidate: function (form)		{
			//Get current date values
			
			var dob=new Date();
			//var ndate=dob.getDate();
			var nmonth=dob.getMonth() + 1;
			var nyear=dob.getFullYear();
			var ckieage=0;			
				
			var year = parseInt(document.getElementById("year").options[document.getElementById("year").selectedIndex].value);
			var month = parseInt(document.getElementById("month").options[document.getElementById("month").selectedIndex].value);
			//var day = parseInt(document.getElementById("day").options[document.getElementById("day").selectedIndex].value);
			ckieage = (nyear - year);
			$.cookie("plufage", ckieage, { expires: 1 });
				if (((nyear - year) > 13) || ((nyear - year)>=13 && (month>=nmonth))){
					Util.embedVideo("video-replace", Util.current_vid_url, Util.current_video_title)					
				} else {					
					Util.ageGateFail();
				}				
			},
			
	ageGateFail: function () {
		$("#replace-me-agegate").html('<div id = "sorry"><h2>Sorry, but you cannot access this content at this time.</h2></div>');		
	}
}
	

////////////////////////////
//FILTERABLE CONTENT
////////////////////////////
var Filter = {
	
	nav: '#portfolio-filter', 
	content: '#portfolio-list',
	list_padding: '15px 0',
	current_sec_path: "",
	current_third_path: "",
	current_video_url: "",
	current_video_title: "",
	current_video_extra: "",
	filter_type: "",
		
	init: function (whichURL, whichStyle) {
		$.getJSON(whichURL, function(data) {
						
			Filter.categoryParse(data.categories);				
			if(whichStyle != "games") {
				var itemListArray = data.items;				
				if(whichStyle == "list") {
					Filter.listParse(itemListArray)
				} else if(whichStyle == "video"){
					Filter.videoParse(itemListArray)
				}			
			} else {	
					Filter.gameParse(data.threeds, 'nintendo-3ds');
					Filter.gameParse(data.wii, 'wii');
					Filter.gameParse(data.ds, 'nintendo-ds');					
			}			
		});
		setTimeout(function(){ Filter.enableEvents(whichStyle); }, 500);	
	},
	
	categoryParse: function (categoryArray) {
		$.each(categoryArray, function(index) {				
				$(Filter.nav).append("<li><a id='filter-"+categoryArray[index].toLowerCase().replace(/ /g,'-')+"' href='#"+categoryArray[index].toLowerCase().replace(/ /g,'-')+"' rel='address:/"+categoryArray[index].toLowerCase().replace(/ /g,'-')+"'>"+categoryArray[index]+"</a></li");
			});
	},
	
	videoParse: function(itemListArray) {
		Util.videoItemsObject = itemListArray;
		$.each(itemListArray, function(index) {
			var isThisNew = $.cookie("NE32011_VIDWATCHED_"+itemListArray[index].vidURL);
			var isNew = "";
			if(isThisNew == null) {
				$.cookie("NE32011_VIDWATCHED_"+itemListArray[index].vidURL, 0, { path: '/videos', expires: 30 });
				isNew = "<span class='new-bubble'>New</span>";
			} else if(isThisNew == 0){
				isNew = "<span class='new-bubble'>New</span>";
			}
			var isMature = (itemListArray[index].agegate == "true") ? "mature" : "";
			var hasExtra = (itemListArray[index].extra != "" && itemListArray[index].extra != undefined ) ? "<span class='hidden-text' style='display:none'>"+itemListArray[index].extra+"</span>" : "";
			$(Filter.content).append("<li class='"+itemListArray[index].category.toLowerCase().replace(/ /g,'-')+ "' rel='"+itemListArray[index].vidURL+"'>"+isNew+"<a class='btn-video' href = '#"+itemListArray[index].vidURL+"' rel='"+isMature+"' ><div class='img-container' style='background:url(/_ui/img/videos/thumbs/"+itemListArray[index].thumb+") no-repeat bottom #000;'><div class='cta-overlay'><span>Currently Playing</span></div></div><span class='vid-title'>"+itemListArray[index].title+"</span>"+itemListArray[index].text+hasExtra+"</a></li>");
		
		});
	},
	
	listParse: function(itemListArray) {
		$.each(itemListArray, function(index) {
			var urlTarget = (itemListArray[index].target == "_blank") ? "_blank": "_self";

			$(Filter.content).append("<li class='"+itemListArray[index].category.toLowerCase().replace(/ /g,'-')+ "'><a href = '"+itemListArray[index].url+"' target='"+urlTarget+"'>"+itemListArray[index].text+"</a></li>");
		});
	},
	
	gameParse: function (gameObject, hardware) {
		var newMarkup = '';
		var hardwarDir = "wii";
		
		switch(hardware) {
			case "nintendo-3ds":
				hardwarDir = "3ds";
				break;
			case "nintendo-ds":
				hardwarDir = "ds";
				break;
		}
		newMarkup += "<ul class='"+hardware+ "'> <div id='' class='system-logo "+hardware+ "-logo' ></div>"
		$.each(gameObject, function(index) {
			newMarkup += "<li class='"+hardware+ "' ><a class='' href = 'detail/#/"+hardwarDir+"/"+gameObject[index].contentURL+"' ><div class='img-container' style='background:url(/_ui/img/games/"+hardwarDir+"/"+gameObject[index].contentURL+"/thumb.jpg) no-repeat bottom #000;'><div class='cta-overlay'><span>Learn More</span></div></div><span class='game-title'>"+gameObject[index].title+"</span></a></li>";
		});	
		newMarkup += "</ul>"	
		$(Filter.content).append(newMarkup);
	},	
	enableEvents: function (whichStyle) {
		switch(whichStyle) {
			case "list":
				Filter.filter_type = "updates";
				break;
			case "video":
				Filter.filter_type = "videos";
				break;
			case "games":
				Filter.filter_type = "games";
				break;
		}
		//on load
		var filterTest = $.address.value().split("/");	
		if(whichStyle == "video" ) {

			$(Filter.content).filterable();		
			
			//events
			$.address.change(function(event) {
				var filterTest = $.address.value().split("/");
				if(filterTest[2]!= undefined) {
					Filter.current_third_path = filterTest[2]; 
					Filter.triggerBoxChange(filterTest[1]);
				} else {
					$.address.path(Filter.current_sec_path+"/"+Filter.current_third_path);
				}
			});			
			$(".btn-video").click(function (e) {
				e.preventDefault();
				var newUrl = $(this).attr('href').split("#");				
				Filter.current_third_path = newUrl[1] || "";			
				$.address.path(Filter.current_sec_path+"/"+Filter.current_third_path);
			});			
			$('a[id^="filter-"]').live('click', function (e) {
				e.preventDefault();
				var newUrl = $(this).attr('href').split("#");
				var filterTest = $.address.value().split("/");
				Filter.current_third_path =   filterTest[2] || ""
				$.address.path(newUrl[1]+"/"+Filter.current_third_path)
			});			
			
			//init loading
			Filter.current_third_path = filterTest[2] || "";	
			if(filterTest[1] == "" || filterTest[1] == undefined) {				
				$("#filter-all").trigger('click');
			} else {
				Filter.triggerBoxChange(filterTest[1]);	
			}
			
		} else if(whichStyle == 'list' || whichStyle == "games") {
			//events
			$.address.change(function(event) {
				var filterTest = $.address.value().split("/");
				Filter.current_sec_path = filterTest[1];		
				Filter.triggerListChange($("#filter-"+ filterTest[1]));
			});
			//init loading			
			if(filterTest[1] == "" || filterTest[1] == undefined) {	
				//$("#filter-all").trigger('click');
				Filter.triggerListChange($("#filter-all"), true);
			} else {
				Filter.triggerListChange($("#filter-"+ filterTest[1]), true);	
			}
		}
		Util.alertTest();

	},	
	triggerListChange: function (currentDom, isFirstLoad) {
		if(Filter.filter_type == "games" && !isFirstLoad) {
				Util.smoothScroll("#content");
		}
		$(Filter.nav+' .selected').removeClass('selected'); 
		currentDom.parent().addClass('selected');	
		var filter_name = currentDom.text().toLowerCase().replace(/ /g,'-');
		Omniture.trackLink(6, Filter.filter_type +":"+ filter_name, 7, "Filter Change");
		
		if(filter_name == 'all') { 
			 $(Filter.content+' .hidden').slideDown(1000).removeClass('hidden');				 
		} else {
			$(Filter.content).children().each(function(index) {  
			  if(!$(this).hasClass(filter_name) && !$(this).hasClass('hidden')) { 
					$(this).slideUp(1000).addClass('hidden');
				} else if($(this).hasClass(filter_name) && $(this).hasClass('hidden')){
					$(this).slideDown(1000).removeClass('hidden');				
				}  		
			});		
		}
	},
	triggerBoxChange: function (whichFilter) {
		//change video if diff
		if(Filter.current_third_path != Filter.current_video_url && Filter.current_third_path != "") {			
			//highlight
			var curDom = $('a[href*="#'+Filter.current_third_path+'"]');
			$('#right-content li').removeClass('selected');
			Filter.current_video_extra = curDom.children('.hidden-text').html();
			Filter.current_video_title = curDom.children('.vid-title').text();
			$('#right-content').find('li[rel*="'+Filter.current_third_path+'"]').addClass('selected');
			$('#right-content').find('.open-cta').animate({bottom:"-35px"}, 'normal').removeClass('open-cta');
			curDom.find('.cta-overlay').animate({bottom:"0px"}, 'normal').addClass('open-cta');
			curDom.parent().find('.new-bubble').fadeOut();
			//set this cookie to watched
			$.cookie("NE32011_VIDWATCHED_"+Filter.current_third_path, 1, { path: '/videos', expires: 30 });			
			
			//content
			Filter.current_video_url = Filter.current_third_path;
			$("#video-title").text(Filter.current_video_title);
			Omniture.trackPage("videos:"+Filter.current_video_title);
			if(Filter.current_video_extra != "" && Filter.current_video_extra != undefined) {
				$("#video-float").animate({left:"20px"});
				$("#video-extra").html(Filter.current_video_extra).fadeIn(function () {
					if ($.browser.msie) {
						this.style.removeAttribute("filter"); 
					};		
				});
			} else {
				$("#video-float").animate({left:"153px"});
				$("#video-extra").fadeOut();
			}
			if($(curDom).attr('rel') != "mature") {		
				setTimeout(function(){ Util.embedVideo("video-replace", Filter.current_video_url, Filter.current_video_title) }, 500);
			} else {
				Util.current_video_title = Filter.current_video_title;
				Util.current_vid_url = Filter.current_video_url;
				setTimeout(function(){ Util.ageGateAdd("video-replace") }, 300);
			}
			Util.smoothScroll('#promo');		
		}
		if(Filter.current_sec_path != whichFilter) {
			Filter.current_sec_path = whichFilter;
			$('#left-nav').find('.selected').removeClass('selected');
			$("#filter-"+whichFilter).parent().addClass('selected');
			
			$(Filter.content).trigger('filter', [ "#"+whichFilter ]);
			
			Omniture.trackLink(6, Filter.filter_type +":"+ whichFilter, 7, "Filter Change");
			
		}

		if (Filter.current_third_path == "") {
			//no video selected
			$("#right-content li .btn-video:first").trigger('click');		
		}
		
	}	
}


	
////////////////////////////
//GAMES DETAIL
////////////////////////////	
	var Games = {
		current_path: "",
		current_movie_title: "",
		more_games: "",
		current_more_games: "",
		current_system: "",
		contentURL: "",
		init: function () {
			Games.showloadingScreen();
			$.address.change(function(event) {
				current_path = $.address.value();
				var gameTest = current_path.split("/");				
				Games.current_system = 	gameTest[1];
				Games.contentURL = gameTest[2];
								
				var systemTempBg;
				switch(Games.current_system) {
					case "wii":
						systemTempBg = "bg-blue";
						break;
					case "3ds":
						systemTempBg = "bg-red";
						break;
					case "ds":
						systemTempBg = "bg-grey2";
						break;					
				}
				$("#background").removeClass().addClass(systemTempBg);
				$("#system").html(Games.current_system);
				$("#system").removeClass().addClass((Games.current_system == "3ds") ? "n3ds" : Games.current_system);
				Games.loadHTML(gameTest[1], gameTest[2]);
			}); 
		
		},
		loadHTML: function(systemDir, whichURL) {
			var url = "/_ui/img/games/"+systemDir+"/"+whichURL+".html";
			$.get(url, function(data){				
				$("#txt-gametitle").html($("#game-title", data).html());				
				//details
				var detailVar = "";
				$("#details", data).children("li").each(function (i) {		
					detailVar += $(this).html()+"<br/>";
				});				
				$("#txt-gamedetails").html(detailVar);				
				//add this
				$("#txt-gamedetails").append('<div id="addthis-container" addthis:url="'+Util.domain+'/_ui/img/games/'+systemDir+'/'+whichURL+'.html"  addthis:title="'+$("#game-title", data).text()+' : Nintendo Network @ E3"></div>');				
				$("#addthis-container").append(' <div class="addthis_toolbox addthis_default_style">\
												<a class="addthis_button_facebook"></a>\
												<a class="addthis_button_twitter"></a>\
												<a class="addthis_button_facebook_like" fb:like:layout=""></a>\
												<a class="addthis_button_compact"></a>\
												</div>');				
				addthis.toolbox("#addthis-container");
				//end add this

				//media		
				var mediaVar = "<ul>";
				$("#videos", data).children("li").each(function (i) {			
					mediaVar += "<li class='btn-vid' rel='"+$(this).text()+"' title='"+$(this).attr("title")+"'><span class='rated' style='display:none' rel='"+$(this).attr("rel")+"'></span><span class='cta-video'></span><img src='/_ui/img/games/"+Games.current_system+"/"+Games.contentURL+"/"+$(this).attr("thumb")+"' height='59' width='109'/></li>"
				});
				var ssAmount = parseInt($("#screenshot_amount", data).text());
				for (var ss = 0; ss<ssAmount; ss++) {
					mediaVar += "<li class='btn-ss' rel='/_ui/img/games/"+Games.current_system+"/"+Games.contentURL+"/s"+ss+"'><img src='/_ui/img/games/"+Games.current_system+"/"+Games.contentURL+"/t"+ss+".jpg'/></li>"
				};
				mediaVar += "</ul>";
				$("#media-carousel").html(mediaVar);
				
				$("#media-carousel ul").carouFredSel({
					circular: false,
					infinite: false,
					padding:0,
					width: 590,
					height: 85,
					items: {
						width: 109,
						height: 85,
						visible: 5				
					},
					auto: {
						play:false					
					},
					prev: {
						button: "#media-arrowleft"	
					},
					next: {
						button: "#media-arrowright"	
					}					
				});
				
				//media alert box
				if($("#media-alert", data).html() != null) {
					$("#media-alert").show().html($("#media-alert", data).html());
				} else {
					$("#media-alert").hide();
				}
				
				//body
				$("#txt-body").html($("#bodytext", data).html());
				
				//more links
				var moreLinksTemp = "";
				
				$("#more_links", data).children("li").each(function (i) {			
					moreLinksTemp += $(this).html();
				})
				if(moreLinksTemp == "") {
					$("#more-links").hide();
				} else {
					$("#more-links").show().html(moreLinksTemp);
				}
				Omniture.trackPage("games:detail:"+Games.current_system+":"+$("#game-title", data).html());
				Games.enableEvents();
				Games.loadMoreGames();
				Util.alertTest();
				
			}).error(function () {
				//alert("ERROR: Could not load data.");
				window.location.href= "/games/";		
			});	
			Util.enableFader();
			
		},
		
		enableEvents: function () {			
			$.address.change(function(event) {   
				Util.smoothScroll("#system")	
			}); 
			
			$("#media-carousel ul li.btn-ss").click(function () {
				$('.selected').removeClass('selected');
				$(this).addClass('selected');
				//$("#media-box").html("<img src='"+$(this).attr('rel')+"'>");
				Omniture.movieStop(Games.current_movie_title);
				var imgUrl = $(this).attr('rel');
				var imgLoader = new Image();
				$(imgLoader).attr('src', imgUrl+".jpg");
				
				var loadingTimeout = setTimeout(function(){ Games.showloadingScreen(); }, 300);
				
				$(imgLoader).load(function () {	
					$("#media-box").html(this);
					clearTimeout(loadingTimeout)
				}).error(function () {
					//load png instead
					imgLoader = new Image();
					$(imgLoader).attr('src', imgUrl+".png");
					$(imgLoader).load(function () {	
						$("#media-box").html(this);
						clearTimeout(loadingTimeout)
					})
				});
			});
			
			$("#media-carousel ul li.btn-vid").click(function () {
				$('.selected').removeClass('selected');
				$(this).addClass('selected');
				$("#media-box").html('<div id="video-replace-container"><div id="video-replace"></div></div>')
				var thisURL = $(this).attr('rel');
				Games.current_movie_title = $(this).attr('title');				
				
				if($(this).children('.rated').attr('rel') != "mature") {		
					Util.embedVideo("video-replace", thisURL, Games.current_movie_title, "false");
				} else {
					Util.current_video_title = Games.current_movie_title;
					Util.current_vid_url = thisURL;
					Util.ageGateAdd("video-replace");
				}
				
				
			});			
			
			$("#media-carousel ul li:first").trigger('click');			
			
			$(".btn-moregames").click(function (e) {
				e.preventDefault();
				$("#more-titles a").removeClass('selected');
				$(this).addClass('selected');
				Games.current_more_games = $(this);
				Games.initNewCarousel("."+$(this).attr('href'));	
			});
			
			Util.alertTest();
		},
		
		showloadingScreen: function () {
			$("#media-box").html('<div id="loader"><span id="loading-animate">Loading...</span></div>');
			
			function animateLoading() {
				$("#loading-animate").animate({paddingTop: "+=20px"}, 2500).animate({paddingTop: "-=20px"}, 2500);
			}
			animateLoading();
			//setInterval(function(){	animateLoading();	}, 5000);
		},
			
		//more games
		loadMoreGames: function() {
			if(Games.more_games == "") {
				$.getJSON('/_ui/data/games_list.json', function(data) {
					Games.more_games = data;
					Games.addGames(Games.more_games.wii, 'wii');
					Games.addGames(Games.more_games.ds, 'ds');
					Games.addGames(Games.more_games.threeds, '3ds');					
					$("#more-games").find('li[rel="'+Games.current_system+"-"+Games.contentURL+'"]').hide();
					setTimeout(function(){	$("#n3ds a.btn-moregames").trigger('click');	}, 500);					
					
				});
			} else {
				$("#more-games").find('li').show();	
				$("#more-games").find('li[rel="'+Games.current_system+"-"+Games.contentURL+'"]').hide();
				setTimeout(function(){	
					var var_cur_system = "wii"					
					switch(Games.current_system) {
						case "3ds":
							var_cur_system = "n3ds";
							break;
						case "ds":
							var_cur_system = "nds";
							break;
					}
					
					$("#"+var_cur_system+" a.btn-moregames").trigger('click');
				}, 500);
			}
		},
				
		addGames: function(gameObject, hardware) {
			var newMarkup = "<ul>";
			$.each(gameObject, function(index) {
				newMarkup += "<li rel='"+hardware+"-"+gameObject[index].contentURL+"'><a href='#' rel='address:/"+hardware+"/"+gameObject[index].contentURL+"'><img src='/_ui/img/games/"+hardware+"/"+gameObject[index].contentURL+"/thumb.jpg' width='160' height='90'><span class='more-games-txt'>"+gameObject[index].title+"</span></a></li>";
			});
			newMarkup += "</ul>"
			$(".more-games-"+hardware).hide().append(newMarkup);				
		},
		
		initNewCarousel: function (whichElement) {
			if($('.moregames-selected ul').carouFredSel()){
				$('.moregames-selected ul').trigger("destroy");
				$('.moregames-selected').hide().removeClass('moregames-selected');
				$(".moregames-selected .caroufredsel_wrapper").contents().unwrap();
			}
			$(whichElement).show().addClass('moregames-selected');
			
			$(whichElement+ " ul").carouFredSel({
				circular: false,
				infinite: false,
				padding:0,
				height: 200,
				items: {
					visible: 5			
				},
				auto: {
					play:false					
				},
				prev: {
					button: "#moregames-arrowleft"	
				},
				next: {
					button: "#moregames-arrowright"	
				}
				
			});
		
		}
	}
////////////////////////////
//HOME
////////////////////////////	

var Home = {	
	init: function() {
		Home.enableEvents();
		Home.featuredPromos();
	},
	enableEvents: function() {
		$("#promo a").mouseover(function() {
			$("a[rel='onHoverActivate']").addClass('active');
		}).mouseout(function() {	
			$("a[rel='onHoverActivate']").removeClass('active');
		});
		
	},	
	featuredPromos: function () {
		$.getJSON("/_ui/data/featured_home.json", function(data) {
						
			var itemsArray = data.items;
			$.each(itemsArray, function(index) {
					$("#featured-content ul").append('<li><a href = "'+itemsArray[index].url+'"><img src = "'+itemsArray[index].thumb+'" width="209" height="123" /><p>'+itemsArray[index].text+'</p></a></li>');
			});
			
			$("#featured-content ul").carouFredSel({
				circular: true,
				infinite: true,
				padding:0,
				height: 160,
				items: {
					visible: 3			
				},
				auto: {
					play:true,
					items: 3,
					pauseOnHover:true,
					pauseDuration: 6000					
				},
				pagination: {
					container: "#pagination",
					pauseOnHover: true
				}
				
			});
			Util.alertTest();
			Home.recentUpdates();
		});			
	},
	
	recentUpdates: function () {
		$.getJSON("/_ui/data/updates.json", function(data) {
			var itemsArray = data.items;
			$.each(itemsArray, function(index) {
					var newStr = (itemsArray[index].text.length > 100) ? Util.cutString(itemsArray[index].text, 100) + "..." : itemsArray[index].text;
					var urlTarget = (itemsArray[index].target == "_blank") ? "_blank": "_self";
					$("#recent-updates ul").append('<li><a href = "'+itemsArray[index].url+'" target="'+urlTarget+'">'+newStr+'</a></li>');
					if(index >=6) {
						return false;
					}
			});	
			
			$("#recent-updates ul").carouFredSel({
				circular: true,
				infinite: true,
				padding:0,
				height: 115,
				width: 280,
				direction:'up',
				items: {
					visible: 3,
					height: 30		
				},
				auto: {
					play:true,
					delay: 5000,
					items: 2,
					pauseOnHover:true					
				}				
			});			
			Util.alertTest();			
		});
	}
}

////////////////////////////
//HW
////////////////////////////

var Hw = {
	current_section: "",
	current_video_title: "",
	current_vid_url: "",	
	
	init: function () {
		Hw.enableEvents();		
	},
	
	enableEvents: function () {
		$.address.change(function(event) {   
			var filterTest = $.address.value().split("/");
			if(Hw.current_section != filterTest[1]) {
				if(Hw.current_section != ""){
					$("#content #"+Hw.current_section).fadeOut(500).addClass('hw-hidden');
				}
				Hw.resetVideo();
				Hw.current_section = filterTest[1];
				$("#tab-nav li").children('.selected').removeClass('selected');
				$("#hw-nav-"+Hw.current_section).addClass('selected');
				$("#content #"+Hw.current_section).delay(500).fadeIn(500).removeClass('hw-hidden');
				
				if(Hw.current_section != "gallery"){
					Omniture.trackPage("hw:"+Hw.current_section);
				}
			}
			
			if(Hw.current_section == "gallery") {
				if(filterTest[2] == "" || filterTest[2] == undefined || filterTest[2] == "device") {
					$("#device a").addClass('selected');
					$("#play-style a").removeClass('selected');
					$("#gallery-thumbs-playstyle").hide();
					$("#gallery-thumbs-device").fadeIn(500);
					setTimeout(function(){ $("#gallery-thumbs-device a:first").trigger('click'); }, 500);
					Omniture.trackPage("hw:"+Hw.current_section+":device");
				} else if(filterTest[2] == "playstyle") {
					$("#device a").removeClass('selected');
					$("#play-style a").addClass('selected');
					$("#gallery-thumbs-device").hide();
					$("#gallery-thumbs-playstyle").fadeIn(500);
					setTimeout(function(){ $("#gallery-thumbs-playstyle a:first").trigger('click'); }, 500);
					Omniture.trackPage("hw:"+Hw.current_section+":playstyle");					
				}
			}
			if(Hw.current_section == "video") {
				if(filterTest[2] == "" || filterTest[2] == undefined) {
					setTimeout(function(){ $("#portfolio-list li .btn-video:first").trigger('click'); }, 500);
				} else {				
					Hw.changeVideo(filterTest[2])
				}
			}
		});		
		
		$(".btn-video").click(function (e) {
				e.preventDefault();
				var newUrl = $(this).attr('href');
				//Hw.changeVideo(newUrl)			
				$.address.path("video/"+newUrl);
			});	
			
		$("#gallery-thumbs-device a, #gallery-thumbs-playstyle a").click(function (e) {
				e.preventDefault();
				
				var newUrl = $(this).attr('href');
				$("#gallery-thumbs-device .selected, #gallery-thumbs-playstyle .selected").removeClass('selected');
				$(this).delay(100).addClass("selected")
				
				var imgLoader = new Image();
				$(imgLoader).attr('src', newUrl);
				$("#gallery-container").html("");			
				$(imgLoader).load(function () {	
					$("#gallery-container").html(this);
				})
			});	
		
		
		$("#game-thumbs ul li a").click(function (e) {
			e.preventDefault();
			
			var newUrl = $(this).attr('href');
				$("#game-thumbs .selected").removeClass('selected');
				$(this).delay(100).addClass("selected")
				
				var imgLoader = new Image();
				$(imgLoader).attr('src', newUrl+".jpg");
				$("#game-enlarged").html("");			
				$(imgLoader).load(function () {	
					$("#game-enlarged").html('<a href="'+newUrl+'_large.jpg" rel="prettyPhoto" title=""><img src="'+this.src+'"/></a>');
					$("a[rel^='prettyPhoto']").prettyPhoto({theme:'light_square', deeplinking:false, social_tools:false, title:false, allow_resize:false});

				})
		});
		//first load
		var filterTest = $.address.value().split("/");
		if(filterTest[1] == "" || filterTest[1] == undefined) {
			$.address.path("introduction")
		}
		Util.alertTest();
	},
	
	changeVideo: function (current_vid_url) {
			//highlight
			var curDom = $('a[href*="'+current_vid_url+'"]');				
			$('#portfolio-list li').removeClass('selected');
			Hw.current_video_title = curDom.children('.vid-title').text();
			Hw.current_vid_url = current_vid_url;
			curDom.parent('li').addClass('selected');
			$('#portfolio-list li').find('.open-cta').animate({bottom:"-35px"}, 'normal').removeClass('open-cta');
			curDom.find('.cta-overlay').animate({bottom:"0px"}, 'normal').addClass('open-cta');							
			//content
			$("#video-title").text(Hw.current_video_title);
			Omniture.trackPage("hw:videos:"+Hw.current_video_title);
			Util.smoothScroll('#tab-nav');
			if($(curDom).attr('rel') != "mature") {		
				setTimeout(function(){ Util.embedVideo("video-replace", Hw.current_vid_url, Hw.current_video_title) }, 300);
			} else {
				Util.current_video_title = Hw.current_video_title;
				Util.current_vid_url = Hw.current_vid_url;
				setTimeout(function(){ Util.ageGateAdd("video-replace") }, 300);
			}
	},
	
	resetVideo: function() {
		$('#portfolio-list li').removeClass('selected');
		$("#video-title").text('');
		$('#video-replace-container').html('<div id="video-replace"></div>')
		$('#portfolio-list li').find('.open-cta').animate({bottom:"-35px"}, 'normal').removeClass('open-cta');
		if(Hw.current_video_title != ""){
			Omniture.movieStop(Hw.current_video_title);
		}
	}
	
	
	
}


//the end
$(document).ready(function () {
	jQuery.easing.def = "easeInOutQuad";
	Util.includeNav();
});

