window.onload = function() {
    SetDefaultOMCVersion();
};

//function GetOMCVersionFAQ() {
//    // determine which divs to show based on the selected version
//    for (var i = 0; i < document.OMCOptions.OMCVersion.length; i++) {
//        var OMCVersionOption = document.OMCOptions.OMCVersion[i].value;
//        var divCollection = document.getElementsByTagName("div");
//        var OMCVersionCheckedValue = (document.OMCOptions.OMCVersion[i].checked ? OMCVersionOption : OMCVersionCheckedValue);        
//        for (var divCount = 0; divCount < divCollection.length; divCount++) {
//            if (divCollection[divCount].id == null || divCollection[divCount].id == "" || divCollection[divCount].id.indexOf("-") == -1
//                    || (OMCVersionCheckedValue != null && divCollection[divCount].id.indexOf("-" + OMCVersionCheckedValue.toLowerCase()) != -1)
//                ) {
//                divCollection[divCount].style.display = "block";
//            } else {
//                if (document.OMCOptions.OMCVersion[i].checked == false && divCollection[divCount].id.indexOf("-" + OMCVersionOption.toLowerCase()) != -1) {
//                    divCollection[divCount].style.display = "none";
//                } else if (document.OMCOptions.OMCVersion[i].checked && divCollection[divCount].id.indexOf("-" + OMCVersionOption.toLowerCase()) != -1) {
//                    divCollection[divCount].style.display = "block";
//                }
//            }
//        }
//    }
//}

function GetOMCVersionFAQ(content) {
    if (content != null) {
        $("div[id*=FAQ]").attr('style', 'display:none');        
        document.getElementById(content).style.display = "block";
    }
}

function GetOMCVersionDefault() {
    var downloadlink = document.getElementById("downloadlink");
    var downloadinstructions = document.getElementById("downloadinstructions");
    var sidebardownloadlink = document.getElementById("downloadlinksidebar");    
    for (var i = 0; i < document.OMCOptions.OMCVersion.length; i++) {
        var OMCVersionOption = document.OMCOptions.OMCVersion[i].value;
        var OMCMobileVersionOption = "";
        var sysreqdiv = OMCVersionOption;
        if (sysreqdiv == "MobilePC") {
            sysreqdiv = "Mob";
            for (var j = 0; j < document.OMCOptions.OMCMobileVersion.length; j++) {
                if (document.OMCOptions.OMCMobileVersion[j].checked) {
                    OMCMobileVersionOption = document.OMCOptions.OMCMobileVersion[j].value;
                    break;
                }                    
            }
        }
        if (document.OMCOptions.OMCVersion[i].checked) {
            // if the option is checked, make sure that the respective system reqs div is visible
            var divOMCActiveVersion = document.getElementById(sysreqdiv + "Reqs");
            if (divOMCActiveVersion != null) {
                divOMCActiveVersion.style.display = "block";
            }
            if (downloadlink != null) {
                if (OMCVersionOption != "MobilePC")
                    downloadlink.href = "http://www.overdrive.com/partners/redirects.aspx?product=OMCFor" + OMCVersionOption;
                else
                    downloadlink.href = "http://www.overdrive.com/partners/redirects.aspx?product=OMCFor" + OMCMobileVersionOption;
            }
            if (downloadinstructions != null) {
                downloadinstructions.style.display = "block";
                downloadinstructions.href = "javascript:showInstallInstructions('downloadinstructions" + sysreqdiv.toLowerCase() + ".asp');";
            }
            if (trtakemore != null) {
                if (OMCVersionOption == "Win")
                    trtakemore.style.display = "";
                else
                    trtakemore.style.display = "none";
            }
            if (winburn != null) {
                if (OMCVersionOption == "Win")
                    winburn.style.display = "";
                else
                    winburn.style.display = "none";
            }
            if (macburn != null) {
                if (OMCVersionOption == "Mac")
                    macburn.style.display = "";
                else
                    macburn.style.display = "none";
            }
            if (winwording != null) {
                if (OMCVersionOption == "Win")
                    winwording.style.display = "";
                else
                    winwording.style.display = "none";
            }
            if (macwording != null) {
                if (OMCVersionOption == "Mac")
                    macwording.style.display = "";
                else
                    macwording.style.display = "none";
            }        
        } else {
            // if the option isn't checked, make sure that the respective system reqs div isn't visible
            var divOMCInactiveVersion = document.getElementById(OMCVersionOption + "Reqs");
            if (divOMCInactiveVersion != null) {
                divOMCInactiveVersion.style.display = "none";
            }
        }
    }
}

function CloseAllFAQ() {
    // if id starts with question, set display to none
    var divCollection = document.getElementsByTagName("div");
    for (var i = 0; i < divCollection.length; i++) {
        if (divCollection[i].id.toString().indexOf("question") == 0) {
            divCollection[i].style.display = "none";
        }
    }
}

function SetDefaultOMCVersion() {
    var szDefaultVersion = "";
    var szFAQVersion = "";
    if (navigator.userAgent != null && navigator.userAgent.indexOf("Android") != -1) {
        szDefaultVersion = "Android";
        szFAQVersion = "FAQAndroid";
    } else if (navigator.userAgent == null || !(navigator.userAgent.indexOf("Mac OS") != -1 || navigator.userAgent.indexOf("Macintosh") != -1)) {
        szDefaultVersion = "Win";
        szFAQVersion = "FAQWindows";
    }
    else if (navigator.userAgent.indexOf("iPhone") != -1 || navigator.userAgent.indexOf("iPad") != -1) {
        szDefaultVersion = "iPhone";
        szFAQVersion = "FAQiPhone";
    } else {
        szDefaultVersion = "Mac";
        szFAQVersion = "FAQMac";
    }
    if (document.OMCOptions != null) {
        document.OMCOptions.style.display = "block";
        if (document.OMCOptions.OMCVersion != null) {
            for (var i = 0; i < document.OMCOptions.OMCVersion.length; i++) {
                document.OMCOptions.OMCVersion[i].checked = (document.OMCOptions.OMCVersion[i].value == szDefaultVersion);
            }
        }
        if (window.location.toString().indexOf("faq.asp") != -1) {
            GetOMCVersionFAQ(szFAQVersion);
        } else if (window.location.toString().indexOf("default") != -1 || window.location.toString().lastIndexOf("/") == window.location.toString().length - 1) {
            GetOMCVersionDefault();
        }
    }
}

function shToggle(content) {
    if (document.getElementById(content).style.display == "none")
        document.getElementById(content).style.display = "block"
    else
        document.getElementById(content).style.display = "none"
}

function showInstallInstructions(szPageName) {
    window.open(szPageName, null, 'height=500px, width=750px, status=no, menubar=no, scrollbars=yes');
}
