﻿

// **********************************************************************
// PURPOSE: to set the relative position of one element, relative to another element
//    This can be used to place the element in the same or offset position relative to other element
//    eg: to position a floating image relative to a static img/a/div/span on the page
// PARAMETERS:
//    (string) elemToSet: this is the Id of the element that we will position
//        often - we style this (hidden) as style="position: absolute; left: -1000px; top: -1000px; z-index: 500;"
//    (string) elemReference:  this is the Id visible img/a/div/span on the page
//    (int) addToTop: this is the additional pixels to add to the elemToSet top property
//            to offset the top position to be different from elemReference
//    (int) addToLeft: this is the additional pixels to add to the elemToSet left property
//            to offset the left position to be different from elemReference
// SAMPLE:
//    SetRelativePosition("starImg", "toprightcorner",-59,-163); 
// **********************************************************************
function SetRelativePosition(elemToSet, elemReference, addToTop, addToLeft) 
{ 
        var star = document.getElementById(elemToSet);
        var corner = document.getElementById(elemReference);        
        
        if (!document.getElementById && document.all) {
          star = document.all[elemToSet];
          corner = document.all[elemReference];     
        }
        var left = 0;
        var top = 0;               
       
        try
        {
            while (corner.offsetParent)
            {
                // While we haven’t got the top element in the DOM hierarchy
                // Add the offsetLeft
                left += corner.offsetLeft;
                // If my parent scrolls, then subtract the left scroll position
                if (corner.offsetParent.scrollLeft) {left -= corner.offsetParent.scrollLeft; }

                // Add the offsetTop
                top += corner.offsetTop;
                // If my parent scrolls, then subtract the top scroll position
                if (corner.offsetParent.scrollTop) { top -= corner.offsetParent.scrollTop; }
        	
                // Grab
                corner = corner.offsetParent;
            } //end while
        } //end try
        catch (e)
        {
            // Do nothing
        }

        if (PositionElementsBrowserDetect.browser == "Opera") 
        {
             top += 3; //fix opera bug displaying 3 pixels too low.
        }
//        if (PositionElementsBrowserDetect.browser == "Explorer" && parseInt(navigator.appVersion)==6 ) //IE 6
//        {
//             top += -10; //fix opera bug displaying 3 pixels too low.
//        }
        // Add the top element left offset and the windows left scroll and subtract the body's client left position.
        left += corner.offsetLeft + document.body.scrollLeft - document.body.clientLeft;
        	
        // Add the top element topoffset and the windows topscroll and subtract the body's client top position.
        top += corner.offsetTop + document.body.scrollTop - document.body.clientTop;
       
        //check Mozilla - if have #...
        //alert("raw top: " + top);
        
        top = top + addToTop;
        left = left + addToLeft;
        
        if (document.getElementById || document.all) {
            star.style.top = top + "px";
            star.style.left = left + "px";
        } 
        else if (document.layers) 
        {
            //old netscape
            star.top = top;
            star.left = left;
        } 
        
        //above code doesnt work in Firefox.... - fix....
        //  - note: not work on wide-screen  computers... TO DO
        if (!top) {
        
           top = 0;
           left = 0;
           
           var objFirefox = document.getElementById(elemReference);
           
           top = getTopPosition(objFirefox) + addToTop;
           left = getLeftPosition(objFirefox) + addToLeft; // + 53;
         
           document.getElementById(elemToSet).style.top = top + "px";
           document.getElementById(elemToSet).style.left = left + "px";
          
        }
        
  }
  
  
//  function getElementPosition(elemID){
//    var offsetTrail = document.getElementById(elemID);
//    var offsetLeft = getLeft(offsetTrail);
//    var offsetTop = getTop(offsetTrail);
//    while (offsetTrail){
//    offsetLeft += offsetTrail.offsetLeft;
//    offsetTop += offsetTrail.offsetTop;
//    offsetTrail = offsetTrail.offsetParent;
//    }
//    if (navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != 'undefined'){
//    offsetLeft += document.body.leftMargin;
//    offsetTop += document.body.topMargin;
//    }
//    return {left:offsetLeft,top:offsetTop};
//  }
  
  
function getLeftPosition(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return curleft;
	//return [curleft,curtop];
}

function getTopPosition(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return curtop;
	//return [curleft,curtop];
}
  
//http://www.quirksmode.org/js/findpos.html
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}




//============================================================
// Capturing The Mouse Position in IE4-6 & NS4-6
// (C) 2000 www.CodeLifter.com
// Free for all users, but leave in this  header
//============================================================
/*
var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;
*/
var _mouseXPos = 0;
var _mouseYPos = 0;
  
function getMouseXY(e) {  
  _mouseXPos=0;
  _mouseYPos=0;
  if (IE) { // grab the x-y pos.s if browser is IE
    _mouseXPos = event.clientX + document.body.scrollLeft
    _mouseYPos = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    _mouseXPos = e.pageX
    _mouseYPos = e.pageY
  }  
  // catch possible negative values in NS4
  if (_mouseXPos < 0){_mouseXPos = 0}
  if (_mouseYPos < 0){_mouseYPos = 0}    
  
  return true;
}


//not fully tested....
function SetRelativeToMouse(elemToSet, addToTop, addToLeft, allowOffScreen) {
  var newX = _mouseXPos + addToLeft;
  var newY = _mouseXPos + addToTop;
  
  if (!allowOffScreen) {
     if (newX<0) {
       newX = 0;
     }
     if (newY<0) {
       newY = 0;
     }
  } 
     
  if (document.all) {
    document.all[elemToSet].style.top = newY + "px";
    document.all[elemToSet].style.left = newX + "px";
  } else if (document.getElementById) {
    document.getElementById(elemToSet).style.top = newY + "px";
    document.getElementById(elemToSet).style.left = newX + "px";
  }
  
}


// derived from http://www.dynamicdrive.com/dynamicindex17/stickynote.htm
//
function CenterDiv(divId, addToTop, addToLeft){
    var ie=document.all && !window.opera;
    var dom=document.getElementById;
    iebody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body;
    objref=(dom)? document.getElementById(divId) : document.all[divId];
    var scroll_top=(ie)? iebody.scrollTop : window.pageYOffset;
    var docwidth=(ie)? iebody.clientWidth : window.innerWidth;
    docheight=(ie)? iebody.clientHeight: window.innerHeight;
    var objwidth=objref.offsetWidth;
    objheight=objref.offsetHeight;
    
    var left = docwidth/2-objwidth/2 + addToLeft;
    var top = scroll_top+docheight/2-objheight/2 + addToTop;
    objref.style.left=left+"px";
    objref.style.top=top+"px";
}


var PositionElementsBrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
PositionElementsBrowserDetect.init();

