﻿/********************/
/* Flash parameters */
/********************/
var swfInstallPath = "../../swf/expressinstall.swf";
var gameFlashParams =
{
	allowScriptAccess: "always",
	wmode: "opaque"
};
var flashParams =
{
	allowScriptAccess: "always",
	wmode: "transparent"
};
var fullScreenFlashParams =
{
	allowFullScreen: "true",
	allowScriptAccess: "always",
	wmode:"transparent"
};
/********************/
/*   Video player   */
/********************/
var videoArray = null;
var activeLink = null;
function playVideo(id, isOut)
{   
	if (activeLink)
	{
		activeLink.className = activeLink.className.replace(/ active_link/, "");
	}
	activeLink = document.getElementById(currentVideoPopup + "_popup_link_" + id);
	if (activeLink)
	{
		activeLink.className += " active_link";
		if (isOut)
		{
			activeLink.parentNode.parentNode.parentNode.scrollTop = activeLink.offsetTop - activeLink.parentNode.offsetTop;
		}
	}
	if (videoArray)
	{
		var video = null;
		for (var i = 0; i < videoArray.length; i++)
		{
			if (videoArray[i].id == id)
			{
				video = videoArray[i];
				break;
			}
		}
		if (video)
		{
			if (currentVideoPopup == "video")
			{
				currentVideoId = video.id.replace(/&apostrophe;/, "'");
			}
			else
			{
				currentWinnerId = video.id.replace(/&apostrophe;/, "'");
			}
			document.getElementById("VideoTitleLabel").innerHTML = video.title.replace(/&apostrophe;/, "'");
			document.getElementById("VideoDescriptionLabel").innerHTML = video.description.replace(/&apostrophe;/, "'");
			swfobject.embedSWF(
				video.src.replace(/&apostrophe;/, "'"),
				"VideoboxContent",
				"400",
				"326",
				"9.0.0",
				swfInstallPath,
				{},
				fullScreenFlashParams);
		}
	}
}
function OpenVideoPlayer(id)
{
	ShowPopupLayer('PlayVideoPopup');
	playVideo(id, true);
}
function OpenWinnerVideo(id)
{
	currentVideoPopup = "winner";
	videoArray = winnerVideos;
	currentWinnerId = id;
	document.getElementById("popup_container_winner").style.display = "block";
	document.getElementById("popup_container_video").style.display = "none";
	OpenVideoPlayer(id);
}
function OpenVideoGallery(id, type, url)
{     
    if (type  && url)
    {    
        _hbLink(id, type, url);
    }
	currentVideoPopup = "video";
	videoArray = videos;
	currentVideoId = id;
	document.getElementById("popup_container_winner").style.display = "none";
	document.getElementById("popup_container_video").style.display = "block";
	OpenVideoPlayer(id);
}
/********************/
/* Visual timeline  */
/********************/
function ShowImageLayer()
{
	ShowPopupLayer('ImageDetailsPopup');
}
function ShowImage(url)
{
	var image = document.getElementById('PopupDetailedImage');
	image.src = url;
	image.onload = ShowImageLayer;
}
function OpenVisualTimeline(itemId)
{
	ShowPopupLayer('VisualTimelinePopup');
	swfobject.embedSWF(
		"swf/timeline/main.swf",
		"TimelineVideo",
		"580",
		"375",
		"9.0.115",
		swfInstallPath,
		{
			libUrl:"swf/timeline/library.swf",
			DataUrl:"xml/NesquikExperience_Ads_ImageGallery.xml",
			defaultImage:itemId ? itemId : 0
		},
		flashParams);
}
/********************/
/*    Play games    */
/********************/
var currentPopupContent = null;
var adultGamesLoaded = false;
var mobileGamesLoaded = false;
var pengingGameID = null;
function GameTimeout(intervalId)
{
	if (parent != window)
	{
		parent.GameTimeout(intervalId);
	}
	else
	{
		if (currentPopupContent)
		{
			currentPopupContent.style.display = "none";
		}
		currentPopupContent = document.getElementById("popup_timeout_content_" + intervalId);
		if (currentPopupContent)
		{
			currentPopupContent.style.display = "block";
		}
		ShowPopupLayer('GameTimeoutContent');
	}
}
function adult_onload()
{
	adultGamesLoaded = true;
}
function mobile_onload()
{
	mobileGamesLoaded = true;
}
var gameUrls =	[
	{id: 0, src: 'BunnyGroove.aspx'},
	{id: 1, src: 'PathToHappiness.aspx'},
	{id: 2, src: 'DeliveringHappiness.aspx'},
	{id: 3, src: 'HappyHill.aspx'},
	{id: 4, src: 'BunnySurf.aspx'},
	{id: 5, src: 'Scrumble.aspx'},
	{id: 6, src: 'Pinball.aspx'},
	{id: 7, src: 'Tennis.aspx'}];
function goto_game(id, name, url, track)
{
	currentGameId = id;
	document.getElementById("CurrentGameTitle").innerHTML = name;
	for (var i = 0; i < gameUrls.length; i++)
	{
		if (gameUrls[i].id == id)
		{
			document.getElementById("GameIframe").src = adultGameUrls[i].src;
			break;
		}
	}
	if (pendingGameID != id)
	{
		location.href = track;
	}
	pendingGameID = null;
}
var adultGameUrls =	[
	{id: 0, src: 'BunnyGroove.aspx'},	
	{id: 1, src: 'PathToHappiness.aspx'},
	{id: 2, src: 'DeliveringHappiness.aspx'},
	{id: 3, src: 'HappyHill.aspx'},
	{id: 4, src: 'BunnySurf.aspx'},
	{id: 5, src: 'Scrumble.aspx'},
	{id: 6, src: 'Pinball.aspx'},
	{id: 7, src: 'Tennis.aspx'}];
var mobileGameUrls = [
	{id: 0, src: 'Bowling.aspx'},
	{id: 1, src: 'Chase.aspx'}];
function goto_adult_game(id, name, url, track)
{
	currentGameId = id;
	currentMobileGameId = null;
	if (mobileGamesLoaded)
	{
		document.getElementById("mobilegamelist").select();
	}
	document.getElementById("flashgamelist").select(id);
	document.getElementById('MobileGamesContainer').style.display = "none";
	document.getElementById('WebGamesContainer').style.display = "block";
	document.getElementById("GameNameLabel").innerHTML = name;
	document.getElementById("MobileGamesIframe").src = "";
	for (var i = 0; i < adultGameUrls.length; i++)
	{
		if (adultGameUrls[i].id == id)
		{
			document.getElementById("WebGamesIframe").src = adultGameUrls[i].src;
			break;
		}
	}
	if (pendingGameID != id)
	{
		location.href = track;
	}
	pendingGameID = null;
}
function _hbLink(game, position, coords)
{
}
function show_mobile_game(id, name, url)
{
	currentMobileGameId = id;
	currentGameId = null;
	document.getElementById("mobilegamelist").select(id);
	document.getElementById('WebGamesContainer').style.display = "none";
	document.getElementById("WebGamesIframe").src = "";
	document.getElementById('MobileGamesContainer').style.display = "block";	
	for (var i = 0; i < mobileGameUrls.length; i++)
	{
		if (mobileGameUrls[i].id == id)
		{
			document.getElementById("MobileGamesIframe").src = mobileGames[i].pageUrl;
			document.getElementById('MobileGameTitleLabel').innerHTML = mobileGames[i].name;
	        document.getElementById('MobileGamePreviewImage').src = mobileGames[i].link;
	        document.getElementById("GameNameLabel").innerHTML = mobileGames[i].title;
	        document.getElementById("mobileGameTitleInFooter").innerHTML = mobileGames[i].title;	        
			break;
		}
	}
}
function return_adult_game()
{
	document.getElementById("mobilegamelist").select();
	if (currentGameId != null)
	{
		document.getElementById("flashgamelist").select(currentGameId);
	}
}
function goto_mobile_game(id, name, url)
{
	if (adultGamesLoaded)
	{
		document.getElementById("flashgamelist").select();
	}
	DoAdultAction("show_mobile_game('" + id + "', '" + name +"', '"+ url + "')", "return_adult_game()");
}
function OpenGame(id, game, position, coords)
{
	_hbLink(game, position, coords);
	ShowPopupLayer('GameDetailsPopup', true);
	pendingGameID = id;
	swfobject.embedSWF(
		"swf/game_detail.swf?id=1",
		"flashgamelist",
		"110",
		"458",
		"9.0.0",
		swfInstallPath,
		{
			url_data_1: "xml/PlayGames.xml",
			method_1: "goto_game",
			active_1: 1,
			game_id_1: id
		},
		flashParams);
}
function OpenAdultGame(id, game, position, coords)
{
	_hbLink(game, position, coords);
	ShowPopupLayer('GameDetailsPopup', true);
	document.getElementById('MobileGamesContainer').style.display = "none";
	document.getElementById('WebGamesContainer').style.display = "none";
	document.getElementById("WebGamesIframe").src = "";
	document.getElementById("MobileGamesIframe").src = "";
	swfobject.embedSWF(
		"swf/game_list.swf?id=2",
		"mobilegamelist",
		"110",
		"346",
		"9.0.0",
		swfInstallPath,
		{
			url_data_2: "xml/PlayMobileGames.xml",
			method_2: "goto_mobile_game",
			onload_2: "mobile_onload",
			active_2: 0,
			game_id_2: -1
		},
		flashParams);
	pendingGameID = id;
	swfobject.embedSWF(
		"swf/game_list.swf?id=1",
		"flashgamelist",
		"110",
		"346",
		"9.0.0",
		swfInstallPath,
		{
			url_data_1: "xml/PlayGames.xml",
			method_1: "goto_adult_game",
			onload_1: "adult_onload",
			active_1: 1,
			game_id_1: id
		},
		flashParams);
}
function LaunchMobileGame(id)
{
	ShowPopupLayer('GameDetailsPopup', true);
	document.getElementById('WebGamesContainer').style.display = "none";
	document.getElementById("WebGamesIframe").src = "";
	document.getElementById("MobileGamesIframe").src = "";
	document.getElementById('MobileGamesContainer').style.display = "none";
	swfobject.embedSWF(
		"swf/game_list.swf?id=1",
		"flashgamelist",
		"110",
		"346",
		"9.0.0",
		swfInstallPath,
		{
			url_data_1: "xml/PlayGames.xml",
			method_1: "goto_adult_game",
			onload_1: "adult_onload",
			active_1: 0,
			game_id_1: -1
		},
		flashParams);
	swfobject.embedSWF(
		"swf/game_list.swf?id=2",
		"mobilegamelist",
		"110",
		"346",
		"9.0.0",
		swfInstallPath,
		{
			url_data_2: "xml/PlayMobileGames.xml",
			method_2: "goto_mobile_game",
			onload_2: "mobile_onload",
			active_2: 1,
			game_id_2: id
		},
		flashParams);
}
function OpenMobileGame(id, name, url)
{
	DoAdultAction("LaunchMobileGame('" + id + "', '" + name + "', '" + url + "')");
}

function OpenPlayGames()
{
	swfobject.embedSWF(
		"swf/play_games.swf",
		"playgamescontent",
		"655",
		"325",
		"9.0.0",
		swfInstallPath,
		{url_data: "xml/PlayGamesGallery.xml"},
		flashParams);
}

function OpenPlayAdultGames()
{
	swfobject.embedSWF(
		"swf/play_games.swf",
		"playgamescontent",
		"655",
		"325",
		"9.0.0",
		swfInstallPath,
		{url_data: "xml/PlayGamesGallery.xml"},
		flashParams);
	swfobject.embedSWF(
		"swf/game_list.swf?id=2",
		"mobilegamescontent",
		"110",
		"325",
		"9.0.0",
		swfInstallPath,
		{
			url_data_2: "xml/PlayMobileGamesGallery.xml",
			method_2: "OpenMobileGame"
		},
		flashParams);
}