﻿var dmi;
var exid;

function showAfterInstallDiv() {
    jQuery(divInstall).css('display', 'none');
    jQuery(divAfterInstall).css('display', 'block');
}

function enableDownloadMethodArea(enabled) {
    jQuery(divExercisesWithDm + ' input').each(function(index, item) {
        jQuery(item).attr('disabled', !enabled);
    });
    if (enabled == true) {
        jQuery('#divDownloadImage').css('display', 'none');
        if (jQuery(rbUseLdcDm).is(':checked')) {
            jQuery(divDownloadButtonWithDm).css('display', 'block');
            jQuery(divDownloadButtonNoDm).css('display','none');
        }
        else {
            jQuery(divDownloadButtonWithDm).css('display', 'none');
            jQuery(divDownloadButtonNoDm).css('display', 'block');
        }
    }
    else {
        jQuery(divDownloadButtonWithDm).css('display', 'none');
        jQuery(divDownloadButtonNoDm).css('display', 'none');
        jQuery('#divDownloadImage').css('display', 'block');
    }
}

function CloseOverlay(parent, newLocation) {
    //closeJQModal();
    window.location = newLocation;
}

function dmPostDetection(dmInstalled) {
    dmi = null;
    if (dmInstalled == 'true') {
        dmi = true;
    }
    else {
        if (dmInstalled == 'false') {
            dmi = false;
        }
    }
    setDownloadMethod(jQuery(rbUseLdcDm).is(':checked'), dmi);
}

function setDownloadMethod(useLdcDm, dmInstalled) {
    if (dmInstalled == null) {
        dmInstalled = dmi;
    }
    if (useLdcDm == true) {
        jQuery(ibDownload).unbind('click', prepareNativeDl);
        if (dmInstalled == true) {
            jQuery(aDownload).attr('href', jQuery(hfExFile).val());
            jQuery(aDownload).unbind('click');
            jQuery(aDownload).click(function() {
                logAccessedExerciseFile(exid);
                return true;
            });
        }
        else {
            jQuery(aDownload).attr('href', jQuery(hfOverlay).val());
            jQuery(aDownload).unbind('click');
            jQuery(aDownload).click(function() {
                showDmOverlay();
                return false;
            });
        }
    }
    else {
        jQuery(ibDownload).unbind('click', prepareNativeDl).bind('click', { 'eid': exid }, prepareNativeDl);
    }
}

function showDmOverlay() {
    openJQModal(jQuery(hfOverlay).val(), 735, 480, '', 'DM', null, true);
}

function initDownloadArea(eid, useLdcDm, dmInstalled) {
    exid = eid;
    jQuery(ibDownload).unbind('click', prepareNativeDl).bind('click', { 'eid': eid }, prepareNativeDl);
    jQuery(aDownload).unbind('click');
    jQuery(aDownload).click(function() {
        logAccessedExerciseFile(eid);
        return true;;
    });
    useLdcDm = (useLdcDm == 'True');
    dmInstalled = (dmInstalled == 'True');
    setDownloadMethod(useLdcDm, dmInstalled);
}

function prepareNativeDl(event) {
    eid = event.data.eid;
    exid = eid;
    downloadExFileNoDm(eid, true);
    return false;
}

