//-----------------------------------------------
// NANOS - Very tiny framework for Ajax apps.
// 
// Only the most useful and least bloated
// code has been put together to build
// powerful apps.
//-----------------------------------------------

function hide_element(elem) {
    try {
        elem.style.visibility = "hidden";
		elem.style.display = "none";
    } catch(e) {
    }
}

function show_element(elem) {
    try {
        elem.style.visibility = "visible";
		elem.style.display = "block";
    } catch(e) {
    }
}

function pop(array) {
    if (array.pop) {
        return array.pop();
    } else {
        var elem = array[array.length-1];
        array.length--;
        return elem;
    }
}

function findPosX(obj)
{
    var curleft = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj)
{
    var curtop = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

function findHeight(elem) {
    var result = 0;

    if (elem.offsetHeight) {
        result = elem.offsetHeight;
    } else if (elem.clip && elem.clip.height) {
        result = elem.clip.height;
    } else if (elem.style && elem.style.pixelHeight) {
        result = elem.style.pixelHeight;
    }
    return parseInt(result);
}

function setHeight(obj, height) {
    if (height < 0)
        height = 0;
    obj.style.height = Math.round(height) + 'px';
}

function setOpacity(obj, opacity) {
    opacity = (opacity == 100) ? 99 : Math.round(opacity);
    if (opacity < 0)
        opacity = 0;

    var str = "alpha(opacity:"+opacity+")";

    // IE/Win
    obj.style.filter = str;

    // Safari<1.2, Konqueror
    obj.style.KHTMLOpacity = opacity/100;

    // Older Mozilla and Firefox
    obj.style.MozOpacity = opacity/100;

    // Safari 1.2, newer Firefox and Mozilla, CSS3
    obj.style.opacity = opacity/100;
}

//---------------------
// Cookies
//---------------------
function _setCookie(name, value, nDays, path, domain, secure) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	If the expires variable is set, translate into the correct expiration time
	This implementation assumes expiration is set in days, which are
	converted into milliseconds
	*/
	if ( nDays ) { nDays = nDays * 1000 * 60 * 60 * 24; }
	var expires_date = new Date( today.getTime() + (nDays) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( nDays ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function setCookie(name, value, nDays) {
    if (nDays==null || nDays==0) nDays=1;
 
    setTimeout("_setCookie('"+name+"','"+value+"','" + nDays + "', '/', '.terrapass.com', '')",0);
}

function getCookie(name) {
  var cookieRE=new RegExp(name+"=([^;]+)");
  if (document.cookie.search(cookieRE)!=-1) {
      return unescape(RegExp.$1);
  }
  else {
    return "";
  }
}


//-----------------------------------------------
// Prototype functions & Helpers
//-----------------------------------------------

Function.prototype.bind = function(object) {
  var __method = this;

  return function() {
    return __method.apply(object, arguments);
  }
}

Function.prototype.bind2 = function(object, arg) {
  var __method = this;

  return function() {
    return __method.apply(object, [arg]);
  }
};

function dec2hex(number) {
    var basen = 16;
    var count = 99;
    var now = 0;
    var div = 0;
    var rem = 0;
    var hex = 0;
    var hexstr = "";

    while (count >=0 ) {
        now = Math.pow(basen , count);
        div = number / now;
        div = Math.floor(div);
        rem = number % now;
        rem = parseInt(rem);
        if (div >= 1) {
            hex = tohex(div);
            hexstr = hexstr + hex;
        }
        else {
            if (hexstr.length != 0) {
                hexstr = hexstr + "0";
            }
        }
        number = rem;
        count--;
    }

    return hexstr;
}

function tohex(numb) {
  var letter = numb
  if (numb == 10) { letter = "A" }
  if (numb == 11) { letter = "B" }
  if (numb == 12) { letter = "C" }
  if (numb == 13) { letter = "D" }
  if (numb == 14) { letter = "E" }
  if (numb == 15) { letter = "F" }
  return letter;
}

function GetElementById(doc, id) {
    var stack = new Array();
   
    stack.push(doc);
    var ntotal = 0;
    
    while (stack.length > 0) {
        elem = stack.shift();

        ntotal++;

        i=0;
        while (elem.childNodes && i<elem.childNodes.length) {
            stack.push(elem.childNodes[i]);
            i++;
        }

        if (elem.id == id)
            return elem;
    }

    return null;
}

function list_remove(list, item) {
    var i;
    var n = list.length;

    for (i=0; i<n; i++) {
        if (list[i] == item) {
            break;
        }
    }

    while (i<n-1) {
        list[i] = list[i+1];
        i++;
    }

    list.length = n-1;
}

function AttachEvent(elem, event, obj, methodName) {
    elem[event] = obj[methodName].bind(obj);
}

function AttachEvent2(elem, event, obj, methodName, args) {
    elem[event] = obj[methodName].bind2(obj, args);
}

function Import(path){
    var base = "";
    var str = "<" + "script src=\"" + base + path + "\" type=\"text/javascript\"></" + "script>";
	document.write(str);
}
