﻿function CenterDivHor(e) {
    var widthparent = $(e.parentNode).innerWidth();
    var thiswidth = $(e).innerWidth();
    if (thiswidth < widthparent) {
        margeL = (widthparent - thiswidth) / 2;
        margeR = (widthparent - thiswidth - margeL);
        $(e).css('marginLeft', margeL);
        $(e).css('marginRight', margeR);
    }
}
function CenterDivVert(e) {
    var heightparent = $(e.parentNode).innerHeight();
    var thisheight = $(e).innerHeight();
    if (thisheight < heightparent) {
        margeT = (heightparent - thisheight) / 2;
        margeB = (heightparent - thisheight - margeL);
        $(e).css('marginTop', margeT);
        $(e).css('marginBottom', margeB);
    }
}
function setRollOver(node) {
    var e = $(node);
    var thumb = node.getAttribute("thumbSrc");
    var src = node.getAttribute("src");
    var mediaWidth = node.getAttribute("mediaWidth");
    var mediaHeight = node.getAttribute("mediaHeight");
    var thumbWidth = node.getAttribute("thumbWidth");
    var thumbHeight = node.getAttribute("thumbHeight");
    var imgWidth = node.getAttribute("imgWidth");
    var imgHeight = node.getAttribute("imgHeight");
    var idxImgOut=node.getAttribute("idxImgOut");
    var idxImgOver=node.getAttribute("idxImgOver");
    var rollover = node.getAttribute("rollover");
    var alt = node.getAttribute("alt");
    var divThumb = node;
    
    var divBt = document.createElement("div");
    $(divBt).css("backgroundImage", "url(" + src + ")");
    $(divBt).css("backgroundRepeat", "no-repeat");
    if (isNum(imgHeight)) {
        $(divBt).height(parseInt(imgHeight));
    } else {
        imgHeight=parseInt($(node).height());
        node.setAttribute("imgHeight", imgHeight);
    }
    if (isNum(imgWidth)) {
        $(divBt).width(parseInt(imgWidth));
    } else {
        imgWidth = parseInt($(node).width());
        node.setAttribute("imgWidth", parseInt($(node).width()));
    }    
    if (!IsString(idxImgOut)) node.setAttribute("idxImgOut", "0");
    if (!IsString(idxImgOver)) node.setAttribute("idxImgOver", "1");
    $(divBt).addClass("bt");
    $(divBt).css("backgroundPosition", "0 " + (parseInt(node.getAttribute("idxImgOut")) * 100) + "%");
    $(divBt).width(imgWidth);
    $(divBt).height(imgHeight);
    if (!IsString(rollover) || rollover == 'true') {
        divBt.onmouseout = function() {
            $(this).css("backgroundPosition", "0 " + parseInt(node.getAttribute("idxImgOut") * 100) + "%");
        };
        divBt.onmouseover = function() {
            $(this).css("backgroundPosition", "0 " + parseInt(node.getAttribute("idxImgOver") * 100) + "%");
            $(this).css("cursor", "hand");
        };
    }
    $(divThumb).append(divBt);
    if (IsString(thumb)) $(divBt).css("position", "absolute");
    CenterDivHor(divBt);
    CenterDivVert(divBt);
    
    if (IsString(thumb)) {
        var divThumb = document.createElement("img");
        divThumb.setAttribute("src", thumb);  //css("backgroundImage", "url(" + thumb + ")");
        divThumb.setAttribute("alt", alt);
        $(divThumb).addClass("captionImage");
        $(divThumb).css("backgroundRepeat", "no-repeat");
        var larg = thumbWidth;
        if (!isNum(larg)) larg = mediaWidth;
        if (!isNum(larg)) larg = $(node).innerWidth();
        var haut = thumbHeight;
        if (!isNum(haut)) haut = mediaHeight;
        if (!isNum(haut)) haut = $(node).innerHeight();
        $(divThumb).width(larg);
        $(divThumb).height(haut);
        e.append(divThumb);
        CenterDivHor(divThumb);
        CenterDivVert(divThumb);

        /*var fake = document.createElement("div");
        $(fake).width(1);
        $(fake).height(1);
        $(divThumb).append(fake);*/
    }    
}
function InitFeatures() {
    $.fn.media.defaults.flvPlayer = '/flash/player.swf';
    $.fn.media.defaults.mp3Player = '/flash/player.swf';
    jQuery(function($) {
        $.datepicker.regional['en'] = {
            closeText: 'Done',
            prevText: 'Prev',
            nextText: 'Next',
            currentText: 'Today',
            monthNames: ['January', 'February', 'March', 'April', 'May', 'June','July', 'August', 'September', 'October', 'November', 'December'],
            monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun','Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
            dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
            dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
            dayNamesMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
            weekHeader: 'Wk',
            dateFormat: 'mm/dd/yy',
            firstDay: 1,
            isRTL: false,
            showMonthAfterYear: false,
            yearSuffix: ''
        };
    });
    jQuery(function($) {
        $.datepicker.regional['fr'] = {
            closeText: 'Fermer',
            prevText: '&#x3c;Préc',
            nextText: 'Suiv&#x3e;',
            currentText: 'Courant',
            monthNames: ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin','Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'],
            monthNamesShort: ['Jan', 'Fév', 'Mar', 'Avr', 'Mai', 'Jun','Jul', 'Aoû', 'Sep', 'Oct', 'Nov', 'Déc'],
            dayNames: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'],
            dayNamesShort: ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam'],
            dayNamesMin: ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa'],
            weekHeader: 'Sm',
            dateFormat: 'dd/mm/yy',
            firstDay: 1,
            isRTL: false,
            showMonthAfterYear: false,
            yearSuffix: ''
        };
    });
    $.datepicker.setDefaults($.datepicker.regional[GL_LANGUE]);
    $('div.item').each(function(no) {
        this.onmouseover = function() {
            this.className += " over ";
        },
				    this.onmouseout = function() {
				        this.className = this.className.replace(/\ over\ /gi, "");
				    }
    });

    $('.centreH').each(function(no) {
        CenterDivHor(this);
    });
    $('.centreV').each(function(no) {
        CenterDivVert(this);
    });    
    $('div.divrollover').each(function(no) {
        setRollOver(this);
    });
    $('.mediaplayer').each(function(no) {
        this.GetSize = function(e) {
            if (null == e) e = $(this);
            var larg = this.getAttribute("mediaWidth");
            if (!isNum(larg)) larg = e.innerWidth();
            var haut = this.getAttribute("mediaHeight");
            if (!isNum(haut)) haut = e.innerHeight();
            return { e: e, haut: parseInt(haut), larg: parseInt(larg) };
        }
        this.AddDiv = function(e) {
            if (null == e) e = $(this);
            var div = document.createElement("div");
            var id = "id" + new Date().getTime();
            div.setAttribute("id", id);
            e.append(div);
            return { e: e, div: div, id: id };
        }
        var src = this.getAttribute("mediaSrc");
        var alt = this.getAttribute("alt");
        if (src.indexOf(".jpg") > 0 || src.indexOf(".jpeg") > 0 || src.indexOf(".png") > 0 || src.indexOf(".gif") > 0) {
            var ret = this.GetSize();
            var img = document.createElement("img");
            img.setAttribute("src", src);
            img.setAttribute("alt", alt);
            $(img).addClass("captionImage");

            //$(ret.e).css("backgroundImage", "url(" + src + ")");
            //$(ret.e).css("backgroundRepeat", "no-repeat");
            //$(img).width(ret.larg);
            //$(ret.e).height(ret.haut);
            $(ret.e).append(img);
        } else {
            if (src.indexOf(".swf") > 0) {
                var ret = this.GetSize();
                var src = this.getAttribute("mediaSrc");
                $(this).flash({ src: src, width: ret.larg, height: ret.haut }, { version: 8 });                
            } else {
                setRollOver(this);
                this.onclick = function() {
                    this.onclick = null;
                    var ret = this.GetSize();
                    $(this).empty();
                    var ret2 = this.AddDiv(ret.e);
                    var src = this.getAttribute("mediaSrc");
                    $(ret2.div).media({
                        width: ret.larg,
                        height: ret.haut,
                        autoplay: true,
                        src: src,
                        caption: false,
                        flashvars: { autoplay: 'true' }
                    });
                }
            }
        }
    });
    $('.DFcalendar').each(function(no) {
        $(this).datepicker({
            showOn: 'button',
            buttonImage: '/images/btCalendar.gif',
            buttonImageOnly: true
        });
    });
    $('#EditerPage').each(function(no) {
        this.onclick = function() {
            var q1 = query.replace(/Methode=([^&]*)/, "Methode=FDetail");
            q1 = q1.replace("/client/", "/admin/");
            window.open(q1, 'Admin', '');
        }
    });
    $('img.rollover').each(function(no) {
        this.onmouseover = function() {
            this.srcorg = this.src.replace(/_(out|over)\./gi, "_.");
            this.src = this.srcorg.replace(/_\./gi, "_over.");
        },
				    this.onmouseout = function() {
				        this.src = this.srcorg.replace(/_\./gi, "_out.");
				    }
    });
    $(".dialog-modal").each(function() {
        var width=$(this).innerWidth();
        var height=$(this).innerHeight();
        $(this).dialog({
            width:width,
            height:height,
            modal:true,
            resizable:false,
            show: 'scale'   
        });
    });
    $(".captionImage").each(function() {
        if (this.alt != "")
        $(this).captify({
            // all of these options are... optional
            // ---
            // speed of the mouseover effect
            speedOver: 'fast',
            // speed of the mouseout effect
            speedOut: 'normal',
            // how long to delay the hiding of the caption after mouseout (ms)
            hideDelay: 50,
            // 'fade', 'slide', 'always-on'
            animation: 'always-on',
            // text/html to be placed at the beginning of every caption
            prefix: '',
            // opacity of the caption on mouse over
            opacity: '0.3',
            // the name of the CSS class to apply to the caption box
            className: 'caption-bottom',
            // position of the caption (top or bottom)
            position: 'bottom',
            // caption span % of the image
            spanWidth: '95%'        
        });
    });
    var EditerPage = $("#EditerPage");
    if (null != EditerPage) {
        var oRegEx = new RegExp("Composant=([^&]*)");
        var tResult = oRegEx.exec(query);
        var bok = false;
        if (tResult != null) {
            switch (tResult[1].toLowerCase()) {
                case "sitemap":
                case "clienthome":
                    break;
                default:
                    bok = true;
                    break;
            }
        }
        if (!bok) EditerPage.css({ "display": "none" });
    }
    try {
        InitFeatures_Ex();
    } catch (e) { ; }
}
$(document).ready(function() {
    InitFeatures();
});

