/////////////////////////
// SETTINGS
/////////////////////////

var flashPopupHeaderHeight = 26;
var flashPopupCloserID = "flashPopupCloser";
var flashPopupContentID = "flashPopupContent";
var htmlLoaderID = "htmlLoader";
var flashPopupPadding = 16;
var flashPopupCloseData = new Object();

var dimHeight = 0;
var globalNavHeaderHeight = 167;
var gameTopMarginOffset = 30;

var makeBigText = "BIG&nbsp;SCREEN";
var makeSmallText = "SMALL&nbsp;SCREEN";
var isEnlarged = false;
var isPlayingGame = false;
var readyToPlay = false;
var gameEnlargeScaleModifier = 1.2;

var gameType = "";
var unityExt = "unity3d";
var flashExt = "swf";

function queryString( parameter ) {
	var loc = location.search.substring(1, location.search.length);
	var param_value = false;
	var params = loc.split("&");
	for (i=0; i<params.length;i++) {
		param_name = params[i].substring(0,params[i].indexOf('='));
		if (param_name == parameter) {
			param_value = params[i].substring(params[i].indexOf('=')+1)
		}
	}
	if (param_value) {
		return param_value;
	} else {
		return '';
	}
}

function updateDimOverlay(p_height) {

}

function closeFlashPopup() {
	
	isPlayingGame = false;
	
	swfobject.removeSWF( flashPopupContentID );	
	swfobject.removeSWF( flashPopupCloserID );	
	
	removeUnity( flashPopupContentID );	
	
	$("#flashpopup").css("visibility",  "hidden");	
	
	document.getElementById( 'controller' ).closeFlashPopup(flashPopupCloseData);
	try {
		refreshYahooAds();
	} catch (error) {}
}


$(document).ready(function() {
	readyToPlay = true;	
});



function openFlashPopupNoEnlarge( p_url, p_params, p_width, p_height, p_fps, p_bgcolor, minWidthForDim, minHeightForDim, topOffset, leftOffset, minLeftOffset, closeData ) {

	//short circuit if they click too soon	
	if(!readyToPlay) return;
	if(isPlayingGame) return;
	
	$("#dimoverlay").css("visibility",  "visible");
		
	$(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.9, callback);
			
		},				
		
		beforeHideContent: function(elts, settings, callback) {
			closeFlashPopup();
			isEnlarged = false;
			callback();			
		},
		
		endRemove: function(elts, settings) {
			$("#dimoverlay").css("visibility",  "hidden");					
		},		
		
		cssOpt: {
			wrapper: {
				top: (p_height/2 + gameTopMarginOffset),
				width: (p_width + 20),
				height: (p_height + 20)
			}
		}
	  });
	});

	var flashpopup = document.getElementById( 'flashpopup' );
		
	minWidthForDim = minWidthForDim ? minWidthForDim : -1;
	minHeightForDim = minHeightForDim ? minHeightForDim : -1;

	loadFlash( p_url, p_params, p_width, p_height, p_fps, p_bgcolor, false, minWidthForDim, minHeightForDim );	
	
	document.getElementById( 'controller' ).openFlashPopup(true);
	
	// generate the data for closing popup so it can be passed to the controller when the popup is closed
	flashPopupCloseData = closeData;
		
	$('#flashpopup').height(p_height);
	$('#flashpopup').width(p_width);
	
/*
	if(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 - 57) + "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();
	
	$("#closeBtnDiv").click(function(){		
		$.nyroModalRemove();
	});
	
	//The following needs to be reset each game launch
	$('#enlargeBtn').html(makeBigText);		
	$("#closeBtnDiv").css("margin-left", (p_width/2 - 57) + "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");		
}


function openFlashPopup( p_url, p_params, p_width, p_height, p_fps, p_bgcolor, minWidthForDim, minHeightForDim, topOffset, leftOffset, minLeftOffset, closeData ) {

	//short circuit if they click too soon	
	if(!readyToPlay) return;
	if(isPlayingGame) return;
	
	$("#dimoverlay").css("visibility",  "visible");
		
	$(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.9, callback);
			
		},				
		
		beforeHideContent: function(elts, settings, callback) {
			closeFlashPopup();
			isEnlarged = false;
			callback();			
		},
		
		endRemove: function(elts, settings) {
			$("#dimoverlay").css("visibility",  "hidden");					
		},		
		
		cssOpt: {
			wrapper: {
				top: (p_height/2 + gameTopMarginOffset),
				width: (p_width + 20),
				height: (p_height + 20)
			}
		}
	  });
	});

	var flashpopup = document.getElementById( 'flashpopup' );
		
	minWidthForDim = minWidthForDim ? minWidthForDim : -1;
	minHeightForDim = minHeightForDim ? minHeightForDim : -1;

	loadFlash( p_url, p_params, p_width, p_height, p_fps, p_bgcolor, false, minWidthForDim, minHeightForDim );	
	
	document.getElementById( 'controller' ).openFlashPopup(true);
	
	// generate the data for closing popup so it can be passed to the controller when the popup is closed
	flashPopupCloseData = closeData;
		
	$('#flashpopup').height(p_height);
	$('#flashpopup').width(p_width);
	

	if(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 - 57) + "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();
	
	$("#closeBtnDiv").click(function(){		
		$.nyroModalRemove();
	});
	
	//The following needs to be reset each game launch
	$('#enlargeBtn').html(makeBigText);		
	$("#closeBtnDiv").css("margin-left", (p_width/2 - 57) + "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");		
}

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, p_close, minWidthForDim, minHeightForDim ) {
	
	var flashPopup = document.getElementById( "flashpopup" );
	
	//need to ensure that the old DIV tag is gone
	removeUnity( flashPopupContentID );	
	swfobject.removeSWF( flashPopupContentID ); 
	var flashPopupContent = document.getElementById( flashPopupContentID );
	if(flashPopupContent)
	{
		flashPopup.removeChild( flashPopupContent );
	}		
	
	createHtmlLoaderContainer();
	
	createFlashPopupContentContainer();	
	
	var gameType = p_url.substr(p_url.lastIndexOf( '.' ) + 1, 7).toLowerCase();
	
	p_params += '&' + location.search.substring(1, location.search.length); //pass entire querystring as flashvars
	
	// popup loader
	// ===============================================
	var attributes = {};
	attributes.bgColor = p_bgcolor;
	attributes.width = p_width;
	attributes.height = p_height;
	attributes.data = p_url;
	
	var basePath = p_url.substring( 0, p_url.lastIndexOf( '/' )+1 );
	attributes.base = basePath;		
	
	if(gameType == flashExt)
	{
		var flashvars = "swfPath=" + p_url + "&width=" + p_width + "&height=" + p_height + "&frameRate=" + p_fps + "&bgColour=" + p_bgcolor + "&" + p_params + "&showClose=" + p_close;
		
		var params = { flashvars:flashvars, base:basePath, bgColor:p_bgcolor };
		
		swfobject.createSWF( attributes, params, flashPopupContentID );
	}
	else if (gameType == unityExt)
	{
		createUnity(flashPopupContentID, p_url, p_width, p_height, p_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 createHtmlLoaderContainer() {
	var htmlLoader = document.getElementById( htmlLoaderID );
	
	if ( !htmlLoader ) {
		var htmlLoader = document.createElement( "div" );
		htmlLoader.setAttribute( "id", htmlLoaderID );
		document.getElementById( "flashpopup" ).appendChild( htmlLoader );
	}	
	
	htmlLoader.innerHTML  = "<img id='loaderImage' src='/global/img/loading.gif'/>";
}

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  = 'CLOSE';
		return true;
	}		
	return false;
}

//HIGH SCORES
function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    } else {
        return document[movieName];
    }
}

function refreshHighScores() {
	thisMovie("controller").refreshHighScores();
}
