/*
 * ADDPLUS JS Framework
 * Copyright 2007 ADDPLUS
 * Code by Yuri D'Elia
 */


// XMLHttpLoader

function XMLHttpLoader()
{
  this.getXMLHttpRequest = function()
  {
    try { return new XMLHttpRequest(); } catch(e) {}
    try { return new ActiveXObject("MSXML3.XMLHTTP"); } catch(e) {}
    try { return new ActiveXObject("MSXML2.XMLHTTP.3.0"); } catch(e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {}
    try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
    throw "no XMLHttpRequest support";
  };

  this.getSync = function(url)
  {
    var req = this.getXMLHttpRequest();
    req.open("GET", url, false);
    req.send(undefined);
    return req.responseText;
  };

  this.getAsync = function(url, callback)
  {
    var req = this.getXMLHttpRequest();
    req.open("GET", url, true);
    req.onreadystatechange = function()
    {
      if(req.readyState == 4)
	callback(req.responseText);
    };
    req.send(undefined);
  };
};



// hooks

function addHook(eventName, callback)
{
  if(window.addEventListener)
    return window.addEventListener(eventName, callback, false);
  if(!window.hooks)
    window.hooks = new Array();
  if(!window.hooks[eventName])
  {
    window.hooks[eventName] = new Array();
    eval("window.on" + eventName
	 + " = function() { runHooks('" + eventName + "'); };");
  }

  for(var i in window.hooks[eventName])
    if(window.hooks[eventName][i] == callback)
      return true;

  window.hooks[eventName].push(callback);
  return false;
}


function runHooks(eventName)
{
  for(var i in window.hooks[eventName])
    (window.hooks[eventName][i])();
}



// portable css

function winInnerHeight(window)
{
  return (window.innerHeight?
	  window.innerHeight: window.document.body.offsetHeight);
}

function winInnerWidth(window)
{
  return (window.innerWidth?
	  window.innerWidth: window.document.body.offsetWidth);
}

function setObjAlpha(obj, alpha)
{
  if(obj.style.opacity !== undefined)
    obj.style.opacity = alpha;
  else
    obj.style.filter = "alpha(opacity=" + parseInt(alpha * 100) + ")";
}

function globalOffsetTop(obj)
{
  return obj.offsetTop +
    (obj.offsetParent? globalOffsetTop(obj.offsetParent): 0);
}

function globalOffsetLeft(obj)
{
  return obj.offsetLeft +
    (obj.offsetParent? globalOffsetLeft(obj.offsetParent): 0);
}

function DID(id)
{
  return document.getElementById(id);
}


// browser language

function browserLang()
{
  return (navigator.language? navigator.language: navigator.userLanguage);
}

function browserLangId()
{
  var lang = browserLang();
  var dash = lang.indexOf('-');
  lang.toLowerCase();
  if(dash >= 0) return lang.substr(0, dash);
  return lang;
}


// native language support

function getCat(cat, id)
{
  return cat[id];
}

function NLS()
{
  this.deflt = "nodefault";
  this.lang = browserLangId();
  var objs = new Array();

  this.getCat = function(cat)
  {
    var buf = getCat(cat, this.lang);
    if(!buf) buf = getCat(cat, this.deflt);
    return buf;
  };

  this.regCat = function(obj, cat)
  {
  objs.push({'obj':obj, 'cat':cat});
    //objs.push({'obj':obj.firstChild, 'cat':cat});
    if(this.lang != this.deflt)
      //obj.firstChild.nodeValue = this.getCat(cat);
    obj.innerHTML = this.getCat(cat);
    console.log(obj);  
  };

  this.regCatId = function(id, cat)
  {
    this.regCat(DID(id), cat);
  }

  this.setLang = function(lang)
  {
    if(lang == this.lang) return;
    this.lang = lang;

    for(var i in objs)
      //objs[i]['obj'].nodeValue = this.getCat(objs[i]['cat']);
	objs[i]['obj'].innerHTML = this.getCat(objs[i]['cat']);
	console.log(objs[i]['obj']);
  };

  return this;
}

var nls = new NLS();


// string utilities

function trim(buf, sep)
{
  if(!buf.length)
    return buf;

  var i = 0;
  var l = buf.length - 1;

  for(; i != buf.length; ++i)
    if(sep.indexOf(buf.charAt(i)) < 0)
      break;

  for(; l >= i; --l)
    if(sep.indexOf(buf.charAt(l)) < 0)
      break;

  return (l < i? "": buf.slice(i, l + 1));
}


function trimWs(buf)
{
  return trim(buf, " \t\n\r");
}


// animation utilities

function Transition(cb, finalize, speed, ms)
{
  var start;
  var intId;
  var self = this;

  this.stop = function()
  {
    if(!intId) return;
    clearTimeout(intId);
    (cb)(1);
    if(finalize)
      (finalize)();
  };
  
  this.anim = function()
  {
    var t = ((new Date()) - start);
    if(t < ms) (cb)(t / ms);
    else self.stop();
  };

  this.animating = function()
  {
    return (intId? true: false);
  };

  this.reset = function()
  {
    this.stop();
    start = new Date();
    intId = setInterval(this.anim, speed);
  };

  this.reset();
  return this;
}


function MultiLoadImg(paths, onload)
{
  var toLoad = paths.length;
  this.images = new Array();

  for(var i in paths)
  {
    var img = new Image();
    img.onload = onImgLoad;
    img.src = paths[i];
    this.images.push(img);
  }

  function onImgLoad()
  {
    if(!(--toLoad) && onload)
      (onload)();
  }

  this.toLoad = function()
  {
    return toLoad;
  };

  return this;
}

