// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
// -
// -   framework pro ulehceni a sjednoceni prace s Eventama, AJAXem, ...
// -
// -     Zdenek Lefner [zdenek.lefner@trustport.com]
// -
// -------------------------------------------------------------------------------------------------
// -
// -   created: 20.01.2008
// -   change log:
// -
// -------------------------------------------------------------------------------------------------
// -
// -   methods:
// -      - (bind/unbind) Events
// -
// -
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------

fwk = {

  // ------------------------------------------------------------
  // --- obsluha udalosti (pripichnuti ci sejmuti akce z objektu)
  event: {
    defaultCatching: false,
    bind: function( element, action, fn, catching ) {
      var right = true
      if( element == null || typeof element == 'undefined' ) return false
      if( typeof catching == 'undefined' ) catching = this.defaultCatching
      if( element.addEventListener ) element.addEventListener( action, fn, catching )
      else if( element.attachEvent ) element.attachEvent( 'on'+action, fn )
      else right = false
      return right
    },
    unbind: function( element, action, fn, catching ) {
      var right = true
      if( element == null || typeof element == 'undefined' ) return false
      if( typeof catching == 'undefined' ) catching = this.defaultCatching
      if( element.removeEventListener ) element.removeEventListener( action, fn, catching )
      else if( element.detachEvent ) element.detachEvent( 'on'+action, fn )
      else right = false
      return right
    }
  },
  // ------

  // ------------------------------------------------------------
  // --- ajax functions
  ajax: {
    // --- pomocne funkce:
    // prevede objekt na retezcovou reprezentaci: klic1=hodnota&klic2:hodnota2...
    objToString: function(obj) {
      var str = ""
      for( var key in obj )
        str += key+'='+obj[key]+'&'
      return str.substring( 0, str.length-1 )
    },
    // vytvori objekt na komunikaci pomoci ajaxu napric vsemi prohlizecmi (snad)
    getHttp: function () {
      //Use IE's ActiveX items to load the file.
      var http = null
  		if(typeof ActiveXObject != 'undefined') {
  			try {http = new ActiveXObject("Msxml2.XMLHTTP");}
  			catch (e) {
  				try {http = new ActiveXObject("Microsoft.XMLHTTP");}
  				catch (E) {http = null;}
  			}
      } else if (XMLHttpRequest) {
			  try {http = new XMLHttpRequest();}
			  catch (e) {http = null;}
		  }
      return http
    },
    // --- funkce pro posilani dat
    // POST
    post: function( url, params, successFunction ) {
      var http = this.getHttp()
      if( http == null )
        return false
      http.open("POST", url, true)
      var sparams = ""
      if( typeof params != 'undefined' )
        sparams = this.objToString(params)
      http.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
      http.setRequestHeader("Content-length", sparams.length)
      http.setRequestHeader("Connection", "close")
      http.onreadystatechange = function() {
      	if(http.readyState == 4)
          if( http.status == 200 ) successFunction(http.responseText)
          //else { iBox.add( http.status + ': ' + url ) }
      }
      http.send(sparams)
    },
    // GET
    get: function( url, params, success ) {
      http.open("GET", url+"?"+params, true)
      http.onreadystatechange = function() {
        if(http.readyState == 4)
          if( http.status == 200 ) successFunction(http.responseText)
          //else { iBox.add( http.status + ': ' + url ) }
      }
      http.send(null);
    }
  },

  // ------------------------------------------------------------
  // Document Object Model
  dom: {
    stopPropagation: function(e) {
      // musi se zastavit dalsi probublavani udalosti k dalsim objektum
      if( e.stopPropagation ) e.stopPropagation();
      else e.cancelBubble = true;
    },
    removeChilds: function( node ) {
      if ( node.hasChildNodes() )
        while( node.childNodes.length > 0 )
          node.removeChild( node.firstChild )
    },
    hasClass: function( item, clsName ) {
      var classes = item.className.explode(' ')
      for( var i = 0; i < classes.length; i++ )
        if( classes[i] == clsName )
          return true
      return false
      /*
      if( item.className.indexOf(clsName) >= 0 ) return true
      return false
      */
    },
    addClass: function( item, newClass ) {
      // musim zjistit, zda jiz nahodou trida neni obsazena, pokud ne, v klidu ji prdam,
      // pokud ano, musim si byt jist, ze je to opravu ona - muze se stat, ze nazev se
      // bude shodovat s casti nazvu jine, cili pred a za ni musi byt mezera, ci to musi byt
      // prvni, ci posledni trida v seznamu
      if( typeof item.className == 'undefined' )
        return

      var cname = item.className.explode( ' ' ),
        found = false
      for( var i = 0; i < cname.length; i++ )
        if( cname[i] == newClass ) {
          found = true
          break
        }
      if( !found )
        item.className += ' '+newClass
    },
    removeClass: function( item, classname ) {
      if( typeof item.className == 'undefined' )
        return

      if( item.className.indexOf(classname) != -1 ) {
        // zbezne jsem nasel hledany retezec, proverim poradne
        var cname = item.className.explode( ' ' ),
          newcname = ''
        for( var i = 0; i < cname.length; i++ )
          if( cname[i] != classname )
            // vsechny tridy, ktery nenajdu, zaradim do novyho cname
            newcname += cname[i]+' '

        if( newcname.length > 0 )
          newcname = newcname.substr( 0, newcname.length-1 )
        item.className = newcname
      }
    },
    findByClass: function( parent, clsName ) {
      // vytvorim frontu open
      var open = new Array( parent )
      // dokud neni prazdna, zkousim ji prohledavat
      while( open.length > 0 ) {
        var item = open.pop()
        if( this.hasClass(item,clsName) )
          return item
        else
          for( var i = 0; i < item.childNodes.length; i++ )
            open.push( item.childNodes[i] )
      }
      /*
      for( var i = 0; i < parent.childNodes.length; i++ )
        if( this.hasClass(parent.childNodes[i],clsName) )
          return parent.childNodes[i]
      */
      return null
    },
    attr: function( element, attributes ) {
      for( var attr in attributes ) {
        element.setAttribute( attr, attributes[attr] )
      }
    }
  },
  parser: {
    obj: function( data ) {
      data = data.explode( "\n" )
      var obj = new Array()
      for( var i = 0; i < data.length; i++ ) {

        var row = data[i].explode( '|' )
        if( row[0].length <= 0 )
          continue
        var srow = 'obj.push( {'
        for( var j = 0; j < row.length; j++ ) {

          var pom = row[j].explode(':',2)
          srow +=  pom[0]+': "'+pom[1]+'",'

        }
        srow = srow.substr(0,srow.length-1)+' } )'
        eval( srow )

      }
      return obj
    }
  }
}