﻿/*
--------------------------------------------------------------------------
Code for link-hover text boxes
By Nicolas Hoening (Web Site: http://nicolashoening.de)
usage: <a onmouseover="popup('popup content', width)">a link</a>
(width is optional - default is in CSS: #popupContainer {width: x;},
escape " in content with &quot;)
--------------------------------------------------------------------------
*/

// create the popup box - remember to give it some width in your styling - moved to master page body HTML
//document.write('<div id="popupContainer" style="position:abolute; display:none; z-index:200;"></div>');

var minMargin = 15; // set how much minimal space there should be to
// between the popup and everything else (borders, mouse)
var ready = false; // we are ready when the mouse event is set up
var default_width = '500px'; // will be set to width from css in document.ready
var useTouchEvents;

jQuery(document).ready(function() {
    jQuery('#popupContainer').hide();
    default_width = jQuery('#popupContainer').width();
    // set dynamic coords when the mouse moves or touch starts  
    useTouchEvents = isEventSupported("touchstart");
    if(useTouchEvents){
        jQuery(document).live('touchstart', positionPopup);        
    }
    else {
        jQuery(document).mousemove(positionPopup);
    }
    ready = true;
});

function positionPopup(e)
{
    var x, y;

    if (useTouchEvents) {      
        x = jQuery(document).scrollLeft() + e.touches[0].pageX;
        y = jQuery(document).srollTop() + e.touches[0].pageY;
    }
    else {      
        x = jQuery(document).scrollLeft() + e.clientX;
        y = jQuery(document).scrollTop() + e.clientY;
    }
    x += 10; // important: if the popup is where the mouse is, the hoverOver/hoverOut events flicker
    //var x_y = nudge(x, y); // avoids edge overflow
    var x_y = [x, y];

    // remember: the popup is still hidden
    jQuery('#popupContainer').css('top', x_y[1] + 'px');
    jQuery('#popupContainer').css('left', x_y[0] + 'px');
}

function hidePopup(e){
    jQuery('#popupContainer').hide().width(default_width)
}

// avoid edge overflow
function nudge(x, y) {
    var win = jQuery(window);

    // When the mouse is too far on the right, put window to the left
    var xtreme = jQuery(document).scrollLeft() + win.width() - jQuery('#popupContainer').width() - minMargin;
    if (x > xtreme) {
        x -= jQuery('#popupContainer').width() + 2 * minMargin;
    }
    x = max(x, 0);

    // When the mouse is too far down, move window up
    if ((y + jQuery('#popupContainer').height()) > (win.height() + jQuery(document).scrollTop())) {
        y -= jQuery('#popupContainer').height() + minMargin;
    }

    return [x, y];
}

function popup(trigger, contentId, width) {  
    var content = jQuery('#' + contentId).html();
    if (typeof width === "undefined") {
        width = default_width;
    }   
    // write content and display
    if (ready) {
        jQuery('#popupContainer').width(width).html(content).show();
    }
    if(useTouchEvents) {
        jQuery(trigger).live('touchend', hidePopup);
    }
    else {
        jQuery(trigger).mouseout(hidePopup);
    }
}

function max(a, b) {
    if (a > b) return a;
    else return b;
}

var isEventSupported = (function() {
    var TAGNAMES = {
        'select': 'input', 'change': 'input',
        'submit': 'form', 'reset': 'form',
        'error': 'img', 'load': 'img', 'abort': 'img'
    }
    function isEventSupported(eventName) {
        var el = document.createElement(TAGNAMES[eventName] || 'div');
        eventName = 'on' + eventName;
        var isSupported = (eventName in el);
        if (!isSupported) {
            el.setAttribute(eventName, 'return;');
            isSupported = typeof el[eventName] == 'function';
        }
        el = null;
        return isSupported;
    }
    return isEventSupported;
})();





