﻿
// Variables
var g_PopupTimeoutId = null;
var g_HasDoctorsAssigned = false;

// Functions
function TopNav_Refresh() {
    var assignedDoctorsList = TopNav_GetAssignedDoctorsList();
    if (assignedDoctorsList == null)
        return;
        
    var html = TopNav_GetHtml(assignedDoctorsList);
    
    $('.TopMenu > div:last > a.blue').remove();
    $('.TopMenu > div:last').append(html);

    if (g_PopupTimeoutId != null) {
        clearTimeout(g_PopupTimeoutId);
        g_PopupTimeoutId = null;
    }
    // Hide 'New Doctor' popup
    $('#BubbleMsg').hide();
}

function TopNav_GetAssignedDoctorsList() {
    var assignedDoctorsList = null;
    g_HasDoctorsAssigned = false;

    $.ajax({
        async: false,
        cache: false,
        type: "POST",
        data: "onlyHan=true",
        url: "DoctorListHandler.ashx",
        success: function (data, ResponseInfo) {
            if (data != '') {
                var response = eval('(' + data + ')');

                g_HasDoctorsAssigned = response.hasDoctorsAssigned;
                assignedDoctorsList = response.list;
            }
        }
    });

    return assignedDoctorsList;
}

function TopNav_GetHtml(assignedDoctorsList) {
    var html = '';

    if (assignedDoctorsList == null || assignedDoctorsList.length == 0) {
        if (g_HasDoctorsAssigned)
            html = moreDoctorsNonHanHTML;
        else
            html = myDoctorsHTML;
    }
    else {

        if (assignedDoctorsList.length <= 3) {

            for (var i = 0; i < assignedDoctorsList.length; i++)
                html += TopNav_GetDocHtml(assignedDoctorsList[i]);

            $('#BubbleHidden').hide();
            $('#BubbleDefault').show();
        }
        else if (assignedDoctorsList.length > 3) {
            html += TopNav_GetDocHtml(assignedDoctorsList[0]);
            html += TopNav_GetDocHtml(assignedDoctorsList[1]);
            html += moreDoctorsHTML;

            // Get the last added doctor
            var lastDoc = assignedDoctorsList[assignedDoctorsList.length - 1];

            var text = $('#BubbleHidden').text();
            text = text.replace("{PREFIX}", lastDoc.prefix);
            text = text.replace("{LAST_NAME}", lastDoc.lastName);
            $('#BubbleHidden').text(text);

            $('#BubbleDefault').hide();
            $('#BubbleHidden').show();            
        }
    }

    return html
}

function TopNav_GetDocHtml(doctor) {
    if (doctor == null)
        return '';

    var lastName = doctor.lastName;
    if (lastName.length >= 18) {
        lastName = lastName.substring(0, 13) + '...';
    }

    var docTitle = doctor.prefix + ' ' + lastName;
    var href = doctor.homePage;
    if (href == null || href == '') {
        href = doctor.vitalsUrl;
    }

    return '<a id="doc' + doctor.id + '" ' +
            'href="' + href + '" ' +
            'title="' + doctor.prefix + ' ' + doctor.lastName + '" ' +
            'class="blue" onclick="s.addOmnitureVar(\'prop41\',\'mydoctors_customdrtab\');s.commitOmnitureData();"><span>' +
            docTitle +
            '</span></a>';
}

function TopNav_TriggerTabPopup() {
    var assignedDoctorsList = TopNav_GetAssignedDoctorsList();
    if (assignedDoctorsList != null) {
        if (assignedDoctorsList.length <= 3) {
            $('#BubbleHidden').hide();
            $('#BubbleDefault').show();
        }
        else if (assignedDoctorsList.length > 3) {
	// Get the last added doctor
            var lastDoc = assignedDoctorsList[assignedDoctorsList.length - 1];

            var text = $('#BubbleHiddenTemplate').text();
            text = text.replace("{PREFIX}", lastDoc.prefix);
            text = text.replace("{LAST_NAME}", lastDoc.lastName);
            $('#BubbleHidden').text(text);

            $('#BubbleDefault').hide();
            $('#BubbleHidden').show();            
        }
	}

    var jqBubbleMsg = $('#BubbleMsg');
    $('.getStarted').before(jqBubbleMsg);
    var leftCoord = 290 + ($('.TopMenu div a[class=blue]').length - 1) * 128;
    jqBubbleMsg[0].style.left = leftCoord + 'px';
    jqBubbleMsg.show();


    if (g_PopupTimeoutId != null)
        clearTimeout(g_PopupTimeoutId);
    g_PopupTimeoutId = setTimeout("$('#BubbleMsg').hide();", 10000);
}
