﻿var persistantExtraWindow = null;
var persistantSportsWindow = null;
var persistantHelpWindow = null;
var persistantGameWindow = null;
var persistantBingoWindow = null;
var persistantCasinoWindow = null;
var persistantAffWindow = null;
var persistantChatWindow = null;

function PerformNavigation(appPath,
    loggedIn,
    newWindowName,
    linkType,
    launchType,
    sourcePath,
    width,
    height,
    classId,
    requiredFlashVersion,
    destination)
{    
    var returnValue = false;    
    if((destination != null) && (destination.length > 0))
    {
        try 
        { 
            LogTrackingEvent(appPath, linkType, sourcePath);
        }
        catch(err)
        {
        }
        
        var sHeight = GetScreenRes(); 
        var sLeft = GetScreenWidth(); 
        var sTop = GetScreenTop();
        
        var popupParameters;
            
        switch(launchType)
        {
            case 4:
                popupParameters = "height=" + height + ",width=" + width + ",status=yes,toolbar=yes,menubar=yes,location=yes,left=" + sLeft + ",top=" + sTop + ",scrollbars=yes,resizable=yes";
                break;
            default:   
                popupParameters = "height=" + height + ",width=" + width + ",status=yes,toolbar=no,menubar=no,location=no,left=" + sLeft + ",top=" + sTop + ",scrollbars=yes,resizable=yes";
        }
        
        if(launchType == 1)
        {
            window.location=destination;
        }
        else if(launchType == 3)
        {
            RedirectParentWindow(classId, destination, width, height);
        }
        else
        {

            switch(linkType)
            {                  
                case 2: // sports
                case 11:
                                   
                    var persitentWindowToUse;
                    var persitentWindowString;
                    
                    if(linkType == 11)
                    {
                        persitentWindowToUse = persistantExtraWindow;
                        persitentWindowString = "persistantExtraWindow";
                    }
                    else if(newWindowName == "MMM")
                    {
                        persitentWindowToUse = persistantSportsWindow;
                        persitentWindowString = "persistantSportsWindow";
                    }
                    else if(newWindowName == "AAA")
                    {
                        persitentWindowToUse = persistantAffWindow;
                        persitentWindowString = "persistantAffWindow";
                    }
                    else if(newWindowName == "CCC")
                    {
                        persitentWindowToUse = persistantChatWindow;
                        persitentWindowString = "persistantChatWindow";
                    }
                    else
                    {
                        persitentWindowToUse = persistantHelpWindow;
                        persitentWindowString = "persistantHelpWindow";
                    }
                                        
                    if(persitentWindowToUse != null)
                    { 
                        try 
                        { 
                            persitentWindowToUse.close(); 
                        } 
                        catch(err)
                        {
                        }
                    } 
                    try 
                    { 
                        persitentWindowToUse = window.open(destination, newWindowName, popupParameters);
                    }
                    catch(err)
                    {
                        persitentWindowToUse = window.open(destination, '_blank', popupParameters);
                    } 
                    if(persitentWindowToUse != null)
                    {             
                        if(linkType == 11)
                        {
                            persistantExtraWindow = persitentWindowToUse;
                        }
                        else if(newWindowName == "MMM")
                        {
                            persistantSportsWindow = persitentWindowToUse;
                        }
                        else if(newWindowName == "AAA")
                        {
                            persistantAffWindow = persitentWindowToUse;
                        }
                        else if(newWindowName == "CCC")
                        {
                            persistantChatWindow = persitentWindowToUse;
                        }
                        else
                        {
                            persistantHelpWindow = persitentWindowToUse;
                        }        
                        var browser = GetBrowser();
                        if(browser == "IE8")
                        {
                            setTimeout("try{" + persitentWindowString + ".focus();}catch(err){};", 500);
                        }
                        else
                        {
                            setTimeout("try{" + persitentWindowString + ".blur(); " + persitentWindowString + ".focus();}catch(err){};", 500);
                        }
                    }          
                    break;     
                    
                case 15:
                case 6: // games launch
                    
                    CheckFlashAvailableAndLaunchGame(requiredFlashVersion, destination, newWindowName, height, width, loggedIn);
                    break;     
                             
                default:  
                
                   window.open(destination, newWindowName, popupParameters);      
                   
            }   
        }     
    }
    else
    {
        returnValue = true;
    }
    return returnValue;
}

function CheckFlashAvailableAndLaunchGame(requiredFlashVersion, destinationURL, targetName, height, width, isLI)
{
    var hasRequestedVersion = swfobject.hasFlashPlayerVersion(requiredFlashVersion);

    if(!hasRequestedVersion)
    {
        //flash not available so trigger modal popup
             
        var modalPopupObj = $find("NoFlashPopupBehavior");
        if(modalPopupObj != null)
        {
            modalPopupObj.hide();
        }         

        showNoFlashPrompt();

        //run twice to avoid instances after the first popup miss aligning
        if(modalPopupObj != null)
        {
            modalPopupObj.hide();
        }

        showNoFlashPrompt();
    }
    else
    {
        //flash available so launch game popup
        var sHeight = GetScreenRes(); 
        var sLeft = GetScreenWidth();
        var sTop = GetScreenTop();
        
        if (!isLI)
        {
            if(persistantGameWindow != null)
            { 
                try 
                {                   
                    persistantGameWindow.close(); 
                } 
                catch(err) {}; 
             } ;
             
             try
             {
                var gamesWindow = window.open(destinationURL, targetName, 'height=' + height + ',width=' + width + ',status=yes,toolbar=no,menubar=no,location=no,left=' + sLeft + ',top=' + sTop + ',scrollbars=yes,resizable=yes') ;        
             }
             catch(err)
             {
                var gamesWindow = window.open(destinationURL, '_blank', 'height=' + height + ',width=' + width + ',status=yes,toolbar=no,menubar=no,location=no,left=' + sLeft + ',top=' + sTop + ',scrollbars=yes,resizable=yes') ;        
             }
             
             persistantGameWindow=gamesWindow; 
             
            if(persistantGameWindow != null)
            {
                var browser = GetBrowser();
                if(browser == "IE8")
                {
                    setTimeout("try{persistantGameWindow.focus();}catch(err){};", 500);                
                }
                else
                {
                    setTimeout("try{persistantGameWindow.blur(); persistantGameWindow.focus();}catch(err){};", 500);                
                }
            }
        }
        else
        {
            window.open(destinationURL, '_blank', 'height=' + height + ',width=' + width + ',status=yes,toolbar=no,menubar=no,location=no,left=' + sLeft + ',top=' + sTop + ',scrollbars=yes,resizable=yes') ;        
        }
    }

    return false;
}
