﻿
//Medicine Search
function medSearch_LettersCheckClicked() {
    var checkbox = $(this);
    if(checkbox.attr("checked"))
        $(".medSearch .letters span").show();
    else
        $(".medSearch .letters span").hide();
}

//Medicine List

var globalNameKeyCount = 0;
var globalLettersKeyCount = 0;

function MedicineNameKeyPressed() {
    globalNameKeyCount++;
    setTimeout("MedicineNameLookUp(" + globalNameKeyCount + ")", 500);
}

function MedicineNameLookUp(keyCount) {
    if (keyCount == globalNameKeyCount) {
        MedicineListSearch(defaultStartIndex, defaultCount);
    }
}

function MedicineLettersKeyPressed() {
    globalLettersKeyCount++;
    setTimeout("MedicineLettersLookUp(" + globalLettersKeyCount + ")", 500);
}

function MedicineLettersLookUp(keyCount) {
    if (keyCount == globalLettersKeyCount) {
        MedicineListSearch(defaultStartIndex, defaultCount);
    }
}

function MedicinePrevPage() {

    var pageIndex = parseInt($(".medicines .paging:first span.index").text());

    if (pageIndex > 0) {
    	$.scrollTo('#medicineList', 1);
        //Load medicine
        MedicineListSearch((pageIndex-1) * defaultCount, defaultCount);       
    }
    else {
        //Hide the prev page link
        $(".medicines .paging a.prev").hide();
    }
}

function MedicineNextPage() {
    var pageIndex = parseInt($(".medicines .paging:first span.index").text());
    var lastPage = $(".medicines table").hasClass("lastPage");

    if (!lastPage) {    		
		$.scrollTo( '#medicineList', 1 );
        //Load medicine
        MedicineListSearch((pageIndex + 1) * defaultCount, defaultCount);        
    }
    else {
        //Hide the next page link
        $(".medicines .paging a.next").hide();
    }
}

function InitalizePager(pageIndex) {

    var lastPage = $(".medicines table").hasClass("lastPage");
    $(".medicines .paging span.index").text(pageIndex);
    $(".medicines .paging span.page").text(pageIndex + 1);

    $(".medicines .paging a.prev").attr("href", "#page" + pageIndex)
    $(".medicines .paging a.next").attr("href", "#page" + (pageIndex + 2))

    if (pageIndex == 0 && lastPage) {
        //Hide the pager
        $(".medicines .paging").hide();        
    }
    else {
        //Show the pager
        $(".medicines .paging").show();
        
        if (pageIndex > 0) {
            //show the prev page link
            $(".medicines .paging a.prev").show();
        }
        else {
            //Hide the prev page link
            $(".medicines .paging a.prev").hide();
        }

        if (lastPage) {
            //Hide the next page link
            $(".medicines .paging a.next").hide();
        }
        else {
            //Show the next page link
            $(".medicines .paging a.next").show();
        }
    }
}

function HidePager() {
    //Hide the pager
    $(".medicines .paging").hide();
}

function ClearMedicineSearchForm() {
    if (!loadInProgress) {
        $(".medSearch .name input:text").val('');
        //Divider
        $(".medSearch .divider input.check").removeAttr("checked");
        //Letters
        $(".medSearch .letters input:check").removeAttr("checked");
        $(".medSearch .letters input:text").val('');
        $(".medSearch .letters span").hide();
        //Color    
        $(".medSearch .colors input:check").removeAttr("checked");
        //$(".medSearch .colors .select select").val(0);
        $(".medSearch .colors .select li").removeClass("selected");
        //Shape
        //$(".medSearch .shapes .select select").val(0);
        $(".medSearch .shapes .select li").removeClass("selected");
        
        //Mark that an medicine load is progress
        loadInProgress = true;

        //Get the medicine data
        Lyfjabok.Services.Medicine.GetMedicines('', false, false, '', 0, false, 0, '', 0, defaultCount + 1, MedicineListSearchComplete, MedicineListSearchFailed, /*page index*/0);
    }
}

var loadInProgress = false;

function MedicineListSearch(startIndex, count) {
    if (!loadInProgress) {

        var name = '';
        var divider = false;
        var useLetters = false;
        var letters = '';
        var twoColors = false;
        var colorID = 0;
        var shapeID = 0;
        
        var atcName = $(".medicines input[type=hidden]:first").val();
        if (atcName == '') {
            name = $(".medSearch .name input:text").val();
            //Divider
            divider = $(".medSearch .divider input.check").attr("checked");
            //Letters
            useLetters = $(".medSearch .letters input:check").attr("checked");
            letters = $(".medSearch .letters input:text").val();
            //Color    
            twoColors = $(".medSearch .colors input:check").attr("checked");
            var color = $(".medSearch .colors .select li.selected");
            if (color.length > 0)
                colorID = color.attr("rel");            
            //Shape
            var shape = $(".medSearch .shapes .select li.selected");
            if (shape.length > 0)
                shapeID = shape.attr("rel");
        }        

        //Mark that an medicine load is progress
        loadInProgress = true;

        //Get the medicine data
        Lyfjabok.Services.Medicine.GetMedicines(name, divider, useLetters, letters, colorID, twoColors, shapeID, atcName, startIndex, count + 1, MedicineListSearchComplete, MedicineListSearchFailed, /*page index*/startIndex / count);
    }
}

function MedicineListSearchComplete(data, pageIndex, methodName) {
    if (data != null) {
        if (data.length > 0) {
            //Clear the list
            $(".medicines tbody").html('');

            var Medicine;
            var medCount = data.length;
            
            //Loop through the medicines
            for (var i = 0; i < data.length; i++) {
                if (i < defaultCount) {
                    Medicine = data[i];

                    var nameLink = "<a href='/Lyf/" + Medicine.FriendlyName + "/' title='" + Medicine.Name + "'>" + Medicine.Name + "</a>";
                    
                    if (Medicine.IsInactive)
                        nameLink = "<span class='n'>" + Medicine.Name + "</span>";
                    /*
                    if (Medicine.IsInactive)
                        nameLink = "<span class='n'>" + Medicine.FriendlyName + "</span>";
                    */
                    if (Medicine.ImageMissing || Medicine.PillID <= 0)
                        $(".medicines tbody").append("<tr><td class='name'>" + nameLink + "<p>" + Medicine.Description + "</p></td><td class='chemicals'>"+Medicine.Chemicals+"</td><td class='type'>" + Medicine.AtcName + "</td><td class='strength'>" + Medicine.Strength + "</td><td class='image'><img src='/Images/no_pill.png' alt='' title='" + Medicine.Name + " " + Medicine.Strength + "' /></td></tr>");
                    else
                        $(".medicines tbody").append("<tr><td class='name'>" + nameLink + "<p>" + Medicine.Description + "</p></td><td class='chemicals'>" + Medicine.Chemicals + "</td><td class='type'>" + Medicine.AtcName + "</td><td class='strength'>" + Medicine.Strength + "</td><td class='image'><img src='/ImageTh.aspx?height=66&width=66&img=/modules/Medicines/Pills/pill_" + Medicine.PillID + ".jpg' alt='' title='" + Medicine.Name + " " + Medicine.Strength + "' /></td></tr>");
                }
            }

            if (medCount <= defaultCount)
                $(".medicines table").addClass("lastPage");
            else
                $(".medicines table").removeClass("lastPage");
                
            //InitalizePager
            InitalizePager(pageIndex);
            
            Medicine = null;

            var atcCategory = $(".medicines input[type=hidden]:first").val();
            
            var criteria = $(".medSearch .name input:text").val();
            if (atcCategory == '' && criteria != '')
                $('".medicines tbody td.name a, .medicines tbody td.chemicals').highlight(criteria);
             
            //Add the class to the alternating table rows
            $(".medicines tbody tr:odd").addClass("alt");
            
        }
        else {
            //Hide the Pager
            HidePager();
            
            $(".medicines tbody").html('<tr><td colspan="6" class="nothing">Engin lyf fundust.</td></tr>');
            
        }
    }
    else {
        //Hide the Pager
        HidePager();
        
        $(".medicines tbody").html('<tr><td colspan="6" class="nothing">Engin lyf fundust.</td></tr>');
    }

    loadInProgress = false;
}

function MedicineListSearchFailed(exception, pageIndex, methodName) {
    alert('2. Medicine data error');
    loadInProgress = false;
}

//Medicine
function ToggleCrossEffectList(lnk) {
    var crossContainer;
    var link;

    if (lnk == "more1") {
        crossContainer = $(".medicine .rightCol .crossEffects .crossEffect1Container");
        link = $(".medicine .rightCol .crossEffects a.more1");
    }
    else {
        crossContainer = $(".medicine .rightCol .crossEffects .crossEffect2Container");
        link = $(".medicine .rightCol .crossEffects a.more2");
    }

    crossContainer.removeClass("minimized");
    link.hide();
}

var pillLoadInProgress = false;

//Pill
function LoadPill( ) {
    if (!pillLoadInProgress) {
        var pillID = $(this).parent("li").attr("rel");
        var medicineID = 0;
        pillLoadInProgress = true;
        Lyfjabok.Services.Medicine.GetPill( pillID, medicineID, LoadPillComplete, LoadPillFailed );
    }
}

function FormatDay(day) {
    if (day < 10)
        return "0" + day;
    else
        return day;

}

function FormatMonth(month) {
    month = month + 1;
    if (month < 10)
        return "0" + month;
    else
        return month;
}

function LoadPillComplete(data, userContext, methodName) {
    if (data != null) {
        var pill = data[0];
        $(".medicine .rightCol .pills .tabs li").removeClass("selected");
        $(".medicine .rightCol .pills .tabs li[rel=" + pill.PillID + "]").addClass("selected");

        if (pill.DateRemoved != null && pill.DateRemoved.getFullYear() != 1) {

            if (pill.DateRemoved.getFullYear() > 1990)
                $(".medicine .rightCol .pills .dateRemoved").text("Afskráð: " + FormatDay(pill.DateRemoved.getDate()) + '.' + FormatMonth(pill.DateRemoved.getMonth()) + '.' + pill.DateRemoved.getFullYear()).show();
            else
                $(".medicine .rightCol .pills .dateRemoved").text("Afskráð").show();
        }
        else
            $(".medicine .rightCol .pills .dateRemoved").hide();

        //Image
        if (pill.ImageMissing) {
            $(".medicine .rightCol .pills .image").html( "<img src='/Images/no_pill_large.png' title='' />");
            
        }
        else {
            $(".medicine .rightCol .pills .image").html("<a href='/ImageTh.aspx?width=800&img=/modules/Medicines/Pills/pill_" + pill.PillID + ".jpg' title='" + pill.Strength + ' ' + pill.StrengthType + "'><img src='/ImageTh.aspx?width=150&img=/modules/Medicines/Pills/pill_" + pill.PillID + ".jpg' title='" + pill.Strength + ' ' + pill.StrengthType + "' /></a>");
            $(".medicine .rightCol .pills .image a").lightBox({
                overlayBgColor: '#000',
                overlayOpacity: 0.6,
                imageLoading: '/images/lightbox-ico-loading.gif',
                imageBtnClose: '/images/lightbox-btn-close.gif',
                imageBtnPrev: '/images/lightbox-btn-prev.gif',
                imageBtnNext: '/images/lightbox-btn-next.gif',
                containerResizeSpeed: 350,
                txtImage: 'Mynd',
                txtOf: 'af'
            });
        }

        //Info
        if (pill.PillColorAID != 0)
            $(".medicine .rightCol .pills td.a_color").html(pill.PillColorAName);
        else
            $(".medicine .rightCol .pills td.a_color").html("");

        if (pill.PillColorBID != 0)
            $(".medicine .rightCol .pills td.b_color").html(pill.PillColorBName);
        else
            $(".medicine .rightCol .pills td.b_color").html("");
            
        $(".medicine .rightCol .pills td.a_letters").html(pill.LettersA);
        $(".medicine .rightCol .pills td.b_letters").html(pill.LettersB);

        if (pill.ImageA)
            $(".medicine .rightCol .pills td.a_image").html("Já");
        else
            $(".medicine .rightCol .pills td.a_image").html("");

        if (pill.ImageB)
            $(".medicine .rightCol .pills td.b_image").html("Já");
        else
            $(".medicine .rightCol .pills td.b_image").html("");

        if (pill.PillTypeID != 0)
            $(".medicine .rightCol .pills td.type").html(pill.PillTypeName);
        else
            $(".medicine .rightCol .pills td.type").html("");
        
        if (pill.PillShapeID != 0)
            $(".medicine .rightCol .pills td.shape").html(pill.PillShapeName);
        else
            $(".medicine .rightCol .pills td.shape").html("");

        if (pill.PillBaseShapeID != 0)
            $(".medicine .rightCol .pills td.baseshape").html(pill.PillBaseShapeName);
        else
            $(".medicine .rightCol .pills td.baseshape").html("");
        
        if (pill.Divider)
            $(".medicine .rightCol .pills td.divider").html("Já");
        else
            $(".medicine .rightCol .pills td.divider").html("");
        if (pill.Strength != "")
            $(".medicine .rightCol .pills td.strength").html(pill.Strength + ' ' + pill.StrengthType);
        else
            $(".medicine .rightCol .pills td.strength").html("");

        if (pill.Height != "")
            $(".medicine .rightCol .pills td.height").html(pill.Height + "mm");
        else
            $(".medicine .rightCol .pills td.height").html("");

        if (pill.Width != "")
            $(".medicine .rightCol .pills td.width").html(pill.Width + "mm");
        else
            $(".medicine .rightCol .pills td.width").html("");

        if (pill.Length != "")
            $(".medicine .rightCol .pills td.length").html(pill.Length + "mm");
        else
            $(".medicine .rightCol .pills td.length").html("");

        if (pill.Diameter != "")
            $(".medicine .rightCol .pills td.diameter").html(pill.Diameter + "mm");
        else
            $(".medicine .rightCol .pills td.diameter").html("");
    }
    pillLoadInProgress = false;
}

function LoadPillFailed(exception, userContext, methodName) {
    pillLoadInProgress = false;
}
