// Snapsis.DNN utilities
var snapsis_MoveObject = null;
var snapsis_MoveOffsetX = 0;
var snapsis_MoveOffsetY = 0;
var snapsis_CurrentContainerId=null;
var snapsis_zIndexTop=2000;
function snapsis_onLoad()
{
 //put calls to any javascript that you want executed when the page loads here   
}
function snapsis_popUp(oContainerId,leftPos, topPos){
   	var oContainer = document.getElementById(oContainerId);
    if (oContainer != null )
    {
		if (oContainer.style.zIndex < snapsis_zIndexTop )
		        oContainer.style.zIndex=snapsis_zIndexTop++;

		if (leftPos != null)
			oContainer.style.left=leftPos;
	    else
	        oContainer.style.left=event.clientX+10;
		if (topPos != null)
			oContainer.style.top=topPos;
    	else
	        oContainer.style.top=event.clientY+10;
	    
		var oMoveHandle = document.getElementById(oContainer.id + "Handle");
	    if ( oMoveHandle )
		   oMoveHandle.attachEvent("onmousedown",snapsis_startMouseMove);

		oContainer.style.display="block";
		oContainer.style.visibility="visible";
		snapsis_CurrentContainerId = oContainer.id;
	}
}
function snapsis_hidePopUp(oContainerId){
    if (oContainerId)
        snapsis_CurrentContainerId = oContainerId;
	if (snapsis_CurrentContainerId != null){
		var oContainer=document.getElementById(snapsis_CurrentContainerId);
		if (oContainer != null){
			oContainer.style.display="none";
			oContainer.style.visibility="hidden";
			var oMoveHandle = document.getElementById(oContainer.id + "Handle");
			if ( oMoveHandle )
			{
			    oMoveHandle.detachEvent("onmousedown",snapsis_startMouseMove);
			    oMoveHandle.detachEvent("onmouseup",snapsis_cancelMouseMove);
			}
		}
	}
	return;
}

function snapsis_ToggleDisplay(oContainerId,onoff){

		var oContainer=document.getElementById(oContainerId);
		if (oContainer != null)
		{
		    
		    if (onoff)
		    {
    		    if ( oContainer.style.display == "block" || onoff.toLowerCase() == 'off')
    		    {
    		       oContainer.style.display = "none";
    		       oContainer.style.visibility = "hidden";
                }
                else
                {
                   oContainer.style.display = "block";
                   oContainer.style.visibility = "visible";
                }
            }
            else
            {
    		    if ( oContainer.style.display == "block")
    		    {
    		       oContainer.style.display = "none";
    		       oContainer.style.visibility = "hidden";
                }
                else
                {
                   oContainer.style.display = "block";
                   oContainer.style.visibility = "visible";
                }
            }                
		}
	return;
}

function snapsis_startMouseMove(){
    var HandleId = new String(event.srcElement.id)
    //the Container being moved needs to have the same id as the handle without the "Handle" suffix
    snapsis_MoveObject = document.getElementById(HandleId.substring(0,HandleId.length - 6));
    if (snapsis_MoveObject)
    {
        snapsis_MoveOffsetX = event.clientX - snapsis_MoveObject.style.pixelLeft
        snapsis_MoveOffsetY = event.clientY - snapsis_MoveObject.style.pixelTop
    }
    document.attachEvent("onmousemove",snapsis_handleMouseMove);
    document.attachEvent("onmouseup",snapsis_cancelMouseMove);
}
function snapsis_cancelMouseMove(){
	document.detachEvent("onmousemove",snapsis_handleMouseMove);
	document.detachEvent("onmouseup",snapsis_cancelMouseMove);
    snapsis_MoveObject = null;
}
function snapsis_handleMouseMove(){

    if ( snapsis_MoveObject )
    {
        snapsis_MoveObject.style.pixelLeft = event.clientX  - snapsis_MoveOffsetX;
        snapsis_MoveObject.style.pixelTop = event.clientY  - snapsis_MoveOffsetY;
	    event.returnValue = false;
	    event.cancelBubble = true;
    }
}
function snapsis_ElementTop(eSrc)
{
	var iTop = 0;
	var eParent;
	eParent = eSrc;
	while (eParent.tagName.toUpperCase() != "BODY")
	{
		iTop += eParent.offsetTop;
		eParent = eParent.offsetParent;
	}
	return iTop + document.body.scrollTop;
}
function snapsis_ElementBottom(eSrc)
{
	var iTop = 0;
	var eParent;
	eParent = eSrc;
	while (eParent.tagName.toUpperCase() != "BODY")
	{
		iTop += eParent.offsetTop;
		eParent = eParent.offsetParent;
	}
	return iTop + document.body.scrollTop + eSrc.offsetHeight;
}
function snapsis_ElementLeft(eSrc)
{	
	var iLeft = 0;
	var eParent;
	eParent = eSrc;
	while (eParent.tagName.toUpperCase() != "BODY")
	{
		iLeft += eParent.offsetLeft;
		eParent = eParent.offsetParent;
	}
	return iLeft + document.body.scrollLeft;
}
function snapsis_ElementRight(eSrc)
{	
	var iLeft = 0;
	var eParent;
	eParent = eSrc;
	while (eParent.tagName.toUpperCase() != "BODY")
	{
		iLeft += eParent.offsetLeft;
		eParent = eParent.offsetParent;
	}
	return iLeft + eParent.offsetWidth + document.body.scrollLeft ;
}
/**
    This is a Javascript implementation of a Hashtable object.
    credit to:
    http://m.synovic.home.att.net/hashtable/hashtable.html
    
    Contructor(s):
     Hashtable()
              Creates a new, empty hashtable
    
    Method(s):
     void clear() 
              Clears this hashtable so that it contains no keys. 
     boolean containsKey(String key) 
              Tests if the specified object is a key in this hashtable. 
     boolean containsValue(Object value) 
              Returns true if this Hashtable maps one or more keys to this value. 
     Object get(String key) 
              Returns the value to which the specified key is mapped in this hashtable. 
     boolean isEmpty() 
              Tests if this hashtable maps no keys to values. 
     Array keys() 
              Returns an array of the keys in this hashtable. 
     void put(String key, Object value) 
              Maps the specified key to the specified value in this hashtable. A NullPointerException is thrown is the key or value is null.
     Object remove(String key) 
              Removes the key (and its corresponding value) from this hashtable. Returns the value of the key that was removed
     int size() 
              Returns the number of keys in this hashtable. 
     String toString() 
              Returns a string representation of this Hashtable object in the form of a set of entries, enclosed in braces and separated by the ASCII characters ", " (comma and space). 
     Array values() 
              Returns a array view of the values contained in this Hashtable. 
            
*/
function Hashtable(){
    this.clear = hashtable_clear;
    this.containsKey = hashtable_containsKey;
    this.containsValue = hashtable_containsValue;
    this.get = hashtable_get;
    this.isEmpty = hashtable_isEmpty;
    this.keys = hashtable_keys;
    this.add = hashtable_add;
    this.remove = hashtable_remove;
    this.size = hashtable_size;
    this.toString = hashtable_toString;
    this.values = hashtable_values;
    this.hashtable = new Array();
}                
/*=======Private hashtable methods for internal use only========*/

function hashtable_clear(){
    this.hashtable = new Array();
}

function hashtable_containsKey(key){
    var exists = false;
    for (var i in this.hashtable) {
        if (i == key && this.hashtable[i] != null) {
            exists = true;
            break;
        }
    }
    return exists;
}

function hashtable_containsValue(value){
    var contains = false;
    if (value != null) {
        for (var i in this.hashtable) {
            if (this.hashtable[i] == value) {
                contains = true;
                break;
            }
        }
    }
    return contains;
}

function hashtable_get(key){
    return this.hashtable[key];
}

function hashtable_isEmpty(){
    return (this.size == 0) ? true : false;
}

function hashtable_keys(){
    var keys = new Array();
    for (var i in this.hashtable) {
        if (this.hashtable[i] != null) 
            keys.push(i);
    }
    return keys;
}

function hashtable_add(key, value){
    if (key == null || value == null) {
        throw "NullPointerException {" + key + "},{" + value + "}";
    }else{
        this.hashtable[key] = value;
    }
}

function hashtable_remove(key){
    var rtn = this.hashtable[key];
    this.hashtable[key] = null;
    return rtn;
}

function hashtable_size(){
    var size = 0;
    for (var i in this.hashtable) {
        if (this.hashtable[i] != null) 
            size ++;
    }
    return size;
}

function hashtable_toString(){
    var result = "";
    for (var i in this.hashtable)
    {      
        if (this.hashtable[i] != null) 
            result += "{" + i + "},{" + this.hashtable[i] + "}\n";   
    }
    return result;
}

function hashtable_values(){
    var values = new Array();
    for (var i in this.hashtable) {
        if (this.hashtable[i] != null) 
            values.push(this.hashtable[i]);
    }
    return values;
}
