var CInfoWindowManager = Class.create();
var CInfoWindow = Class.create();


// Priority Levels
var PRIORITY_MESSAGE_SYSTEM           = 4;
var PRIORITY_MESSAGE_ON_MAP_HISTORY   = 3;
var PRIORITY_MESSAGE_ON_MAP           = 2;
var PRIORITY_FEED_ON_MAP              = 1;
var PRIORITY_DEFAULT                  = 1;
// type des fenetres selon la priorite
var messagePriorityDefaultOptions = new Array();
messagePriorityDefaultOptions[PRIORITY_MESSAGE_SYSTEM] = {
  'expiration'  : -1,
  'forceDisplay':true,
  'noQueue':true
};
messagePriorityDefaultOptions[PRIORITY_MESSAGE_ON_MAP_HISTORY] = {
  'expiration'  : -1,
  'forceDisplay':true,
  'noQueue':true
};
messagePriorityDefaultOptions[PRIORITY_MESSAGE_ON_MAP] = {
  'expiration'  : 5000,
  'forceDisplay':true,
  'noQueue':false
};
messagePriorityDefaultOptions[PRIORITY_FEED_ON_MAP] = {
  'expiration'  : 7000,
  'forceDisplay':false,
  'noQueue':true
};




CInfoWindow.prototype = {
	'initialize' : function(handle, content, options){
    this.handle = handle;
    this.content = content;
    this.options = {
      'content' : content,
      'onOpen' : function(){},
      'onClose' : function(isManualClose){},
      'openInfoWindow' : function(map, marker){
        marker.GoogleMarker.openExtInfoWindow(
                    map,
                    'custom_info_window_red',
                    '<div style="margin-left:5px; margin-right:5px;">'+this.content+'</div>',
                    {beakOffset: 1, noCloseOnClick:true, beakLeft:20});
      }
    };
    if(Object.isUndefined(options)){
      options = {};
    }
    if(Object.isUndefined(options.level)){
      options.level = PRIORITY_DEFAULT;
    }
    if(Object.isUndefined(messagePriorityDefaultOptions[options.level])){
      Object.extend(this.options, messagePriorityDefaultOptions[PRIORITY_DEFAULT]);
    } else {
      Object.extend(this.options, messagePriorityDefaultOptions[options.level]);
    }
    Object.extend(this.options, options);
  }
};




// DEBUG
//    Replace "//debug" -> "/*debug*/"
// NO DEBUG
//    Replace "/*debug*/" -> "//debug"

CInfoWindowManager.prototype = {
	'initialize' : function(map){
//debug    this.niveauFunction = 0;
    this.map = map;
    this.messages = new Array();
    this.minLevel = 1;
    this.maxLevel = 0;
    this.minLevelPriority = this.minLevel;
    this.noMessage = true;
    this.activeMessage = null;
    this.dnmTimeout = null;
    
    this.isStarted = true;

    GEvent.addListener(
                this.map.map,
                'extinfowindowclose',
                this.onCloseEvent.bindAsEventListener(this));
    GEvent.addListener(
                this.map.map,
                'infowindowclose',
                this.onCloseEvent.bindAsEventListener(this));
  },
//debug  'addMessageLog' : function(message){
//debug    var count = this.niveauFunction;
//debug    while(count!=0){
//debug      message = '  '+message;
//debug      count--;
//debug    }
//debug    $('whereToGoHistory').innerHTML += message+"\n";
//debug  },
//debug  'addLog' : function(message, begin){
//debug    if(begin){
//debug      this.addMessageLog('b-'+message);
//debug      this.niveauFunction++;
//debug    } else {
//debug      this.niveauFunction--;
//debug      this.addMessageLog('e-'+message);
//debug    }
//debug  },
  'addMessage' : function(message){
//debug    this.addLog('addMessage:'+Object.toJSON(message), true);
    this.maxLevel = Math.max(this.maxLevel, message.options.level);

    if(message.options.noQueue){
//debug     this.addMessageLog('noQueue:'+message.options.level);
      this.messages[message.options.level] = new Array(message);
    } else {
//debug     this.addMessageLog('Queue:'+message.options.level);
      if(Object.isUndefined(this.messages[message.options.level])){
//debug       this.addMessageLog('CreationQueue:'+message.options.level);
        this.messages[message.options.level] = new Array();
      }
      if(message.options.forceDisplay){
//debug       this.addMessageLog('forceDisplay:'+message.options.level);
        this.messages[message.options.level].unshift(message);
      } else {
//debug       this.addMessageLog('putInQueue:'+message.options.level);
        this.messages[message.options.level].push(message);
      }
    }

    if(this.noMessage){
      this.displayNextMessage();
    } else if(message.options.forceDisplay && this.getNextMessageLevel()==message.options.level){
      this.closeActiveInfoWindow();
    }
//debug    this.addLog('addMessage'+"\n\n\n", false);
  },
  'getNextMessage' : function(){
//debug    this.addLog('getNextMessage', true);
    for(var i=this.maxLevel; i>=this.minLevelPriority; i--){
      if(!Object.isUndefined(this.messages[i])){
        if(this.messages[i].length>0){
//debug          this.addLog('getNextMessage:'+i, false);
          return this.messages[i].shift();
        }
      }
    }
//debug    this.addLog('getNextMessage:u', false);
    return null;
  },
  'getNextMessageLevel' : function(){
//debug    this.addLog('getNextMessageLevel', true);
    for(var i=this.maxLevel; i>=this.minLevelPriority; i--){
      if(!Object.isUndefined(this.messages[i])){
        if(this.messages[i].length>0){
//debug          this.addLog('getNextMessageLevel:'+i, false);
          return this.messages[i][0].options.level;
        }
      }
    }
//debug    this.addLog('getNextMessageLevel:u', false);
    return 0;
  },
  'displayNextMessage' : function(){
//debug    this.addLog('displayNextMessage', true);
    this.clearDNMTimeout();
    if(this.isStarted){
      this.activeMessage = this.getNextMessage();
      if(this.activeMessage==null){
        this.noMessage = true;
      } else {
        this.noMessage = false;
        this.displayMessage(this.activeMessage);
      }
    }
//debug    this.addLog('displayNextMessage', false);
  },
  'displayMessage' : function(message){
//debug    this.addLog('displayMessage', true);
    var marker = this.map.getMarker(message.handle);
    if(!Object.isUndefined(marker)
        && !marker.GoogleMarker.isHidden()
        && this.map.map.getBounds().containsLatLng(marker.GoogleMarker.getLatLng())){
      message.options.openInfoWindow(this.map.map, marker);
      if(this.activeMessage != null){
        if(Object.isFunction(this.activeMessage.options.onOpen)){
          this.activeMessage.options.onOpen();
        }
      }
      if(message.options.expiration>0){
        this.isManualClose = true;
        this.dnmTimeout = setTimeout(this.closeActiveInfoWindow.bindAsEventListener(this), message.options.expiration);
      } else {
        this.clearDNMTimeout();
      }
    } else {
      this.displayNextMessage();
    }
//debug    this.addLog('displayMessage', false);
  },
  'closeActiveInfoWindow' : function() {
//debug    this.addLog('closeActiveInfoWindow', true);
    this.isManualClose = false;
    while(this.map.map.getExtInfoWindow()!=null) this.map.map.closeExtInfoWindow();
    this.map.map.closeInfoWindow();
//debug    this.addLog('closeActiveInfoWindow', false);
  },
  'onCloseEvent' : function() {
//debug    this.addLog('onCloseEvent', true);
    this.clearDNMTimeout();
    if(this.activeMessage != null){
      if(Object.isFunction(this.activeMessage.options.onClose)){
        this.activeMessage.options.onClose(this.isManualClose);
      }
    }
    this.activeMessage = null;
    this.dnmTimeout = setTimeout(this.displayNextMessage.bindAsEventListener(this), 200);
//debug    this.addLog('onCloseEvent', false);
  },
  'clearDNMTimeout' : function(){
//debug    this.addLog('clearDNMTimeout', true);
    if(this.dnmTimeout!=null){
      clearTimeout(this.dnmTimeout);
      this.dnmTimeout = null;
    }
//debug    this.addLog('clearDNMTimeout', false);
  },
  'clearMinLevelPriority' : function() {
    this.minLevelPriority = this.minLevel;
  },
  'setMinLevelPriority' : function(level) {
    this.minLevelPriority = level;
  },
  'isMessagePriorityAllowed' : function(level) {
    return level>=this.minLevelPriority;
  },
  'start' : function(){
    if(!this.isStarted){
      this.isStarted = true;
      this.displayNextMessage();
    }
  },
  'stop' : function(){
    if(this.isStarted){
      this.isStarted = false;
      this.closeActiveInfoWindow();
    }
  }
};

