(function()
{
	
	var SHELL_PATH = tacoBell.swfPath + 'subbrands/sub_brand_shell.swf';
	var SHELL_WIDTH = 920;
	var SHELL_HEIGHT = 473;
	var SHELL_NODE = 'subbrandSWF';
	
	var FLASH_MODAL_OPENED = 'modalOpened';
	var FLASH_MODAL_CLOSED = 'modalClosed';
	
	var _brand_to_load;
	var _extra_flashvars;
	var _shell_params;
	var _shell_flashvars;
	
	var _win;
	
	function loadBrand(brand, extra_flashvars)
	{
		_brand_to_load = brand;
		_extra_flashvars = extra_flashvars || {};
	}
	
	$(function()
	{
		if (!swfobject.hasFlashPlayerVersion(tacoBell.constants.REQUIRED_FLASH_PLAYER_VERSION))
			$('#' + SHELL_NODE + ' div').show();
		
		$(window).bind('mdm:opened', modalOpened);
		$(window).bind('mdm:closed', modalClosed);
		
		if (_brand_to_load)
		{
			Tracker.trackPage('sub_brands>' + _brand_to_load, 'sub_brands', {
				prop2: _brand_to_load
			});
			
			setShellEmbedAttributes();
			embedShell();
		}
	});
	
	function openWindow(absolute, loc, w, h)
	{
		if (_win) _win.close();
		
		_win = window.open(absolute? loc : tacoBell.swfPath + loc, 'subbrand_win', 'width=' + w + ',height=' + h);
		
		if (!_win)
			alert('Hmm. It looks like a popup blocker is preventing us from opening a new window. Try turning it off and try again.');
	}
	
	function openShareModal()
	{
		tacoBell.utils.ShareManager.openShareDialog();
	}
	
	function modalOpened()
	{
		if (swfobject.getObjectById(SHELL_NODE)[FLASH_MODAL_OPENED])
			swfobject.getObjectById(SHELL_NODE)[FLASH_MODAL_OPENED]();
	}
	
	function modalClosed()
	{
		if (swfobject.getObjectById(SHELL_NODE)[FLASH_MODAL_CLOSED])
			swfobject.getObjectById(SHELL_NODE)[FLASH_MODAL_CLOSED]();
	}
	
	function setShellEmbedAttributes()
	{
		_shell_params = {
			wmode: 'transparent',
			scale: 'noscale',
			allowscriptaccess: 'always',
			menu: 'false',
			salign: 'tl'
		};
		
		_shell_flashvars = {
			swf_path: tacoBell.swfPath,
			brand: _brand_to_load,
			openShareModal: 'TacoBellSubBrandController.openShareModal'
		};
		
		for (var key in _extra_flashvars)
			_shell_flashvars[key] = _extra_flashvars[key];
	}
	
	function embedShell()
	{
		swfobject.embedSWF(
			SHELL_PATH,
			SHELL_NODE,
			SHELL_WIDTH,
			SHELL_HEIGHT,
			tacoBell.constants.REQUIRED_FLASH_PLAYER_VERSION,
			tacoBell.swfPath + tacoBell.constants.EXPRESS_INSTALL_PATH,
			_shell_flashvars,
			_shell_params
		);
	}
	
	window.TacoBellSubBrandController = {
		loadBrand: loadBrand,
		openWindow: openWindow,
		openShareModal: openShareModal
	};
	
	window.TacoBellSubBrand = {
		FOURTHMEAL: 'fourthmeal',
		FRUTISTA: 'frutista',
		VOLCANO: 'volcano',
		WHY_PAY_MORE: 'why_pay_more'
	};
})();
