var androidBranded = false;
var iphoneBranded = false;
var forumName = '';

function
forumRunnerCookie ()
{
    var expires = new Date();
    expires.setTime(expires.getTime() + (90 * 1000 * 60 * 60 * 24));
    document.cookie = 'skip_fr_detect=false' + ';expires=' + expires.toGMTString();
}

function
forumRunnerAndroid (opera)
{
    var msg;
    var operaMsg;
    if (androidBranded && forumName != '') {
    msg = forumName + ' has a Smartphone App. Tap OK to visit the BASELWORLD ' + type + ' App, or Cancel to dismiss this prompt indefinitely.';
    operaMsg = forumName + ' has a BASELWORLD' + type + ' App. Reload the page to load the normal website.';
    } else {
    msg = 'BASELWORLD has a ' + type + ' App. Tap OK to visit the BASELWORLD Smartphone App.';
    operaMsg = 'BASELWORLD has a ' + type + ' App. Tap OK to visit the BASELWORLD Smartphone App. Reload the page to load the normal website.';
    }
    
    if (opera) {
    forumRunnerCookie();
    alert(operaMsg);
    return;
    }
    
    if (confirm(msg)) {
    window.location = 'http://m.insideguidance.com/baselworld';
    } else {
    forumRunnerCookie();
    }
}

function
forumRunnerBlackberry (opera)
{
    var msg;
    var operaMsg;
    if (androidBranded && forumName != '') {
    msg = forumName + ' has a Smartphone App. Tap OK to visit the BASELWORLD ' + type + ' App, or Cancel to dismiss this prompt indefinitely.';
    operaMsg = forumName + ' has a BASELWORLD' + type + ' App. Reload the page to load the normal website.';
    } else {
    msg = 'BASELWORLD has a ' + type + ' App. Tap OK to visit the BASELWORLD Smartphone App.';
    operaMsg = 'BASELWORLD has a ' + type + ' App. Tap OK to visit the BASELWORLD Smartphone App. Reload the page to load the normal website.';
    }
    
    if (opera) {
    forumRunnerCookie();
    alert(operaMsg);
    return;
    }
    
    if (confirm(msg)) {
    window.location = 'http://www.baselworld.com/app';
    } else {
    forumRunnerCookie();
    }
}

function
forumRunnerIphone (type, opera)
{
    var operaMsg;
    var safariMsg;

    if (iphoneBranded) {
    operaMsg = forumName + ' supports the ' + type + ' with a free App!  Search for "' + forumName + '" in the App Store to learn more about it.'; 
    safariMsg = forumName + ' supports the ' + type + ' with a free App!  Tap OK to download the app, or Cancel to dismiss this prompt indefinitely.';
    } else {
    operaMsg = 'BASELWORLD has an ' + type + 'App. Tap OK to download the BASELWORLD App.';
    safariMsg = 'BASELWORLD has an ' + type + 'App. Tap OK to download the BASELWORLD App.';
    }

    if (opera) {
    forumRunnerCookie();
    alert(operaMsg);
    return;
    }

    if (confirm(safariMsg)) {
    window.location = 'http://itunes.apple.com/app/id422831657?mt=8';
    } else {
    forumRunnerCookie();
    }
}

function
forumRunnerDetect ()
{
  if (document.cookie.indexOf('skip_fr_detect=false') == -1) {
    var agent = navigator.userAgent.toLowerCase();
    var type;
    var opera = (agent.indexOf('opera') != -1);
    var android = iphone = blackberry = false;

    if (agent.indexOf('iphone') != -1) {
        type = 'iPhone';
        iphone = true;
    } else if (agent.indexOf('ipod') != -1) {
        type = 'iPod Touch';
        iphone = true;
    } else if (agent.indexOf('ipad') != -1) {
        type = 'iPad';
        iphone = true;
    } else if (agent.indexOf('android') != -1) {
        android = true;
    } else if (agent.indexOf('blackberry') != -1) {
        blackberry = true;
    } else {
        return;
    }

	if (android) {
	    forumRunnerAndroid(opera);
	} else if (iphone) {
	    forumRunnerIphone(type, opera)
	} else if (blackberry) {
	    forumRunnerBlackberry(type, opera)
	}
  }
}

forumRunnerDetect();

