var popuptext_dialogDiv;
var popuptext_arrowDiv;
///ShowDialog
///direction: arrow value: 0: top right
///						   1: bottom right
///						   2: top left				
///						   3: bottom left
function OpenPopupText(posX, posY, content, dialogDiv, arrowDiv) {
    popuptext_dialogDiv = dialogDiv;
    popuptext_arrowDiv = arrowDiv;
    var direction = 2;



    var left_arrow; // left of arrow div (px)
    var top_arrow; // topof arrow div (px)
    var left_dialog; // left of dialog div (px)
    var top_dialog; // topof dialog div (px)

    dialogDiv.fadeIn(300);
    arrowDiv.fadeIn(300);

    arrowDiv.css("left", (posX - 14) + "px");
    arrowDiv.css("top", (posY - 14) + "px");

    dialogDiv.html(content);

    left_arrow = arrowDiv.position().left;
    top_arrow = arrowDiv.position().top;


    // Ajust direction [start]
    var w = dialogDiv.outerWidth() + 20;
    var h = dialogDiv.height();
    if ((posX - $(window).scrollLeft()) < w) {
        if (($(window).scrollTop() + $(window).height() - posY) < h) {

            direction = 3;
        }
        else {
            direction = 2;
        }
    }
    else {
        //$("#txtSubject").val($(window).scrollTop()+$(window).height() - posY);
        $("#txtSubject").val(h);
        if (($(window).scrollTop() + $(window).height() - posY) < h) {
            direction = 1;
        }
        else {
            direction = 0;
        }
    }
    // Ajust direction [ end ]

    switch (direction) {
        case 0: left_dialog = left_arrow - dialogDiv.outerWidth();        
            top_dialog = top_arrow - 20;

            arrowDiv.css("border-color", "transparent transparent transparent #ccd");
            dialogDiv.css("-moz-box-shadow", "-3px 3px 3px #777");
            dialogDiv.css("-webkit-box-shadow", "-3px 3px 3px #777");
            dialogDiv.css("box-shadow", "-3px 3px 3px #777");
            break;
        case 1: left_dialog = left_arrow - dialogDiv.outerWidth();        
            top_dialog = top_arrow - dialogDiv.height() + 30;

            arrowDiv.css("border-color", "transparent transparent transparent #ccd");
            dialogDiv.css("-moz-box-shadow", "-3px 3px 3px #777");
            dialogDiv.css("-webkit-box-shadow", "-3px 3px 3px #777");
            dialogDiv.css("box-shadow", "-3px 3px 3px #777");
            break;
        case 2: left_dialog = left_arrow + 24;
            top_dialog = top_arrow - 20;

            arrowDiv.css("border-color", "transparent #ccd transparent transparent");
            dialogDiv.css("-moz-box-shadow", "3px 3px 3px #777");
            dialogDiv.css("-webkit-box-shadow", "3px 3px 3px #777");
            dialogDiv.css("box-shadow", "3px 3px 3px #777");
            break;
        case 3: left_dialog = left_arrow + 24;        
            top_dialog = top_arrow - dialogDiv.height() + 30;

            arrowDiv.css("border-color", "transparent #ccd transparent transparent");
            dialogDiv.css("-moz-box-shadow", "3px 3px 3px #777");
            dialogDiv.css("-webkit-box-shadow", "3px 3px 3px #777");
            dialogDiv.css("box-shadow", "3px 3px 3px #777");
            break;
    }
    dialogDiv.css("left", left_dialog + "px");
    dialogDiv.css("top", (top_dialog + 10) + "px");
};

/*
Close popup text
*/
function ClosePopupText() {
    if (popuptext_dialogDiv) {
        $(popuptext_dialogDiv).fadeOut(300);
    }
    if (popuptext_arrowDiv) {
        $(popuptext_arrowDiv).fadeOut(300);
    }
};


/*
  Open default system email to send
*/
function MailTo(email, subject) {
    window.open("mailto:" + email + "?subject=" + subject, '_self');
};