var map;
var whereToGoObject;
var lookingForObject;
var infoWindowManager;
var windowModalManager;
var lookingForValue = new Array();
var clearMemory = null;
var publingToon = null;
var filterPrivateChat = null;
var inputTextPrivateChat = null;

function generateMap(mapOptions){
  startConnectionVideo();
  
  // 30 minutes de non activite
  clearMemory = new CClearMemory(30*60*1000);

  // INIT INPUTS
  new CInputGestion('loginWindow.login', {
		  'defaultValue':'Login',
		  'initValue':$('loginWindow.login').value
		  });
  filterPrivateChat = new CInputGestion('privateChat.filter', {
                                  'defaultValue':'Search a contact',
                                  'onKeyUp':function(){
                                    refreshPrivateChatContactList(this.element.value);
                                  }
                                  });
  inputTextPrivateChat = new CInputGestion('privateChat.inputText', {
                                  'defaultValue':'Chat',
                                  'onSubmit':function(){
                                    if(openedChat!=0){
                                      controller.requestController( 'addPrivateMessage',
                                                                    'idUser='+openedChat+'&message='+encodeURIComponent(this.element.value),
                                                                    onMessageSend);
                                    }
                                    this.element.value = '';
                                  }});
  whereToGoObject = new CInputGestion('whereToGoText', {
                                      'defaultValue':'Where to go ?',
                                      'onSubmit':function(address){
                                        map.whereToGo(address);
                                      },
                                      'onFocus':function(){
                                        openPoonMessage(40);
                                      }});
  lookingForObject = new CInputGestion('lookingForText', {
                                      'defaultValue':'',
                                      'allowEmpty':true,
                                      'onKeyPress':function(eventHandle){
                                      },
                                      'onFocus':function(){
                                        playPoonWorkflow([30,31], 5000);
                                      },
                                      'onKeyUpFeeds':function(eventHandle){
                                        if(Object.isUndefined(this.lastValue)) this.lastValue='';
                                        if(     eventHandle.keyCode == 32  // ESPACE
                                            ||  this.element.value==''
                                            || (eventHandle.keyCode == Event.KEY_BACKSPACE && countWord(this.lastValue)>countWord(this.element.value))) {
                                          if(this.lastValue!=this.element.value){
                                            this.onSubmit(this.element.value);
                                          }
                                        }
                                        this.lastValue=this.element.value;
                                      },
                                      'onKeyUpPeople':function(eventHandle){
                                      },
                                      'onKeyUpPlaces':function(eventHandle){
                                      },
                                      'onKeyUp':function(eventHandle){
                                        switch($('lookingForType').value) {
                                          case 'Feeds' :
                                            this.onKeyUpFeeds(eventHandle);
                                            break;
                                          case 'People' :
                                            this.onKeyUpPeople(eventHandle);
                                            break;
                                          case 'Places' :
                                            this.onKeyUpPlaces(eventHandle);
                                            break;
                                        }
                                      },
                                      'onSubmitFeeds':function(search){
                                        newFeedSearch = 1;
                                        controller.requestController('lookingFor', 'search='+encodeURIComponent(search), refreshPodList);
                                      },
                                      'onSubmitPeople':function(search){
                                        if(search!=''){
                                          if(windowModalManager.activeWindowPile && windowModalManager.activeWindowPile.length>0 && windowModalManager.activeWindowPile[0]=='userSearch'){
                                            applyPeopleSearch(search);
                                          } else {
                                            openPeopleSearch(search);
                                          }
                                        } else {
                                          if(windowModalManager.activeWindowPile && windowModalManager.activeWindowPile.length>0 && windowModalManager.activeWindowPile[0]=='userSearch'){
                                            closeWindowModal();
                                          }
                                        }
                                      },
                                      'onSubmitPlaces':function(search){
                                        if(search!=''){
                                          if(windowModalManager.activeWindowPile && windowModalManager.activeWindowPile.length>0 && windowModalManager.activeWindowPile[0]=='podSearch'){
                                            controller.requestController('lookingForPOD', 'search='+encodeURIComponent(search), onPODSearch);
                                          } else {
                                            openWindowModal('podSearch', {'onMap':true, 'onOpen':function(){
                                              controller.requestController('lookingForPOD', 'search='+encodeURIComponent(search), onPODSearch);
                                            }});
                                          }
                                        } else {
                                          if(windowModalManager.activeWindowPile && windowModalManager.activeWindowPile.length>0 && windowModalManager.activeWindowPile[0]=='podSearch'){
                                            closeWindowModal();
                                          }
                                        }
                                      },
                                      'changeType':function(){
                                        var theType = $('lookingForType').value;
                                        if(Object.isUndefined(lookingForValue[theType]))lookingForValue[theType]='';
                                        this.setValue(lookingForValue[theType]);
                                        this.onSubmit($('lookingForText').value);
                                      },
                                      'onSubmit':function(search){
                                        var theType = $('lookingForType').value;
                                        lookingForValue[theType] = search;
                                        switch(theType) {
                                          case 'Feeds' :
                                            this.onSubmitFeeds(search);
                                            this.onSubmitPeople('');
                                            this.onSubmitPlaces('');
                                            break;
                                          case 'People' :
                                            this.onSubmitFeeds('');
                                            this.onSubmitPeople(search);
                                            this.onSubmitPlaces('');
                                            break;
                                          case 'Places' :
                                            this.onSubmitFeeds('');
                                            this.onSubmitPeople('');
                                            this.onSubmitPlaces(search);
                                            break;
                                        }
                                      }});


  // INIT MAP
  var the_options = {
    "zoomStartEvent":OnZoomStart,
    "zoomEndEvent":OnZoomEnd,

    "moveEvent":OnMove,
    "moveStartEvent":OnMoveStart,
    "moveEndEvent":OnMoveEnd,

    "dragEvent":OnDrag,
    "dragStart":OnDragStart,
    "dragEnd":OnDragEnd,

    "click":OnMapClick
  };
	Object.extend(the_options, mapOptions.position || {});
  map = new CMapManager('map', the_options);
  initContextMenuOnMap();


  // PLACE PODS
  refreshPodList(mapOptions);



  // INIT System de bulles on map
  infoWindowManager = new CInfoWindowManager(map);
  
  
  
  // INIT Poon
  publingToon = new CPoon('poon', {'imgPath':'./Img/Poon/'});
  hidePoon(false);
  
  //    => Transmission au serveur des donnees
  //       Juste au cas ou les donnes ne seraient pas completes
  OnDragEnd();
}

function openPeopleSearch(search){
  openWindowModal('userSearch', {'onOpen':function(){
    applyPeopleSearch(search);
  }});
}

function applyPeopleSearch(search){
  controller.requestController('lookingForUser', 'search='+encodeURIComponent(search), onUserSearch);
}
