var Sopratec = {};

Sopratec.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":Sopratec.profileCandidat.deleteFileCallback.bind(el)});
    }
  },
  "deleteFileCallback" : function(h){
    if (h["error"] == 0){
      alert('Fichier supprimé');
      Kwo.reload();
    } else {
      Kwo.callback(h);
    }
  }
};

Sopratec.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});
    }
  }
};

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

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

Sopratec.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 = Sopratec.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(Sopratec.calendar.printDate.bind(el), cal, true);
		cal.render();
    cal.show();
  }

};

Sopratec.Map = {
  "map": null,
  "bounds": null,
  "init": function () {
    if (GBrowserIsCompatible()) {
      Event.observe(window,'unload',GUnload);
      Sopratec.Map.map = new GMap2($('gmap'));
      Sopratec.Map.bounds = new GLatLngBounds();
      var control = new GLargeMapControl();
      Sopratec.Map.map.addControl(control);
/*      Sopratec.Map.map.enableScrollWheelZoom();*/
      var point = new GLatLng(0,0);
      Sopratec.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});
      Sopratec.Map.map.addOverlay(marker);
      Sopratec.Map.map.setCenter(point,zoom);
      Sopratec.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',Sopratec.Map.viewInfo.curry(lat,lng,name,id,url));
      Sopratec.Map.map.addOverlay(marker);
      Sopratec.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>";
      Sopratec.Map.map.openInfoWindowHtml(point,html);
    }
  },
  "autoZoom": function () {
    if (GBrowserIsCompatible()) {
      var point = Sopratec.Map.bounds.getCenter();
      var zoom = Sopratec.Map.map.getBoundsZoomLevel(Sopratec.Map.bounds);
      Sopratec.Map.map.setCenter(point,zoom);
      Sopratec.Map.map.savePosition();
    }
  }
};

Sopratec.Secteur = {
  "populate" : function (id, h, value) {
    var id = $(id);
    id.length = 0;
    h.each(function(item) {
      if (typeof item['key'] != 'number') return;
      id.options[id.length] = new Option(item['value'],item['key']);
      if (item['key'] == value) id.options[id.length-1].selected = "selected";
    });
  }
};
Sopratec.display = {
  "alphaBackgrounds" : function (e) {
    var bg = e.currentStyle.backgroundImage;
    if (bg.match(/\.png/i) != null) { 
      var mypng = bg.substring(5,bg.length-2);
      e.setStyle({
        filter : "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + mypng + "', sizingMethod='" + (e.currentStyle.backgroundRepeat == "no-repeat" ? "crop" : "scale") + "')", backgroundImage : "none"});
    }
  }
};

Sopratec.Candidat = {
  "compteur" : 1,
  "ajoutLicence" : function() {
    var myTemplate = new Template($('default_licence').innerHTML);
    var show = {compteur: Sopratec.Candidat.compteur};
    myTemplate.evaluate(show);
    $('default_licence').insert({'after' : myTemplate.evaluate(show)});
    $('offset_licence').value=Sopratec.Candidat.compteur;
    Sopratec.Candidat.compteur ++;
  },
  "supprLicence" : function(candidat_id, licence_id) {
    if (!confirm("Etes-vous sûr de vouloir supprimer cette licence ?")) return;
    Kwo.exec('/cvtheque/candidat.licence.delete', {"candidat":candidat_id, "licence":licence_id}, {"callback":Sopratec.Candidat.editProfile});
  },
  
  "editProfile" : function(){
    Kwo.reload();
  }
}

document.observe("dom:loaded", function() {
  if (Prototype.Browser.IE) {
    var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, "");
    if (rslt != null && Number(rslt[1]) >= 5.5 && Number(rslt[1]) < 7) {
      $$(".trans").each(Sopratec.display.alphaBackgrounds);
    }
  }
});