<?

/* Exports current community as JSON
 *
 * @todo move this to a controller
 */
if ($this->community) {
	/* @var $this->community Crunchbutton_Community */
	if ($this->admin) {
		$community = $this->community->exports();
	} else {
		$community = $this->community->exports();
	}
	$communityJson = json_encode($community);
	$communityJson = str_replace("'","\\'", $communityJson);
}

?>
<script type="text/javascript">
if (App) {
	App.config = {};
	App.logService = 'http<?=$_SERVER['SERVER_NAME'] == 'crunchbutton.com' ? 's' : ''?>://log.<?=$_SERVER['SERVER_NAME']?>/api/';
} else {
	$(function() {
		App.logService = 'http<?=$_SERVER['SERVER_NAME'] == 'crunchbutton.com' ? 's' : ''?>://log.<?=$_SERVER['SERVER_NAME']?>/api/';
		App.config = {};
	});
}
</script>
</head>
<body class="bg no-init">
<?=$this->display('layout/html.body')?>
</body>
<?
	switch ( c::env() ) {
		case 'local':
			$facebook = c::config()->facebook->{ 'crunchbutton.localhost' };
			break;
		case 'beta':
			$facebook = c::config()->facebook->{ 'beta.crunchr.co' };
			break;
		case 'live':
		default:
			$facebook = c::config()->facebook->{ 'crunchbutton.com' };
			if( $_SERVER['SERVER_NAME'] == 'live.ci.crunchbutton.crunchr.co' ){
				$facebook = c::config()->facebook->{ 'staging.crunchr.co' };
			}
			break;
	}
?>
<script type="text/javascript">
	$(function() {
		window.fbAsyncInit = function() {
			console.debug('Init facebook <?=$facebook->app;?>');
			FB.init({
				appId: '<?=$facebook->app;?>',
				cookie: true,
				xfbml: true,
				oauth: true,
				version: 'v2.2'
			});
			angular.element(document).ready(function (){
				setTimeout(function(){
					var facebookService = angular.element('html').injector().get('FacebookService');
					FB.Event.subscribe('auth.statusChange', facebookService.processStatus);
				},100);

			});

		};

		(function(d, s, id) {
		var js, fjs = d.getElementsByTagName(s)[0];
		if (d.getElementById(id)) return;
		js = d.createElement(s); js.id = id;
		js.src = '//connect.facebook.com/en_US/sdk<?=strpos($_SERVER['__HTTP_HOST'],'localhost') !== false ? '/debug' : ''?>.js';
		fjs.parentNode.insertBefore(js, fjs);
		}(document, 'script', 'facebook-jssdk'));
	});
</script>