/** Sandboxed Facebook manipulations */
var isInFBTab = /app_runner/.test(window.name),
	isInFBCanvas = /iframe_canvas/.test(window.name);

if (isInFBTab || isInFBCanvas) {

	 /** restyle */
	jQuery(document).ready(function($) {
		if (!document.body.offsetWidth)
			console.info('cannot read document.body.offsetWidth');
		else setTimeout(function(){
			try{
				var width = parseInt(document.body.offsetWidth);
				/** if we are in a narrow Facebook tab, shrink */
				if (width < 725)	{
					document.body.className += " facebook-520 ";

					if ((function(){var s = false;try{s = !!IPSLMap;}catch(e){};return s;})()) {
						IPSLMap.resultTpl.html = IPSLMap.resultTpl.html
							.replace(/\n|\r|\t/g,'').replace(/&nbsp;/g,' ').replace(
								'</p></div><div class="column three"><h2> </h2><p>',
								'<br/>'
							)
							.replace(/\n|\r|\t/g,'').replace(/&nbsp;/g,' ').replace(
								'</P></DIV><DIV class="column three"><H2> </H2><P>',
								'<br/>'
							)
							.replace(/<DIV/g, '<div').replace(/DIV>/g, 'div>')
							.replace(/<H2/g, '<h2').replace(/H2>/g, 'h2>')
							.replace(/<P/g, '<p').replace(/P>/g, 'p>')
							.replace(/<A/g, '<a').replace(/A>/g, 'a>')
							.replace(
								'<div class="column three">',
								'<div class="column three" style="display:none">'
							).replace(/three columns/gi, 'two columns');
					}
				}
			} catch(e){ console.info(e); }
		},1);
	});

	var css = document.createElement("link");
	css.rel = "stylesheet";
	css.type = "text/css";
	css.href = "templates/IntraCommon/css/iframed.css";
	document.getElementsByTagName("head")[0].appendChild(css);

	/** lookup myStore */
	jQuery(document).ready(function($)	{
		try{
			var cookie,
				storeId = parseInt(window["storeId"]),
				myStoreId = parseInt(window["myStoreId"]),
				isMyStore = false,
				cookieArr = document.cookie.split(";");
			for (var i=0;i<cookieArr.length;i++)	{
				cookie = cookieArr[i].split("=");
				if (cookie[0] == "myIntraStore")	{
					storeId = cookie[1]*1;
					break;
				}
				else if (cookie[0] == " myIntraStore")	{
					storeId = cookie[1]*1;
					break;
				}
			}
			/** handle myStore if set */
			if (storeId)	{
				var base = document.getElementsByTagName("base")[0].href.split("index.php")[0];
				isMyStore = new RegExp("file_"+storeId+" ").test(document.body.className);
				/** redirect to store page */
				if (!isMyStore)
					document.location.href = base+"index.php?file_id="+storeId;
				/** on store page: change links */
				else  {
					/** open all links in a new window */
					$("a[href]").attr("target", "new window");
					/** besides our myStore reset */
					var btnArr = $("a[title='Instellen als Mijn Intratuin']");
					btnArr.each(function(i, el)    {
						el.removeAttribute("target");
					});
					if (storeId == myStoreId) btnArr.each(function(i, el)    {
						el.href = base+"winkels/?myStore=-1";
						el.title = "Zoek een andere Intratuin";
						el.innerHTML = el.title;
					});
					else {
						/** add backlink */
						btnArr[0].parentNode.innerHTML += '<a class="button" href="'+base+'winkels/?myStore=-1" title="Zoek een andere Intratuin">Zoek een andere Intratuin</a>';
					}
					if (typeof FB != undefined) setTimeout(function()	{
						try {
							FB.Canvas.setSize({
								width: '*',
								height: document.body.offsetHeight + 50
							});
						} catch (e) {};
					},1);
				}
			}

			var heading = $(".col-main .content-body h1"),
				timesLink = document.createElement("a");
			if (heading.length) {
				timesLink.className = "timesLink";
				timesLink.target = "_blank";
				timesLink.innerHTML = "koopzondagen en feestdagen";
				timesLink.href = "http://www.intratuin.nl/winkels/koopzondagen-en-feestdagen/";
				heading.append(timesLink);
			}


		} catch(e){ console.info(e); }
	});
}
