﻿// ************************************* Browser component API functions **********************************************
// Private keys functions
function StoreKey(key,value) {
    var apiObj = _BCAPIHelper.getApiObj("storeKey");
    if (apiObj == null) _BCAPIHelper.executeApiFunction('EBStoreKey(' + key + ',' + value + ');');
    else if (apiObj != -1) apiObj.storeKey(key, value);
    else return _BCAPIHelper.UNSUPPORTED;
};

function RetrieveKey(key) {
    var apiObj = _BCAPIHelper.getApiObj("retrieveKey");
    if (apiObj == null) _BCAPIHelper.executeApiFunction('EBRetrieveKey(' + key + ');');
    else if (apiObj != -1) return apiObj.retrieveKey(key);
    else return _BCAPIHelper.UNSUPPORTED;
};

function DeleteKey(key) {
    var apiObj = _BCAPIHelper.getApiObj("deleteKey");
    if (apiObj == null) _BCAPIHelper.executeApiFunction('EBDeleteKey(' + key + ');');
    else if (apiObj != -1) apiObj.deleteKey(key);
    else return _BCAPIHelper.UNSUPPORTED;
};
// End Private keys functions

// Global Keys functions
function StoreGlobalKey(key, value) {
    var apiObj = _BCAPIHelper.getApiObj("storeGlobalKey");
    if (apiObj == null) _BCAPIHelper.executeApiFunction('EBStoreGlobalKey(' + key + ',' + value + ');');
    else if (apiObj != -1) apiObj.storeGlobalKey(key, value);
    else return _BCAPIHelper.UNSUPPORTED;
};

function RetrieveGlobalKey(key) {
    var apiObj = _BCAPIHelper.getApiObj("retrieveGlobalKey");
    if (apiObj == null) _BCAPIHelper.executeApiFunction('EBRetrieveGlobalKey(' + key + ');');
    else if (apiObj != -1) return apiObj.retrieveGlobalKey(key);
    else return _BCAPIHelper.UNSUPPORTED;
};

function DeleteGlobalKey(key) {
    var apiObj = _BCAPIHelper.getApiObj("deleteGlobalKey");
    if (apiObj == null) _BCAPIHelper.executeApiFunction('EBDeleteGlobalKey(' + key + ');');
    else if (apiObj != -1) apiObj.deleteGlobalKey(key);
    else return _BCAPIHelper.UNSUPPORTED;
};
// End Global Keys functions
    
function SelfNavigate(url, newSize) {
    if (typeof (newSize) == 'undefined') newSize = -1;
    
    var apiObj = _BCAPIHelper.getApiObj("selfNavigate");
    if (apiObj == null) _BCAPIHelper.executeApiFunction('EBNavigate(' + url + ',' + newSize + ');');
    else if (apiObj != -1) apiObj.selfNavigate(url, newSize);
    else return _BCAPIHelper.UNSUPPORTED;
};

function ChangeWidth(newWidth) {
    var apiObj = _BCAPIHelper.getApiObj("changeWidth");
    if (apiObj == null) _BCAPIHelper.executeApiFunction('EBChangeSize(' + newWidth + ');');
    else if (apiObj != -1) apiObj.changeWidth(newWidth);
    else return _BCAPIHelper.UNSUPPORTED;
};

function ChangeHeight(newHeight) {
    var apiObj = _BCAPIHelper.getApiObj("changeHeight");
    if (apiObj == null) _BCAPIHelper.executeApiFunction('EBChangeSize(' + myWidth + ',' + newHeight + ');');
    else if (apiObj != -1) apiObj.changeHeight(newHeight);
    else return _BCAPIHelper.UNSUPPORTED;
};

function ChangeSize(newWidth, newHeight) {
    var apiObj = _BCAPIHelper.getApiObj("changeSize");
    if (apiObj == null) _BCAPIHelper.executeApiFunction('EBChangeSize(' + newWidth + ',' + newHeight + ');');
    else if (apiObj != -1) apiObj.changeSize(newWidth, newHeight);
    else return _BCAPIHelper.UNSUPPORTED;
};

function RefreshToolbar() {
    var apiObj = _BCAPIHelper.getApiObj("refreshToolbar");
    if (apiObj == null) _BCAPIHelper.executeApiFunction('EBRefreshToolbar();');
    else if (apiObj != -1) apiObj.refreshToolbar();
    else return _BCAPIHelper.UNSUPPORTED;
};

function EBOpenPopHtml(url, newWidth, newHeight) {
	OpenFloatingWindow(url, newWidth, newHeight);
};

function OpenFloatingWindow(url, newWidth, newHeight) {
    var apiObj = _BCAPIHelper.getApiObj("openGadget");
    if (apiObj == null) _BCAPIHelper.executeApiFunction('EBOpenPopHtml(' + url + ',' + newWidth + ',' + newHeight + ');');
    else if (apiObj != -1) apiObj.openGadget(url, newWidth, newHeight);
    else return _BCAPIHelper.UNSUPPORTED;
};

function OpenGadget(url, width, height, features) {
    if (!features) features = "";
    var apiObj = _BCAPIHelper.getApiObj("openGadget2");
    if (apiObj == null) _BCAPIHelper.executeApiFunction('EBOpenGadget(\'' + url + '\',' + width + ',' + height + ',' + features + ');');
    else if (apiObj != -1) apiObj.openGadget2(url, width, height, features);
    else return _BCAPIHelper.UNSUPPORTED;
};

function CloseFloatingWindow() {
    var apiObj = _BCAPIHelper.getApiObj("closeWindow");
    if (apiObj == null) _BCAPIHelper.executeApiFunction('EBCloseFloatingWindow();');
    else if (apiObj != -1) apiObj.closeWindow();
    else return _BCAPIHelper.UNSUPPORTED;
};

function NavigateInMainFrame(strUrl) {
    var apiObj = _BCAPIHelper.getApiObj("navigateInMainFrame");
    if (apiObj == null) _BCAPIHelper.executeApiFunction('EBMainFrameNavigation(' + strUrl + ');');
    else if (apiObj != -1) apiObj.navigateInMainFrame(strUrl);
    else return _BCAPIHelper.UNSUPPORTED;
};

function GetMainFrameUrl() {
    var apiObj = _BCAPIHelper.getApiObj("getMainFrameUrl");
    if (apiObj == null) {
        if (!myIsAllowNonPrivacy) SetLastError(1);
        return mainFrameUrl;
    }
    else if (apiObj != -1) return apiObj.getMainFrameUrl();
    else return _BCAPIHelper.UNSUPPORTED;
};

function GetMainFrameTitle() {
    var apiObj = _BCAPIHelper.getApiObj("getMainFrameTitle");
    if (apiObj == null) {
        if (!myIsAllowNonPrivacy) SetLastError(1);
        return mainFrameTitle;
    }
    else if (apiObj != -1) return apiObj.getMainFrameTitle();
    else return _BCAPIHelper.UNSUPPORTED;
};

function GetCurrentHeight() {
    var apiObj = _BCAPIHelper.getApiObj("getHeight");
    if (apiObj == null) return myHeight;
    else if (apiObj != -1) return apiObj.getHeight();
    else return _BCAPIHelper.UNSUPPORTED;
};

function GetCurrentWidth() {
    var apiObj = _BCAPIHelper.getApiObj("getWidth");
    if (apiObj == null) return myWidth;
    else if (apiObj != -1) return apiObj.getWidth();
    else return _BCAPIHelper.UNSUPPORTED;
};

function GetSearchTerm() {
    var apiObj = _BCAPIHelper.getApiObj("getSearchTerm");
    if (apiObj == null) {
        if (!myIsAllowNonPrivacy) SetLastError(1);
        return recentSearchTerm;
    }
    else if (apiObj != -1) return apiObj.getSearchTerm();
    else return _BCAPIHelper.UNSUPPORTED;

};

function GetVersion() {
    var apiObj = _BCAPIHelper.getApiObj("getVersion");
    if (apiObj == null) return myVersion;
    else if (apiObj != -1) return apiObj.getVersion();
    else return _BCAPIHelper.UNSUPPORTED;
};

function RefreshComponentById(strCompId) {
    var apiObj = _BCAPIHelper.getApiObj("refreshComponentById");
    if (apiObj == null) return _BCAPIHelper.UNSUPPORTED;
    else if (apiObj != -1) return apiObj.refreshComponentById(strCompId);
    else return _BCAPIHelper.UNSUPPORTED;
};

function HandleHeight() {
    var apiObj = _BCAPIHelper.getApiObj("handleHeight");
    //toolbar not installed or toolbar doesn't support this function
    if (!apiObj || apiObj == -1) location.href = "http://hosting.conduit.com/UpdateClientComponent/Default.aspx?ct=EB_TOOLBAR_ID#_self";
};

function GetToolbarId() {
    var apiObj = _BCAPIHelper.getApiObj("getToolbarId");
    if (apiObj == null) return _BCAPIHelper.UNSUPPORTED;
    else if (apiObj != -1) return apiObj.getToolbarId();
    else return _BCAPIHelper.UNSUPPORTED;
};

function GetToolbarName() {
    var apiObj = _BCAPIHelper.getApiObj("getToolbarName");
    if (apiObj == null) return _BCAPIHelper.UNSUPPORTED;
    else if (apiObj != -1) return apiObj.getToolbarName();
    else return _BCAPIHelper.UNSUPPORTED;
};

function GetDownloadPageUrl() {
    var apiObj = _BCAPIHelper.getApiObj("getDownloadPageUrl");
    if (apiObj == null) return _BCAPIHelper.UNSUPPORTED;
    else if (apiObj != -1) return apiObj.getDownloadPageUrl();
    else return _BCAPIHelper.UNSUPPORTED;
};

function GetLastError() {
    var apiObj = _BCAPIHelper.getApiObj("lastError");
    if (apiObj == null) return myLastError;
    else if (apiObj != -1) return apiObj.lastError;
    else return _BCAPIHelper.UNSUPPORTED;
};

//BCAPIObject used for Safari MAC, that injects it before
// ************************************* Internal use functions **********************************************
var BCAPI = (typeof BCAPIObject != "undefined") ? BCAPIObject : new Object();

var _BCAPIHelper = {
    UNSUPPORTED: null,

    isIE: function() {
        return navigator.userAgent.indexOf("MSIE") != -1;
    },

    isFF: function() {
        return navigator.userAgent.indexOf("Firefox") != -1;
    },

    isSafari: function() {
        return navigator.userAgent.indexOf("Safari") != -1;
    },

    getApiObj: function(strFuncName) {
        SetLastError("");
        //FF or Safari
        if (BCAPI.storeKey) {
            if (BCAPI[strFuncName])
                return BCAPI;
            else
            //unsupported
                return -1;
        }
        //Not new FF API or Safari
        else {
            //New IE API
            try {
                if (this.isIE() && window.external) {
                    if (window.external.isSupportedFunction(strFuncName))
                        return window.external;
                    else
                    //unsupported
                        return -1;
                }
                //old FF API
                else
                    return null;
            }
            //old IE will return exception on "window.external"
            catch (ex) {
                return null;
            }
        }
    },

    GetUpgareUrl: function() {
        return "http://hosting.conduit.com/upgrade/?ct=EB_TOOLBAR_ID";
    },

    GetToolbarUnknownCommandApiFuncUrl: function(strApiFunc) {
        var strToolbarDomain = this.GetUpgareUrl();
        return strToolbarDomain + "&apifunc=" + strApiFunc;
    },

    //execute function for old API toolbars
    executeApiFunction: function(strFunction) {
        var strFunctionsWrapper = this.GetToolbarUnknownCommandApiFuncUrl(strFunction);
        location.assign(unescape(strFunctionsWrapper));
    }
};

//Helper functions
var recentSearchTerm = "";
var mainFrameUrl = "";
var mainFrameTitle = "";
var myWidth = "";
var myHeight = "";
var myVersion = "";
var myKey = "";
var myIsAllowNonPrivacy = true;
var myLastError = "";

function SetLastError(strError) {
    if (typeof BCAPI.lastError != "undefined")
        BCAPI.lastError = strError;
    else
        myLastError = strError;
};

function EBSetIsAllowNonPrivacyFunctions(strBool) {
    //for IE
    if (typeof strBool == "string") myIsAllowNonPrivacy = (strBool.toLowerCase() == "true") ? true : false;
    // for FF
    else myIsAllowNonPrivacy = strBool;
};

function EBSetVersion(version) {
    myVersion = version;
};

function EBSetCurrentWidth(newWidth) {
    myWidth = newWidth;
};

function EBSetCurrentHeight(newHeight) {
    myHeight = newHeight;
};

function EBSetSearchTerm(newSearchTerm) {
    recentSearchTerm = newSearchTerm;
};

function EBSetMainFrameUrl(newMainFrameUrl) {
    mainFrameUrl = newMainFrameUrl;
};

function EBSetMainFrameTitle(newMainFrameTitle) {
    mainFrameTitle = newMainFrameTitle;
};

function EBSetKey(strValue) {
    myKey = strValue;
};