function skipMobileDetectCookie() {
    var expires = new Date();
    expires.setTime(expires.getTime() + (90 * 1000 * 60 * 60 * 24));
    document.cookie = 'skip_mobile_detect=true;expires=' + expires.toGMTString() + ';path=/';
}


function redirectToGoogle(isOpera) {
    var msg = 'Two Peas In A Bucket now has an Android App!  Tap OK to read more about it.';
    var operaMsg = 'Two Peas In A Bucket now has an Android App!  Search for "Two Peas In A Bucket" in the Market to learn more about it.';
    
    if (isOpera) {
        skipMobileDetectCookie();
        alert(operaMsg);
        return;
    }
    
    if (confirm(msg)) {
        window.location = '';
    } else {
        skipMobileDetectCookie();
    }
}


function redirectToApple(type, isOpera) {
    var operaMsg = 'Two Peas In A Bucket now has a ' + type + ' App!  Search for "Two Peas In A Bucket" in the App Store to learn more about it.';
    var safariMsg = 'Two Peas In A Bucket now has a ' + type + ' App!  Tap OK to read more about it.';

    if (isOpera) {
        skipMobileDetectCookie();
        alert(operaMsg);
        return;
    }

    if (confirm(safariMsg)) {
        window.location = 'http://itunes.apple.com/us/app/two-peas-in-a-bucket-scrapbooking/id472987677?ls=1&mt=8';
    } else {
        skipMobileDetectCookie();
    }
}


function mobileDetect() {
    if (document.cookie.indexOf('skip_mobile_detect=true') == -1) {
        var agent = navigator.userAgent.toLowerCase();
        var deviceType;
        var isOpera = (agent.indexOf('opera') != -1);
        var redirectTo = '';

        if (agent.indexOf('iphone') != -1) {
            deviceType = 'iPhone';
            redirectTo = 'apple';
        } else if (agent.indexOf('ipod') != -1) {
            deviceType = 'iPod Touch';
            redirectTo = 'apple';
        //For now we do not have an iPad or Android version
        // } else if (agent.indexOf('ipad') != -1) {
        //     deviceType = 'iPad';
        //     redirectTo = 'apple';
        // } else if (agent.indexOf('android') != -1) {
        //     redirectTo = 'google';
        } else {
            return;
        }

        if (redirectTo === 'google') {
            redirectToGoogle(isOpera);
        } else if (redirectTo === 'apple') {
            redirectToApple(deviceType, isOpera)
        }
    }
}


mobileDetect();


