﻿//*******************************************
//  CONSTRUCTOR
//*******************************************
function Events(oControl, oRootElement, oScope) {

     
    //***************************************
    //  PROPERTIES
    //***************************************

    var __oControl = oControl;
    var __oRootElement = oRootElement;
    var __oScope = oScope;
  
    //***************************************
    // CLASS INSTANCES
    //***************************************
     var __collection = new Collection();

    //***************************************
    //  PRIVATE
    //***************************************


    //***************************************
    //  INITIATE EVENTS
    //***************************************
    function loadEvent(sReference, oElement, sEventType, fFunctionCall) {
     var oProperties = new Object();
     oProperties.oElement = oElement;
     oProperties.sEventType = sEventType; 
    
     //EVENT
     oProperties.oToken = oElement.addEventListener(sEventType, Silverlight.createDelegate(__oScope, fFunctionCall))
     
     //CAPTURE EVENT
      __collection.add(sReference, oProperties);
    }
    

    //*****************************************
    //  REMOVE EVENTS
    //****************************************
     function removeEvent(sReference) {
     
        if(sReference != null) {
         var oElement = __collection.getItem(sReference).oElement;
        }
         var sEventType = __collection.getItem(sReference).sEventType; 
         var oToken = __collection.getItem(sReference).oToken;
     
        oElement.removeEventListener(sEventType, oToken);
        
        if(sReference != undefined) {
            __collection.remove(sReference);
        }
        

        
    }
    
    //****************************************
    //  REMOVE ALL EVENTS
    //****************************************
    function removeAllEvents() {
        var aList = __collection.getAll();
        var nLength = aList.length;
        
        for(var i = 0; i < nLength; i++) {
            removeEvent(aList[i].sName);
        }
    }
 
    //******************************************
    //  PUBLIC
    //******************************************

   
    //******************************************
    //   ADD EVENT
    //******************************************
    this.add = function(sReference, oElement, sEventType, fFunctionCall) {
        loadEvent(sReference, oElement, sEventType, fFunctionCall);
    }
    
    //******************************************
    //  REMOVE EVENTS
    //******************************************
    this.remove = function(sReference) {
       removeEvent(sReference)
    }
    
    //******************************************
    //  REMOVE ALL EVENTS
    //******************************************
    this.removeAll = function() {
        removeAllEvents();
    }
}