﻿var prescriberSoftware = {
	idOfOpenDialog: null,
	openDialog: function(elem, wide) {
		if (prescriberSoftware.idOfOpenDialog != null)
		{
			$(prescriberSoftware.idOfOpenDialog).dialog("close");
		}

		prescriberSoftware.idOfOpenDialog = "#" + elem.id + "Dialog";
		$(prescriberSoftware.idOfOpenDialog).dialog("open");
		var dialogX = $(elem).offset().left - $(window).scrollLeft() + $(elem).width();
		var dialogY = $(elem).offset().top - $(window).scrollTop() + ($(elem).height() / 2) - ($(prescriberSoftware.idOfOpenDialog).height() / 2);

		var width = (wide ? 500 : 300);
		var height = 200;

		if ($(window).width() < (dialogX + width)) {
			var dialogXOld = dialogX;
			dialogX = $(elem).offset().left - $(document).scrollLeft() - width - 10;
			if (dialogX < $(window).scrollLeft()) {
				dialogX = dialogXOld;
			}
		}

		$(prescriberSoftware.idOfOpenDialog).dialog("option", {"position": [dialogX, dialogY], "height": height.toString() + "px", "width": width.toString() + "px"});
	}
};

var idOfOpenDialog = null;

$(document).ready(function() {

    if (window.location.search.search(/print/) > -1) {
        window.print();
        window.close();
    }

    //    if (window.location.search.search(/email/) == -1) {
    //        $("#sendDialog").dialog("open");
    //        infieldlabels();
    //    }

    $(".vendorInformation,.productInfo,#prescriptionBenefitHelp,#medicationHistoryHelp,#prescriptionRoutingHelp,#emrHelp,#standaloneHelp,#goldSolutionProviderHelp").each(function() {
        $(this).next().dialog({
            autoOpen: false,
            close: function() {
                prescriberSoftware.idOfOpenDialog = null;
            }
        });
    });

    $("#emrHelpDialog,#standaloneHelpDialog,#goldSolutionProviderHelpDialog").each(function() {
        $(this).dialog({
            autoOpen: false,
            close: function() {
                prescriberSoftware.idOfOpenDialog = null;
            }
        });
    });

    $(".paginator a,.viewAll,.print,.email").each(function() {
        var href = this.href
			+ ($("#VPKeyword")[0].value != "" ? "&VPKeyword=" + encodeURIComponent($("#VPKeyword")[0].value) : "")
			+ ($(".productType:checked").val() != "" ? "&productType=" + $(".productType:checked").val() : "")
			+ ($("#solutionProviderGold")[0].checked ? "&solutionProviderGold=on" : "")
			+ "&sortBy=" + $("#sortByUE")[0].value;

        this.href = href;
    });

    $(".paginator a").each(function() {
        this.href = this.href + "#top-of-grid";
    });

    $("#sortByUE").change(function() {
        if (window.location.pathname.indexOf('search.aspx') > -1) {
            if ($('#VPKeyword').val() == '') {
                $('#VPFrmSolutionType').attr('action', $('#VPFrmSolutionType').attr('action').split('#')[0].split('?')[0] + '?sortBy=' + $("#sortByUE")[0].value + '#top-of-grid');
                $("#VPSearch").click();
            }
            else {
                $('#VPFrm').attr('action', $('#VPFrm').attr('action').split('#')[0] + '#top-of-grid');
                $(".sortBy").val($("#sortByUE").val());
                $("#KeywordSearchButton").click();
            }
        }
        else {
            window.location = window.location.pathname.split('?')[0] + '?sortBy=' + $("#sortByUE")[0].value + '#top-of-grid';
        }

    });

    $(".vendorInformation").click(function(event) {
        prescriberSoftware.openDialog(this, true);
        return false;
    });

    $(".productInfo,#prescriptionBenefitHelp,#medicationHistoryHelp,#prescriptionRoutingHelp,#emrHelp,#standaloneHelp,#goldSolutionProviderHelp").click(function(event) {
        prescriberSoftware.openDialog(this, $(this).hasClass("prodctInfo"));
        event.preventDefault();
    });


});
