﻿jQuery(document).ready(function($) {
    //Tooltips
    $(".tip_trigger").hover(function(){
        tip = $(this).find('.tip');
        tip.show(); //Show tooltip
    }, function() {
        tip.hide(); //Hide tooltip
    }).mousemove(function(e) {
    	var mousex = (e.pageX - $("#wrapper").position().left) + 20; //Get X coodrinates
        var mousey = (e.pageY - $("#wrapper").position().top) + 20; //Get Y coordinates
        var tipWidth = tip.width(); //Find width of tooltip
        var tipHeight = tip.height(); //Find height of tooltip

        //Distance of element from the right edge of viewport
        var tipVisX =   ($(".slider-container").position().left + $("#wrapper").width()) - (mousex + tipWidth) ;
        //var tipVisX = $("#wrapper").width() - (mousex + tipWidth) ;
        //Distance of element from the bottom of viewport
        var tipVisY = $("#wrapper").height() - (mousey + tipHeight);
        if ( tipVisX < 20 ) { //If tooltip exceeds the X coordinate of viewport
//            mousex = (e.pageX - $("#wrapper").position().left) - tipWidth - 20;
//        if ( tipVisX < (($(".slider-container").position().left + $("#wrapper").width()) - 20) ) { //If tooltip exceeds the X coordinate of viewport
            mousex = (e.pageX - $("#wrapper").position().left - $(".slider-container").position().left) - tipWidth - 20;
        } if ( tipVisY < 20 ) { //If tooltip exceeds the Y coordinate of viewport
            mousey = (e.pageY - $("#wrapper").position().top) - tipHeight - 20;
        }
        //Absolute position the tooltip according to mouse position
        tip.css({  top: mousey, left: mousex });
    });
});

