Object.extend(Event, {
  _domReady : function() {
    if (arguments.callee.done) return;
    arguments.callee.done = true;

    if (this._timer)  clearInterval(this._timer);
    
    this._readyCallbacks.each(function(f) { f() });
    this._readyCallbacks = null;
},
  onDOMReady : function(f) {
    if (!this._readyCallbacks) {
      var domReady = this._domReady.bind(this);
      
      if (document.addEventListener)
        document.addEventListener("DOMContentLoaded", domReady, false);
        
        
        
        
        if (/WebKit/i.test(navigator.userAgent)) { 
          this._timer = setInterval(function() {
            if (/loaded|complete/.test(document.readyState)) domReady(); 
          }, 10);
        }
        
        Event.observe(window, 'load', domReady);
        Event._readyCallbacks =  [];
    }
    Event._readyCallbacks.push(f);
  }
});







var iHound = Class.create();



iHound = {
  
  initialize : function(){
    this._currentContent = null; 
    this._registeredPanels = new Array();

  },
  
  
  
  createInstance : function(instanceId){
    var _newInst = document.createElement('div');
    Element.addClassName(_newInst,'instance');
    _newInst.setAttribute('id', instanceId);
    document.body.appendChild(_newInst);
        
  },

    
  createContentInformation : function(){
    var _currentUrl = new String(document.URL);
    var _queryString = _currentUrl.split('?');

    
    
    


    var _contInfoPanels = document.createElement('div');
    Element.addClassName(_contInfoPanels, 'contentInfoPanels');
    var ct = document.getElementsByClassName('instance')[0];
    ct.appendChild(_contInfoPanels); 

    var _dimen = Element.getHeight(document.body);
    var _barH = Element.getHeight(document.getElementsByClassName('selectionIndicatorBar')[0]);
    var _newH = _dimen - ct.offsetTop - _barH ;
    Element.setStyle(_contInfoPanels,{height: _newH.toString() + 'px'});
    
    
    var lastIf;
    var  _selId;
    
    
    
    instData.instDefs.each(
      function(inst){
        _selId = inst.fieldDefs.contId;
        
        var _incr = 0;
        var _aTxts = new String(decodeURI(_currentUrl.split('?')[1].toQueryParams()[inst.fieldDefs.aTxt])).split('|');
        _aTxts.each(function(_aTxt){
          inst.instData.each( function(selData){
            
            var _regExp = new RegExp(_selId + '=' + selData.iid,'i')
            
            
            if (_regExp.exec(_queryString[1]))
            {
              var _idBase = inst.instId + selData.iid + _incr.toString();
              
              var newIf = document.createElement('iframe');
              
              Element.addClassName(newIf,'contentPanel');
              newIf.setAttribute('id',_idBase); 
              newIf.setAttribute('scrolling','auto');
              newIf.setAttribute('marginheight','0');
              newIf.setAttribute('marginwidth','0');
              newIf.setAttribute('width','100%'); 
              newIf.setAttribute('height','100%');
              newIf.setAttribute('src',selData.url + _aTxt );
              Element.setStyle(newIf,{visibility:'visible'});
              _contInfoPanels.appendChild(newIf);
              
              _incr += 1;
            }
            
          })
        });
      }
    )  
    
  
  },


  
  resizeLayout : function(){
  
    var _selDimen = Element.getDimensions(document.getElementsByClassName('selectionIndicatorBar')[0]);
    var instRef = document.getElementsByClassName('instance')[0];
    var newH = new String(Element.getDimensions(document.body).height - instRef.offsetTop - _selDimen.height) + 'px' ;
    var _contInfoP = document.getElementsByClassName('contentInfoPanels',instRef)[0];
    
    Element.setStyle(_contInfoP,{height:newH});  
    if (this._currentContent!=null){
      Element.setStyle($(this._currentContent),{height:newH}); 
    }

    
    
    
    iHound.Tabstrip.resized();
  },
  
  selectContent : function(e){
    if (typeof e =='string'){
      
      
      if (this._currentContent !=e){
        
        if (this._currentContent == null)
          this._currentContent = e;
        
        
        Element.setStyle(this._currentContent,{zIndex:'0',visibility:'hidden'});
        
        
        
        Element.setStyle(e,{zIndex:'50',visibility:'visible'});
        this._currentContent = e;
        iHound.resizeLayout();

        
      }
      
    }
  },
  
  
  registerPanel : function(){
    
  },
  
  
  unregisterPanel : function(){
    
  },
  
  urlDecode :function(e)
  {
     
     
     
     var HEXCHARS = '0123456789ABCDEFabcdef'; 
     var encoded = e;
     var plaintext = '';
     var i = 0;
     while (i < encoded.length) {
         var ch = encoded.charAt(i);
       if (ch == '+') {
           plaintext += ' ';
         i++;
       } else if (ch == '%') {
        if (i < (encoded.length-2) 
            && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
            && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
          plaintext += unescape( encoded.substr(i,3) );
          i += 3;
        } else {
          
          plaintext += '%[ERROR]';
          i++;
        }
      } else {
         plaintext += ch;
         i++;
      }
    } 
     return plaintext;
  }  
  
  
};


iHound.Custom = {
  
  
  createEngineList : function(){
    
    var el = document.getElementsByClassName('engineList');
    var _selId;
    
    var _currentUrl = new String(document.URL);
    
    var _queryString = _currentUrl.split('?');
    
    var _reqInfo = _currentUrl.parseQuery();
    instData.instDefs.each(
      function(inst){
        
        _selId = inst.fieldDefs.contId; 
        
        
        var preDec = _currentUrl.split('?')[1].toQueryParams()[inst.fieldDefs.aTxt];
        $(inst.fieldDefs.aTxt).setAttribute('value',iHound.urlDecode(preDec).toString()); 
        inst.instData.each( function(selData){
            
          var _ind = document.createElement('li');
          var _txt = document.createTextNode(selData.title);
          var _chk = document.createElement('input');
          
          _chk.setAttribute('name',_selId);
          _chk.setAttribute('id',_selId); 
          _chk.setAttribute('type','checkbox');
          _chk.setAttribute('value',selData.iid);

          var _regExp = new RegExp(_selId + '=' + selData.iid,'i')

          _ind.appendChild(_chk);          
          _ind.appendChild(_txt);
          el[0].appendChild(_ind);

          
          if (_regExp.exec(_queryString[1]))
            _chk.setAttribute('checked','checked');
            
          Event.observe(window,'resize',function(e){iHound.Custom.toggleEngineList({show:false});});
        });
      }
    )
    Event.observe('actionMore', 'click',function(e){iHound.Custom.toggleEngineList();});
  },
  
  
  
  toggleEngineList : function(){
    
    var eId = 'engineListBlock'
    
    if (arguments.length == 0){
      Element.toggle(eId);
    } else {
      if (arguments[0].show == true)
        Element.show(eId);
      else
        Element.hide(eId);
    }
    
    
    if (Element.visible(eId) == true){
      var _newL = document.getElementById('actionMore').offsetLeft - Element.getDimensions(eId).width + Element.getDimensions('actionMore').width;
      Element.setStyle(eId, {left: _newL.toString() + 'px'});
      Element.setStyle('searchActionPanel',{zIndex:'150'});
    }
    else
    {
      Element.setStyle('searchActionPanel',{zIndex:'0'}); 
    }

  }
  
}

iHound.Tabstrip = Class.create();
iHound.Tabstrip.prototype = {

  initialize : function(){
    this._tabstrip = new Object();
    this._overflow = new Object();
    this._overflowList = new Object();
    this._currentIndicator = new String();
    
  },

  createSelectionIndicators : function(){
    
    var _selBar = document.createElement('div');
    Element.addClassName(_selBar,'selectionIndicatorBar');
    Element.addClassName(_selBar, 'iHoundTabstrip');
  
    var _ul = document.createElement('ul');
    Element.addClassName(_ul,'selectionBar');
    
    
    
    var _olist = document.getElementsByClassName('instance')[0].appendChild(Builder.node('div',{className:'overflowList'},
      [Builder.node('ul',{className:'overflowListUl'})]));
    Element.setStyle(_olist,{display:'none'});
    Element.show(_olist);
    
    instData.instDefs.each(
      function(inst){
        
        var _selId = inst.fieldDefs.contId;
        
        var _incr = 0; 
        var _currentUrl = document.URL;
        var _aTxts = new String(decodeURI(_currentUrl.split('?')[1].toQueryParams()[inst.fieldDefs.aTxt])).split('|');
        var multSrch = (_aTxts.length>1);
        _aTxts.each(function(_aTxt){
          _aTxt = iHound.urlDecode(_aTxt);
          inst.instData.each( function(selData){
            if (iHound.QueryString.contains({field:_selId ,value:selData.iid})){
              
              var _idBase = inst.instId + selData.iid + _incr.toString()
              
              var _li = document.createElement('li');
              Element.addClassName(_li,'indicator'); 
              
              var indText = (multSrch == true) ? (selData.title + ':' + _aTxt) : selData.title;
              
              
              var _txt = document.createTextNode(indText);
              
              _li.setAttribute('id',_idBase + '-ind');
              _li.setAttribute('title',selData.title + ':' + _aTxt);
              _li.appendChild(_txt);
              _ul.appendChild(_li);
              Event.observe(_li,'mouseup',function(e){iHound.Tabstrip.select(_idBase);});
              
              
              var _oli = document.createElement('li');
              Element.addClassName(_oli,'indicator'); 
              var _otxt = document.createTextNode(indText);
              _oli.appendChild(_otxt);
              
              _oli.setAttribute('id',_idBase + '-oflow');
              _oli.setAttribute('title',selData.title + ':' + _aTxt);
              _olist.appendChild(_oli); 
              Event.observe(_oli,'mouseup',function(e){iHound.Tabstrip.select(_idBase);});
              Element.setStyle(_oli, {display:'none'});
              _incr += 1;
              
            }
          });
        });
      });
    
    
    var _overFlow =  document.createElement('li');
    Element.addClassName(_overFlow,'overflowPager');
    
    _overFlow.appendChild(Builder.node('img',{src:'images/exp.jpg'}));
    var _appendedEl = _ul.appendChild(_overFlow);



    Event.observe(_overFlow,'click',function(e){iHound.Tabstrip._showOverflow();});
    Element.hide(_overFlow); 

    this._overflow = _overFlow;
    
    
    _selBar.appendChild(_ul);
    var ct = document.getElementsByClassName('instance')[0];    
    ct.appendChild(_selBar);
    this._tabstrip = _selBar; 

  },
  
  
  select : function(e){
    var prev = this._currentIndicator;
    if (prev!=''){
      Element.removeClassName(prev + '-oflow','selected')
      Element.removeClassName(prev + '-ind','selected');
    }

    iHound.selectContent(e);
    this._currentIndicator = e;
    $(e + '-oflow').addClassName('selected');
    $(e + '-ind').addClassName('selected');

  },
  
  
  
  resized : function(){
    
    var _ts = this._tabstrip;
    var _tabs = document.getElementsByClassName('indicator',_ts); 
    var _pager = document.getElementsByClassName('overflowPager')[0];
    var _overflowList = document.getElementsByClassName('overflowList')[0];

    if (Element.visible(_overflowList)){
      Element.hide(_overflowList);
    }
    

    Element.show(_pager); 

      _tabs.each(function (_tab){
        Element.show(_tab);
      });    
    
    
    if (_pager.offsetTop!=0){

      

      var i = _tabs.length - 1;
      while ((_pager.offsetTop !=0) && (i >= 0)){
        
        Element.hide(_tabs[i]);
        i--;
      }
      Element.show(_pager);

    }
    else
    {
      Element.hide(_pager);
    }
    
  }, 
  
  
  
  _showOverflow : function(){
    
    
    var _ts = this._tabstrip;
    var _tabs = document.getElementsByClassName('indicator',_ts); 
    var _pager = document.getElementsByClassName('overflowPager')[0];
    var _overflowList = document.getElementsByClassName('overflowList')[0];

    var _overflowItems = document.getElementsByClassName('indicator',_overflowList);    
    
    _overflowList.show();
    var newOflowloc =  iHound.Tabstrip._calcOverflowListXY();
    newOflowloc =  iHound.Tabstrip._calcOverflowListXY();
    Element.setStyle(_overflowList,{left:newOflowloc.left.toString() + 'px',top:newOflowloc.top.toString() + 'px'});
    
    
    
    for (var i = 0; i<= (_tabs.length -1);i++ ){
      if (Element.visible(_tabs[i]))
        Element.hide(_overflowItems[i]);
      else
        Element.show(_overflowItems[i]);
    }
    
    
  },

  
  _calcOverflowListXY : function(){
    
    var _pager = document.getElementsByClassName('overflowPager')[0];
    
    var _list = document.getElementsByClassName('overflowList')[0];
    
    var newx = _pager.offsetLeft + _pager.getDimensions().width - _list.getDimensions().width;
    
    var newy = _pager.offsetTop + _pager.getDimensions().height;
    
    var ret = {left: newx, top: newy}; 
    return ret;
  }  
  
};
iHound.Tabstrip = new iHound.Tabstrip();


iHound.QueryString = {
  
  contains : function(query){
    
    
    var _regExp = new RegExp( query.field + '=' + query.value ,'i')
    
    if (_regExp.exec(iHound.QueryString._getQueryStringSrc()))
      return true;
    else
      return false;
    
  },
  
  parse : function(){
    
    
    
    
    
  },
  
  appendSrc : function(){
    
    
  },
  
  removeSrc : function(){
    
  },
  
  _getQueryStringSrc : function(){

    return document.URL.split('?')[1];
    
  },
  
  _getSrc : function(){
    
    
    
  },
  
  _hasBookmark : function(){
    
  },
  
  clearSrc : function(){
    
  },
  
  fields : function(){
    
  },
  
  values : function(){
    
  }
  
  
  
  
};





Event.onDOMReady(function(){

  iHound.createInstance('inst1'); 
  iHound.Custom.createEngineList();
  iHound.Tabstrip.createSelectionIndicators();
  iHound.createContentInformation();
  iHound.resizeLayout();
  iHound.Tabstrip.select(document.getElementsByClassName('contentPanel')[0].id);
});

Event.observe(window,'resize', function(e){iHound.resizeLayout();});