﻿var hdp = hdp || {};

hdp.flashManager = {
	prevSlideWasFlashGame : false,
	navigate : function() {
        if($('div.hdp-active-slide').length !== 0) {
		    var blnNewSlideIsFlashGame = $('div.hdp-active-slide #ucl-star-match-no-flash').length === 1;
		    if (blnNewSlideIsFlashGame && !hdp.flashManager.prevSlideWasFlashGame) {
			    // Navigated to the Flash game
			    // If no flash load it - pageLoadCallBack is in ucl-flash-game-js.ascx
			    if ($('div#ucl-star-match-no-flash').length == 1) { pageLoadCallBack(true); }
			    // Otherwise tell it to start
			    else {
				    var flashObj = swfobject.getObjectById('ucl-star-match-no-flash');
				    if (flashObj && typeof(flashObj.SetVariable) != 'undefined') {
					    // Unfortunately, due to cross-domain security issues, the following call
					    // always fails with an exception: "Error calling method on NPObject!".
					    // A possible work-around is to call "System.security.allowDomain('*');"
                        try {
					        flashObj.SetVariable('activePane', 'true');
                        }
                        catch(e) {
	                        /* might fail due to  the above mentioned security exception:
	                            as an emergency measure replace the flash with a fresh reload                            */
	                        pageLoadCallBack(true);
                        }
				    }
			    }
		    } else if (!blnNewSlideIsFlashGame && hdp.flashManager.prevSlideWasFlashGame) {
			    // Navigated away from the Flash game
			    // Totally removing the Flash is the behaviour I was asked to implement
			    // rather than, for instance, just telling it to stop
			    $('object#ucl-star-match-no-flash').replaceWith('<div id="ucl-star-match-no-flash">&nbsp;</div>');
		    }

		    hdp.flashManager.prevSlideWasFlashGame = blnNewSlideIsFlashGame;

		    // hide the country selector for non-IE browsers if the game is visible
		    if(hdp.countryList && hdp.countryList.showHide) { hdp.countryList.showHide(); }
        }
	}
};
