//start domready https://github.com/ded/domready
!function(a,b){typeof define=="function"?define(b):typeof module!="undefined"?module.exports=b():this[a]=this.domReady=b()}("domready",function(a){function l(a){k=1;while(a=b.shift())a()}var b=[],c,d=!1,e=document,f=e.documentElement,g=f.doScroll,h="DOMContentLoaded",i="addEventListener",j="onreadystatechange",k=/^loade|c/.test(e.readyState);e[i]&&e[i](h,c=function(){e.removeEventListener(h,c,d),l()},d),g&&e.attachEvent(j,c=function(){/^c/.test(e.readyState)&&(e.detachEvent(j,c),l())});return a=g?function(c){self!=top?k?c():b.push(c):function(){try{f.doScroll("left")}catch(b){return setTimeout(function(){a(c)},50)}c()}()}:function(a){k?a():b.push(a)}})
// end

var showMobileApps = function () {
	var	appsNames = {
			"android" : "Popcorner",
			"iphone" : "Popcorner",
			"ipad" : false
		},
		urlList = {
			"android" : "http://gazeta.hit.gemius.pl/hitredir/id=zZcwPauvp0u0geO9gNQyPpYTTDMpZwRwvFh_q0zEasf.K7/stparam=rcqifoiqbn/url=market://details?id=pl.agora.popcorner&feature=search_result#?t=W251bGwsMSwxLDEsInBsLmFnb3JhLnBvcGNvcm5lciJd",
			"iphone" : "http://gazeta.hit.gemius.pl/hitredir/id=zZcwPauvp0u0geO9gNQyPpYTTDMpZwRwvFh_q0zEasf.K7/stparam=kalflwkscy/url=http://itunes.apple.com/pl/app/popcorner-serialowy-rozklad/id464762898?mt=8&ls=1",
			"ipad" : false
		},
		text = 'Ogl\u0105dasz seriale? Sprawd\u017a darmow\u0105 aplikacj\u0119 _APPNAME_, kt\u00f3ra przypomni ci, kiedy lec\u0105 twoje ulubione.',
		systemList = {
			"android" : "Androida",
			"iphone" : "iPhone'a",
			"ipad" : "iPada"
		},
		capping = 1,
		expire = 7,
		keyPrefix = 'agMobiPromo',
		Android = (navigator.userAgent.indexOf('Android') >= 0 && urlList.android) || false,
		iPhone = (navigator.userAgent.indexOf('iPhone') >= 0 && urlList.iphone) || navigator.userAgent.indexOf('iPod') >= 0 || false,
		iPad = (navigator.userAgent.indexOf('iPad') >= 0 && urlList.ipad) || false,
		Mobile = Android || iPhone || iPad || false,
		appUrl = '',
		ask = null,
		keyname = '',
		keyvalue = 0;
		
	var getCookie = function(a){var b=a+"=",c=document.cookie.split(";"),d="",e="",f={};for(var g=0;g<c.length;g++){var h=c[g];while(h.charAt(0)==" ")h=h.substring(1,h.length);if(h.indexOf(b)===0){d=h.substring(b.length,h.length),e=d.substring(0,1);if(e=="{"){f=JSON2.parse2(d);if("v"in f)return f.v}return d=="undefined"?undefined:decodeURIComponent(d)}}return null};
	var setCookie = function(a,b,c){var expire = new Date(); expire.setTime(expire.getTime() + c*86400000); document.cookie = a + "=" + encodeURIComponent(b) + "; expires=" + expire.toGMTString();};
	
	var doKeyName = function (name, url) {
		return encodeURIComponent(keyPrefix + name + url);
	};
	var doText = function (device, app) {
		return text.replace('_DEVICENAME_', device).replace('_APPNAME_', app);
	};
	
    setCookie('cookieTest', 'testCookie', 1);
	
	if (Mobile && (getCookie('cookieTest') === 'testCookie')) {
		if (Android) {
			text = doText(systemList.android, appsNames.android);
			appUrl = urlList.android;
			keyname = doKeyName(appsNames.android, urlList.android);
		} else if (iPhone) {
			text = doText(systemList.iphone, appsNames.iphone);
			appUrl = urlList.iphone;
			keyname = doKeyName(appsNames.iphone, urlList.iphone);
		} else if (iPad) {
			text = doText(systemList, appsNames.ipad);
			appUrl = urlList.ipad;
			keyname = doKeyName(appsNames.ipad, urlList.ipad);
		}
		keyvalue = getCookie(keyname) || 0;
		if (keyvalue < capping) {
			ask = window.confirm(text);
            keyvalue++;
            setCookie(keyname, keyvalue, expire);
			if (ask) {
				window.location.href = appUrl;
			}
		}
	}
};
domReady(showMobileApps);
