// twobirds framework AJAX lib V 1.1.0 Author: 2002- Frank Thuerigen / phpbuero.de

tb.loader = {
 path:'service/',
 
 loadlist:'',
 list:'',

 // public

 /*
 loading: function ( pIdentifier ) {
  if ( tb.cache.findindex( pIdentifier ) != -1 ) return true;
  return false;
  },
 */
 
 // private

 fileload: function( pModule, pElement, pIsAsync) {
  var myIdentifier = pModule + '/' + pElement+';';
  if (tb.cache.findindex( myIdentifier ) != -1 ) return;
  var myIsAsync = ( pIsAsync !== null ? pIsAsync : true ); 
  var myUrl = tb.loader.path + 'service.php?url=twobirds/modules/' + pModule + '/' + pElement;
  tb.cache.set( myIdentifier, ( myIsAsync == true ? 'async' : 'sync' ) );
  if ( myIsAsync == true ) {
   return tb.request.add(tb.loader.fileload_cb, 'GET', myUrl, null, true);
   }
  else {
   tb.request.add(tb.loader.fileload_cb, 'GET', myUrl, null, false);
   }
  },
 
 fileload_cb: function( pXml, pText) {
  var myIdentifierStart = pText.indexOf('/*')+2;
  var myIdentifierEnd = pText.indexOf('*/');
  var myIdentifier = pText.slice(myIdentifierStart,myIdentifierEnd);
  var myData = pText.slice( pText.indexOf('*/')+2 );
  tb.cache.set( myIdentifier, myData );
  tb.element.requireok( myIdentifier );
  },

 jsload: function( pModule, pElement, pIsAsync) {
  var myIdentifier = pModule + '.' + pElement+'.js;';
  var myUrl = tb.loader.path + 'service.php?url=twobirds/modules/' + pModule + '/js/' + pElement + '.js';
  var myScript = document.createElement('script');
  myScript.setAttribute('language','javascript');
  myScript.setAttribute('type','text/javascript');
  myScript.setAttribute('src', myUrl);
  document.getElementsByTagName('head')[0].appendChild(myScript);
  },
 
 cssload: function( pModule, pElement, pIsAsync ) {
  var myIdentifier = pModule + '.' + pElement+'.css;';
  var myUrl = tb.loader.path + 'service.php?url=twobirds/modules/' + pModule + '/css/' + pElement + '.css';
  var myScript = document.createElement('link');
  myScript.setAttribute('rel','stylesheet');
  myScript.setAttribute('type','text/css');
  myScript.setAttribute('href', myUrl);
  document.getElementsByTagName('head')[0].appendChild(myScript);
  },

 tplload: function( pModule, pElement, pIsAsync ) {
  var myIdentifier = pModule + '.' + pElement + '.html.tpl;';   
  if ( tb.cache.findindex( myIdentifier ) != -1 ) return;
  var myUrl = tb.loader.path + 'service.php';
  var myParms = 'url=twobirds/modules/' + pModule + '/tpl/' + pElement + '.html.tpl';
  var myIsAsync = ( pIsAsync !== null ? pIsAsync : true ); 
  tb.request.add( tb.loader.tplload_cb, 'GET', myUrl, myParms, myIsAsync );
  },
  
 tplload_cb: function ( pXml, pText) {
  var myIdentifierStart = pText.indexOf('<!--')+4;
  var myIdentifierEnd = pText.indexOf('-->');
  var myIdentifier = pText.slice(myIdentifierStart,myIdentifierEnd);
  var myData = pText.slice( pText.indexOf('>')+1 );
  tb.cache.set( myIdentifier, myData );
  tb.element.requireok( myIdentifier );
  },

 tplget: function( pModule, pElement) {
  var myIdentifier = pModule + '.' + pElement + '.html.tpl;'; 
  return tb.cache.get( myIdentifier );
  }
  
 };

tb.element = {

 // public
 
 show: function ( pDivId, pModule, pElement, pParms, pIsAsync ) {
  var myIsAsync = ( pIsAsync !== null ? pIsAsync : true );
  var myIdentifier = pModule + '.' + pElement+'.js;';
  if ( tb.element.loading( myIdentifier ) == false && tb.element.loaded( myIdentifier ) == false ) 
   tb.loader.jsload( pModule, pElement, myIsAsync );
  var myCondition = '( typeof '+pModule+' != \'undefined\' && typeof '+pModule+'.'+pElement+' != \'undefined\' )';
  var myCallback = 'if ( typeof '+pModule+'.'+pElement+'.init == \'function\') '+pModule+'.'+pElement + '.init(\'' + pDivId + '\');';
  var myFrequency = 50;
  tb.misc.wait( myCondition, myCallback, myFrequency );
  },
 
 require: function ( pRequire, pCallback, pIsAsync) {
  var myRequire = ( typeof pRequire != 'array' ? eval ( pRequire ) : pRequire );
  var myCallback = ( typeof pCallback != 'string' ? '' : pCallback );
  var myIsAsync = ( typeof pIsAsync != 'boolean' ? true : pIsAsync );
  var myDone = true;
  var myRequireString = '';

  var myCallbackGeneral = '';
  if ( pCallback && pCallback.length > 0 )
   myCallbackGeneral = myCallback.substring( 0, pCallback.indexOf('(') ) + '();';

  if ( myCallback === '' || ( myCallbackGeneral.length > 0 && tb.element.eleok.indexOf( myCallbackGeneral ) < 0 ) )
  for ( var i in myRequire ) {
   switch ( myRequire[i][0] ) {
    case 'js':
     var myIdentifier = myRequire[i][1]+'.'+myRequire[i][2]+'.js;';
     if ( ! eval( '('+myRequire[i][1]+'.'+myRequire[i][2]+')' ) ) {
      if ( tb.element.loading( myIdentifier ) == false ) {
       tb.element.requireadd( myIdentifier );
       tb.loader.jsload( myRequire[i][1], myRequire[i][2], myIsAsync );
       myRequireString = myRequireString + ( myRequireString.length > 0 ? '|' : '' ) + myIdentifier;
       if ( myDone == true ) myDone = tb.element.loaded( myIdentifier );
       }
      }
     else {
      this.requireok( myIdentifier );
      }
     break;
    case 'css':
     var myIdentifier = myRequire[i][1]+'.'+myRequire[i][2]+'.css;';
     if ( tb.element.iscss( myRequire[i][1], myRequire[i][2] ) == true ) break;
     tb.element.requireadd( myIdentifier );
     tb.loader.cssload( myRequire[i][1], myRequire[i][2], myIsAsync );
     myRequireString = myRequireString + ( myRequireString.length > 0 ? '|' : '' ) + myIdentifier;
     if ( myDone == true ) myDone = tb.element.loaded( myIdentifier );
     break;
    case 'tpl':
     var myIdentifier = myRequire[i][1]+'.'+myRequire[i][2]+'.html.tpl;';
     if ( tb.element.loaded( myIdentifier ) == true ) break;
     if ( tb.element.loading( myIdentifier ) == false ) {
      tb.element.requireadd( myIdentifier );
      tb.loader.tplload( myRequire[i][1], myRequire[i][2], myIsAsync );
      myRequireString = myRequireString + ( myRequireString.length > 0 ? '|' : '' ) + myIdentifier;
      if ( myDone == true ) myDone = tb.element.loaded( myIdentifier );
      }
     break;
    case 'file':
     var myIdentifier = myRequire[i][1]+'.'+myRequire[i][2];
     if ( tb.element.loaded( myIdentifier ) == true ) break;
     if ( tb.element.loading( myIdentifier ) == false ) {
      tb.element.requireadd( myIdentifier );
      tb.loader.fileload( myRequire[i][1], myRequire[i][2], myIsAsync );
      myRequireString = myRequireString + ( myRequireString.length > 0 ? '|' : '' ) + myIdentifier;
      if ( myDone == true ) myDone = tb.element.loaded( myIdentifier );
      }
     break;
    }
   }
  if ( myDone == true ) {
   if ( myCallback && myCallback.length>0 ) {
    if ( tb.element.eleok.indexOf( myCallbackGeneral ) < 0 ) {
     tb.element.eleok += myCallbackGeneral;
     }
    tb.timer.add( myCallback, 20);
    }
   }
  else {
   myCallback = 'tb.element.requiretest( \''+myRequireString+'\', \''+myCallback+'\' )';
   tb.timer.add ( myCallback, 100 );
   }
  },

 // private

 req: '',
 reqok: '',
 eleok: '',
 
 requiretest: function ( pRequireString, pCallback ) {
  var myRequire = pRequireString.split('|');

  var myCallbackGeneral = '';
  if ( pCallback && pCallback.length > 0 )
   myCallbackGeneral = pCallback.substring( 0, pCallback.indexOf('(') ) + '();';

  for ( var i in myRequire ) {
   if ( tb.element.loading( myRequire[i] ) == true ) {
    var myCallback = 'tb.element.requiretest( \''+pRequireString+'\', \''+pCallback+'\' )';
    tb.timer.add ( myCallback, 50 );
    return;
    }
   }
  if ( pCallback != '' ) {
   if ( tb.element.eleok.indexOf( myCallbackGeneral ) < 0 ) {
    tb.element.eleok += myCallbackGeneral;
    }
   tb.timer.add( pCallback, 20);
   }
  },
  
 iscss: function( pModule, pElement ) {
  var myIdentifier = pModule+'/css/'+pElement;
  if ( document.styleSheets.length > 0 ) {
   for ( i=0; i<document.styleSheets.length; i++ ) {
    try {
     if ( /MSIE/.test(navigator.userAgent ) == true ) {
      var myLinkHref = document.styleSheets(i).href;
      }
     else {
      var myLinkHref = document.styleSheets.item(i).href;
      }
     }
    catch (e) {
     alert( e.toString() + i );
     return false;
     }
    if ( myLinkHref.indexOf( myIdentifier ) > -1 ) {
     tb.element.requireok( pModule+'.'+pElement+';' );
     return true;
     }
    }
   }
  return false;
  },
  
 requireadd: function( pIdentifier ) {
  tb.element.req += pIdentifier;
  },
     
 requireok: function( pIdentifier ) {
  tb.element.reqok += ( tb.element.reqok.indexOf( pIdentifier ) < 0 ? pIdentifier : '' );
  tb.element.req = tb.element.req.replace( new RegExp( pIdentifier ), '');
  },
    
 loaded: function( pIdentifier ) {
  var myModule = pIdentifier.split('.')[0];
  var myElement = pIdentifier.split('.');
  myElement.shift();
  myElement = myElement.join('.');
  myElement = myElement.replace( /;/, '');
  var myType = pIdentifier.split('.');
  myType = myType.pop();
  switch ( myType ) {
   case 'js;': 
    try {
     eval( '( '+myModule+'.'+myElement+' )' );
     tb.element.requireok( pIdentifier );
     return true;
     }
    catch (e) {
     return false; 
     }
    break;
   case 'css;': 
    if ( this.iscss( myModule, myElement ) == true ) {
     tb.element.requireok( pIdentifier );
     return true;
     }
    return false;
    break;
   default:
    return ( tb.element.reqok.indexOf( pIdentifier ) > -1 ? true : false );
   }
  },
     
 loading: function( pIdentifier ) {
  var myModule = pIdentifier.split('.')[0];
  var myElement = pIdentifier.split('.');
  myElement.shift();
  myElement = myElement.join('.');
  myElement = myElement.replace( /;/, '');
  var myType = pIdentifier.split('.');
  myType = myType.pop();
  switch ( myType ) {
   case 'js;': 
    if ( document.getElementsByTagName('head')[0].innerHTML.indexOf( myModule+'/js/'+ myElement ) > -1 && this.loaded( pIdentifier ) == false ) { 
     return true;
     }
    return false;
    break
   case 'css;': 
    if ( document.getElementsByTagName('head')[0].innerHTML.indexOf( myModule+'/css/'+ myElement ) > -1 && this.iscss( myModule, myElement ) == false ) {
     return true;
     }
    return false;
    break;
   default:
    return ( this.req.indexOf( pIdentifier ) > -1 ? true : false );
   }
  }
     
 };
 
tb.div = {

 // addEvent, removeEvent thx john resig
 addEvent: function ( pObj, pType, pFunc ){
  if ( pObj.addEventListener ) {
   pObj.addEventListener( pType, pFunc, false );
   } 
  else if ( pObj.attachEvent) {
   pObj["e"+pType+pFunc] = pFunc;
   pObj[pType+pFunc] = function() { pObj["e"+pType+pFunc]( window.event ); }
   pObj.attachEvent( "on"+pType, pObj[pType+pFunc] );
   }
  },

 removeEvent: function ( pObj, pType, pFunc ) {
  if ( pObj.removeEventListener )
   pObj.removeEventListener( pType, pFunc, false );
  else if ( pObj.detachEvent ) {
   pObj.detachEvent( "on"+pType, pObj[pType+pFunc] );
   pObj[pType+pFunc] = null;
   pObj["e"+pType+pFunc] = null;
   }
  },

 create: function ( pDivId, pId ) {
  try {
   var myParentDiv = document.getElementById( pDivId );
   if ( myParentDiv ) {
    var myDiv = document.createElement('div');
    myDiv.id = pId;
    myDiv.display='inline';
    if ( myParentDiv.hasChildNodes() == false ) {
     myParentDiv.appendChild(myDiv);
     }
    else {
     var myBeforeDiv = myParentDiv.firstChild;
     myParentDiv.insertBefore( myDiv, myBeforeDiv );
     }
    return document.getElementById( pId );
    }
   return false;
   }
  catch (e) {
   tb.debug.add('<p>tb.div.create() Error.</p>');
   }
  },
  
 addfirst: function ( pDivId, pHtml ) {
  try {
   if ( !document.getElementById( pDivId ) ) return false;
   var myDiv = document.getElementById( pDivId );
   if ( myDiv ) {
    var myId = 'div'+tb.misc.getid();
    var myDiv = tb.div.create ( pDivId, myId );
    myDiv.innerHTML = pHtml;
    return myId;
    }
   return false;
   }
  catch (e) {
   tb.debug.add('<p>tb.div.addfirst() Error.</p>');
   }
  },
  
 addlast: function ( pDivId, pHtml ) {
  try {
   var myDiv = document.getElementById( pDivId );
   if ( myDiv ) {
    var myId = tb.div.addfirst( pDivId, pHtml );
    tb.div.movelast(myDiv);
    return myId;
    }
   return false;
   }
  catch (e) {
   tb.debug.add('<p>tb.div.addlast() Error.</p>');
   }
  },
  
 replace: function ( pDivId, pHtml ) {
  try {
   var myDiv = document.getElementById( pDivId );
   if ( myDiv ) {
    document.getElementById( pDivId ).innerHTML = pHtml;
    return true;
    }
   return false;
   }
  catch (e) {
   tb.debug.add('<p>tb.div.replace() error.</p>');
   }
  },
  
 remove: function ( pDivId ) {
  try {
   var myDiv = document.getElementById( pDivId );
   if ( myDiv ) {
    var myIC = document.getElementById(pDivId).parentNode;
    myIC.removeChild( myDiv );
    return true;
    }
   return false;
   }
  catch (e) {
   tb.debug.add('<p>tb.div.remove() error.</p>');
   }
  },
  
 movefirst: function ( pDivId ) {
  try {
   while ( tb.div.moveup( pDivId ) == true  ) {
    }
   }
  catch (e) {
   tb.debug.add('<p>tb.div.movefirst( '+pDivId+' ) error.</p>');
   }
  },
  
 movelast: function ( pDivId ) {
  try {
   while ( tb.div.movedown( pDivId ) == true ) {
    }
   }
  catch (e) {
   tb.debug.add('<p>tb.div.movelast( '+pDivId+' ) error.</p>');
   }
  },
  
 moveup: function ( pDivId ) {
  try {
   var myDiv1 = document.getElementById( pDivId );
   var myDiv2 = myDiv1.previousSibling.id;
   if ( typeof myDiv1 != 'undefined' ) {
    if ( myDiv2 ) {
     tb.div.swapnode( pDivId, myDiv2 );
     return true;
     }
    }
   return false;
   }
  catch (e) {
   tb.debug.add('<p>tb.div.moveup() error.</p>');
   }
  },
  
 movedown: function ( pDivId ) {
  try {
   var myDiv1 = document.getElementById( pDivId );
   var myDiv2 = myDiv1.nextSibling.id;
   if ( myDiv1 ) {
    if ( myDiv2 ) {
     tb.div.swapnode( pDivId, myDiv2 );
     return true;
     }
    }
   return false;
   }
  catch (e) {
   tb.debug.add('<p>tb.div.movedown() error.</p>');
   }
  },
 
 swapnode: function (p1, p2) {
  try {
   var p1e = document.getElementById(p1);
   var p2e = document.getElementById(p2);
   if (p1e != p2e) {
    var pc1 = p1e.cloneNode(true);
    var pc2 = p2e.cloneNode(true);
    p2e.parentNode.replaceChild(pc1,p2e);
    p1e.parentNode.replaceChild(pc2,p1e);
    }
   } 
  catch (e) {
   tb.debug.add('<p>tb.div.swapnode() error.</p>');
   }
  },
  
 set: function ( pElement, pProperty, pValue ) {
  try {
   if ( pElement )
   switch( pProperty ) {
    case 'opacity' :
     if ( pElement.filters ) {
      pElement.style.filter = 'alpha(opacity=' + pValue * 100 + ')';
      if (!pElement.currentStyle.hasLayout) {
       pElement.style.zoom = 1;
       }
      }
     else {
      pElement.style.KHTMLOpacity = ""+pValue;
      pElement.style.MozOpacity = ""+pValue;
      pElement.style.opacity = ""+pValue;
      }
     break;
    default :
     pElement.style[ pProperty ] = pValue;
    }
   }
  catch (e) {
   tb.debug.add('<p>tb.div.set() error.</p>');
   }
  },
     
 get: function ( pElement, pProperty ) {
  try {
   if ( pElement ) {
    var myValue = null;
    var myDv = document.defaultView;
    if ( pProperty == 'opacity' ) { 
     if ( pElement.filters) {
      myValue = 1;
      try {
       myValue = pElement.filters.item('DXImageTransform.Microsoft.Alpha').opacity / 100;
       } 
      catch(e) {
       try {
        myValue = pElement.filters.item('alpha').opacity / 100;
        return myValue;
        } 
       catch(e) {
        }
       }
      }
     }
    else if ( pElement.style[pProperty] ) {
     alert(  pElement.style[pProperty] );
     myValue = pElement.style[pProperty];
     return myValue;
     }
    else if ( pElement.currentStyle && pElement.currentStyle[pProperty] ) {
     value = pElement.currentStyle[pProperty];
     }
    else if ( myDv && myDv.getComputedStyle ) {
     var myCv = '';
     for(i = 0, len = pProperty.length;i < len; ++i) {
      if ( pProperty.charAt(i) == pProperty.charAt(i).toUpperCase()) {
       myCv = myCv + '-' + pProperty.charAt(i).toLowerCase();
       } 
      else {
       myCv = myCv + pProperty.charAt(i);
       }
      }
     if ( myDv.getComputedStyle( pElement, '' ).getPropertyValue( myCv )) {
      myValue = myDv.getComputedStyle( pElement, '' ).getPropertyValue( myCv );
      }
     }
    return myValue;
    }
   }
  catch (e) {
   tb.debug.add('<p>tb.div.get() error.</p>');
   }
  }  
  
 }; 
