/**
 * 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * Copyright (c) 2011 Matteo Spinelli, http://cubiq.org/
 * Released under MIT license
 * http://cubiq.org/dropbox/mit-license.txt
 * 
 * Version 1.0.7 - Last updated: 2011.05.24
 * 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 
 */
;(function(){
var nav = navigator,
	isIDevice = (/iphone|ipod|ipad/gi).test(nav.platform),
	isIPad = (/ipad/gi).test(nav.platform),
	isRetina = 'devicePixelRatio' in window && window.devicePixelRatio > 1,
	isSafari = nav.appVersion.match(/Safari/gi),
	hasHomescreen = 'standalone' in nav && isIDevice,
	isStandalone = hasHomescreen && nav.standalone,
	OSVersion = nav.appVersion.match(/OS \d+_\d+/g),
	platform = nav.platform.split(' ')[0],
	language = nav.language.replace('-', '_'),
	startY = startX = 0,
	expired = localStorage.getItem('_addToHome'),
	theInterval, closeTimeout, el, i, l,
	options = {
		animationIn: 'drop',		// drop || bubble || fade
		animationOut: 'fade',		// drop || bubble || fade
		startDelay: 1000,		// 2 seconds from page load before the balloon appears
		lifespan: 20000,		// 20 seconds before it is automatically destroyed
		bottomOffset: 14,		// Distance of the balloon from bottom
		expire: 10080,			// Minutes to wait before showing the popup again (0 = always displayed)
		message: '',			// Customize your message or force a language ('' = automatic)
		touchIcon: false,		// Display the touch icon
		arrow: false,			// Display the balloon arrow
		iterations:100			// Internal/debug use
	};

OSVersion = OSVersion ? OSVersion[0].replace(/[^\d_]/g,'').replace('_','.')*1 : 0;
expired = expired == 'null' ? 0 : expired*1;

// Merge options
if (window.addToHomeConfig) {
	for (i in window.addToHomeConfig) {
		options[i] = window.addToHomeConfig[i];
	}
}

// Is it expired?
if (!options.expire || expired < new Date().getTime()) {
	expired = 0;
}

/* Bootstrap */
if (hasHomescreen && !expired && !isStandalone && isSafari) {
	document.addEventListener('DOMContentLoaded', ready, false);
}


/* on DOM ready */
function ready () {
	document.removeEventListener('DOMContentLoaded', ready, false);

	var deviceName = isIPad ? "iPad" : "iPhone";
	var deviceURL = isIPad ? "http://itunes.apple.com/ca/app/winnipeg-free-press-news-for/id397788999?mt=8" : "http://itunes.apple.com/ca/app/winnipeg-free-press-news/id439589192?mt=8";
	
	var qes = confirm( "This website has an " + deviceName + " app! Click OK to learn more about Winnipeg Free Press for " + deviceName + ".", "Winnipeg Free Press" );
	if ( qes )
	window.location = deviceURL;

	// Add expire date to the popup
	if (options.expire) localStorage.setItem('_addToHome', new Date().getTime() + options.expire*60*1000);
}

})();

