hind = {
  _into : function(r,ex)
    {
    ex.id.innerHTML = r.responseText ;
    },
  into : function (url , id)
    {
    var iid = document.getElementById(id) ;
    if(!iid)
      {
      hind.inform('Element '+id+ ' no found');
      return ;
      }
    iid.innerHTML = "<img src='/hc/res/wait.gif' alt='loading...'>";
    hind.request(url , {onSuccess:hind._into, id:iid,type:"text/html"});
    },
// -------------- msg status link
  init : function()
  {
  this.statbox = document.createElement('div');
  this.statbox.style.display = 'none' ;
  this.statbox.style.position = 'fixed' ;
  this.statbox.style.top = '10px' ;
  this.statbox.style.right = '10px' ;
  this.statbox.style.zIndex = '999' ;
  this.statbox.style.border = 'solid 1px black' ;
  this.statbox.style.background = '#a9889a' ;
  this.statbox.id='__msg_status' ;
  document.body.appendChild(this.statbox) ; 
  this.waitItem = document.getElementById('waitArea') ;
  },
  status : function (t)
    {
    if(!this.initialized) this.init() ;
    if(t.text)
      {
      this.statbox.style.display = 'block' ;
      this.statbox.innerHTML = t.text ;      
      }else if(t.length>0){
      this.statbox.innerHTML = t ;
      this.statbox.style.display = 'block' ;
      }else{
      this.statbox.style.display = 'none' ;
      }
    },
  inform : function (msg)
    {
    if(!this.initialized) this.init() ;
    this.statbox.innerHTML = msg ;
    this.statbox.style.display = 'block' ;
    setTimeout(function(){document.getElementById('__msg_status').style.display='none';},10000) ;
    },
  request : function(url , extra) {
        var httpRequest;
        if(extra.method)
          var method = extra.method ;
          else
          var method = 'GET' ;
        if(extra.sync)
          extra.sync = true
          else
          extra.sync = false
        if(extra.from && extra.from == 'hc')
          {
          url = this.hcbase+url ;
          }
           
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType && extra.type) {
                httpRequest.overrideMimeType(extra.type);
                // See note below about this line
            }
        } 
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } 
            catch (e) {
                try {
                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } 
                catch (e) {}
            }
        }

        if (!httpRequest) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
//        callback = extar.callback ;
        if(extra.sync != true)
        if(extra.onSuccess)
          httpRequest.onreadystatechange = function(){hind.requestHandler(httpRequest,extra)}
          else
          httpRequest.onreadystatechange = function(){extra.callback(httpRequest,extra)}
        
        httpRequest.open(method, url, !extra.sync);
        if(method == 'POST')
          {
          httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
          if(typeof extra.data != 'undefined' && typeof extra.data != 'string')
            extra.data = hind.requestParseQuery(extra.data)
          httpRequest.send(extra.data);
          }else{
          httpRequest.send(0) ;
          }
    return httpRequest ;
    },
  requestParseQuery : function(q)
    {
    var out = '' ;
    for(i in q)
      {
      out+=i+'='+q[i]+'&' ;
      }
    return out ;
    },
  requestHandler : function(httpRequest,extra)
    {
       if (httpRequest.readyState == 4) {
            if (httpRequest.status == 200) {
              extra.onSuccess(httpRequest,extra)
            } else {
                hind.inform('There was a problem with the request.');
            }
        }
    },
  hcbase : '/',
  waitCount : 0 ,
  wait:function(ad)
    {
    if(!this.initialized) this.init() ;
    if(ad == false)
      {
      this.waitCount-- ;
      }else{
      this.waitCount++ ;
      }

    if(this.waitCount>0)
      {
      this.waitItem.style.display='block';
      }else{
      this.waitItem.style.display='none';
      this.waitCount = 0 ;
      }
    },
  include : function(file)
    {
    document.write('<s'+'cript type="text/javascript" src="'+file+'"></s'+'cript>') ;
    },
  extend : function(f)
    {
    this.include(this.jsDir+f+".js") ;
    },
  locate : function() {
    var scriptTags = document.getElementsByTagName("script");
    for(var i=0;i<scriptTags.length;i++) {
      if(scriptTags[i].src && scriptTags[i].src.match(/core\.js(\?.*)?$/)) {
        this.jsDir = scriptTags[i].src.replace(/core\.js(\?.*)?$/,'');
        break;
      }
    }
  }
}
hind.locate() ;
hind.readyList = [] ;
hind.ready = function (f) {
hind.readyList.push(f) ;
}
hind.onReady = function (f) {
for(i in hind.readyList)
  {
  hind.readyList[i]() ;
  }
}

hind._intoc = function(r,ex)
    {
    ex.id.innerHTML = r.responseText ;
    ex.callback(ex.id) ;
    }
hind.intoc = function (url , id , c)
    {
    var iid = document.getElementById(id) ;
    if(!iid)
      {
      hind.inform('Element '+id+ ' no found');
      return ;
      }
    iid.innerHTML = "<img src='/res/wait.gif' alt='loading...'>";
    hind.request(url , {onSuccess:hind._intoc, id:iid,type:"text/html",callback:c});
    }

hind.task = {
_task:{},
add : function (id,cb)
  {
   var item = _h('@').createElement('div') ;
    item.id = id ;
    item.innerHTML = name ;
    _h('#items').appendChild(item) ;
    cb(id)
  },
del : function (id) {
  clearInterval(this._task[id]) ;
  delete(this._task[id]) ;
  _h("#"+id).parentNode.removeChild(_h("#"+id)) ;
  },
watch:function(name, time, run){
  if(_h(name))
    {
    this._task[name] = setInterval(run,time) ;
    }
  }
} 

hind.menu = {
get : function(id)
  {
  var el = _h(id) ;
  if(el)
    return el.getElementsByClassName('menu-item-name')[0] ;
  throw('app.js menu item no found');
  }
}


//addEventListener('load' , hind.onReady) ;
window.onload = function(){ hind.onReady() ;}
// --------------------------------------
// jQuery
_h = function (a)
  {
  this.each = function(run)
  {
  for (i in this.items)
    run(this.items[i])
  }
this.style = function (styles)
  {
  for(i in this.items)
    for(i2 in styles)
       this.items[i].style.i2=styles.i2
  }
  
  this.items = Array() ;
  if(a == '#') return document.body ;
  if(a == '@') return document ;
  var s = a.substr(0,1)
  var b = a.substr(1) ;
  switch(s)
    {
    case '#' : return document.getElementById(b) ;
    case '.' : all = document.getElementsByTagName('*')
      for(i in all)
        if(all[i].className == b)
        this.items.push(all[i])
      return this ;
    case '@' : 
      this.items = document.getElementsByTagName(b) ;
      return this ;
    case '*' : 
      this.items = document.getElementsByTagName('*') ;
      return this;
    case '[' :
      var i = a.indexOf('=') ;
        if(i){
        var attr = b.substr(0,i) ;
        i++
        var len = a.length-1
        var val = a.substr(i,len);
        var all = document.getElementsByTagName('*') ;
        for(i in all)
          {
          if(all[i].hasAttribute && all[i].hasAttribute(attr) && all[i].getAttribute(attr) == val)
             this.items.push(all[i]);
          }
          }
          return this ;
 //   default: 
 //       this.items = document.getElementsByTagName(a) ;
 //       return this ;
    }
 // return document.getElementsByTagName(a) ;
   return document.getElementById(a) ;
  }
  
  //------------------
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
	do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	return [curleft,curtop];
}}

