 function FCON(libraryBase,HTTPS,domainName,client,account) {
  this.client  = client;
  this.account = account;
  this.HTTPS   = HTTPS;
  this.domainName = domainName;

  this.libraries = {FCON: {Loaded: true, Callbacks: new Array()}};
  this.css       = {};
  this.libraryBase = libraryBase;
  
  this.headTag       = document.getElementsByTagName("head").item(0);
  
  this.reloadInterval = null;
  this.reloadTimer = null;

  this.setAutoReload = function (interval) {
   if (this.reloadTimer != null) {
    if (interval > this.reloadInterval) {
     return;
    }
   }
   this.reloadInterval = interval;
   this.reloadTimer = setTimeout("location.reload()",interval * 1000);
  };
  this.getChangeClass = function (value) {
   if (value > 0) {
    return 'positive';
   } else if (value < 0) {
    return 'negative';
   }
   return 'even';
  };

  this.createURL = function (baseURL,params) {
   var url = new Array();
       if (baseURL.indexOf('?') >= 0) {
        url.push(baseURL);
        url.push('&');
       } else {
        url.push(baseURL);
        url.push('?');
       }
       for (var key in params) {
        url.push(key);
        url.push('=');
        url.push(escape(params[key]));
        url.push('&');
       }
       url.pop();
   return url.join("");
  };

  this.createStudioQuery = function (params) {
   var url = new Array();
       url.push(this.https ? 'https://' : 'http://');
       url.push(this.domainName);
       url.push("/");
       url.push(this.client);
       if (this.account != null) {
        url.push(".");
	url.push(this.account);
       }

       if (params['PATH']) {
        url.push("/");
        url.push(params['PATH']);
	delete(params['PATH']);
       }
       url.push('?');

   for (var key in params) {
    url.push(key);
    url.push('=');
    url.push(escape(params[key]));
    url.push('&');
   }
   url.pop();
   return url.join("");
  };
  
  this.createChartQuery = function (params) {
   var url = new Array();
       url.push(this.https ? 'https://' : 'http://');
       url.push('chart.financialcontent.com');
       url.push("/Chart");
       url.push('?');

   params['Client'] = this.client;
   if (this.account != null) {
    params['Account'] = this.account;
   }
   
   for (var key in params) {
    url.push(key);
    url.push('=');
    url.push(escape(params[key]));
    url.push('&');
   }
   url.pop();
   return url.join("");
  };

  this.gotoStudio = function (params) {
   document.location.href = this.createStudioQuery(params);
  };
 
  this.loadLibrary = function (libraryName) {
   if (this.libraries[libraryName] != null) {
    return;
   }
   this.loadDynamicScript(this.libraryBase + libraryName + "/" + libraryName + ".js");
   this.libraries[libraryName] = {Loaded: false, Callbacks: new Array()};
  };

  this.loadLibraryCallback = function (libraryName,callback) {
   if (this.libraries[libraryName] == null) {
    this.loadLibrary(libraryName);
    this.libraries[libraryName].Callbacks.push(callback);
   } else if (this.libraries[libraryName].Loaded) {
    callback();            
   } else {
    this.libraries[libraryName].Callbacks.push(callback);
   }
  }; 
  
  this.loadLibraryDone = function (libraryName) {
   var libraryEntry = this.libraries[libraryName];
   libraryEntry.Loaded = true;

   var callbacks = libraryEntry.Callbacks;
   libraryEntry.Callbacks = new Array();

   for (var i = 0; i < callbacks.length; i++) {
    callbacks[i]();
   }
  };
   
  this.loadLibraryCSS = function (libraryName,cssFile) {
   var href = this.libraryBase + libraryName + "/" + cssFile;
   if ( this.css[href] != null ) {
    return;
   }

   var linkTag     = document.createElement("link");
       linkTag.rel = 'stylesheet';
       linkTag.type= 'text/css';
       linkTag.href= href;
   
   this.headTag.appendChild( linkTag );
   this.css[href] = true;
  };
  
  this.loadDynamicScript = function (url) {
   var scriptTag     = document.createElement("script");
       scriptTag.src = url;
   this.headTag.appendChild( scriptTag );
  };

  this.addListener = function (el,eventName,func) {
   if (el.addEventListener) {
    el.addEventListener (eventName,func,false);
   } else if (el.attachEvent) {
    el.attachEvent ('on' + eventName,func);
   } 
  }

  this.parseTemplate = function (template, options) {
   var result = template;
   for (var i = 0; i < options.length; i++) {
    result = result.replace('#{?}',options[i]);
   }
   return result;
  }
  
  this.findAbsolutePosition = function(obj) {
   var curleft = curtop = 0;
   if (obj.offsetParent) {
    curleft = obj.offsetLeft
    curtop = obj.offsetTop
    while (obj = obj.offsetParent) {
     curleft += obj.offsetLeft
     curtop += obj.offsetTop
    }
   }
   return {y:curtop,x:curleft};
  };
 
  
  this.parseUri = function (str) {
   var     o   = this.parseUri.options,
           m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
           uri = {},
           i   = 14;

   while (i--) uri[o.key[i]] = m[i] || "";

   uri[o.q.name] = {};
   uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
    if ($1) uri[o.q.name][$1] = $2;
   });
   return uri;
  };

  this.parseUri.options = {
   strictMode: false,
   key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
   q:   {
         name:   "queryKey",
         parser: /(?:^|&)([^&=]*)=?([^&]*)/g
        },
   parser: {
             strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
             loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
           }
  };


  this.setCookie = function (name,value) {
   var curCookie = name + "=" + value + '; expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/';
   document.cookie = curCookie;
  }
  
  this.clearCookie = function (name) {
   var curCookie = name + "=foo; expires=Sun, 15-Apr-1990 12:01:01 GMT; path=/";
   document.cookie = curCookie;
  }

  this.getCookie = function (name) {
   var dc = document.cookie;
   var prefix = name + "=";
   var begin = dc.indexOf("; " + prefix);
   if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return "";
   } else {
    begin += 2;
   }
   var end = document.cookie.indexOf(";", begin);
   if (end == -1) {
    end = dc.length;
   }
   return unescape(dc.substring(begin + prefix.length, end));
  }
 }

 FCON.initialize = function (libraryBase,HTTPS,domainName,client,account) {
  if (document.FCON == null) {
   document.FCON = new FCON(libraryBase,HTTPS,domainName,client,account);
   document.FCON.loadLibraryDone('FCON');
  }
 };

