var AjaxLoader = Class.create({
  
  initialize:function(element, options) {
    this.options = {
      loaderId: 'ajaxloader',
			loaderTag: 'div'
    };
    Object.extend(this.options, options || {});
    this.injectionElement = $(element);
  },
  
  activate: function() { 
    if (this.injectionElement) {
      this.loaderElement =  new Element(this.options.loaderTag, {id: this.options.loaderId});
      this.injectionElement.insert({bottom : this.loaderElement});
    }
  },
  
  deactivate: function() {
    if (this.loaderElement) this.loaderElement.remove();
  }
  
});

var ContactsControl = Class.create({
  
  initialize:function(options) {
    this.options = {
      ajaxUrl: document.location.href,
      ajaxPageType: 888
    };
    Object.extend(this.options, options || {});
  },
  
  displayContactsByTC: function(teamcategoryuid, updateelement, ajaxloader) {
		this.updateelement = $(updateelement);
		this.ajaxloader = ajaxloader;
		this.ajaxloader.activate();
		
		this.currentView = 'contacts';
		
		new Ajax.Updater(this.updateelement, this.options.ajaxUrl, {
      method: 'post',
      evalScripts: true,
      parameters: {
        action: 'displayContactsByTC',
        teamcategory: teamcategoryuid,
        type: this.options.ajaxPageType
      },
      onSuccess: function(request) {
        this.ajaxloader.deactivate();
      }.bind(this)
    });
		
	},
	
	displayLocationInfo: function(locationuid, locationupdateelement, teamupdateelement, ajaxloader) {
		this.locationupdateelement = $(locationupdateelement);
		this.teamupdateelement = $(teamupdateelement);
		
		this.ajaxloader = ajaxloader;
		this.ajaxloader.activate();
		
		if (this.currentView == 'representations') {
			this.representationupdateelement.update('');
			this.countryupdateelement.update('');
			for (var i=0; i<this.divisionselector.options.length; i++) {
				this.divisionselector.options[i].selected = false;
			}
		}
		
		this.currentView = 'locations';
		
		new Ajax.Updater(this.locationupdateelement, this.options.ajaxUrl, {
      method: 'post',
      evalScripts: true,
      parameters: {
        action: 'displayLocationInfo',
        location: locationuid,
        type: this.options.ajaxPageType
      },
      onSuccess: function(request) {
        //ajaxLoader.deactivate();
      }.bind(this)
    });
		
		new Ajax.Updater(this.teamupdateelement, this.options.ajaxUrl, {
      method: 'post',
      evalScripts: true,
      parameters: {
        action: 'displayContactsByLocation',
        location: locationuid,
        type: this.options.ajaxPageType
      },
      onSuccess: function(request) {
        this.ajaxloader.deactivate();
      }.bind(this)
    });
		
	},
	
	updateCountries: function(divisionselector, divisionuid, countryupdateelement, ajaxloader) {
		this.countryupdateelement = $(countryupdateelement);
		this.divisionselector = divisionselector;
		
		this.ajaxloader = ajaxloader;
		this.ajaxloader.activate();
		
		if (this.representationupdateelement) this.representationupdateelement.update('');
		
		if (this.currentView == 'locations') {
			this.locationupdateelement.update('');
			this.teamupdateelement.update('');
		}
		
		this.selectedDivision = divisionuid;
		
		this.currentView = 'representations';
		
		new Ajax.Updater(this.countryupdateelement, this.options.ajaxUrl, {
      method: 'post',
      evalScripts: true,
      parameters: {
        action: 'updateCountries',
        division: divisionuid,
        type: this.options.ajaxPageType
      },
      onSuccess: function(request) {
        this.ajaxloader.deactivate();
      }.bind(this)
    });
		
	},
	
	displayRepresentations: function(countryuid, representationupdateelement, ajaxloader) {
		this.representationupdateelement = $(representationupdateelement);
		
		this.ajaxloader = ajaxloader;
		this.ajaxloader.activate();
		
		new Ajax.Updater(this.representationupdateelement, this.options.ajaxUrl, {
      method: 'post',
      evalScripts: true,
      parameters: {
        action: 'displayRepresentations',
        country: countryuid,
        division: this.selectedDivision,
        type: this.options.ajaxPageType
      },
      onSuccess: function(request) {
        this.ajaxloader.deactivate();
      }.bind(this)
    });
    
	}
  
});
