﻿var loggedIn;
var oldCmsUrl;
var pageTitle = 'Spreadex';

var pageMgr = {
    pageChangedEvent: [],
    initialised: false,
    onPageChanged: function() {
        jQuery.each(this.pageChangedEvent, function(i, handler) {
            handler();
        });
    },
    initialise: function() {
        if (!this.initialised) {
            this.initialised = true;
        }
    },
    registerPageChangeHandler: function(cb) {
        this.pageChangedEvent.push(cb);
    },
    navigateToPageViaHash: function(pageType, parameter) {
        this.initialise();
        $.address.value('/' + pageType + '/' + new Date().getTime().toString() + '/' + parameter);
    },
    navigateToSecurePageViaHash: function(pageType, parameter) {
        this.initialise();
        $.address.value('/' + pageType + '/' + new Date().getTime().toString() + '/' + parameter);
    }
}

$.address.change(function(event) {
    //alert("address change: " + event.path);
    var pathParts = event.path.split('/');
    var pageType = pathParts[1];
    if (pageType == "tcl") {
        if (ignoreTradeClientUpdates == false) {
            ignoreNextTradeClientUpdate = true;
            sendCommand('cmd=SWITCH|to=' + pathParts[3]);
        }
    }
    else if (pageType == "cms") {
        showCmsPage();
    }
    else if (pageType == "lcms") {
        showIframe(oldCmsUrl + pathParts[3]);
    }
    else if (pageType == "ifp") {
        var iframeUrl = "/" + event.path.substring(event.path.indexOf('/', 5) + 1) + "?" + event.queryString;
        if (iframeUrl.substr(0, 2) == "//")
            iframeUrl = iframeUrl.substring(1);
        showIframe(publicServer + iframeUrl);
        $.address.title(pageTitle);
    }
    else if (pageType == "ifs") {
        var iframeUrl = "/" + event.path.substring(event.path.indexOf('/', 5) + 1) + "?" + event.queryString;
        if (iframeUrl.substr(0, 2) == "//")
            iframeUrl = secureServer + iframeUrl.substring(1);
        showIframe(iframeUrl);
        $.address.title(pageTitle);
    }
    else if (pageType == "sfpp") {
        displayHtml();
        pricingPage.loadPage(pathParts[3], $('#htmldiv'));
        window.scrollTo(0, 0);
    }
    else if (pageType == "sfub") {
        displayHtml();
        closeUnsettledBetsPage.loadUnsettledBetsPage($('#htmldiv'));
    }
    else if (pageType == "sfmm") {
        displayHtml();
        moveMoneyPage.loadMoveMoneyPage($('#htmldiv'));
    }
    else if (pageType == "sfat") {
        displayHtml();
        accountTransactionsPage.loadAccountTransactionsPage($('#htmldiv'));
    }
    else if (pageType == "sfsb") {
        displayHtml();
        settledBetsPage.loadSettledBetsPage($('#htmldiv'));
    }
    else if (pageType == "sfdg") {
        displayHtml();
        diagnosticsPage.loadPage($('#htmldiv'));
    }
    else if (pageType == "sflg") {
        displayHtml();
        session.showLoginGoBack(function() {
            // if successful, go back too
            history.go(-1);
        });
    }
    else if (event.path.length < 2) {
        showCmsPage();
    }
});

function navigateToPageViaHash(pageType, parameter) {
    pageMgr.navigateToPageViaHash(pageType, parameter);
}
function navigateToSecurePageViaHash(pageType, parameter) {
    pageMgr.navigateToSecurePageViaHash(pageType, parameter);
}

function loadOldCmsPage(pageKey) {
    displayHtml();
    navigateToPageViaHash('lcms', pageKey);
}

function openNewWindow(url) {
    var win = window.open(url);

    if (win && win.open && !win.closed) {
        // success, window opened
    }
    else {
        // failed, popup blocker might have stopped it; call back into
        // the flash and get it to open the window
        var topmovie = getFlashMovieObject("topmovie");
        if (topmovie.openWindowFromSwf(url) == false) {
            window.location.href = url;
        }
    }
}

function openNewWindowWithParams(url, windowName, windowFeatures, doFocus) {
    var win = window.open(url, windowName, windowFeatures);

    if (win && win.open && !win.closed) {
        // success, window opened
        if (doFocus) {
            win.focus();
        }
    }
    else {
        // failed, popup blocker might have stopped it; call back into
        // the flash and get it to open the window
        var topmovie = getFlashMovieObject("topmovie");
        if (topmovie.openWindowFromSwf(url) == false) {
            window.location.href = url;
        }
    }
}

function showStatsPage(statsUrl) {

    openNewWindowWithParams(
    statsUrl,
    "stats",
    "scrollbars=1,height=720 width=750,left=10,top=10,titlebar=0,status=0,toolbar=0,menubar=0,location=0");

}

function loadOAO() {
    openNewWindow(oaoUrl);
}

// The following JS funcs are called by the Flash/Flex
function showAppropriatenessTest() {
    displayIframe('/websupport/surveys/ApprovalQuestionaire.aspx');
}
function showForgotPasswordPage() {
    displayIframe('/websupport/accountmanagement/PasswordReminder.aspx');
}
function showRegisterForOnlineAccessPage() {
    displayIframe('/websupport/accountmanagement/RegisterForOnlineAccess.aspx');
}
function showRequestInformationPage() {
    displayIframe('/websupport/accountmanagement/RequestFurtherInfo.aspx');
}
function showStatementsPage() {
    displayIframe('/websupport/accountmanagement/Statements.aspx');
}
function showWithdrawFundsPage() {
    displayIframe('/websupport/accountmanagement/WithdrawFunds.aspx');
}
function showDepositFundsPage() {
    openNewWindow(secureServer + '/websupport/accountmanagement/CreditAccount.aspx', '');
}
function userLoggedIn() {
    loggedIn = true;
    if (cmsCurrentlyDisplayed) {
        // switch to Logged In version of home page
        loadMainCmsPage();
    }
}
function userLoggedOut() {
    loggedIn = false;
    loadMainCmsPage();
}
function serverVersionChanged() {
    window.location.reload();
}
function siteTracker(counterName) {
    urchinTracker(counterName);
}
function setFocusToLogin() {
    window.focus();
    getElement('topmovie').focus();
}
function displayTradeChart(marketId, accountNo) {
    var chartUrl = publicServer + '/Chart.aspx?accno=' + accountNo + '&mid=' + marketId;
    openNewWindow(chartUrl);
}


function navigateToMyAccountPage() {
    navigateToPageViaHash('tcl', 'SUMMARY');
}
function loadMainCmsPage() {
    if (window.location.hash.length > 0) {
        navigateToPageViaHash('cms', '');
    }
    else {
        showCmsPage();
    }
}
function displayIframe(url) {
    navigateToPageViaHash('ifp', url);
}
var ignoreTradeClientUpdates = false;
var ignoreNextTradeClientUpdate = false;
function notifyViewChanged(viewName, parameterList, title) {
    if (ignoreNextTradeClientUpdate) {
        ignoreNextTradeClientUpdate = false;
    }
    else if ((viewName != "NULL") && (ignoreTradeClientUpdates == false)) {
        ignoreTradeClientUpdates = true;
        var addressPath = viewName;
        if ((parameterList != null) && (parameterList != "null")) {
            addressPath = addressPath + "|" + parameterList;
        }
        navigateToPageViaHash('tcl', addressPath);
        ignoreTradeClientUpdates = false;
    }
    $.address.title(pageTitle);
    window.scrollTo(0, 0);
}

function getTradeClientCommandFromBackButtonHash() {
    if (window.location.hash.indexOf('/tcl') >= 0) {
        // this is the initial page startup view, so ignore the trade client
        // when it tells us it has successfully switched to it
        ignoreNextTradeClientUpdate = true;
        return 'cmd=SWITCH|to=' + window.location.hash.split('/')[3];
    }
    return '';
}
// work around IE bug where the page title changes to # when you click on a flash movie
document.onpropertychange = function() {
    if (document.title != pageTitle) {
        document.title = pageTitle;
    }
} 
