/* Sticky Tooltip script (v1.0)
* Created: Nov 25th, 2009. This notice must stay intact for usage 
* Author: Dynamic Drive at http://www.dynamicdrive.com/
* Visit http://www.dynamicdrive.com/ for full source code
*/

function getStyle(obj, CssProperty)
{
    if(obj.currentStyle)
    {
        var convertToCamelCase = CssProperty.replace(/\-(.)/g, function(m, l){return l.toUpperCase()});
        return obj.currentStyle[convertToCamelCase];
    }
    else if (window.getComputedStyle)
    {
        var elementStyle = window.getComputedStyle(obj, "");
        return elementStyle.getPropertyValue(CssProperty);
    }
}

var stickytooltip = {
    tooltipoffsets: [20, -30], //additional x and y offset from mouse cursor for tooltips
    fadeinspeed: 200, //duration of fade effect in milliseconds
    rightclickstick: true, //sticky tooltip when user right clicks over the triggering element (apart from pressing "s" key) ?
    stickybordercolors: ["green", "darkred"], //border color of tooltip depending on sticky state
    stickynotice1: ["Press \"s\"", "or right click", "to sticky box"], //customize tooltip status message
    stickynotice2: "Click outside this box to hide it", //customize tooltip status message

    //***** NO NEED TO EDIT BEYOND HERE

    isdocked: false,

    positiontooltip: function ($, $tooltip, e) {
        var x = e.pageX + this.tooltipoffsets[0], y = e.pageY + this.tooltipoffsets[1]
        var tipw = $tooltip.outerWidth(), tiph = $tooltip.outerHeight(),
		x = (x + tipw > $(document).scrollLeft() + $(window).width()) ? x - tipw - (stickytooltip.tooltipoffsets[0] * 2) : x
        y = (y + tiph > $(document).scrollTop() + $(window).height()) ? $(document).scrollTop() + $(window).height() - tiph - 10 : y
        $tooltip.css({ left: x, top: y })
    },

    showbox: function ($, $tooltip, e, popupURL, borderClass, borderColor, tooltipCaption, overwrite, videoURL, container) {
        var html = "<div class='" + borderClass + "' style='border:solid 3px " + borderColor + ";'>"
        if (tooltipCaption != "" && tooltipCaption != null && tooltipCaption != "null")
            html += "<div style='text-align:center;font-size:20px' >" + tooltipCaption + "</div>";

        if (overwrite == null || (overwrite != null && overwrite == false))
            html += "<div style=''><img src='" + gVirtualUploadPath + "showimage2.aspx?img=" + popupURL + "&w=400&h=400' style='line-height:0; margin:0; margin-bottom:-5px; border:0;' /></div>";

        if (videoURL != null && videoURL != "") {
            html += "<div id='" + container + "' style='margin:0 auto' class='flashplayer'>&nbsp;</div>";
            setTimeout(function () {
            PlayGoogleAudio(gVirtualUploadPath + videoURL, container, "true");
            }, 1);
        }
		html += "</div>";

		$tooltip.html(html);
        $tooltip.fadeIn(this.fadeinspeed);
        this.positiontooltip($, $tooltip, e)
    },

    hidebox: function ($, $tooltip) {
        if (!this.isdocked) {
            $tooltip.stop(false, true).hide()
            $tooltip.css({ borderColor: 'green' }).find('.stickystatus:eq(0)').css({ background: this.stickybordercolors[0] }).html(this.stickynotice1)
            //alert($tooltip.find(".flashplayer").html());
            $tooltip.find(".flashplayer").html("");
			$tooltip.find(".flashplayer").empty();
			$tooltip.find("#container").empty();
			$tooltip.find("#container").html("");
        }
    },

    docktooltip: function ($, $tooltip, e) {
        this.isdocked = true
        $tooltip.css({ borderColor: 'darkred' }).find('.stickystatus:eq(0)').css({ background: this.stickybordercolors[1] }).html(this.stickynotice2)
    },


    init: function (targetselector, tipid) {
        $(document).ready(function () {
            var $targets = $(targetselector)
            var $tooltip = $('#' + tipid).appendTo(document.body)
            if ($targets.length == 0)
                return
            var $alltips = $tooltip.find('div.atip')
            if (!stickytooltip.rightclickstick)
                stickytooltip.stickynotice1[1] = ''
            try {
                stickytooltip.stickynotice1 = stickytooltip.stickynotice1.join(' ');
            } catch (err) { }
            stickytooltip.hidebox($, $tooltip)
            $targets.bind('mouseenter', function (e) {
                //$alltips.hide().filter('#'+$(this).attr('data-tooltip')).show()
                var borderClass, borderColor, imgCaption;
                imgCaption = "";

                if ($(e.target).parent().parent().hasClass("individuals_photo_blue")) borderClass = "individuals_photo_blue";
                else if ($(e.target).parent().parent().hasClass("individuals_photo_green")) borderClass = "individuals_photo_green";
                else if ($(e.target).hasClass("memberPhotoBlue")) borderClass = "memberPhotoBlue";
                else if ($(e.target).hasClass("memberPhotoGreen")) borderClass = "memberPhotoGreen";

                if ($(e.target).parent().get(0).tagName.toLowerCase() == "a") {
                    borderColor = getStyle($(e.target).parent().parent().get(0), "border-bottom-color");
                    imgCaption = $(".individualinfo", $(e.target).parent().parent()).html();
                }
                else {
                    borderColor = getStyle($(e.target).parent().get(0), "border-bottom-color");
                    imgCaption = $(".individualinfo", $(e.target).parent()).html();
                }
                borderColor = "#fff"
                stickytooltip.showbox($, $tooltip, e, $(e.target).attr("popupURL"), borderClass, borderColor, imgCaption, ($(e.target).attr("overwrite") == "1"), $(e.target).attr("videoURL"), $(e.target).attr("container"))
            })
            $targets.bind('mouseleave', function (e) {
                stickytooltip.hidebox($, $tooltip)
            })
            $targets.bind('mousemove', function (e) {
                if (!stickytooltip.isdocked) {
                    stickytooltip.positiontooltip($, $tooltip, e)
                }
            })
            $tooltip.bind("mouseenter", function () {
                stickytooltip.hidebox($, $tooltip)
            })
            $tooltip.bind("click", function (e) {
                e.stopPropagation()
            })
            $(this).bind("click", function (e) {
                if (e.button == 0) {
                    stickytooltip.isdocked = false
                    stickytooltip.hidebox($, $tooltip)
                }
            })
            $(this).bind("contextmenu", function (e) {
                if (stickytooltip.rightclickstick && $(e.target).parents().andSelf().filter(targetselector).length == 1) { //if oncontextmenu over a target element
                    stickytooltip.docktooltip($, $tooltip, e)
                    return false
                }
            })
            $(this).bind('keypress', function (e) {
                var keyunicode = e.charCode || e.keyCode
                if (keyunicode == 115) { //if "s" key was pressed
                    stickytooltip.docktooltip($, $tooltip, e)
                }
            })
        }) //end dom ready
    }
}

//stickytooltip.init("targetElementSelector", "tooltipcontainer")
stickytooltip.init("*[data-tooltip]", "mystickytooltip")
