//
//     FLASH : POPCAPGAME
//

    popCapFlashGame.prototype = popCapGame.prototype;
    popCapFlashGame.prototype.constructor = popCapFlashGame;
    popCapFlashGame.prototype.baseClass = popCapGame.prototype.constructor;
    
    function popCapFlashGame(gameName, id, gameWidth, gameHeight, gameSwf, gameLayer)
    {
        prettyLog.log("popCapFlashGame: constructor", 3);
        popCapGame(gameName, id, gameWidth, gameHeight, gameLayer);
        
        popCapFlashGame.prototype.gameSwf = gameSwf;
    }
    
    popCapFlashGame.prototype.applyBasePath = function()
    {   
    //  this.basePath = this.pathConcat(this.basePath, '');
    //  this.gameSwf = this.pathConcat(this.basePath, this.gameSwf);
    };
    
    popCapFlashGame.prototype.write = function()
    {
        prettyLog.log("popCapFlashGame.write: writing to div", 3);
        this.applyBasePath();
    
        var flash_html = "";

        flash_html = '<div id="gamelayer">';
        flash_html += $.flash.create(
                    {
                        swf: this.gameSwf,
                        height: this.gameHeight,
                        width: this.gameWidth,
                        id: this.embedObject,
                        name: this.embedObject,
                        params: 
                        {
                            wmode: "window",
                            play: true,
                            loop: false,
                            menu: false,
                            quality: "high",
                            bgcolor: "#000000",
                            swLiveConnect: true,
							flashvars: "upsell_func=thePopCapGame.OnCustomEvent"
                        }
                    }
                );
      flash_html += '</div>';
 	  flash_html += this.writeListener();
      //alert(flash_html);
      document.write(flash_html);

   };

	popCapFlashGame.prototype.writeListener = function()
	{
		if ((packageSettings.isOldMSNAPI === undefined) || (packageSettings.isOldMSNAPI === false))
		{
			return "";
		}
		else
		{
			var listener = '<script language="JavaScript" type="text/javascript">\r\n';
			listener += 'function '+this.embedObject+'_DoFSCommand(method,params)\r\n';
			listener += '{\r\n';
			listener += '	thePopCapGame.receiveNotification(method,params);\r\n';
			listener += '}\r\n';
			listener += '</script>\r\n';
			listener += '<script for="'+this.embedObject+'" event="FSCommand(method,params)" language="JavaScript">\r\n';
			listener += this.embedObject+'_DoFSCommand(method,params);\r\n';
			listener += '</script>\r\n';
			return listener;
		}
	}

	popCapFlashGame.prototype.sendNotification = function(method)
	{
		if ((packageSettings.isOldMSNAPI !== undefined) && (packageSettings.isOldMSNAPI !== false))
		{
			$("#" + this.embedObject)[0].TCallLabel('/',method);
		}
	}
    
    popCapFlashGame.prototype.getParams = function()
    {    
        var tags = '';
    
        if (this.paramNames === undefined)
        {
            this.paramNames = [];
        }

        if (this.paramNames.length > 0)
        {
            for(var i = 0; i < this.paramNames.length-1; i++)
            {
                tags += this.paramNames[i]+'='+this.params[i]+'&';
            }
            tags += this.paramNames[this.paramNames.length-1]+'='+this.params[this.paramNames.length-1];
        }
        return tags;
    };
    
    popCapFlashGame.prototype.SessionStart = function()
    {
        prettyLog.log("popCapFlashGame.SessionStart: received", 3);
        if ((packageSettings.isOldMSNAPI !== undefined) && (packageSettings.isOldMSNAPI !== false))
        {
    		this.sendNotification('SessionStart','');
        }
		else
		{
			$("#" + this.embedObject)[0].onSessionStart();
		}
    };
    
    popCapFlashGame.prototype.GameStart = function()
    {
        prettyLog.log("popCapFlashGame.GameStart: received", 3);
        if ((packageSettings.isOldMSNAPI !== undefined) && (packageSettings.isOldMSNAPI !== false))
        {
    		this.sendNotification('GameStart','');
        }
		else
		{
			$("#" + this.embedObject)[0].onGameStart();
		}
        trackingGame.updateState("play");   
    };
    
    popCapFlashGame.prototype.GameContinue = function(params)
    {       
        prettyLog.log("popCapFlashGame.GameContinue: received", 3);
        if ((packageSettings.isOldMSNAPI !== undefined) && (packageSettings.isOldMSNAPI !== false))
        {
    		this.sendNotification('GameContinue','');
        }
		else
		{
	        $("#" + this.embedObject)[0].onGameContinue();
		}
        trackingGame.updateState("play");   
    };
    
    popCapFlashGame.prototype.GameMenu = function()
    {
        prettyLog.log("popCapFlashGame.GameMenu: received", 3);
        if ((packageSettings.isOldMSNAPI !== undefined) && (packageSettings.isOldMSNAPI !== false))
        {
    		this.sendNotification('GameMenu','');
        }
		else
		{
	        $("#" + this.embedObject)[0].onGameMenu();
		}
        trackingGame.updateState("idle");   
    };
    
    popCapFlashGame.prototype.CustomReturn = function(params)
    {
        var args = '';
        if (typeof(params) != 'undefined')
        {
            args = params.toString();
        }
            
        prettyLog.log("popCapFlashGame.CustomReturn: received", 3);
        if ((packageSettings.isOldMSNAPI !== undefined) && (packageSettings.isOldMSNAPI !== false))
        {
    			this.sendNotification('CustomReturn',params);
        }
		else
		{
	        $("#" + this.embedObject)[0].onCustomReturn(args);
		}
    };
    
    popCapFlashGame.prototype.Mute = function(isMute)
    {
        prettyLog.log("popCapFlashGame.Mute: received", 3);
        if ((packageSettings.isOldMSNAPI !== undefined) && (packageSettings.isOldMSNAPI !== false))
        {
			this.sendNotification(isMute ? 'MuteOn' : 'MuteOff','');
        }
		else
		{
	        $("#" + this.embedObject)[0].onGameMute(isMute ? 'on' : 'off'); 
		}
    };
    
    popCapFlashGame.prototype.Pause = function(isPause)
    {
        prettyLog.log("popCapFlashGame.Pause: received", 3);
        if ((packageSettings.isOldMSNAPI !== undefined) && (packageSettings.isOldMSNAPI !== false))
        {
			this.sendNotification(isPause ? 'PauseOn' : 'PauseOff','');
        }
		else
		{
	        $("#" + this.embedObject)[0].onGamePause(isPause ? 'on' : 'off');
		}
    };

	//
	//     INSTANTIATE GAME WHEN PAGE IS READY
	//

	 // initialize prettyLog

	 prettyLog.init();
	 prettyLog.log("main: initalizing game", 5);

	 // initialize the PopCapFlashGame

	 thePopCapGame = new popCapFlashGame(
	     packageSettings.gameName,
	     packageSettings.embedObject,
	     packageSettings.gameWidth,
	     packageSettings.gameHeight,
	     packageSettings.gameFile,
	     packageSettings.popCapGameLayer);
	 prettyLog.log("main: created a PopCapFlashGame Object for " + packageSettings.gameName, 5);

	 // create our adspot, which owns our PopCapFlashGame

	 theAdSpot = new adSpot(packageSettings.adSpotLayer, thePopCapGame);
	 prettyLog.log("main: created an adSpot Object", 5);

	 // set up all data values in these objects using packageSettings

	 theAdSpot.splashLayer     = packageSettings.splashLayer;
	 theAdSpot.adWidth         = packageSettings.adWidth;
	 theAdSpot.adHeight        = packageSettings.adHeight;
	 theAdSpot.adlayerWidth    = packageSettings.adlayerWidth;
	 theAdSpot.adlayerHeight   = packageSettings.adlayerHeight;
	 theAdSpot.gameWidth       = packageSettings.gameWidth;
	 theAdSpot.gameHeight      = packageSettings.gameHeight;
	 theAdSpot.ownerBgcolor    = packageSettings.gameBgColor;
	 theAdSpot.duration        = packageSettings.adDuration;
	 theAdSpot.showPreroll     = packageSettings.runPreRoll;
	 theAdSpot.showMidroll     = packageSettings.runMidRoll;
	 theAdSpot.showPostroll    = packageSettings.runPostRoll; 
	 theAdSpot.wait            = packageSettings.timeBetweenAds;
	
	 var env = $.urlParam("env");
 	 if (env == "live")
	 {

		 theAdSpot.prerollSrc      = packageSettings.adSrc.live.preRollSrc;
		 theAdSpot.midrollSrc      = packageSettings.adSrc.live.midRollSrc;
		 theAdSpot.postrollSrc     = packageSettings.adSrc.live.postRollSrc;
	     theAdSpot.upsellBarSrc    = packageSettings.adSrc.live.upsellBarSrc;
	 }
	 else
	 {
		 theAdSpot.prerollSrc      = packageSettings.adSrc.test.preRollSrc;
		 theAdSpot.midrollSrc      = packageSettings.adSrc.test.midRollSrc;
		 theAdSpot.postrollSrc     = packageSettings.adSrc.test.postRollSrc;
	     theAdSpot.upsellBarSrc    = packageSettings.adSrc.test.upsellBarSrc;
	 }

	 thePopCapGame.hide        = packageSettings.runPreRoll;
	 thePopCapGame.basePath    = packageSettings.basePath;
	 thePopCapGame.upsellUrl   = packageSettings.upsellURL;
	 thePopCapGame.enableUpsell= packageSettings.enableUpsell;
	 thePopCapGame.partnerName = packageSettings.partnerName;

	 prettyLog.log("main: configured the adSpot Object", 5);

	 // write to the div for the game

	 thePopCapGame.write();  
	 prettyLog.log("main: updated game layer div", 5);

	 // write to the div for the ad

	 theAdSpot.write();
	 prettyLog.log("main: updated ad layer div", 5);

	 $(document).ready(function() 
	 {
		// check to make sure we are in a parent window
     	//if ((packageSettings.isDebug === 0) && (parent.document.getElementById("debuglayer") !== null))
	 	//{
		//    window.location.replace("http://www.popcap.com/allgames.php?p=online");
	 	//}


        // so we can communicate with our advertisement	

	    document.domain = "popcap.com";

	    // start it up!

       	if($.flash.hasVersion(10.0))
        {
	      if (theAdSpot.canRunAsPreroll()) 
	      {
		    $(theAdSpot.splashLayer).html('<img src="' + packageSettings.splashScreen + '">');
			theAdSpot.whichLayer("splash");
	        setTimeout("theAdSpot.runAsPreroll()", 2000);
	      } 
	      else
	      {
	        theAdSpot.skipPreroll();
	      }
	    }
		else
		{
			theAdSpot.whichLayer("ad");
			var alternateContent = '<p style="background: #000; padding: 0 10px; margin: 175px 0 0 0; text-align: center; line-height: 50px; color: #fff;">This content requires the Adobe Flash Player 10. <a href=http://www.adobe.com/go/getflash/ target=_blank>Get Flash</a></p>';
			$(packageSettings.adSpotLayer).html(alternateContent);
	       	$('a').css("color", '#0f94d7');
			prettyLog.log("main: no flash, we are bailing!", 5);
			return;
		}
		
	    prettyLog.log("main: done setting up", 5);

	    // set up tracking

	    if (packageSettings.isTracking === true)
	    {
	       trackingGame.initTracking();
	    }   
	});


	$(window).unload(function() 
	{
	    // write to omniture
	    if (packageSettings.isOmniture === true)
	    {
	       trackingGame.reportOmniture();
	    }

	    // update big cookie
	    popCapAdSpots.updateWebGamesCookie(packageSettings.cookieDomain, packageSettings.cookieName, packageSettings.gameCode, trackingGame);
	});



