/**
 * event.lib.js - insert headline here
 *
 * COPYRIGHT: All  title   and  proprietary  rights,  including  trade
 * secrets,   in   the   Software   and   any   copies thereof and the
 * accompanying  written   materials,   are  owned  by   schukai  GmbH
 * and  are  protected  by  German  copyright  laws,  other applicable
 * copyright   laws  and  international  treaty  provisions.
 *
 * @package    alvine
 * @author     schukai GmbH <info@schukai.de>
 * @copyright  Copyright (C) 2002, 2003, 2004, 2005, 2006 schukai GmbH
 * @license    http://www.alvine.de/license/
 * @version    20061114
 * @link       http://www.alvine.de/
 */

/*usage:
 * addEvent(object,'mousemove',function);
 * removeEvent(object,'mousemove',function);
 * 
 * important: Function must be given as reference(without quotes!)
*/

function event_add( obj, type, fn, useCapture){

  obj = element_isObject(obj);
  if(!obj) return false;
  
  if(typeof type == 'undefined') return false;
  
  if(typeof obj.alvine_eventListeners == 'undefined') obj.alvine_eventListeners = {};
  if(typeof obj.alvine_eventListeners[type] == 'undefined') obj.alvine_eventListeners[type] = {};

  var fnKey = type.toString()+fn.toString();

  var newEntry = {};
  newEntry.type       = type;
  newEntry.fn         = fn;
  newEntry.useCapture = useCapture;

  obj.alvine_eventListeners[type][fnKey] = newEntry;
  
  //if fn is a string, wrap around a FunctionObject
  if(typeof fn == 'string'){
    var str = fn;
    fn = function(){
      eval(str);
    };
  }

  var ietype = type;
	if(type.indexOf('on')>-1) {
		type = type.substring(2);
	} else{
	 	ietype = 'on'+type;
	}
  
  if (obj.addEventListener) {
    //useCapture = (useCapture===true)?true:false;  //Fetaure deaktiviert aufgrund von Kompatibilität mit IE, der nur Bubbling implementiert
    obj.addEventListener( type, fn, false );
  } else if (obj.attachEvent) {
    fnKey = (typeof str != 'undefined')?str:fn.toString();

		obj['e'+type+fnKey] = fn;
		obj[type+fnKey] = function() { obj['e'+type+fnKey]( window.event ); }
		obj.attachEvent( ietype, obj[type+fnKey] );
  }
  
  return true;
  
}

function event_remove( obj, type, fn ){
  obj = element_isObject(obj);
  if(!obj) return false;

  if(typeof type == 'undefined') return false;

  var fnKey = type.toString()+fn.toString();
  if(typeof obj.alvine_eventListeners != 'undefined' && 
     typeof obj.alvine_eventListeners[type] != 'undefined' && 
     typeof obj.alvine_eventListeners[type][fnKey] != 'undefined') {

    delete(obj.alvine_eventListeners[type][fnKey]);
    
  }

  //if fn is a string, wrap around a FunctionObject
  if(typeof fn == 'string'){
    var str = fn;
    fn = function(){
      eval(str);
    };
  }
  
  var ietype = type;
	if(type.indexOf('on')>-1) {
	 	type = type.substring(2);
	} else{
	 	ietype = 'on'+type;
	}
	
  if (obj.removeEventListener) {
    obj.removeEventListener( type, fn, false );
  } else if (obj.detachEvent) {
    fnKey = (typeof str != 'undefined')?str:fn.toString();

		obj.detachEvent( ietype, obj[type+fnKey] );
		obj[type+fnKey] = null;
		obj['e'+type+fnKey] = null;
  }
  
  return true;
}