
BFAction = function(context) {

	this.controllers = new Object();
	
	this.init = function(controller) {		
		if(! this.controllers[controller] ) {		
			eval(controller + '.prototype = new BFController()');	
			eval(controller + '.prototype.constructor = ' + controller);
			var ctl = eval('new ' + controller + '()');		
			ctl.setContext(context);
			this.controllers[controller] = ctl;
			ctl.onload();
			ctl.register();				
		}
	}
	
	this.getController = function(controller) {
		return this.controllers[controller];
	}
	
}

BFController = function() {

	/* query params */
	this.query_params = null;
	
	/* our controller context */
	this.c = null;	
	
	/* set our context */
	this.setContext = function (context) {
		this.c = context;
	}
	
	/* get query params if any */
	this.params = function() {
		var query = window.location.search.substring(1);
		var vars = query.split("&");
		this.query_params = new Object()
		for (var i=0;i<vars.length;i++) {
			var pair = vars[i].split("=");
   			this.query_params[pair[0]] = unescape(pair[1]);
   			this.query_params[pair[0]] = this.query_params[pair[0]].replace(/\+/g, " ");
  		} 
	}
	
	/* get query paramm by key */
	this.param = function(key) {	
		// TODO does not support mutiple values for one key
		
		// if no params, try and parse them 
		if(! this.query_params) {			
			this.params();
		}
		return this.query_params[key];
	}
	
	this.onload = function() {
		throw('onload() is abstract, must be implemented by a child class');
	}
	
	this.register = function() {
		throw('register() is abstract, must be implemented by a child class');
	}
	
	this.parseTemplate = function(template_id, params) {
		//DP_Debug.dump(params);
		params['_MODIFIERS'] = TT_Filters;
		var tp = TrimPath.parseDOMTemplate(template_id);
		var out = tp.process(params, {throwExceptions:true});
		return out;		
	}
		
	this.getCookie = function(name) {
		var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    	return (cookie ? unescape(cookie[2]) : null);	
	}
	
	this.deleteCookie = function(name, host) {
    		var d = new Date();
    		d.setTime( d.getTime() + ( 86400000 * parseFloat(-100) ) );
    		var expire = '; expires=' + d.toGMTString();	
		var cook = escape(name) + '=' + expire + '; path=/;';
		if(host) cook += ' domain=.' + host;
    		document.cookie = cook;
		
		// Also delete base domain cookie, if set
		if(host == '.www.apartmenttherapy.com') {
  			document.cookie = escape(name) + '=' + expire + '; path=/; domain=.apartmenttherapy.com;';
		}
	}
	
	this.setCookie = function(name, value, host) {
    	var d = new Date();
    	d.setTime( d.getTime() + ( 86400000 * parseFloat(1000) ) );
    	var expire = '; expires=' + d.toGMTString();
    	var cook = escape(name) + '=' + escape(value) + expire + '; path=/';
    	if(host) {	
    		cook += '; domain=.' + host;
    	}
		document.cookie = cook;		
	}
	
	this.stripHTML = function(html) {
		html =  html.replace(/<.*?>/g, '');
		html =  html.replace(/&.*?;/g, '');
		return html;
	}		

	this.cleanHTML = function(html) {
		html =  html.replace(/&/g, '&amp;');		
		html =  html.replace(/</g, '&lt;');
		html =  html.replace(/>/g, '&gt;');
		return html;
	}	
			
}
