function BrowserManager(name,defaultBrowser,controller) {
	this.name = name;
	this.delimeter = '_';
	this.childBrowsers = new Array();
	this.defaultBrowser = defaultBrowser;
	this.pathToFrameWork = "/global/apps/fbml/mtvi/";
	this.swfName = "flash_browser.swf";
	this.controller = controller;
}

BrowserManager.prototype.routeMessage = function(type,args,target,pathSegment) {
	
		if(Debug.active)
			Debug.trace(this.name + ' BrowserManager.routeMessage(' + type + ',' + args + ','+target+','+pathSegment+');');
	
		
		if(pathSegment){
			// it's an iframe we're looking for, with a BrowserManager in it.
			// we found a path segment, so there is a childBrowser w/ the name pathSegment
			// lets continue to route to the target through it.
			var targetNew = pathSegment.slice(pathSegment.lastIndexOf('.')+1,pathSegment.length) + target.split(pathSegment).join("");
			this.childBrowsers[pathSegment].routeMessage(type,args,target);
			return;
		}
		
		if(type=="command"){
			
			var func = args.slice(0,args.indexOf(":"))
			var args = args.slice(args.indexOf(":")+1)
			
			if(this.childBrowsers[target]){
				
				// we have what we are looking for
				this.childBrowsers[target].command(target,func,args);
				
			}else{
				
				// we don't have what we're looking for and we don't have a path segment.
				// so let's see if this framework can find one.
				// if it does, it will kick it back out
				
				this.findFirstChild(target).command(target,func,args);
			}
		}
		
		if(type=="setBrowserProperty"){
			
			// here we don't have a pathSegment, so we'll dig into the first flash thing we find (since we are setting a browser property)
			// if it's in here, we'll set it
			// otherwise maybe we'll find a pathSegment
			var temp = args.split('=');
			this.findFirstChild(target).setBrowserProperty(target,temp[0],temp[1]);
		}
		
		if(type=="navigate"){
			
			var targetSwfBrowser = this.childBrowsers[this.defaultBrowser];
			if(target){

				// the full target will be sent to the SwfBrowser to handle
				args = BrowserManager_buildUrl(args,target);

				// call navigate on the first browser - slice the target down to the first browser.
				targetSwfBrowser = this.findFirstChild(target)//(target.indexOf(this.delimeter)>0) ? target.slice(0,target.indexOf(this.delimeter)) : target;
			}
			
			targetSwfBrowser.navigate(args);
		}
}

BrowserManager.prototype.omniture = function(args) {
	
	if(Debug.active)
		Debug.trace('omniture = ' + args)
		
	try {

		var o = BrowserManager_buildOmnitureCall(args)
		if(Debug.active){

			str = "";
			for(var i in o) {
				str+= '\t' + i + ' = ' + o[i] + '\n';
				for(oi in o[i])
					str+= '\t\t' + oi + ' = ' + o[i][oi] + '\n';

			}

			Debug.trace(str);

		}
		REPORTING.makeCall('fbml_pv',o);

	}catch(e) {
		
		Debug.trace("ominiture error: " + e + " ->\nomniture object :");
		for(var i in o) {
			Debug.trace('\t' + i + ' = ' + o[i]);
			for(oi in o[i])
				Debug.trace('\t\t' + oi + ' = ' + o[i][oi]);

		}
	}
}

BrowserManager.prototype.omniture_grid = function(args) {
    var o = BrowserManager_buildOmnitureCall(args);
    REPORTING.makeCall("fbml_grid", o.tVars);
}


BrowserManager.prototype.omniture_lev = function(args) {
    var o = BrowserManager_buildOmnitureCall(args);
    REPORTING.makeCall("fbml_lev", o.tVars);
}

BrowserManager.prototype.updateTitle = function(title) {
	window.document.title = title ? unescape(unescape(title)) : "";
}

BrowserManager.prototype.updateUrl = function(url) {
	this.controller.onNavigate(url);
}

BrowserManager.prototype.createWindow = function(parentWindow,type,name,position,left,top,width,height,src) {
	
	try{
		
		this.childBrowsers[name] = eval(' new ' + type + '()');
		var temp = BrowserManager_removeItemAt(arguments,1);
		this.childBrowsers[name].createWindow.apply(this.childBrowsers[name],temp);

	}catch(e){
		
		//window.status = 'Error creating ' + name + ' : ' + e;	
	}
}

BrowserManager.prototype.updateWindow = function(name,position,left,top,width,height,src) {
	
	try{
		
		var temp = BrowserManager_removeItemAt(arguments,0);
		this.childBrowsers[name].updateWindow.apply(this.childBrowsers[name],temp);
		
	}catch(e){
		//window.status = 'Error updating ' + name + ' : ' + e;	
	}
}

BrowserManager.prototype.destroyWindow = function(name) {
	
	try{
		
		this.childBrowsers[name].destroyWindow();
		
	}catch(e){
		//window.status = 'Error destroying ' + name + ' : ' + e;	
	}
}

BrowserManager.prototype.findFirstChild = function(name) {
	var path = '';
	var browsers = name.split(this.delimeter);
	for (var i in browsers){
		
		path += (i>0 ? this.delimeter : '') + browsers[i];
		
		try{
			
			var b = this.childBrowsers[path];
			if(b)
				return b;
				
		}catch(e){ /** no error handling here, just move to the next browser.**/}
	}
}

BrowserManager.prototype.onPageLoad = function() {
	for (i in this.childBrowsers){
		this.childBrowsers[i].onPageLoad();
	}
}
BrowserManager.prototype.navigateTop = function(args) {
	top.location.href = args;
}

BrowserManager.prototype.targetOpener = function(args) {
	try {
	
		if(navigator.platform.toLowerCase().indexOf('win') == -1)
			throw "mac"; //weird bug with losing querystring params using gateway.swf
			
		window.opener.browserManager.routeMessage("navigate",args);
		window.opener.focus();
	}catch(e){
		try{
			window.opener.location.href = args;
			window.opener.focus();
		}catch(e){
			var w = window.open(args,"targetOpen");
			w.focus();
			//browserManager.routeMessage("navigate",args);
		}
		
	}
}

function HistoryController() {
	var navigateToUrl;
	var browserModel;
	this.onNavigate = function(hash) {
		
		if(!hash)
			hash = "/"+window.location.search;
		
		this.onNavigate = function(hash) {
			navigateToUrl = hash;
			unFocus.History.addHistory(hash);
		};
		this.onNavigate(hash);
	};

	this.historyListener = function(historyHash) {
		
		if(!historyHash)
			return;
			
		if (historyHash != navigateToUrl) 
			browserModel.routeMessage("navigate", historyHash);
			
		navigateToUrl = null;
	};
	
	this.setBrowser = function(b) {
		browserModel = b;
	}

	unFocus.History.addEventListener('historyChange', this.historyListener);
};