<?php

	$data_setup = $this->getData("setup");
	$page_data = $this->info("data");

	/* ---- Meta Description --- */
	$meta_tag_description = $data_setup->getNodeVal("data.settings.meta_tag_description");
	$meta_tag_description = htmlspecialchars($meta_tag_description);
	$meta_tag_description .= " " . $page_data->getNodeVal("data.settings.description");
	$meta_tag_description = markdown($meta_tag_description);
	$meta_tag_description = preg_replace('/(?:(?<=\>)|(?<=\/\>))(\s+)(?=\<\/?)/', "", $meta_tag_description);
	$meta_tag_description = strip_tags($meta_tag_description);
	$meta_tag_description = str_replace(array("\n", "\r"), " ", $meta_tag_description);
	$meta_tag_description = str_replace(array(".", "!", "?"), ". ", $meta_tag_description);
	if (strlen($meta_tag_description)>256) {
		$meta_tag_description = substr($meta_tag_description, 0, 256);
		$meta_tag_description = substr($meta_tag_description, 0, strrpos($meta_tag_description, ".")+1);
	}
	$meta_tag_description = trim($meta_tag_description);

	/* ----  Navigation --- */
	$main_node_lookup = Flatfile::getSetTable("node");
	$navigation_xhtml = '<ul class="sf-menu">' . "\n";
	foreach ($main_node_lookup as $key => $node_data) {
		$isactive = false;
		$nav_uri = $node_data[2];
		$nav_title = htmlspecialchars($node_data[1]);
		$nav_target = $node_data[3];
		/* if it's an internal link, find out if we're on the same page
			*/
		if ($this->isInternalPage($nav_uri)) {
			$nav_id_bits = explode("/", $nav_uri);
			$nav_id = $nav_id_bits[2];
			$isactive = $nav_id==$this->info("id");
		}
		$navigation_xhtml .= "\t" . '<li class="' . ($isactive?"current":"") . '"><a href="' . $nav_uri . '" target="' . $nav_target . '">' . $nav_title . '</a>';
		/* see about any sub-nodes
			*/
		$sub_node_lookup = Flatfile::getSetTable("node", $node_data[0]);
		if (count($sub_node_lookup)>0) {
			/* ok, we have sub nodes, lets show tohose too
				*/
			$navigation_xhtml .= "\n\t\t<ul>\n";
			foreach ($sub_node_lookup as $key => $sub_node_data) {
				$isactive = false;
				$nav_uri = $sub_node_data[2];
				$nav_title = htmlspecialchars($sub_node_data[1]);
				$nav_target = $sub_node_data[3];
				if ($this->isInternalPage($nav_uri)) {
					$nav_id_bits = explode("/", $nav_uri);
					$nav_id = $nav_id_bits[2];
					$isactive = $nav_id==$this->info("id");
				}
				$navigation_xhtml .= "\t\t\t" . '<li class="' . ($isactive?"current":"") . '"><a href="' . $nav_uri . '" target="' . $nav_target . '">' . $nav_title . '</a></li>' . "\n";
			}
			$navigation_xhtml .= "\t\t</ul>\n\t";
		}
		$navigation_xhtml .= '</li>' . "\n";
	}
	$navigation_xhtml .= "</ul>\n";
	$navigation_xhtml = str_replace(' target="_self"', "", $navigation_xhtml);
	$navigation_xhtml = str_replace(' href=""', ' href="javascript:void(0);"', $navigation_xhtml);

	/* ---- Language --- */
	$language = 				$CONFIG->getNodeVal("setup.language");
	$language_twitter = 			strtolower($language);
	$language_plusone = 			$language . "-" . ($language=="en"?"US":strtoupper($language));
	$language_facebook = 			$language . "_" . ($language=="en"?"US":strtoupper($language));

	/* ----  Setup --- */
	$browser_title_pattern = 		$this->getTypedValue("setup", "settings", "browser_title");
	$page_analytics_script = 		$this->getTypedValue("setup", "settings", "page_analytics_script");
	$site_name = 				$this->getTypedValue("setup", "settings", "site_name");
	$footer = 					$this->getTypedValue("setup", "settings", "footer");
	$page_head_tags = 				$this->getTypedValue("setup", "settings", "page_head_tags");
	$footer_credit_bool = 				$this->getTypedValue("setup", "settings", "footer_credit_bool");

	/* ----  Web Services --- */
	$facebook_like_bool = 			$this->getTypedValue("setup", "web_services", "facebook_like_bool");
	$googleplusone_bool = 			$this->getTypedValue("setup", "web_services", "googleplusone_bool");
	$twitter_tweetme_bool = 		$this->getTypedValue("setup", "web_services", "twitter_tweetme_bool");
	$social_link_types =			array("etsy_link", "facebook_link", "flickr_link", "googleplus_link", "instagram_link", "lastfm_link", "linkedin_link", "myspace_link", "pintrest_link", "rss_link", "skype_link", "tumblr_link", "twitter_link", "vimeo_link", "youtube_link");
	$social_links =				array();
	foreach ($social_link_types as $social_link_type) {
		$social_link = 			$this->getTypedValue("setup", "web_services", $social_link_type);
		if ($social_link["value"]=="") continue;
		$social_links[$social_link_type] = $social_link;
	}

	/* ---- Resampler Settings --- */
	$image_letterboxing_bool = 		$this->getTypedValue("setup", "settings", "image_letterboxing_bool");
	$resample_display_bool = 		$this->getTypedValue("setup", "settings", "resample_display_bool");
	$resampling_quality_num = 		$this->getTypedValue("setup", "settings", "resampling_quality_num");
	$resample_use_imagick_bool = 		$this->getTypedValue("setup", "settings", "resample_use_imagick_bool");
	$resample_sharpen_bool = 		$this->getTypedValue("setup", "settings", "resample_sharpen_bool");
	$watermark_bool = 			$this->getTypedValue("setup", "watermarking", "watermark_bool");
	$watermark_asset = 			$this->getTypedValue("setup", "watermarking", "watermark_asset");
		$watermark_asset = 			$watermark_asset["child"];
	$watermark_x_num = 			$this->getTypedValue("setup", "watermarking", "watermark_x_num");
	$watermark_y_num = 			$this->getTypedValue("setup", "watermarking", "watermark_y_num");
	$watermark_alpha_num = 			$this->getTypedValue("setup", "watermarking", "watermark_alpha_num");
	$crosshairs_bool = 			$this->getTypedValue("setup", "watermarking", "crosshairs_bool");
	$crosshairs_color = 			$this->getTypedValue("setup", "watermarking", "crosshairs_color");
		$crosshairs_color = 			$crosshairs_color["value"];
	$crosshairs_alpha_num = 		$this->getTypedValue("setup", "watermarking", "crosshairs_alpha_num");

	/* ---- Touch Icons --- */
	$touch_screen_icon_asset = 		$this->getTypedValue("setup", "web_services", "touch_screen_icon_asset");
	$apple_touch_icon_default_tag = 	"";
	$apple_touch_icon_72x72_tag = 	"";
	$apple_touch_icon_114x114_tag = 	"";
		if ($touch_screen_icon_asset["source"]!="") {
			$graphic = new Graphic(array());
			$graphic->thumbnail(true);
			$graphic->setBasepath("asset");
			$graphic->setExtension("png");
			$graphic->setFilename($touch_screen_icon_asset["child"]);
			$graphic->resample(true, $resampling_quality_num, $resample_sharpen_bool, $resample_use_imagick_bool);
			$graphic->setSize(57, 57);
			$apple_touch_icon_default_tag .= '<link rel="apple-touch-icon-precomposed" href="' . $graphic->source() . '">' . "\n";
			$graphic->setSize(72, 72);
			$apple_touch_icon_72x72_tag .= '<link rel="apple-touch-icon-precomposed" sizes="72x72" href="' . $graphic->source() . '">' . "\n";
			$graphic->setSize(114, 114);
			$apple_touch_icon_114x114_tag .= '<link rel="apple-touch-icon-precomposed" sizes="114x114" href="' . $graphic->source() . '">' . "\n";
		}

	/* ---- Facebook --- */
	$facebook_share_thumbnail_asset = 	$this->getTypedValue("setup", "web_services", "facebook_share_thumbnail_asset");
	$facebook_image_tag = 			"\n";
	$facebook_pixel_tag = 			"\n";
		if ($facebook_share_thumbnail_asset["source"]!="") {
			$graphic = new Graphic(array());
			$graphic->thumbnail(false);
			$graphic->setBasepath("asset");
			$graphic->setFilename($facebook_share_thumbnail_asset["child"]);
			$graphic->resample(true, $resampling_quality_num, $resample_sharpen_bool, $resample_use_imagick_bool);
			$graphic->setSize(1200, 630);
			$facebook_image_tag = '<meta property="og:image" content="' . Func::getBaseUrl() . "/" . $graphic->source() . '" />' . "\n";
		}
	$facebook_pixel_id = 			$this->getTypedValue("setup", "settings", "facebook_pixel_id");
		if ($facebook_pixel_id["value"]!="") {
			$facebook_pixel_tag .= "<script>!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,document,'script','https://connect.facebook.net/en_US/fbevents.js');fbq('init', '$facebook_pixel_id');</script><noscript><img height='1' width='1' style='display:none' src='https://www.facebook.com/tr?id=$facebook_pixel_id&ev=PageView&noscript=1' /></noscript>\n";
		}
		
	/* ---- Analytics --- */
	$page_tracking_script = $page_analytics_script["value"];
		if (strtolower(substr($page_tracking_script, 0, 7))!="<script") {
			$ga_user_agent = Func::stripGoogleUA($page_tracking_script);
			if (strtolower(substr($ga_user_agent, 0, 3))=="ua-") {
				$page_tracking_script = "<script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');ga('create', '$ga_user_agent', 'auto');</script>";
			}
		}

	/* ---- Favicon --- */
	$website_favicon_asset = 		$this->getTypedValue("setup", "settings", "website_favicon_asset");
	$website_favicon_tag = 			"";
		if ($website_favicon_asset["source"]!="") {
			$website_favicon_tag .= '<link rel="shortcut icon" href="' . $website_favicon_asset["path"] . '" />' . "\n";
		}

	/* ---- Header Logo ---- */
	$logo_asset = 				$this->getTypedValue("design", "settings", "logo_asset");
	$logo_linkage = 				$this->getTypedValue("design", "settings", "logo_linkage");
		if ($logo_linkage["href"]=="") {
			$logo_linkage["href"] = Func::getBaseUrl() . "/";
			$logo_linkage["target"] = "";
		}
		
	/* ---- Badges --- */
	$badges_xhtml = "";

	/* ---- Background --- */
	$background_top_color = 		$this->getTypedValue("design", "background", "background_top_color");
	$background_bottom_color = 		$this->getTypedValue("design", "background", "background_bottom_color");
	$background_mode = 			$this->getTypedValue("design", "background", "background_mode");
	$background_asset = 			$this->getTypedValue("design", "background", "background_asset");

	/* --- Mailing List Stuff ---- */
	$mailing_list = 				$this->getData("contact")!=false ? $this->getTypedValue("contact", "settings", "mailing_list")!="none" : false;
	$mailing_list_button = 			$this->getTypedValue("language", "contact_and_mailing_list", "mailing_list_button");
	$mailing_list_title = 			$this->getTypedValue("language", "contact_and_mailing_list", "mailing_list_title");
	$mailing_list_description = 	$this->getTypedValue("language", "contact_and_mailing_list", "mailing_list_description");
	$mailing_list_name = 			$this->getTypedValue("language", "contact_and_mailing_list", "mailing_list_name");
	$mailing_list_email_address = 	$this->getTypedValue("language", "contact_and_mailing_list", "mailing_list_email_address");
	$mailing_list_submit = 			$this->getTypedValue("language", "contact_and_mailing_list", "mailing_list_submit");
	$form_idle_message = 			$this->getTypedValue("language", "contact_and_mailing_list", "form_idle_message");

	/* ---- Page Props --- */
	$body_width_num = 			$this->getTypedValue("design", "body", "body_width_num");
	$use_clean_urls = 			$this->getTypedValue("setup", "settings", "use_clean_urls_bool");
	$browser_title = 				htmlspecialchars(str_replace("{PAGE_NAME}", $this->info("title"), $browser_title_pattern["value"]));
	$website_sitemap_uri =			$use_clean_urls ? Func::getBaseUrl() . "/" . "sitemap.xml" : Func::getBaseUrl() . "/" . SA_DIR_INDEXPATH . "?/xml/sitemap/";
	$meta_tag_description =			htmlspecialchars($meta_tag_description);
	$itdr_footer_link = 		$footer_credit_bool ? '<p>&nbsp;-&nbsp;</p><a href="http://intothedarkroom.com/" title="photography websites">photography websites</a>' : "";
	$footer_text = 				strlen(trim($footer["value"]))>0 ? $footer["markdown"] . $itdr_footer_link : "";
?>
<!doctype html>
<!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="<?php echo $language; ?>"> <![endif]-->
<!--[if IE 7]>    <html class="no-js ie7 oldie" lang="<?php echo $language; ?>"> <![endif]-->
<!--[if IE 8]>    <html class="no-js ie8 oldie" lang="<?php echo $language; ?>"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="<?php echo $language; ?>"> <!--<![endif]-->
<head>
	<title><?php echo $browser_title; ?></title>
	<meta charset="utf-8">
	<meta name="description" content="<?php echo $meta_tag_description; ?>">
	<!-- Mobile viewport optimized -->
	<meta name="viewport" content="width=device-width,user-scalable=no,maximum-scale=1.0">
	<!-- Icons, favicons, facebook, etc. -->
	<?php echo $website_favicon_tag; ?>
	<?php echo $apple_touch_icon_114x114_tag; ?>
	<?php echo $apple_touch_icon_72x72_tag; ?>
	<?php echo $apple_touch_icon_default_tag; ?>
	<?php echo $facebook_image_tag; ?>
	<?php echo $facebook_pixel_tag; ?>
	<meta property="og:type" content="website" />
	<meta property="og:title" content="<?php echo htmlspecialchars($this->info("title")); ?>" />
	<meta property="og:locale" content="<?php echo $language_facebook; ?>" />
	<?php echo $page_head_tags["value"]; ?>
	<!-- CSS: implied media=all -->
	<link rel="stylesheet" type="text/css" href="<?php echo Func::getBaseUrl() . "/" . SA_DIR_INDEXPATH; ?>?/css/global/<?php echo SA_PRODUCT_VERSION; ?>/" media="all" />
	<!-- sitemap -->
	<link rel="sitemap" type="application/xml" href="<?php echo $website_sitemap_uri; ?>">
	<!-- Javascript -->
	<script src="<?php echo Func::getBaseUrl() . "/" . SA_DIR_INDEXPATH; ?>?/js/global/<?php echo SA_PRODUCT_VERSION; ?>/"></script>
</head>
<body>
	<div id="body-background"><?php if ($background_asset["source"]!=""&&$background_mode=="fill") {?><img src="<?php echo $background_asset["path"]; ?>" width="<?php echo $background_asset["width"]; ?>" height="<?php echo $background_asset["height"]; ?>" style="display:none" /><?php } ?></div>
	<div id="container">
		<div id="container-inner">
		<header>
<?php if ($logo_asset["source"]!="") { ?>
			<a id="logo" <?php echo 'href="'.$logo_linkage["href"].'"'.($logo_linkage["target"]==""?"":' target="'.$logo_linkage["target"].'"'); ?>>
				<img src="<?php echo $logo_asset["path"]; ?>" width="<?php echo $logo_asset["width"]; ?>" height="<?php echo $logo_asset["height"]; ?>" alt="<?php echo $site_name["html"]; ?>" />
			</a>
<?php } ?>
<?php if ($this->info("class")!="Special Splash") { ?>
			<div class="spacer"></div>
			<div id="navigation">
<?php 	echo $navigation_xhtml; ?>
				<div class="clear"></div>
			</div>
			<script type="text/javascript">
				Navigation.start();
			</script>
<?php } ?>
			<div class="clear"></div>
		</header><!-- end of header -->
		<div id="main" role="main" class="<?php echo $this->info("class"); ?>">
<?php
	include "2.content." . ($this->flag_is_special ? $this->flag_special_type : strtolower($this->info("class"))) . ".tmpl";
?>
		</div><!-- end of #main -->
<?php if ($this->info("class")=="Special Splash") {
	echo $badges_xhtml; ?>
<?php } ?>
		<footer>
			<div id="social-bar">
<?php if ($googleplusone_bool) { ?>
				<div class="SocialBarItem" style="position:relative;top:2px">
					<div class="g-plusone" data-size="medium"></div>
					<script type="text/javascript">
						window.___gcfg = {lang: '<?php echo $language_plusone; ?>'};
						(function() {
							var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
							po.src = 'https://apis.google.com/js/plusone.js';
							var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
						})();
					</script>
				</div>
<?php } if ($twitter_tweetme_bool) { ?>
				<div class="SocialBarItem" style="position:relative;top:2px">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-lang="<?php echo $language_twitter; ?>">Tweet</a>
					<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
				</div>
<?php } if ($facebook_like_bool) { ?>
				<div class="SocialBarItem" style="position:relative;top:2px;margin-right:17px">
					<div id="fb-root"></div>
					<script>(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.net/<?php echo $language_facebook; ?>/all.js#xfbml=1";
						fjs.parentNode.insertBefore(js, fjs);
					}(document, 'script', 'facebook-jssdk'));</script>
					<div class="fb-like" data-href="" data-send="false" data-layout="button_count" data-width="100" data-show-faces="false"></div>
					<script>$(".fb-like").attr("data-href", $(location).attr('href'));</script>
				</div>
<?php } ?>
				<div class="clear-skinny"></div>
				<div id="footer-links">
<?php
	foreach ($social_links as $social_link_type => $social_link) {
		switch ($social_link_type) {
			case "googleplus_link" :
			 print "\t\t\t\t" . '<a id="' . $social_link_type . '" class="SocialLink" href="' . $social_link["value"] . '" target="_blank" rel="publisher">' . $social_link["html"] . '</a>' . "\n";
			 break;
			case "skype_link" :
			 print "\t\t\t\t" . '<script type="text/javascript" src="http://download.skype.com/share/skypebuttons/js/skypeCheck.js"></script>' . "\n";
			 print "\t\t\t\t" . '<a id="' . $social_link_type . '" class="SocialLink" href="skype:' . $social_link["value"] . '?call" target="_blank">' . $social_link["html"] . '</a>' . "\n";
			 break;
			default :
			 print "\t\t\t\t" . '<a id="' . $social_link_type . '" class="SocialLink" href="' . $social_link["value"] . '" target="_blank">' . $social_link["html"] . '</a>' . "\n";
		}
	}
	if ($mailing_list) {
?>
				<button id="mailing-list-button"><?php echo $mailing_list_button["html"]; ?></button>
				<div class="clear"></div>
				</div>
					<div id="sign-up">
						<div class="Inner">
							<h6><?php echo $mailing_list_title["html"]; ?></h6>
							<div class="Description Markdown"><?php echo $mailing_list_description["markdown"]; ?>
							</div><!-- End .Description -->
							<form id="mailing-list-form" action="<?php echo SA_NAMESPACE; ?>-gateway.php" method="post">
								<fieldset class="Inputs">
									<input class="Default" type="text" name="fullname" value="<?php echo $mailing_list_name["html"]; ?>" />
									<input class="Default" type="email" name="email" value="<?php echo $mailing_list_email_address["html"]; ?>" />
								</fieldset>
								<fieldset class="ButtonGroup">
									<input type="submit" name="submit" value="<?php echo $mailing_list_submit["html"]; ?>" />
								</fieldset>
								<div class="Result"><?php echo $form_idle_message["html"]; ?></div>
							</form>
						</div>
					</div>
				<script>MailingList.start();</script>
<?php } ?>
				<div class="clear"></div>
			</div><!-- end of #social-bar -->
			<div id="footer">
<?php echo $footer_text; ?>
			</div><!-- end of #footer -->
			<div class="clear"></div>
		</footer>
		</div><!-- end of #container-inner -->
	</div><!-- end of #container -->
	<!-- Page Tracking Code -->
<?php echo $page_tracking_script; ?>
</body>
</html>