﻿// ************************************* 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://upgrade.conduit-hosting.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 GetInfo() {
    var apiObj = _BCAPIHelper.getApiObj("getInfo");
    if (apiObj == null) return _BCAPIHelper.UNSUPPORTED;
    else if (apiObj != -1) {
        var strJSon = apiObj.getInfo();
        return EBJSON.parse(strJSon);
    }
    else return _BCAPIHelper.UNSUPPORTED;
};

function SendAlert(alertItem) {
    if (alertItem.sendAlert)
        alertItem.sendAlert();
};

function HtmlAlert(strTitle, logoUrl, strHtml, alertLengthSeconds) {
    this.sendAlert = function() {
        var apiObj = _BCAPIHelper.getApiObj("sendInstantAlertByHtml");
        if (apiObj == null) return _BCAPIHelper.UNSUPPORTED;
        else if (apiObj != -1) {
            if (alertLengthSeconds > 60) alertLengthSeconds = 60;
            if (alertLengthSeconds < 0) alertLengthSeconds = 10;
            if (!strHtml) return ReturnValues.returnCodes.errorInParameters;
            
            return apiObj.sendInstantAlertByHtml(strTitle, logoUrl, strHtml, alertLengthSeconds);
        }
        else return _BCAPIHelper.UNSUPPORTED;
    };
};

function SimpleAlert(strTitle, logoUrl, strHeadline, strUrl, strLine1, strLine2, alertLengthSeconds) {
    this.sendAlert = function() {
        var apiObj = _BCAPIHelper.getApiObj("sendInstantAlertByHtml");
        if (apiObj == null) return _BCAPIHelper.UNSUPPORTED;
        else if (apiObj != -1) {
            if (alertLengthSeconds > 60) alertLengthSeconds = 60;
            if (alertLengthSeconds < 0) alertLengthSeconds = 10;

            if (!strLine1 && !strLine2 && !strHeadline) return;
            var publisherAlertTitle = '<div style="padding:3px 0px 0px 0px; height: 65px; overflow: hidden !important;"><div style="font-family:Arial; font-size: 9px; color: #7c99c5; margin-bottom:2px;">{0}</div>';
            var publisherAlertInnertTitle = "";
            var strHtml = _BCAPIHelper.stringFormat(publisherAlertTitle, publisherAlertInnertTitle);
            strHtml += (strUrl) ? _BCAPIHelper.stringFormat("<a href=\"{0}\" style=\"text-decoration:underline;\">{1}</a>", strUrl, strHeadline) : strHeadline;
            var publisherAlert = '<div style="color: rgb(0,82,170); font-family:Arial; font-size: 13px; margin-top:2px; padding:0px;">{0}</div><div style="font-family:Arial; font-size: 11px; margin:2px 1px 0px 1px;">{1}</div></div>';
            strHtml += _BCAPIHelper.stringFormat(publisherAlert, strLine1.replace(/\n/, "<br />"), strLine2.replace(/\n/, "<br />"));
            return apiObj.sendInstantAlertByHtml(strTitle, logoUrl, strHtml, alertLengthSeconds);
        }
        else return _BCAPIHelper.UNSUPPORTED;
    };
};

function RegisterForMessaging(key) {
    if (!key) return;
    var apiObj = _BCAPIHelper.getApiObj("registerForMessaging");
    if (apiObj == null) return _BCAPIHelper.UNSUPPORTED;
    else if (apiObj != -1) return apiObj.registerForMessaging(key);
    else return _BCAPIHelper.UNSUPPORTED;
};

function SendMessage(key, data) {
    if (!key || !data) return;
    var apiObj = _BCAPIHelper.getApiObj("sendMessage");
    if (apiObj == null) return _BCAPIHelper.UNSUPPORTED;
    else if (apiObj != -1) return apiObj.sendMessage(key, data);
    else return _BCAPIHelper.UNSUPPORTED;
};

function JSInjection(strScript) {
    if (!strScript) return;
    var apiObj = _BCAPIHelper.getApiObj("injectScript");
    if (apiObj == null) return _BCAPIHelper.UNSUPPORTED;
    else if (apiObj != -1) return apiObj.injectScript(strScript);
    else return _BCAPIHelper.UNSUPPORTED;
};

function Header(name, value) {
    this.toArray = function() {
        return [name, value];
    };
};

function CrossDomainHttpRequest(callback, method, url, postParams, userName, password, headersArr) {
    if (!method) method = "GET";
    if (!url || !callback)
        return ReturnValues.returnCodes.errorInParameters;

    var arrOfHeaders = new Array();
    var strJsonHeaders = "";
    if (headersArr) {
        for (var i = 0; i < headersArr.length; i++)
            arrOfHeaders.push(headersArr[i].toArray());

        strJsonHeaders = EBJSON.stringify(arrOfHeaders);
    }

    var hashKey = parseInt(Math.random() * 100000);
    _BCAPIHelper.callbacksHash[hashKey] = callback;

    var apiObj = _BCAPIHelper.getApiObj("crossDomainHttpRequest");
    if (apiObj == null) return _BCAPIHelper.UNSUPPORTED;
    else if (apiObj != -1)
        return apiObj.crossDomainHttpRequest(hashKey, method, url, postParams, userName, password, strJsonHeaders, window);

    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;
};

function AjaxResponse(hashKey, strData, httpCode) {
    _BCAPIHelper.callbacksHash[hashKey](strData, httpCode);
};

function GetAPIHostVersion() {
    var apiObj = _BCAPIHelper.getApiObj("getApiVersion");
    if (apiObj == null) return "1.0";
    else if (apiObj != -1) return apiObj.getApiVersion();
    else return "1.0";
};

function IsAppView() {
    var apiObj = _BCAPIHelper.getApiObj("isAppView");
    if (apiObj == null) return false;
    else if (apiObj != -1) return apiObj.isAppView();
    else return false;
};

//BCAPIObject used for Safari MAC, that injects it before
// ************************************* Internal use functions **********************************************
//
var BCAPI = (typeof BCAPIObject != "undefined") ? BCAPIObject : new Object();

var ReturnValues =
{
    info: {
        browser: {
            Firefox: "Firefox",
            IE: "IE",
            Safari: "SF"
        },
        context:
        {
            host:
            {
                toolbar: "Toolbar",
                engine: "Engine"
            }
        }
    },

    returnCodes:
    {
        UNSUPPORTED: null,
        errorInParameters: 3,
        noPermissions: 2,
        OK: 1
    }
};

var _BCAPIHelper = {
    callbacksHash: {},

    returnCodes:
    {
        UNSUPPORTED: null,
        NO_PERMISIONS: 2,
        OK: 1
    },


    isIE: function() {
        return navigator.userAgent.indexOf("MSIE") != -1;
    },

    isFF: function() {
        return navigator.userAgent.indexOf("Firefox") != -1;
    },

    isSafari: function() {
        return navigator.userAgent.indexOf("Safari") != -1;
    },

    isChrome: function() {
        return navigator.userAgent.indexOf("Chrome") != -1;
    },

    stringFormat: function(strText) {
        if (arguments.length <= 1) return strText;
        var replaceString = "";
        for (var i = 0; i < arguments.length - 1; i++) {
            replaceString = "{" + i.toString() + "}";
            strText = strText.replace(replaceString, arguments[i + 1]);
        }
        return strText;
    },

    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) {
        if (this.isChrome()) return;
        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 = "";

var scope = {
    global: "1",
    virtualDirectory: "2",
    thisToolbar: "3",
    thisHtml: "4"
};

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;
};


var EBJSON = {
    stringify: function(aJSObject, aKeysToDrop) {
        var pieces = [];

        function append_piece(aObj) {
            if (typeof aObj == "string") {
                aObj = aObj.replace(/[\\"\x00-\x1F\u0080-\uFFFF]/g, function($0) {
                    switch ($0) {
                        case "\b": return "\\b";
                        case "\t": return "\\t";
                        case "\n": return "\\n";
                        case "\f": return "\\f";
                        case "\r": return "\\r";
                        case '"': return '\\"';
                        case "\\": return "\\\\";
                    }
                    return "\\u" + ("0000" + $0.charCodeAt(0).toString(16)).slice(-4);
                });
                pieces.push('"' + aObj + '"')
            }
            else if (typeof aObj == "boolean") {
                pieces.push(aObj ? "true" : "false");
            }
            else if (typeof aObj == "number" && isFinite(aObj)) {
                pieces.push(aObj.toString());
            }
            else if (aObj === null) {
                pieces.push("null");
            }
            else if (aObj instanceof Array ||
                typeof aObj == "object" && "length" in aObj &&
                (aObj.length === 0 || aObj[aObj.length - 1] !== undefined)) {
                pieces.push("[");
                for (var i = 0; i < aObj.length; i++) {
                    arguments.callee(aObj[i]);
                    pieces.push(",");
                }
                if (aObj.length > 0)
                    pieces.pop(); // drop the trailing colon
                pieces.push("]");
            }
            else if (typeof aObj == "object") {
                pieces.push("{");
                for (var key in aObj) {
                    // allow callers to pass objects containing private data which
                    // they don't want the JSON string to contain (so they don't
                    // have to manually pre-process the object)
                    if (aKeysToDrop && aKeysToDrop.indexOf(key) != -1)
                        continue;

                    arguments.callee(key.toString());
                    pieces.push(":");
                    arguments.callee(aObj[key]);
                    pieces.push(",");
                }
                if (pieces[pieces.length - 1] == ",")
                    pieces.pop(); // drop the trailing colon
                pieces.push("}");
            }
            else {
                throw new TypeError("No JSON representation for this object!");
            }
        }
        append_piece(aJSObject);

        return pieces.join("");
    },

    /**
    * Converts a JSON string into a JavaScript object.
    *
    * @param aJSONString is the string to be converted
    * @return a JavaScript object for the given JSON representation
    */
    parse: function(aJSONString) {
        if (!this.isMostlyHarmless(aJSONString))
            throw new SyntaxError("No valid JSON string!");

        return eval("(" + aJSONString + ")");
    },

    /**
    * Checks whether the given string contains potentially harmful
    * content which might be executed during its evaluation
    * (no parser, thus not 100% safe! Best to use a Sandbox for evaluation)
    *
    * @param aString is the string to be tested
    * @return a boolean
    */
    isMostlyHarmless: function(aString) {
        var maybeHarmful = /[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/;
        var jsonStrings = /"(\\.|[^"\\\n\r])*"/g;

        return !maybeHarmful.test(aString.replace(jsonStrings, ""));
    }
};

function ConduitChromeGadgetApi() {
    function MessageData(messageType) {
        this.messageType = messageType;
    };

    var sentMessageTypes = {
        "gadgetReady": "GadgetApiReady",
        "changeWidth": "changeWidth",
        "changeHeight": "changeHeight",
        "changeSize": "changeSize",
        "selfNavigate": "selfNavigate",
        "refreshApp": "refreshApp",
        "navInMainFrame": "navInMainFrame",
        "closeWindow": "closeWindow"
    };

    var receivedMessageTypes = {
        "static": "static data",
        "dynamic": "dynamic data",
        "event": "event"
    };

    var eventTypes = {
        "documentCompleted": "documentCompleted",
        "navigateCompleted": "navigateCompleted",
        "tabChanged": "tabChanged"
    };

    var mStaticData = null;
    var mDynamicData = null;


    var onEventReceived = function(eventType) {
        switch (eventType) {
            case eventTypes.tabChanged:
                if (window.EBTabChange)
                    window.EBTabChange(objData.url);
                break;
            case eventTypes.documentCompleted:
                if (window.EBDocumentComplete)
                    window.EBDocumentComplete(mDynamicData.mainFrameUrl);
                break;
            case eventTypes.navigateCompleted:
                if (window.EBNavigateComplete)
                    window.EBNavigateComplete(mDynamicData.mainFrameUrl);
                break;
            default: break;
        }
    };

    var onMessageReceived = function(event) {
        var objData = EBJSON.parse(event.data);
        if (!objData.messageType) return;
        switch (objData.messageType) {
            case receivedMessageTypes.dynamic:
                mDynamicData = objData;
                break;
            case receivedMessageTypes.static:
                mStaticData = objData;
                break;
            case receivedMessageTypes.event:
                onEventReceived(objData.event);
                break;
            default: break;
        }
    };

    var sendReadyMessage = function() {
        var message = new MessageData(sentMessageTypes.gadgetReady);
        window.top.postMessage(JSON.stringify(message), "*");
    };

    var registerForMessaging = function() {
        window.addEventListener("message", onMessageReceived, false);
    };

    var init = function() {
        sendReadyMessage();
        registerForMessaging();
    };

    init();

    //public functions
    this.getMainFrameTitle = function() {
        return mDynamicData.mainFrameTitle;
    };

    this.getMainFrameUrl = function() {
        return mDynamicData.mainFrameUrl;
    };

    this.getWidth = function() {
        return mDynamicData.width;
    };

    this.getHeight = function() {
        return mDynamicData.height;
    };

    this.getVersion = function() {
        return mStaticData.version;
    };

    this.getToolbarId = function() {
        return mStaticData.toolbarId;
    };

    this.getToolbarName = function() {
        return mStaticData.toolbarName;
    };

    this.getDownloadPageUrl = function() {
        return mStaticData.downloadPageUrl;
    };

    this.changeWidth = function(width) {
        var message = new MessageData(sentMessageTypes.changeWidth);
        message.width = width;
        window.top.postMessage(JSON.stringify(message), "*");
    };

    this.changeHeight = function(height) {
        var message = new MessageData(sentMessageTypes.changeHeight);
        message.height = height;
        window.top.postMessage(JSON.stringify(message), "*");
    };

    this.changeSize = function(width, height) {
        var message = new MessageData(sentMessageTypes.changeSize);
        message.width = width;
        message.height = height;
        window.top.postMessage(JSON.stringify(message), "*");
    };

    this.storeKey = function(key, value) {
        key += mStaticData.guid;
        localStorage.setItem(key, value);
    };

    this.retrieveKey = function(key) {
        key += mStaticData.guid;
        return localStorage.getItem(key);
    };

    this.deleteKey = function(key) {
        key += mStaticData.guid;
        localStorage.removeItem(key);
    };

    this.storeGlobalKey = function(key, value) {
        key += "_Global";
        localStorage.setItem(key, value);
        
        if (EBGlobalKeyChanged)
            EBGlobalKeyChanged(key, value);
    };

    this.retrieveGlobalKey = function(key) {
        key += "_Global";
        return localStorage.getItem(key);
    };

    this.deleteGlobalKey = function(key) {
        key += "_Global";
        localStorage.removeItem(key);
    };

    this.selfNavigate = function(url, width) {
        var message = new MessageData(sentMessageTypes.selfNavigate);
        message.url = url;
        message.width = width;
        window.top.postMessage(JSON.stringify(message), "*");
    };

    this.refreshToolbar = function() {
        var message = new MessageData(sentMessageTypes.refreshApp);
        window.top.postMessage(JSON.stringify(message), "*");
    };

    this.navigateInMainFrame = function(url) {
        var message = new MessageData(sentMessageTypes.navInMainFrame);
        message.url = url;
        window.top.postMessage(JSON.stringify(message), "*");
    };

    this.closeWindow = function() {
        var message = new MessageData(sentMessageTypes.closeWindow);
        window.top.postMessage(JSON.stringify(message), "*");
    };

    this.openGadget = function(url, width, height) {
        this.changeSize(width, height);
        this.selfNavigate(url);
    };

    this.openGadget2 = function(url, width, height) {
        this.openGadget(url, width, height);
    };

    this.getApiVersion = function() {
        return mStaticData.apiVersion;
    };

    //currently not implemented
    this.getSearchTerm = function() { return ""; };
    this.handleHeight = function() { };
    this.refreshToolbar = function() { };
    this.refreshComponentById = function() { };
};


if (_BCAPIHelper.isChrome())
    BCAPI = new ConduitChromeGadgetApi();
