<!--

/*
    FreePokerAlliance Game specific functions (depends on jquery)
    (c) 2009, MGame Solutions
*/

var fpaGame = function() {
	
	var swfID = 'FPA_Poker_Game',
		bannerFrameID = 'FPA_Banner_Frame',
		baseURL = 'http://clients.freepokeralliance.com',
		client = '',
		useBanner = true,
		useResizer = true,
		useFullWindow = true,
		locationExternal,
		roundExternal,
		apploadExternal,
		swfAppLoaded = false;

    function getInternetExplorerVersion() {
    	var rv = -1; // Return value assumes failure.
    	if (navigator.appName == 'Microsoft Internet Explorer')	{
    		var ua = navigator.userAgent;
    		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    		if (re.exec(ua) != null)
    			rv = parseFloat( RegExp.$1 );
    	}
    	return rv;
    }

    return {
        set_swfID: function(newval) {
            swfID = newval;
        },
        set_client: function(newval) {
        	client = newval;
        },
        set_bannerBaseURL: function(newval) {
        	bannerBaseURL = newval;
        },
        set_useResizer: function(newval) {
        	useResizer = newval;
        },
        set_useBanner: function(newval) {
        	useBanner = newval;
        },
        set_useFullWindow: function(newval) {
        	useFullWindow = newval;
        },
        set_locationExternal: function(newval) {
        	locationExternal = newval;
        },
        set_roundExternal: function(newval) {
        	roundExternal = newval;
        },
        set_apploadExternal:function(newval) {
        	apploadExternal = newval;
        },
        focusSWF: function() {
       		$(swfID).focus();
        },
        refreshBanner: function(locationId) {
        	if (locationId && useBanner) {
        		$('iframe#'+bannerFrameID).attr("src", baseURL+'/'+client+'/banner/'+locationId);
        	}
        },
        closeLocal: function() {
       		$('#'+swfID)[0].closeLocal();
        },
        resizeGame: function(newval) {
       		$('#'+swfID)[0].callResize(newval,$('#'+swfID).width(),$('#'+swfID).height());
        },
        setBannerPos: function() {
    	    if (useBanner) {
    	    	bannertop = Math.floor(($(window).height() - $('#'+bannerFrameID).height()) / 2);
    	    	if (bannertop<0) bannertop = 0;
    	    	$('#'+bannerFrameID).parent().css("top", bannertop+"px");
    	    	$('#'+bannerFrameID).parent().css("right", "2px");
    	    }
        },
        setGameSize: function() {
        	var bannerwidth = 0;
    		if (useBanner)
    			bannerwidth = $('#'+bannerFrameID).width() + 4; // +4 because of the 2px borders
    		if (useFullWindow) {
    			$('#'+swfID).width($(window).width() - bannerwidth);
    			$('#'+swfID).height($(window).height());
    		}
        },
        resizeHandler: function() {
        	if (useResizer) {
        		var bannerwidth = 0,
        			bannertop = 0,
        			scale = 0,
        			s = 'w: '+$(window).width()+' x '+$(window).height();
        		if (useBanner) {
        			bannerwidth = $('#'+bannerFrameID).width() + 4; // +4 because of the 2px borders
        			s+=' b: '+bannerwidth;
        		}
        	    scale = Math.round($(window).width() - bannerwidth);
        	    if (scale<1) scale=1;
        	    scale = scale/996;
        	    if (720*scale > $(window).height())
        	    	scale = $(window).height()/720;
        	    this.setBannerPos();
        	    this.setGameSize();
        	    this.resizeGame(scale);
        	}
        },
        locationChange: function(newval) {
        	// newval can be: visit / login / lobby / table / round
        	switch (newval) {
        		case 'visit':
        		case 'login':
        		case 'lobby':
        		case 'table':
        		case 'round':
        			this.refreshBanner(newval);
        			break;
        		default:
        			// do nothing
        			break;
        	}
        	if (locationExternal)
        		return locationExternal.call(this,'/flash/'+client+'/'+newval);
        },
        roundChange: function(newval) {
        	if (roundExternal)
        		return roundExternal.call(this,newval);
        	// currently we use here the same event as the location change, this may change in the future
        	this.locationChange('round');
        },
        init: function(swfurl, width, height, version, flashvars, params, attributes) {
        	swfobject.embedSWF(swfurl, swfID, width, height, version, "/expressInstall.swf", flashvars, params, attributes, this.setGameSize);
            $('#'+swfID).show();
            window.onbeforeunload = this.closeLocal;
            this.focusSWF();
    	    if (useBanner)
    	    	this.setBannerPos();
    	    if (useResizer) {
    	    	$(window).resize(function() {
    	    		fpaGame.resizeHandler();
    	    	});
    	    }
    	    this.locationChange('visit');
        },
        displayTitle: function(newval) {
        	if (newval) {
        		document.title = newval;
                if (!swfAppLoaded && useResizer) {
                    this.resizeHandler();
                    // ie7 + ffox2 fix follows...
                    setTimeout("fpaGame.resizeHandler()",50);
                }
                if (!swfAppLoaded && apploadExternal)
                	apploadExternal.call(this);
                swfAppLoaded = true;
        	}
        }
    };
}();

//-->

