﻿$(function() {

	/*****************************************************************************/
	/* Fonctions */
	/*****************************************************************************/
	$.extend({

		/* ENTITY : Create trading */
		// currentCote : jsonTrading
		SelectedAnnonce: function(param, success, failure) {
			$.AjaxCall("services/AjaxService.asmx/GetSelectedAds",
				"{'top': " + param + "}",
				false,
				function(response) { if (success) success(response.d); },
				function() { if (failure) failure(); }
			);
		},

		/* HTML : Set Selected Car Ad */
		SetSelectedAnnonce: function() {
			if ($("#Selection").length > 0) {
				//$.ShowLoading();
				var currentAnnonce = {};
				$.SelectedAnnonce(1,
					function(response) {
						if (response.Key == 1 && response.Value[0] != null) {
							currentAnnonce = response.Value[0];
							$("#Selection").addClass("bloc-selection");
							$("<h3>La s&eacute;lection du jour</h3>").appendTo("#Selection");
							$('<a id="imgLink" href="' + $.UrlFicheVehiculeByAnnonce(currentAnnonce) + '" title="' + $.TitreFicheVehicule(currentAnnonce) + '"></a>').appendTo("#Selection");
							if (currentAnnonce.Photos != null && currentAnnonce.Photos[0] != null) {
								var image = $('<img src="' + currentAnnonce.Photos[0] + '" alt="' + $.TitreFicheVehicule(currentAnnonce) + '"/>').appendTo("#imgLink");
								image.load().error(function() { image.attr({ "src": ResolveUrl("~/img/photo-indisponible.gif") }); });
							}
							else
								$('<img src="' + ResolveUrl("~/img/photo-indisponible.gif") + '" alt="' + $.TitreFicheVehicule(currentAnnonce) + '"/>').appendTo("#imgLink");
							$('<div id="divSelection" class="bloc-gris"></div>').appendTo("#Selection");
							$("<h4>" + currentAnnonce.Marque.Libelle + " " + currentAnnonce.Modele.Libelle + "</h4>").appendTo("#divSelection");
							$('<h4 class="espace">' + $.VehiculeScreenVersion(currentAnnonce) + '</h4>').appendTo("#divSelection");
							$("<p>" + currentAnnonce.Energie.Libelle + " | " + currentAnnonce.AnneeModele + "</p>").appendTo("#divSelection");
							$("<p>" + currentAnnonce.Kilometrage + " Km | " + currentAnnonce.NbPorte + " portes</p>").appendTo("#divSelection");
							$('<p class="prix">' + $().number_format(currentAnnonce.PrixVente, { numberOfDecimals: 0, decimalSeparator: ',', thousandSeparator: ' ' }) + ' &euro;</p>').appendTo("#divSelection");
							$('<a href="' + $.UrlFicheVehiculeByAnnonce(currentAnnonce) + '" title="Détail de l\'annonce">D&eacute;tail de l&#39;annonce</a>').appendTo("#divSelection");
						}
						//$.HideLoading();
					},
					function() {
						$("#Selection").hide();
						//$.HideLoading();
					}
				);
			}
		},

		/* HTML : Set Selecteds Car Ad */
		SetAnnoncesDuMoment: function() {
			if ($("#SelectionJour").length > 0) {
				$.ShowLoading();
				$.SelectedAnnonce(5,
					function(response) {
						if (response.Key == 1) {
							var listeAnnonces = response.Value;
							$.each(listeAnnonces, function(index, annonce) {
								if (index == 0) {
									$("#Fiche").attr("title", $.TitreFicheVehicule(annonce));
									$("#Fiche").attr("href", $.UrlFicheVehiculeByAnnonce(annonce));
									if (annonce.Photos != null && annonce.Photos[0] != null) {
										$("#PhotoPrincipale").attr({ "src": annonce.Photos[0], "alt": $.TitreFicheVehicule(annonce) });
										$("#PhotoPrincipale").load().error(function() { $("#PhotoPrincipale").attr({ "src": ResolveUrl("~/img/photo-indisponible.gif") }); });
									}
									$("#MarqueModele").html(annonce.Marque.Libelle + " " + annonce.Modele.Libelle);
									$("#Version").html($.VehiculeScreenVersion(annonce));
									$("#Caracteristiques1").html(annonce.Energie.Libelle + " | " + annonce.AnneeModele + " | " + annonce.Kilometrage + " Km");
									$("#Caracteristiques2").html(annonce.Couleur.Libelle + " | " + annonce.NbPorte + " portes");
									$("#Caracteristiques3").html("Boîte " + annonce.Boite.Libelle);
									$("#PrixVente").html("&nbsp;" + $().number_format(annonce.PrixVente, { numberOfDecimals: 0, decimalSeparator: ',', thousandSeparator: ' ' }) + " &euro;&nbsp;");
									$("#FicheDetail").attr("href", $.UrlFicheVehiculeByAnnonce(annonce));
								}
								else if (index < 5) {
									$("#Fiche" + index).empty();
									$("#Fiche" + index).attr("title", $.TitreFicheVehicule(annonce));
									$("#Fiche" + index).attr("href", $.UrlFicheVehiculeByAnnonce(annonce));
									if (annonce.Photos != null && annonce.Photos[0] != null) {
										var image = $('<img src="' + annonce.Photos[0] + '" alt="' + $.TitreFicheVehicule(annonce) + '" />').appendTo("#Fiche" + index);
										image.load().error(function() { image.attr({ "src": ResolveUrl("~/img/photo-indisponible.gif") }); });
										$('<p class="vignette">' + $().number_format(annonce.PrixVente, { numberOfDecimals: 0, decimalSeparator: ',', thousandSeparator: ' ' }) + ' &euro;' + '</p>').appendTo("#Fiche" + index);
									}
									else
										$('<img src="' + ResolveUrl("~/img/photo-indisponible.gif") + '" alt="' + $.TitreFicheVehicule(annonce) + '" /><p class="vignette">' + $().number_format(annonce.PrixVente, { numberOfDecimals: 0, decimalSeparator: ',', thousandSeparator: ' ' }) + ' &euro;' + '</p>').appendTo("#Fiche" + index);

								}
							});
						}
						$.HideLoading();
					},
					function(response) {
						$.HideLoading();
					}
				);
			}
		}
	});


	/*****************************************************************************/
	/* Initialisation */
	/*****************************************************************************/

	/* La sélection du jour */
	$(document).ready(function() {
		$.SetSelectedAnnonce();
	});
});
