myMoro = {
	iphoneUrl: 'http://road.ie/the-fader',
	ipadUrl: 'http://road.ie/the-fader',
	androidUrl: 'http://road.ie/the-fader',
	appName: 'The FADER',
	appIcon: 'http://dv1.mblrd.com/i/512-512/c/aHR0cDovL21vYmlsZXJvYWRpZS5jb20vZmlsZXMvdXBsb2Fkcy8xMi8xMjA4OGZhNWUwNTc1MTc4YjRkNTJhMzVkM2IxNmQyMw,,',
	appId: '731',
	run: function(type) {
		var platform = false;
		if (navigator.userAgent.match(/iPhone|iPad|iPod|Android/i)) {
			platform = navigator.userAgent.match(/iPhone|iPad|iPod|Android/i).toString();
		}
		if (platform && platform.match(/iPhone|iPod/i) && this.iphoneUrl) {
			landingUrl = this.iphoneUrl;
		} else if (platform && platform.match(/iPad/i) && this.ipadUrl) {
			landingUrl = this.ipadUrl;
		} else if (platform && platform.match(/Android/i) && this.androidUrl) {
			landingUrl = this.androidUrl;
		} else {
			return;
		}
		if (this.cookie.get(platform) == 'viewed') {
			return;
		}
		this.cookie.set(platform);
		eval('this.display.' + type + '(landingUrl, platform)');
	},
	display: {
		confirmPopup: function(landingUrl, platform) {
			var confirmed = confirm("Download " + platform + " app?");
			if (confirmed) {
				// setTimeout is used so that back button will be available.
				setTimeout(function(){
					window.location = landingUrl;
				},1000);
			}
		},
		modalPopup: function(landingUrl, platform) {
			var newMeta = document.createElement('meta');
			newMeta.setAttribute("name","viewport");
			newMeta.setAttribute("content","width=device-width, height=device-height, initial-scale = 1.0");
			document.head.insertBefore(newMeta, document.head.childNodes[0]);

			var newStylesheet = document.createElement('link');
			newStylesheet.setAttribute("href","http://mobileroadie.com/css/clientmodal.css");
			newStylesheet.setAttribute("type","text/css");
			newStylesheet.setAttribute("rel","stylesheet");
			document.body.appendChild(newStylesheet);

			var modalHtml = escape("<div id='moroOverlay' style='z-index: 999998'></div><div id='moroAppModal' style='z-index: 999999'><div tabindex='-1' id='moroAppInner' style='display: block; outline: 0px none; height: auto; width: 300px; margin: 10px auto;'><div class='moroTitlebar'><span>&nbsp;</span><a role='moroButton' href='#' onclick='myMoro.display.modalToggle(); return false'><span class='moroClose'>close</span></a></div><div scrollleft='0' scrolltop='0' style='width: auto; min-height: 49px; height: auto;' id='moroDialog'><p class='moroDevice'>We noticed you're visiting on your <span id='moroPlatform'></span></p><h2>Would you like to download the <strong id='moroAppName'></strong></h2><div><img src='' alt='App Icon' id='moroAppIcon' width='128'><br><a id='moroLandingUrl' class='moroButton' href='' onclick='myMoro.display.modalToggle(); return true'>Download the App</a></div></div><div class='moroButtonset'><button type='moroButton' onclick='myMoro.display.modalToggle(); return false'><span class='moronobutton'>No Thanks</span></button></div></div></div>");
			var newElement = document.createElement('div');
			newElement.id = 'moroAppModalWrap';
			document.body.appendChild(newElement);
			document.getElementById('moroAppModalWrap').innerHTML = unescape(modalHtml);
			document.getElementById('moroPlatform').innerHTML = platform;
			document.getElementById('moroLandingUrl').setAttribute("href", landingUrl + ',,js');
			document.getElementById('moroAppName').innerHTML = myMoro.appName + ' App?';
			iconImg = new Image();
			iconImg.src = myMoro.appIcon;
			document.getElementById('moroAppIcon').setAttribute("src", myMoro.appIcon);

			this.modalToggle();
		},
		modalToggle: function() {
			var el = document.getElementById("moroAppModalWrap");
			el.style.display = (el.style.display == "block") ? "none" : "block";
		}
	},
	cookie: {
		get: function(platform) {
			var cookieName = 'moroAppDetect' + platform + myMoro.appId + '=';
			var ca = document.cookie.split(';');
			for (var i=0; i < ca.length; i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') {
					c = c.substring(1, c.length);
				}
				if (c.indexOf(cookieName) == 0) {
					return c.substring(cookieName.length, c.length);
				}
			}
			return null;
		},
		set: function(platform) {
			var date = new Date();
			date.setTime(date.getTime() + (5*365*24*60*60*1000));
			document.cookie = "moroAppDetect" + platform + myMoro.appId + "=viewed; expires=" + date.toGMTString() + "; path=/";
		},
		unset: function(platform) {
			var date = new Date();
			date.setTime(date.getTime() - (122323604800));
			document.cookie = "moroAppDetect" + platform + myMoro.appId + "=; expires=" + date.toGMTString() + "; path=/";
		}
	}
};

// checking DOM readiness (http://www.javascriptkit.com/dhtmltutors/domready.shtml)
var alreadyrunflag = 0 //flag to indicate whether target function has already been run
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", function(){
		alreadyrunflag = 1;
		moroLaunch();
	}, false);
} else if (document.all && !window.opera) {
	document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>');
	var contentloadtag=document.getElementById("contentloadtag");
	contentloadtag.onreadystatechange = function(){
		if (this.readyState == "complete") {
			alreadyrunflag = 1
			moroLaunch();
		}
	}
}
window.onload=function(){
	setTimeout("if (!alreadyrunflag) moroLaunch()", 0);
}

function moroLaunch() {
	myMoro.run('modalPopup');
}

