/*
 * Cellblock general-purpose management console, a component of
 * the Gloto Platform.  See www.gloto.com for more information,
 * terms, and licensing.
 *
 * Copyright (C) 2005-2008, Gloto Corp.
 * http://www.cellblock.com/terms.htm
 */

function Config()
{
	var config = this;
	
	var initObj = {
		context: "http://www.cellblock.com", // No trailing slash
      	apiKey: "http://ymca.gloto.com",
		realm: getUrlParameter("realm"),
      	id: getUrlParameter("cb"),
		frame: getUrlParameter("frame"),
		adminMode: false, // Will be auto-filled on login
		user: null, // Will be auto-filled on login
		receiptEnabled: false, // Receipts enabled by default for new cellblocks? Boolean: true or false
		receiptSubject: "Default Subject", // Edit this to your liking
		receiptMessage: "Default response message.", // Edit this to your liking
		feeds: {}
	};

	function getUrlParameter( name )
	{
		name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regexS = "[\\?&]"+name+"=([^&#]*)";
		var regex = new RegExp( regexS );
		var results = regex.exec( window.location.href );
		if( results === null )
			return null;
		else
			return results[1];
	}
	
	this.isAdmin = function(){
		return config.get("adminMode");
	};
	
	// Getter Methods

	this.get = function( key )
	{
		return initObj[key];
	};

	this.getInit = function()
	{
		return initObj;
	};

	this.getContext = function()
	{
		return config.get("context");
	};
	
	this.getApiKey = function()
	{
		return config.get("apiKey");
	};
	
	this.getRealm = function()
	{
		return config.get("realm");
	};
	
	this.getId = function()
	{
		return config.get("id");
	};

	this.getFeeds = function(){
		return config.get("feeds")[config.getId()];
	};

	this.hasFeeds = function( aka ){
		var feeds = config.get("feeds");
		aka = aka || config.getId();
		return (feeds != null && feeds[aka] != null && feeds[aka].length > 0 );
	};

	this.isFeed = function( aka ){
		var feeds = config.get("feeds");
		for( var key in feeds ){
			if( feeds[key].indexOf( aka ) >= 0 ){
				return true;
			}
		}
		return false;
	};
	
	// Generic Setter
	
	this.set = function( key, value )
	{
		initObj[key] = value;
	};
}

// Create config
var config = new Config();
