/////////////////////////
// SETTINGS
/////////////////////////
var flashPopupContentID = "game";
var flashPopupCloseData = new Object();

var globalNavHeaderHeight = 0;
var gameTopMarginOffset = 100;

var makeBigText = "BIG&nbsp;SCREEN";
var makeSmallText = "SMALL&nbsp;SCREEN";
var isEnlarged = false;
var isPlayingGame = false;
var readyToPlay = false;
var gameEnlargeScaleModifier = 1.2;
var closeBtnOffset = 36;

function closeFlashPopup() {
	
	isPlayingGame = false;
		
	swfobject.removeSWF( flashPopupContentID );	
		
	$("#flashpopup").css("visibility",  "hidden");	
	$("#flashpopup").css("display", "none");
	//document.getElementById( 'flashcontentID' ).closeFlashPopup(flashPopupCloseData);
}


$(document).ready(function() {
	readyToPlay = true;	
});

function openFlashPopup( p_url, p_params, p_width, p_height, p_fps, p_bgcolor, closeData ) {
	
	//console.log( "openFlashPopup : " + p_url, p_params );
	//short circuit if they click too soon	
	if(!readyToPlay) return;
	if(isPlayingGame) return;
	
	$("#dimoverlay").css("visibility", "visible");
	$("#flashpopup").css("display", "block");
	$("#dimoverlay").css("display", "block");
		 
	$(function() {
		
	  $.nyroModalSettings({
		windowResize: false,
		resizable: true,
		modal: true,
		blocker: "#dimoverlay",
		showContent: showGameOpenAnimation,
		
		showBackground: function (elts, settings, callback) {
			elts.bg.css({opacity:0}).fadeTo(500, 0.8, callback);
			
		},
		
		beforeHideContent: function(elts, settings, callback) {
			closeFlashPopup();
			isEnlarged = false;
			callback();			
		},
		
		endRemove: function(elts, settings) {
			$("#dimoverlay").css("visibility",  "hidden");
			$("#dimoverlay").css("display", "none");
		},
		
		cssOpt: {
			wrapper: {
				top: (p_height/2 + gameTopMarginOffset),
				width: (p_width + 2),
				height: (p_height + 2)
			}
		}
	  });
	});
	
	var flashpopup = document.getElementById( 'flashpopup' );
	loadFlash( p_url, p_params, p_width, p_height, p_fps, p_bgcolor );	
	
	//document.getElementById( 'flashcontentID' ).openFlashPopup(true);
	
	// generate the data for closing popup so it can be passed to the flash app when the popup is closed
	flashPopupCloseData = closeData;
	
	$('#flashpopup').height(p_height);
	$('#flashpopup').width(p_width);
	
	if(false)//createEnlargeButton())
	{
		$("#enlargeBtn").click(function(){
			
			var newW = isEnlarged ? p_width : p_width * gameEnlargeScaleModifier;
			var newH = isEnlarged ? p_height : p_height * gameEnlargeScaleModifier;
			
			if(newH > $(window).height() - globalNavHeaderHeight - 15 ||
				newW > $(window).width() - 15
			)
			{
				//add logic here for handling a screen that's too small
			}
			
			$('#enlargeBtn').html(isEnlarged ? makeBigText : makeSmallText);	
			$('#enlargeBtn').removeClass(isEnlarged ? "enlargeBtnSmallScreen": "enlargeBtnBigScreen");	
			$('#enlargeBtn').addClass(isEnlarged ? "enlargeBtnBigScreen" : "enlargeBtnSmallScreen");	
						
			$("#closeBtnDiv").css("margin-left", (newW/2 - closeBtnOffset) + "px");	
			
			$('#nyroModalWrapper').height(newH+10);
			$('#nyroModalWrapper').width(newW+10);	
			$('#nyroModalWrapper').css("margin-left", (-1*(newW+10)/2)+"px");			
			
			$('#'+flashPopupContentID).height(newH);
			$('#'+flashPopupContentID).width(newW);
			
			$('#nyroModalContent').height(newH);
			$('#nyroModalContent').width(newW);		
			
			isEnlarged = !isEnlarged;		
		});	
	}
		
	createCloseButton();
	
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	
	$("#closeBtnDiv").click(function(){		
		$.nyroModalRemove();
		if (isIE) 
		{
			$("body").css("overflow",  "auto");
		}
	});
	
	//The following needs to be reset each game launch
	$('#enlargeBtn').html(makeBigText);		
	$("#closeBtnDiv").css("margin-left", (p_width/2 - closeBtnOffset) + "px");
	if($('#enlargeBtn').hasClass("enlargeBtnSmallScreen"))
	{
		$('#enlargeBtn').removeClass("enlargeBtnSmallScreen");		
	}	
	
	$('#enlargeBtn').addClass("enlargeBtnBigScreen");
	
	//set the content of the modal dialogue
	$.nyroModalManual({
      url: '#flashpopup'
    });		
	
	isPlayingGame = true;
		
	$('#nyroModalFull').css("top", "0px");
	
	if (isIE) 
	{
		$("body").css("overflow",  "hidden");
	}
	scroll(0,0);
}

function showGameOpenAnimation(elts, settings, callback) {
	
  elts.loading
    .css({
      //marginTop: settings.marginTopLoading+'px',
      top: '' +(settings.height/2 + gameTopMarginOffset)+ 'px',	  
      marginLeft: settings.marginLeftLoading+'px',
      height: '20px'
    })
    .show()
    .animate
    (
      {
          width: settings.width+'px'
        , marginLeft: settings.marginLeft+'px'
      }
      , 
      {
        duration: 500, 
        complete: function() 
        {
          elts.loading.animate(
            {
                height: settings.height+'px'
              , marginTop: settings.marginTop+'px'
            }
            , 
            {
                duration: 500
              , 
                complete: function() 
                {
                  elts.contentWrapper
                    .css
                    (
                      {
                          width: settings.width+'px'
                        , height: settings.height+'px'
                        , marginTop: settings.marginTop+'px'
                        , marginLeft: settings.marginLeft+'px'
                      }
                    )
                    .show()
                  ;
                  elts.loading.fadeOut(200, callback);
                }
            }
          );
        }
      }
    );
}

function loadFlash( p_url, p_params, p_width, p_height, p_fps, p_bgcolor ) {
	var flashPopup = document.getElementById( "flashpopup" );
	
	//need to ensure that the old DIV tag is gone
	swfobject.removeSWF( flashPopupContentID ); 
	var flashPopupContent = document.getElementById( flashPopupContentID );
	if(flashPopupContent)
	{
		flashPopup.removeChild( flashPopupContent );
	}		
	
	createFlashPopupContentContainer();	
	
	p_params += '&' + location.search.substring(1, location.search.length); //pass entire querystring as flashvars
	
	// popup loader
	// ===============================================
	var flashvars;
	var swfPath;
	
	switch(p_url)
	{
		case "digitalDJ":{
			flashvars = {
				apiUrl: "http://a.dolimg.com/media/en-US/dxd/code/minigame_api/MinigameAPI_2_2.swf",
				apiConfigUrl: "http://a.dolimg.com/media/en-US/dxd/data/minigameAPIConfig.xml",
				gameConfigUrl: "http://home.disney.go.com/dxd/dynamic/item?id=1909457"
			};
			swfPath = "http://a.dolimg.com/media/en-US/games/dos_tron_spl_mus_digitaldj/dos_tron_spl_mus_digitaldj.swf"; //mediaRoot + "digitaldjgame/games/dos_tron_spl_mus_digitaldj/MainStandalone.swf";//dos_tron_spl_mus_digitaldj.swf
			break;
		}
		case "arcadeGame":{
			flashvars = {
				apiUrl: "http://a.dolimg.com/media/en-US/dxd/code/minigame_api/MinigameAPI_2_2.swf",
				apiConfigUrl: "http://a.dolimg.com/media/en-US/dxd/data/minigameAPIConfig.xml",
				gameConfigUrl: "http://home.disney.go.com/dxd/dynamic/item?id=1636945"
			};
			swfPath = "http://a.dolimg.com/media/en-US/games/trn_spl_arc_tronarcade/trn_spl_arc_tronarcade.swf";
			break;
		}
		case "discsoftron":{
			flashvars = {
				apiUrl: "http://a.dolimg.com/media/en-US/dxd/code/minigame_api/MinigameAPI_2_2.swf",
				apiConfigUrl: "http://a.dolimg.com/media/en-US/dxd/data/minigameAPIConfig.xml",
				gameConfigUrl: "http://home.disney.go.com/dxd/dynamic/item?id=1322798"
			};
			swfPath = "http://a.dolimg.com/media/en-US/games/trn_spl_arc_discsoftron/trn_spl_arc_discsoftron.swf";
			break;
		}
		case "deadlydisc":{

			break;
		}
	}
	var params = { 
		scale: "exactfit",
		allowScriptAccess: "always",
		wmode: "opaque"
	};
	
	var attributes = {
		id: "game",
		name: "game"
	};
	
	//console.log( "swfobject.embedSWF : " + p_url );
	swfobject.embedSWF(swfPath, "game", "750", "500", "10.0.0", "expressInstall.swf", flashvars, params, attributes); 

}


function createFlashPopupContentContainer() {
	var flashPopupContent = document.getElementById( flashPopupContentID );
	if ( !flashPopupContent) {
		var flashPopupContent = document.createElement( "div" );
		flashPopupContent.setAttribute( "id", flashPopupContentID );
		document.getElementById( "flashpopup" ).appendChild( flashPopupContent );
	}		
}

function createEnlargeButton() {
	var enlargeBtn = document.getElementById( "enlargeBtn" );
	if ( !enlargeBtn ) {
		var enlargeBtn = document.createElement( "div" );
		enlargeBtn.setAttribute( "id", "enlargeBtn" );
		enlargeBtn.setAttribute( "class", "enlargeBtnBigScreen" );
		document.getElementById( "flashpopup" ).appendChild( enlargeBtn );			
		return true;
	}		
	return false;
}

function createCloseButton() {
	var closeBtnDiv = document.getElementById( "closeBtnDiv" );
	if ( !closeBtnDiv ) {
		var closeBtnDiv = document.createElement( "div" );
		closeBtnDiv.setAttribute( "id", "closeBtnDiv" );
		closeBtnDiv.setAttribute( "class", "nyroModalClose" );
		document.getElementById( "flashpopup" ).appendChild( closeBtnDiv );			
		
		closeBtnDiv.innerHTML  = '';
		return true;
	}
	return false;
}

