bbcdotcom.createObject('bbcdotcom.mobilePrompt');
bbcdotcom.mobilePrompt = function(userAgentString,opts) {
    var userAgentString = userAgentString,
        opts = opts || {},
        numerator = opts.numerator || 1,
        cookieName = opts.cookieName || 'appPrompted',
        denominator = opts.denominator || 1,
        device = {
            'ios':{
              'appStoreUrl':'http://itunes.apple.com/app/bbc-news/id364147881?mt=8&uo=4'
            },
            'android':{
              'appStoreUrl':'market://details?id=bbc.mobile.news.ww'
            }
        },
        prompt = opts.prompt || function() {
            return confirm("Would you like to download the free BBC News app?");
        };
    return {

        init:function() {
            if(this.userAgentStringIsMobileSafariOnIphone() && this.surveyShouldBeShown()) {
                this.displayPrompt(prompt, 'ios');
            }
            if(this.userAgentStringIsAndroid() && this.surveyShouldBeShown()) {
                this.displayPrompt(prompt, 'android');
            }
        },
        setUserAgentString:function(UAString) {
            userAgentString = UAString;
        },
        isSupportedOsVersion:function(){
            return userAgentString.search('CPU OS') == -1 ? false : true;
        },
        userAgentStringIsMobileSafariOnIphone:function() {
            var isMobile            = userAgentString.search('Mobile/'),
                isSafari            = userAgentString.search('Safari/'),
                isTargettedDevice   = userAgentString.search('iPhone;');

            if(isTargettedDevice == -1){
                isTargettedDevice = userAgentString.search('iPod');
            }

            if(isMobile != -1 && isSafari != -1 && isTargettedDevice != -1){
                return true;
            } else {
                return false
            }
        },
        userAgentStringIsAndroid:function() {
            var isAndroid = userAgentString.search('Android');
            return isAndroid != -1;
        },
        surveyShouldBeShown:function() {
            if(numerator/Math.ceil(Math.random() * denominator) == 1 && !this.cookiePresent()){
                return true;
            } else {
                return false;
            }
        },
        cookiePresent:function() {
            return this.readCookie(cookieName);
        },
        createCookie:function(name,value,hours,path,domain,secure) {
            var cookieOpts = [];
            if (hours !== null && hours !== undefined) {
                var dt = new Date(new Date(new Date()).getTime()+hours*3600000).toUTCString();
                cookieOpts.push("; expires="+dt);
            }
            if (path !== null && path !== undefined) {
                cookieOpts.push("; path="+path);
            }
            if (domain !== null && domain !== undefined) {
                cookieOpts.push("; domain="+domain)
            }
            if (secure !== null && secure !== undefined) {
                cookieOpts.push("; secure="+secure)
            }
            return name+"="+value+cookieOpts.join("");
        },
        deleteCookie:function(name,path,domain,secure) {
            document.cookie = this.createCookie(name, "", -1, path, domain, secure);
        },
        readCookie:function(name) {
            var nameEQ = name + "=",
            ca = document.cookie.split(';'),
            i=0;
            for(i;i < ca.length;i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
            }
            return false;
        },
        displayPrompt:function(fn,deviceName) {
            var prompt = fn.apply();
            if(prompt==true) {
                if(undefined !== typeof s) {
                    s.trackExternalLinks(deviceName + '-news|bbc:news:' + deviceName + '-app-download-button');
                }
                window.location.assign(device[deviceName].appStoreUrl);
            }
            document.cookie = this.createCookie(cookieName,1,730);
        }
    }
}
