// Blast Radius
// Nintendo DSi(TM) teaser site




// Global Variables
var hideTagline = 1;
var container = "container";
var content = "content";
var contentObj = $(content);
//var videoId = 3;
var flashMin = 9;

var sectionCenterOffset = new Array();
	sectionCenterOffset[0] = -347;
	sectionCenterOffset[1] = -2047;
	sectionCenterOffset[2] = -3147;
	sectionCenterOffset[3] = -4547;
	sectionCenterOffset[4] = -5947;
	sectionCenterOffset[5] = -7347;
	sectionCenterOffset[6] = -8747;
	sectionCenterOffset[7] = -10147;
	sectionCenterOffset[8] = -11547;

	sectionCenterOffset[9] = -12947;
	sectionCenterOffset[10] = -14347;
	sectionCenterOffset[11] = -15747;
	sectionCenterOffset[12] = -17147;
	sectionCenterOffset[13] = -18547;
	sectionCenterOffset[14] = -19947;
	sectionCenterOffset[15] = -21347;
	sectionCenterOffset[16] = -1747;



var startDelay;
var periodical;
var slideshowInit = 6;
var slideshowTimer = 8;
var slideshowPostVid = 2;

var isIE='\v'=='v';
var isIE6 = ((isIE)&&(Browser.Engine.version==4)) ? true: false;

TB_WIDTH = 0;
TB_HEIGHT = 0;
var TB_doneOnce = 0;




function blankAnchors(obj) {
	for (var i=0; i<obj.length; i++) obj[i].target = "_blank";
}



function setExternalTargets() {
	blankAnchors($$("a.dsi-logos"));
	blankAnchors($$("ul#dsi-callouts li a"));
	blankAnchors($$("a.external"));
}




function rePositionComponents() {
	var resizeObj = $("dsi-teaser-resize");
	resizeObj.value = 1;
	setDimensions("container");
	setDimensions("header");
	setDimensions("navigation");
	setDimensions("dsi-callouts");
	setDimensions("legal");
	setDimensions("languages");
	setDimensions("dsi-overlay");
	moveContent();
	centerComponent($("dsi-preorder-container"), 692, 460);
	resizeObj.value = 0;
}




function centerComponent(_obj, _width, _height) {
	if (typeof(_obj) != "undefined") {

		_width =  (typeof(_width) != "undefined") ? _width : _obj.getWidth();
		_height = (typeof(_height) != "undefined") ? _height : _obj.getHeight();

		if (isIE) {
			var windowWidth  = window.getWidth();
			var windowHeight = window.getHeight();
		} else {
			var windowWidth  = eval(window.innerWidth);
			var windowHeight = eval(window.innerHeight);
		}
		if (windowHeight < 600) windowHeight = 600;
		if (windowWidth < 1000) windowWidth = 1000;

		var targetTop  = Math.round(Math.abs(windowHeight)/2) - Math.round(Math.abs(_height)/2);
		var targetLeft = Math.round(Math.abs(windowWidth)/2) - Math.round(Math.abs(_width)/2);

		if (Browser.Engine.name.toUpperCase()=="WEBKIT") targetLeft = targetLeft - 18;
		targetTop = targetTop + window.getScrollTop();

		_obj.style.top  = targetTop + "px";
		_obj.style.left = targetLeft + "px";
	}
}


function setDimensions(_container) {
	if (typeof(_container) != "undefined") {
		var containerObj = $(_container);
		var initObj = containerObj.getCoordinates();

		var targetWidth  = new Fx.Tween(containerObj, {duration: 100, transition: Fx.Transitions.Quad.easeInOut});
		var targetHeight = new Fx.Tween(containerObj, {duration: 100, transition: Fx.Transitions.Quad.easeIn});

		var windowHeight = window.getHeight();
		var windowWidth  = window.getWidth();
		if (windowWidth < 1000) windowWidth = 1000;
		if (Browser.Engine.name.toUpperCase()=="WEBKIT") {
			if (windowHeight < $(container).getHeight()) windowWidth  = windowWidth - 16;
		}

		if (!isIE6) {
			if (initObj.width!=windowWidth) targetWidth.start("width", initObj.width, windowWidth);
		} else {
			if (initObj.width!=windowWidth) containerObj.style.width = windowWidth + "px";
		}
	}
}


function highlightMarker(markerObj) {
	var allMarkers = $$("ul#mainnav li.marker");
	for (var i=0; i<allMarkers.length; i++) allMarkers[i].removeClass("selected");
	if (typeof(markerObj)=="undefined") {
		var currentState = eval($("dsi-teaser-section").value) - 1;
		if (allMarkers[currentState]) {
			if (currentState<0) {
				allMarkers[allMarkers.length-1].addClass("selected");
			} else {
				allMarkers[currentState].addClass("selected");
			}
		}
	} else {
		markerObj.addClass("selected");
	}
}


function calloutSwitch(callOut) {
	var allCallObj = $$("ul#dsi-callouts li a");
	for (var i=0; i<allCallObj.length; i++) allCallObj[i].removeClass("selected");
	if (typeof(callOut)!="undefined") callOut.addClass("selected");
}

function setNav(_targetState, _navObj) {
	_navObj.removeClass("selected");
	_navObj[eval(_targetState)].addClass("selected");

}

function flashVidComplete() {
	$("dsi-teaser-video").value = 0;
	if ($("dsi-teaser-autoplay").value == 1) initializeSlideshow(slideshowPostVid);
}


function pauseVid() {
//	alert($("dsi-teaser-section").value);
	if (appLanguage=="en") {
		if (eval(Browser.Plugins.Flash.version)>=flashMin) {
			var flashObj = $("flashvideoplayer");
			var vidPlayObj = $("dsi-teaser-video");
			if (vidPlayObj.value ==1) {
				if (flashObj) flashObj.pauseVid();
			}
		}
	}
	$("dsi-teaser-video").value = 0;
}


function moveContent(_id, _swapImg) {
	var transition = $("dsi-teaser-transition");
	if (!eval(transition.value)) {
		var currentState = eval($("dsi-teaser-section").value);
		var targetState = (typeof(_id) == "undefined") ? currentState : _id;
		var contentObj = $("feature");
		var pageCenter = Math.round($("content").getWidth()/2);
		var duration = 900 + (Math.abs((Math.abs(currentState) - Math.abs(targetState)))*120);
		var interimState;

		$("dsi-teaser-section").value = targetState;
		var navObj = $$('ul#mainnav li span');

		if (hideTagline) {
			var taglineObj = $("dsi-tagline");
			var moveFX = new Fx.Tween(taglineObj, {duration: 700, transition: Fx.Transitions.Quad.easeInOut});
			moveFX.start('height', taglineObj.getHeight(), 1).addEvent('onComplete', function() {
				hideTagline = 0;
				taglineObj.style.display = "none";
			});
		}

		transition.value = 1;
		if (_swapImg) $("dsi-teaser-section").value = (targetState==0) ? sectionCenterOffset.length-2 : $("dsi-teaser-section").value = "1";
		highlightMarker();

		if (!isIE6) {
			var moveFX = new Fx.Tween(contentObj, {duration: duration, transition: Fx.Transitions.Expo.easeInOut});
		} else {
			var moveFX = new Fx.Tween(contentObj, {duration: 0});
		}
		moveFX.start('left', contentObj.getLeft(), sectionCenterOffset[targetState]+pageCenter).addEvent('onComplete', function() {
			transition.value = 0;
			if (_swapImg) {
				interimState = (targetState==0) ? interimState = sectionCenterOffset.length-2 : interimState = 1;
				$("dsi-teaser-section").value = interimState;
				targetState = interimState;
				navObj[interimState].addClass("selected");
				contentObj.style.left = (sectionCenterOffset[interimState]+pageCenter) + "px";
			}
			setNav(targetState,navObj);
			var videoPlay = $("dsi-teaser-video");
			if (($("dsi-teaser-section").value==videoId)&&(videoPlay.value==0)) {
				if ($("dsi-teaser-resize").value==0) {
					if (appLanguage == "en") {
						stopSlideshow()
						$("dsi-teaser-video").value = 1;
						if (eval(Browser.Plugins.Flash.version)>=flashMin) {
							var flashObj = $("flashvideoplayer");
							if (flashObj) flashObj.playVid();
						}
					}
				}
			}
		});
//	} else { // pauses video during browser resizing
//		var flashObj = $("flashvideoplayer");
//		if (flashObj) flashObj.pauseVid();
	}
}


function paginate(direction) {
	var currentState = eval($("dsi-teaser-section").value);
	var nextState = 0;
	var swapImage = false;
	direction = (typeof(direction)=="undefined") ? 1 : -1;
	if (currentState == 1) {
		if (direction>0) {
			nextState = currentState + direction;
		} else {
			nextState = 0;
			swapImage = true;
		}
	} else if (currentState == sectionCenterOffset.length -2) {
		if (direction>0) {
			nextState = sectionCenterOffset.length -1;
			swapImage = true;
		} else {
			nextState = sectionCenterOffset.length -2 + direction;
		}
	} else {
		nextState = currentState + direction;
	}
	$("dsi-teaser-autoplay").value=1;
	moveContent(nextState, swapImage);
}


var refresh = (function() {
	paginate();
});


function initializeSlideshow(waitTime) {
	$clear(startDelay);
	if (typeof(waitTime)=="undefined") waitTime = slideshowPostVid;
	var slideObj = $("dsi-teaser-slideshow");
	//startDelay = setTimeout ("startSlideshow()", (waitTime*1000));
	startDelay = startSlideshow.delay(waitTime*1000);
	//startSlideshow();
}


function startSlideshow(waitTime) {
	clearTimeout(startDelay);
	if (typeof(waitTime)=="undefined") waitTime = slideshowPostVid;
	$clear(periodical);
	$("dsi-teaser-slideshow").value = 0;
	paginate();
	periodical = paginate.periodical(slideshowTimer * 1000, this);
}

function delaySlideshow(theEvent) {
	if ($("dsi-teaser-transition").value==0) {
		new Event(theEvent).stop();
		stopSlideshow();
		initializeSlideshow(slideshowTimer);
	}
}

function stopSlideshow() {
//	new Event(theEvent).stop();
	clearTimeout(startDelay);
	$clear(periodical);
	$("dsi-teaser-transition").value==0;
}

function stopAutoplay() {
	$("dsi-teaser-autoplay").value=0;
	stopSlideshow();
}


function lazyLoadBGImages() {
	var bgObj = $$("ul#dsi-teaser-feature li");
	var bgURL, bgTarget;
	for (var i=0; i<bgObj.length; i++) {
		if (i==0) {
			bgTarget = eval(bgObj.length-2);
		} else if (i==bgObj.length-1) {
			bgTarget = eval(1);
		} else {
			bgTarget = i;
		}
//				bgURL = "url(" + appRoot + "img/bg/p" + bgTarget + "-" + appLanguage + ".jpg)";

	bgURL = "url(" + appRoot + "img/bg/p" + bgTarget + ".jpg)";

//		bgURL = "url(" + appRoot + "img/bg/p09.jpg)";


		bgObj[i].style.backgroundImage = bgURL;
	}
}


function loadFlashComponent() {
	if ((eval(Browser.Plugins.Flash.version)>=flashMin)&&(appLanguage=="en")) {
		var flashVidObj = new Swiff(appRoot + "videoPlayer.swf", {
			id: "flashvideoplayer",
			name: "flashvideoplayer",
			container: "dsivideo",
			width: 403,
			height: 410,
			params: {
				wmode: "transparent",
				bgcolor: "transparent",
				loop: false
			},
			vars: {
				videoURL: "vid/dsi_distort_on2vp6.flv",
				loop: false
			},
			callBacks: {
			}
		});
	} else {
		var flashVidObj = $("dsivideo");
		flashVidObj.innerHTML = '<div class="noflash" id="noflash-' + appLanguage + '"></div>';
	}
}

function videoDisplay(show) {
	var flashObj = $("dsivideo");
	var moveFX = new Fx.Tween(flashObj, {duration: 0});
	if (typeof(show) != "undefined") {
		moveFX.start('height', 1, 412);
	} else {
		moveFX.start('height', flashObj.getHeight(), 1);
	}
}

function showVideo() {
	var flashObj = $("dsivideo");
	var moveFX = new Fx.Tween(contentObj, {duration: 0});
	moveFX.start('height', flashObj.getHeight(), 1);
}


function track(params) {
	//Check to see if the omniture javascript library is loaded
	var tmp = {};
	var linkVars = [];
	var linkEvents = [];
	if (s) {
		for(key in params) {
			if(key.indexOf("prop") == 0 || key.indexOf("eVar") == 0) linkVars.push(key);
			if(key.indexOf("event") == 0) linkEvents.push(key);

			tmp[key] = s[key];
			s[key] = params[key];
		}
		s.tl(); //sends a transaction to omniture (pseudo http request)
		for(key in tmp) {
			s[key] = tmp[key];
		}
	}
}


function postInit() {
	hideTagline = 1;
	lazyLoadBGImages();
	loadFlashComponent();
}


function showPreOrder(preorderBoolean) {
	if (typeof(preorderBoolean) != "undefined") {
		if ((preorderBoolean != 1)||(preorderBoolean != true)) {
			preorderBoolean = false;
		} else {
			preorderBoolean = true;
		}
	} else {
		preorderBoolean = false;
	}

	var preorderObj = $("dsi-preorder-container");
	preorderObj.set('tween', { duration: 100 });
//	preorderObj.tween('opacity', 0, 1);

	var overlayObj = $("dsi-overlay");
	var windowHeight = window.getScrollHeight()-20;
	var windowWidth = window.getWidth() -20;

	if (Browser.Engine.name.toUpperCase()=="WEBKIT") windowWidth  = windowWidth - 40;
	overlayObj.style.height = windowHeight + 'px';
	overlayObj.style.width = windowWidth + 'px';
//	centerComponent(overlayObj);

	var moveFX = new Fx.Tween(preorderObj, {duration: 150});
	if (preorderBoolean) {
		videoDisplay();
		preorderObj.style.opacity = 0;
		preorderObj.style.display = "block";
		moveFX.start('opacity', 0, 1).addEvent('onComplete', function() {
			overlayObj.style.display = "block";

		});
	} else {
		preorderObj.style.opacity = 1;
		preorderObj.style.display = "block";
		moveFX.start('opacity', 1, 0).addEvent('onComplete', function() {
			preorderObj.style.display = "none";
			preorderObj.style.opacity = 1;
			overlayObj.style.display = "none";
			videoDisplay(1);
		});
	}

}


function showDSIunits(dsUnit) {
	var ds = $$('ul.dsNav a');
	var stores = $$('div.stores ul');
	var intro = $$('div.introCopy');

//	stores.each(function(store, storeIndex){ store.style.display="none"; });
	intro.each(function(introItem, introIndex){ introItem.style.display = "block"; });
	intro.each(function(introItem, introIndex){
		introItem.style.display = "none";
	});

	//resetting onState class
	ds.each(function(dsI, index){ dsI.removeClass("onState"); });
	if (typeof(dsUnit) != "undefined") {
		var selectedUnit = dsUnit.className;
			selectedUnit = selectedUnit.replace(" ","");
			dsUnit.addClass("onState");
	}

	stores.each(function(store, storeIndex) {
		if (typeof(dsUnit) != "undefined") {
			if (store.className==selectedUnit) {
				store.style.display="block";
			} else {
				store.style.display="none";
			}
		} else {
			$$("div.introCopy")[0].style.display = "block";
		}
	});

}



// Begin Initialize
window.addEvent('load', function(browserStart) {

	// clear starting values
	$("dsi-teaser-transition").value = 0;
	$("dsi-teaser-section").value = 1;
	$("dsi-teaser-slideshow").value = 0;
	$("dsi-teaser-autoplay").value = 1;
	$("dsi-teaser-video").value = 0;
	$("dsi-teaser-resize").value = 0;
	initializeSlideshow(slideshowInit);
	rePositionComponents();
	setExternalTargets();


	// set button observers
	window.addEvent('resize',	function() { rePositionComponents() });

	// navigation
	$$("a.dsi-logos").addEvent('click',		function() { stopAutoplay(); pauseVid() });
	$$("a.external").addEvent('click',		function() { stopAutoplay(); pauseVid() });

	$$("a#btn-preorder").addEvent('click',	function(e) {
		e.stop();
		pauseVid();
		centerComponent($("dsi-preorder-container"), 692, 460);
		stopAutoplay();
		showDSIunits();
		showPreOrder(1);
		var params = { "eVar12": "Pre-Order Nintendo DSi", "events": "event14" }; return track(params);
	}).addEvent('mouseover', function() {
		this.addClass("selected");
	}).addEvent('mouseout', function() {
		this.removeClass("selected");
	});

	var ds = $$('ul.dsNav a');
	ds.each(function(dsItem, index){
		dsItem.addEvent('click' , function(e){
			e.stop();
			showDSIunits(this);
		});
	});

	var dsClose = $$("h3.btn-preorder-close");
	dsClose.each(function(dsbtn,index){
		dsbtn.addEvent('click', function(){ showPreOrder(); });
	});


	$$("ul#mainnav li.marker").addEvent('mouseover',	function() { highlightMarker(this) });
	$$("ul#mainnav li").addEvent('mouseout',			function() { highlightMarker() }).addEvent('click', function() { pauseVid(); });




	$("s-s1").addEvent('click',	function(e) { stopAutoplay(); moveContent(1) });
	$("s-s2").addEvent('click',		function(e) { stopAutoplay(); moveContent(2) });
	$("s-s3").addEvent('click',	function(e) { stopAutoplay(); moveContent(3) });
	$("s-s4").addEvent('click',	function(e) { stopAutoplay(); moveContent(4) });
	$("s-s5").addEvent('click',		function(e) { stopAutoplay(); moveContent(5) });
	$("s-s6").addEvent('click',	function(e) { stopAutoplay(); moveContent(6) });
	$("s-s7").addEvent('click',	function(e) { stopAutoplay(); moveContent(7) });
	$("s-s8").addEvent('click',	function(e) { stopAutoplay(); moveContent(8) });

	$("s-s9").addEvent('click',	function(e) { stopAutoplay(); moveContent(9) });
	$("s-s10").addEvent('click',	function(e) { stopAutoplay(); moveContent(10) });
	$("s-s11").addEvent('click',	function(e) { stopAutoplay(); moveContent(11) });
	$("s-s12").addEvent('click',	function(e) { stopAutoplay(); moveContent(12) });
	$("s-s13").addEvent('click', function(e) { stopAutoplay(); moveContent(13) });
	$("s-s14").addEvent('click',	function(e) { stopAutoplay(); moveContent(14) });
	$("s-s15").addEvent('click',	function(e) { stopAutoplay(); moveContent(15) });











	$("nav-back").addEvent('click',		function(e) { pauseVid(); delaySlideshow(e); paginate(1) });
	$("nav-next").addEvent('click',		function(e) { pauseVid(); delaySlideshow(e); paginate() });


	// callouts
	$$("ul#dsi-callouts li a").addEvent('mouseover', function(e) {
				calloutSwitch(this);
			}).addEvent('mouseout',function(e) {
				calloutSwitch();
			}).addEvent('click', function(e) {
				stopSlideshow();
				pauseVid();
	});
	$$("li#call-press a").addEvent('click',		function(e) { var params = { "_global.omniture.channel" : "", "_global.omniture.prop1" : "" }; return track(params); });
	$$("li#call-news a").addEvent('click',		function(e) { var params = { "eVar12": "Upcoming Nintendo product button", "events": "event17" }; return track(params); });
	$$("li#call-clubn a").addEvent('click',		function(e) { var params = { "eVar11": "Club Nintendo button", "events": "event16" }; return track(params); });
	$$("li#call-iwata a").addEvent('click',		function(e) { var params = { "_global.omniture.channel" : "", "_global.omniture.prop1" : "" }; return track(params); });

	$$("li#nintendo-logo a").addEvent('click',	function(e) { var params = { "_global.omniture.channel" : "", "_global.omniture.prop1" : "" }; return track(params); });




	postInit();
});
// End Initialize


