﻿var deviceIphone = "iphone";
var deviceIpod = "ipod";
var deviceAndroid = "android";
var uagent = navigator.userAgent.toLowerCase();

$(document).ready(function () {
    $("#mobileButtonYes").click(function () {
        createCookie("usemobile", "true", 30);
        $("#mobileSelection").hide();
        window.location = $(this).attr('href');
        return false;
    });
    $("#mobileButtonNo").click(function () {
        createCookie("usemobile", "false", 30);
        $("#mobileSelection").hide();
    });
    $(window).resize(function () {
        $("#mobileSelection div.modalbackground").width($(document).width());
        $("#mobileSelection div.modalbackground").height($(document).height());
        var margleft = ($(window).width() - $("#modalPopupMobile").outerWidth()) / 2;
        $("#modalPopupMobile").css("marginLeft", margleft);
    });
    if (DetectSupportedMobile()) {
        var qsMobile = getParameterByName("mobile").toLowerCase();
        if (qsMobile == "false") {
            createCookie("usemobile", "false", 30);
        } else if (qsMobile == "clear") {
            createCookie("usemobile", "false", -1);
        } else {
            var usemobile = readCookie("usemobile");
            if (usemobile == null) {
                $("#mobileSelection").show();

                $("#mobileSelection div.modalbackground").width($(document).width());
                $("#mobileSelection div.modalbackground").height($(document).height());
                var margleft = ($(window).width() - $("#modalPopupMobile").outerWidth()) / 2;
                $("#modalPopupMobile").css("marginLeft", margleft);
            } else {
                if (usemobile == "true") { redirectToMobile(); }
            }
        }
    }
});
function DetectSupportedMobile() {
    if (uagent.search(deviceIphone) > -1) return true;
    else if (uagent.search(deviceIpod) > -1) return true;
    else if (uagent.search(deviceAndroid) > -1) return true;
    else return false;
}
function redirectToMobile() {
    $("#mobileButtonYes").trigger("click");
}
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; 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 null;
}
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}
