﻿jQuery.fn.LabelDansTexte = function () {
    // faire apparaitre le label de renseignement dans le texte
    var $SpanGeneral = this.addClass('overlabel')
    var $ChampsTexte = $SpanGeneral.find('input');
    var $LabelDuTexte = $SpanGeneral.find('label');

    $ChampsTexte
    .focus(function () {
        $LabelDuTexte.hide();
    })
     .blur(function () {
         if (this.value == '') { $LabelDuTexte.show(); }
     });

    $LabelDuTexte.click(function () {
        $ChampsTexte.trigger('focus');
    });
};

function imageLoader(img, callback) // La fonction prend en realite en compte tout les arguments qui lui sont donné, chacun representant l'url d'une image
{
    var count = img.length;
    var argv = img;
    var section = parseInt(100 / count);
    var width = 0;
    var span = new Array;
    var flag = new Array;
    var images = new Array;

    if (typeof (callback) != "function")
        callback = 0;

    for (var i in img) { // Pour chacune des images
        flag[i] = 0;
        loadImage(i);
    }

    function addElement(tag, parent) { // Fonctions qui raccourci la creation d'un element
        var elem = document.createElement(tag);
        return parent.appendChild(elem);
    }

    function $(id) { // Fonction raccourci de getElementById
        return document.getElementById(id);
    }

    function loadImage(i) { // Fonction recursive qui charge les images et ne modifie le code HTML qu'une fois l'image chargée
        if (flag[i] == 0) { // Initialisation (creation image)
            flag[i] = 1;
            images[i] = new Image;
            images[i].src = argv[i];
        }

        if (flag[i] == 1) { // Boucle recursive
            if (images[i].complete)
                flag[i] = 2;

            setTimeout(function () { loadImage(i) }, 50);
        }
        else if (flag[i] == 2) { // Fin de la recursivite, l'image est chargée
            if (count == 1) { // Fin de chargement de la derniere image, FIN
                //progress(100); // On affiche directement "100%" pour eviter d'avoir la troncature a 99%
                if (callback) // Appel du callback
                    callback();
            }
            else {
                --count;
            }
        }
    }
}

//Webservice par javascript
//function CallService() { CMS_UrbaLyon.USSI.Bienvenue("", Callback); }
//function Callback(result) { var outDiv = document.getElementById("txtRechercheSH"); outDiv.value = result; }
//OnClientClick = "CallService();return false;"

$(document).ready(
function () {

//    imageLoader(
//				    [
//					    "../Images/tooltip_bas.png",
//					    "../Images/tooltip.png"
//				    ],
//				    function () { }
//			    );

    // gestion du clic sur les liens 
    $('a.ImageDocument').parent().hover(
         function () {
             $(this).addClass('LienSurvole');
         }, function () {
             $(this).removeClass('LienSurvole');
         }
    );

    $("a.ImageDocument").parent().click(function () {
        if ($(this).find("a").attr("target") == "_blank") {
            window.open($(this).find("a").attr("href"));
        } else {
            window.location = $(this).find("a").attr("href");
        }

        return false;
    });

    // gestion des accents
    //$("div.Bando a").attr('title', $("div.Bando a").attr('title').replace('é', '&eacute;'));


    //correctif bug hauteur des navigateurs autre que IE
    var hauteur = 0;
    $('ul.NivPlanSite1 li').each(
        function () {
            if ($(this).height() > hauteur) { hauteur = $(this).height(); }
        }
    )
    $('#MainContent_DivPlanSite').height(hauteur);

    // fonctionnalité HTML5  : placeholder
    var isPlaceholderSupported = function () {
        var o = document.createElement('input');
        return "placeholder" in o;
    };

    if (!isPlaceholderSupported()) {
        var togglePlaceholderText = function (el) {
            var color = '#999';
            var v = $.trim(el.val());
            var p = el.attr('placeholder');
            if (v == '') {
                el.val(p);
                el.css({ 'color': color });
            } else if (v == p) {
                el.val('');
                el.css({ 'color': '' });
            }
        };
        $('input[placeholder]')
			.each(function () { togglePlaceholderText($(this)); })
			.click(function () { togglePlaceholderText($(this)); })
			.blur(function () { togglePlaceholderText($(this)); })
			.closest('form').submit(function () { $(this).find('input[placeholder]').each(function () { togglePlaceholderText($(this)); }) });
    }

    //Label Dans le Texte
    $('#DivRechercheSH').LabelDansTexte();
    $('#DivMotdePasse').LabelDansTexte();
    $('#DivUser').LabelDansTexte();
    $('#DivRechercheMenu').LabelDansTexte();

    //$('#txtRechercheSH').focus();

    //Infobulle
    $("[title].Infobulle").tooltip({
        predelay: 250,

        // tweak the position
        offset: [10, 2],

        // use the "slide" effect
        effect: 'slide'

        // add dynamic plugin with optional configuration for bottom edge
    }).dynamic({ bottom: { direction: 'down', bounce: true} });

    $("[title].InfobulleBas").tooltip({
        predelay: 250,

        // tweak the position
        //offset: [10, 2],
        position: ['bottom'],
        tipClass: 'tooltip_bottom',
        // use the "slide" effect
        effect: 'slide'

        // add dynamic plugin with optional configuration for bottom edge
    }).dynamic({ bottom: { direction: 'down', bounce: true} });
    //    // gestion du clic sur les liens "Explorer par thématique"
    //    $("#ListeThematique li.Niveau3").click(function () {
    //        //Récupérer l'attribut href du lien
    //        window.location = $(this).find("a").attr("href");
    //        return false;
    //    });

    // gestion du clic sur les liens "Explorer par territoire"
    $("li.Niveau3").click(function () {
        //Récupérer l'attribut href du lien
        window.location = $(this).find("a").attr("href");
        return false;
    });

    // gestion du clic sur les liens "bas de page d'UrbaLyon"
    $("#ListeUrbaLyon li").click(function () {
        //Récupérer l'attribut href du lien
        window.location = $(this).find("a").attr("href");
        return false;
    });

    // gestion du clic sur les liens "pdf rattachés"
    $(".PDFRattaches li").click(function () {
        //Récupérer l'attribut href du lien et ouvrir une nouvelle fenêtre
        window.open($(this).find("a").attr("href"));
        return false;
    });

    $("input[type=checkbox]").parent(0).css("cursor", "pointer");

    $(".Publications").click(function () {
        window.location = $(this).find("a.LienDocument").attr("href");
        return false;
    });

    $(".Publications .TelechargerPDF").click(function (e) {
        e.stopPropagation();
    });

    $(".Video").click(function () {
        window.location = $(this).find("a.LienVideo").attr("href");
        return false;
    });


});


