var Gitec = {};

Gitec.profileCandidat = {
  "send": function (args) {
    Kwo.exec("/account/cvtheque/my_infos.store", args, {"callback":true});
  },
  "deleteFile" : function (el, type) {
    if (confirm ('Etes-vous sur ?')) {
      Kwo.exec("/account/cvtheque/my_infos.delete.file", {"type":type}, {"callback":Gitec.profileCandidat.deleteFileCallback.bind(el)});
    }
  },
  "deleteFileCallback" : function(h){
    if (h["error"] == 0){
      alert('Fichier supprimé');
      Kwo.reload();
    } else {
      Kwo.callback(h);
    }
  }
};

Gitec.crm = {
  "send": function(args){
    if (confirm('Veuillez confimer l\'envoi du compte rendu.\nAucune modification ne sera possible après.')) {
      Kwo.exec("/crm.store", args, {"callback":true});
    }
  }
};

Gitec.offres = {
  "submit": function (arg){
    if (arg.word.value == _wordReplace) {
      arg.word.value = '';
    }
		arg.submit();
  }
};

Gitec.offre = {
  "postulate": function (arg) {
    Kwo.exec("/cvtheque/offre.postulate.send", arg, {"callback":true});
  }
};

Gitec.calendar = {
  "y2k" : function(str) {
    str = str.split("/");
    return parseInt(str[0])+'/'+parseInt(str[1])+'/'+parseInt(str[2]);
  },
  "k2y" : function(str) {
    str = str.split("/");
    return parseInt(str[1])+'/'+parseInt(str[0])+'/'+parseInt(str[2]);
  },
  "printDate" : function(type,args,obj) {
    var date = obj.getSelectedDates()[0];
    this.value = date.getDate()+'/'+(date.getMonth() + 1)+'/'+date.getFullYear();
    obj.hide();
  },
  "print": function (el, container){
    if (el.value.empty()) {
     dayDate = new Date();
      date = (dayDate.getMonth()+1)+'/'+dayDate.getDate()+'/'+dayDate.getFullYear();
    } else {
      date = Gitec.calendar.k2y(el.value);
    }

    opts = { title : "", close : true};
		var cal = new YAHOO.widget.Calendar(container, opts);

    cal.select(date);

    str = date.split("/");

    cal.cfg.setProperty("pagedate", str[0]+'/'+str[2]);
    cal.selectEvent.subscribe(Gitec.calendar.printDate.bind(el), cal, true);
		cal.render();
    cal.show();
  }

};

Gitec.Map = {
  "map": null,
  "bounds": null,
  "init": function () {
    if (GBrowserIsCompatible()) {
      Event.observe(window,'unload',GUnload);
      Gitec.Map.map = new GMap2($('gmap'));
      Gitec.Map.bounds = new GLatLngBounds();
      var control = new GLargeMapControl();
      Gitec.Map.map.addControl(control);
/*      Gitec.Map.map.enableScrollWheelZoom();*/
      var point = new GLatLng(0,0);
      Gitec.Map.map.setCenter(point,1);
    }
  },
  "viewMarker": function (lat,lng,zoom) {
    if (GBrowserIsCompatible()) {
      var point = new GLatLng(lat,lng);
      var icon = new GIcon(G_DEFAULT_ICON);
      var marker = new GMarker(point,{'icon':icon});
      Gitec.Map.map.addOverlay(marker);
      Gitec.Map.map.setCenter(point,zoom);
      Gitec.Map.map.savePosition();
    }
  },
  "addMarker": function (name,id,lat,lng,url) {
    if (GBrowserIsCompatible()) {
      var point = new GLatLng(lat,lng);
      var icon = new GIcon(G_DEFAULT_ICON);
      var marker = new GMarker(point,{'icon':icon});
      GEvent.addListener(marker,'click',Gitec.Map.viewInfo.curry(lat,lng,name,id,url));
      Gitec.Map.map.addOverlay(marker);
      Gitec.Map.bounds.extend(point);
    }
  },
  "viewInfo": function (lat,lng,name,id,url) {
    if (GBrowserIsCompatible()) {
      var point = new GLatLng(lat,lng);
      var html = "<div class='infowindow'><div class='title'>"+$(name+"-"+id).innerHTML+"</div>"+$(name+"-info-"+id).innerHTML+"</div>";
      Gitec.Map.map.openInfoWindowHtml(point,html);
    }
  },
  "autoZoom": function () {
    if (GBrowserIsCompatible()) {
      var point = Gitec.Map.bounds.getCenter();
      var zoom = Gitec.Map.map.getBoundsZoomLevel(Gitec.Map.bounds);
      Gitec.Map.map.setCenter(point,zoom);
      Gitec.Map.map.savePosition();
    }
  }
};

Gitec.Secteur = {
  "populate" : function (id, h, value) {
    var id = $(id);
    id.length = 0;
    h.each(function(item) {
      id.options[id.length] = new Option(item['value'],item['key']);
      if (item['key'] == value) id.options[id.length-1].selected = "selected";
    });
  }
};