/*----------------------------------------------------------------------------//
// Javascript pour le site client  -------------------------------------------//
//----------------------------------------------------------------------------//
// Client : ADNM
// Date   : octobre 2010
//----------------------------------------------------------------------------*/
(function($, w, undefined) {

    var A = {};

    var dict = {
        validEmail: {
            fr: 'Vous devez inscrire un courriel valide.',
            en: 'Please enter a valid email.'
        }
    };


    function isEmpty(text) {
        return (text.replace(/^\s*|\s*$/g, '').replace(/^\t*|\t*$/g, '') == '') ? true : false;
    };

    function isEmail(text) {
        text = text.replace(/^\s*|\s*$/g, '');
        text = text.replace(/^\t*|\t*$/g, '');
        return (/^\w+([\.\+-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(text) == 0) ? false : true;
    };

    function equalHeight(s) {
        var H = 0;
        $(s)
			.each(function() {
			    var h = $(this).height();
			    if (h > H) {
			        H = h;
			    }
			})
			.height(H + 'px');
        ;
    };


    // on load
    $(document).ready(function() {

        A.Lang = $('body').attr('id').split('-')[1].toLowerCase();

        // style
        //--------------------------------------------------------------------

        // boutons
        $('.BtnPetit, .BtnMoyen, .BtnLarge, .BtnGris, #NavFixe li a').each(function() {
            $(this).wrapInner('<span>');
        });

        // menu On
        if ($('#MenuPrincipal li.On').length) {
            var $li = $('#MenuPrincipal li.On');
            $li.parent().css('background-position', ($li.position().left + Math.floor(($li.width() - 175) / 2)) + 'px top');
        }


        // fonctionnalités
        //--------------------------------------------------------------------

        // externe
        $('a[rel="externe"]').live('click', function(e) { e.preventDefault(); window.open(this.href); });

        // bouton imprimer
        $('li.Imprimer a').bind('click', function(e) { e.preventDefault(); window.print(); });

        // partager
        w.addthis_config = { ui_language: A.Lang };

        $('.Partager a').each(function() {
            addthis.button($(this).get(0), {}, {
                url: window.location.toString(),
                title: document.title
            });
        });

        // focus
        $('input[type="text"], textarea, select')
			.bind('focus', function() {
			    $(this).prev('label').andSelf().addClass('Focus');
			})
			.bind('blur', function() {
			    $(this).prev('label').andSelf().removeClass('Focus');
			}
		);

        // inline label
        $('input[title!=""], textarea[title!=""]').each(function() {

            var $this = $(this);

            $this
				.data('Label', $this.attr('title'))
				.attr('title', '')
				.bind('focus', function() {
				    var $this = $(this);
				    if (isEmpty($this.val()) || $this.val() == $this.data('Label')) {
				        $this.removeClass('Label').val('');
				    }
				})
				.bind('blur', function() {
				    var $this = $(this);
				    if (isEmpty($this.val()) || $this.val() == $this.data('Label')) {
				        $this.addClass('Label').val($this.data('Label'));
				    }
				})
				.blur()
			;
        });

        //Emplois
        $('div.Emploi').each(function() {

            var $curr = $(this), $all = $('div.Emploi');

            $curr.find('> h3 a, > p a')
				.bind('click', function() {
				    if ($curr.hasClass('On')) {
				        $curr.removeClass('On');
				    } else {
				        $all.removeClass('On');
				        $curr.addClass('On');
				    }
				});
        });
        $("input.BogusInput:file").css("opacity", "0").change(function() {
            $("input.BogusInput:text").val($(this).val());
        });



        // Accueil
        //-------------------------

        // Caroussel 
        if ($('#Bannieres').length) {
            Abso.Caroussel.init({
                id: 'Bannieres',
                transition: 'fade',
                wrap: true,
                autostart: {
                    interval: 4000
                }
            });
        }

        // Liens 
        equalHeight('body.Accueil #AccueilBlocs li a');

        // Inscription infolettre
        $('body.Accueil div.Infolettre a.Go').click(function(e) {
            e.preventDefault();

            var $this = $(this);
            if (!isEmail($this.siblings('input[type="text"]').val())) {
                A.alert(dict.validEmail[A.Lang], true);
            } else {
                eval($this.attr('href').substring(11));
            }
        });

        // confirmation
        if ($('body').hasClass('Accueil') && document.location.search == '?m=true') {
            A.alert(w.newsLetterConfirmation);
        }
    });














    // alert()
    A.alert = function(t, error) {
        $.colorbox({ html: '<p id="Alert"' + ((error) ? ' class="Erreur"' : '') + '>' + t + '</p>', transition: 'none', opacity: 0.5 });
    };



    w.ADNM = A;

})(jQuery, window);




function RenderClick(e, btnID, validationGroup) {
    if (window.event) // IE
    {
        keynum = e.keyCode;
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which;
    }
    // Si Enter
    if (keynum == 13) {
        // Lancer un postback sur le bouton passé en paramètre
        if (Page_ClientValidate(validationGroup)) {
            __doPostBack(btnID, '');
        }
        return false;
    }
    return true;
}

function MaxLength(obj, mlength) {
    if (obj.getAttribute) {
        if (obj.value.length > mlength) {
            obj.value = obj.value.substring(0, mlength)
        }
    }
}

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-18908884-1']);
_gaq.push(['_trackPageview']);

(function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

