﻿// JScript File

//<div id="photoPopup" class="photoPopup">
//    <img id="imgPhotoPreview"  />
//</div>   


    var popupXOffset = 10;
    var popupYOffset = 10;
    
    // onload of the image I am using these values
    var currentMouseX=0;
    var currentMouseY=0;
    
    var currentMovingPopup;

    function WritePhotoPreviewDiv()
    {
        document.write('<div id=\"photoPopup\" class=\"photoPopup\">');
        document.write('<img id=\"imgPhotoPreview\" class=\"photoPreviewImage\"  />');
        //document.write('<img id=\"imgPhotoPreview\"  />');
        document.write('<img style=\"display: none;\" src=\"images/ajaxupdateblue.gif\" id=\"PhotoPreview_LoadingImg\" />');
        document.write('<div id=\"testDiv\"></div>');
        document.write('</div>');
    } 

    function OpenPhotoPreview(popupid, url)
    {
    
        ge('PhotoPreview_LoadingImg').style.display = 'block';
        ge('imgPhotoPreview').style.display = 'none';
        
        currentMovingPopup = document.getElementById(popupid);
        
//        popupXOffset = -400;
//        popupYOffset = 20;           
        
        //document.getElementById('imgPhotoPreview').src = 'http://www.mochalife.com/' + url;
        
        
        document.onmousemove = getMouseXY;
        
        
        document.getElementById('imgPhotoPreview').src = url;
        var image = ge('imgPhotoPreview');        
        image.onload = PreviewImage_OnLoad;        
    }        
    
    function HidePhotoPreview(popupId)
    {
        document.onmousemove = null;   
        //var popup = ge(popupId);
          
        document.getElementById(popupId).style.display = 'none';
    }    
    
    // Main function to retrieve mouse x-y pos.s

    function getMouseXY(e) 
    {               
        var testDiv = ge('testDiv');
        var windowWidth = f_clientWidth();
        var windowHeight = f_clientHeight();
        var mouseY;
        
        if ( navigator.appName.indexOf("Microsoft")!=-1) 
        {            
            
            tempY = event.clientY;
            tempX = event.clientX;                                   
        }
        else
        {
            
            tempY = e.clientY;
            tempX = e.clientX;                        
        }   
        
        currentMouseX = tempX;
        currentMouseY = tempY;        

        var xPos = CalculatePopupX(tempX);
        var yPos = CalculatePopupY(tempY);
                
        currentMovingPopup.style.left = xPos + 'px';
        currentMovingPopup.style.top = yPos + 'px';

        currentMovingPopup.style.display = 'block';

        return true
    }
    
    function CalculatePopupX(mouseX)
    {            
        var windowWidth = f_clientWidth();
    
         var photoPopup = ge('photoPopup');
        
        if( (mouseX + photoPopup.offsetWidth) > windowWidth )
        {
            //var diffX = (tempX + photoPopup.offsetWidth) - windowWidth;
            mouseX -= photoPopup.offsetWidth + 20;
        }

        //testDiv.innerHTML = f_clientWidth() + ' ' + f_clientHeight();          
        
        // catch possible negative values in NS4
        if (mouseX < 0){mouseX = 0}        

        return mouseX + popupXOffset + document.documentElement.scrollLeft;        
    }        
    
    function CalculatePopupY(mouseY)
    {
        var windowHeight = f_clientHeight();
        var photoPopup = ge('photoPopup');

        if( (mouseY + photoPopup.offsetHeight) > windowHeight )
        {
            var diffY = (mouseY + photoPopup.offsetHeight) - windowHeight;
            mouseY -= diffY + 20;
        }                
        
        // catch possible negative values in NS4        
        if (mouseY < 0){mouseY = 0}  
        
        return mouseY + popupYOffset + document.documentElement.scrollTop;       
    }    
    
    function PreviewImage_OnLoad()
    {
        ge('PhotoPreview_LoadingImg').style.display = 'none';
        ge('imgPhotoPreview').style.display = 'block';
        
        var photoPopup = ge('photoPopup');

        photoPopup.style.left = CalculatePopupX(currentMouseX) + 'px';
        photoPopup.style.top = CalculatePopupY(currentMouseY) + 'px';
    }    
    
    function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
    }
    function f_clientHeight() {
	    return f_filterResults (
		    window.innerHeight ? window.innerHeight : 0,
		    document.documentElement ? document.documentElement.clientHeight : 0,
		    document.body ? document.body.clientHeight : 0
	    );
    }
    function f_scrollLeft() {
	    return f_filterResults (
		    window.pageXOffset ? window.pageXOffset : 0,
		    document.documentElement ? document.documentElement.scrollLeft : 0,
		    document.body ? document.body.scrollLeft : 0
	    );
    }
    function f_scrollTop() {
	    return f_filterResults (
		    window.pageYOffset ? window.pageYOffset : 0,
		    document.documentElement ? document.documentElement.scrollTop : 0,
		    document.body ? document.body.scrollTop : 0
	    );
    }
    function f_filterResults(n_win, n_docel, n_body) {
	    var n_result = n_win ? n_win : 0;
	    if (n_docel && (!n_result || (n_result > n_docel)))
		    n_result = n_docel;
	    return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
    }
