﻿function GoToLinkEx(anchor, name, width, height) {
    if (anchor.href == null) {
        alert("aucun lien de définit");
        return;
    }

    window.open(anchor.href, name, "menubar=no, status=no, scrollbars=yes, menubar=no, width=" + width + ", height=" + height);
    return false;
}
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

function SetDepartement( dep, add )  {
    var OmerDep = $.cookie('OmerDep');
    // alert(OmerDep);
    if (add) {
        if (OmerDep == null) {
            $.cookie('OmerDep', dep, { expires: 7 });
            return;
        }
        if (OmerDep.indexOf(dep,0) == -1) {
            OmerDep += "," + dep;
            $.cookie('OmerDep', OmerDep, { expires: 7 });
            // alert("Save 1 :" + OmerDep);
        }
    }
    else {
        if (OmerDep == null) return;
        OmerDep = OmerDep.replace(dep,"");
        OmerDep = OmerDep.replace(",,", ",");
        if (OmerDep[0] == ",") 
            OmerDep = OmerDep.substring(0);
        if( OmerDep[OmerDep.length - 1] == ",")
            OmerDep = OmerDep.substring(0, OmerDep.Length - 2);
        // alert("Save 2 : " + OmerDep);
        $.cookie('OmerDep', OmerDep, { expires: 7 });
    }
}

function ShowDepartement() {
    $('.Communes').hide();
    $('.Departement').removeClass("open").children('.BlocDepartement').removeClass("open");
    var OmerDep = $.cookie('OmerDep');
    if (OmerDep == null) return;
    var dep = OmerDep.split(",");
    for (var i = 0; i < dep.length; i++) {
        $('.Departement[rel="' + dep[i] + '"]').children('.BlocDepartement').addClass("open").children('UL.Communes').show();
        $('.Departement[rel="' + dep[i] + '"]').addClass("open").children('UL.Communes').show()
    }
}

function InitDepartement() {
        ShowDepartement();
        $('.Departement').hover(function () { $(this).addClass("hover"); }, function () { $(this).removeClass("hover"); });
        $('DIV.BlocDepartement')
                    .click(
                        function () {
                            var parent = $(this).parent();
                            var dep = parent.attr("rel");
                            if (parent.hasClass("open")) {
                                SetDepartement(dep, false);
                                parent.removeClass("open").children('UL.Communes').hide(); // .fadeOut('slow');
                                $(this).removeClass("open")
                            }
                            else {
                                SetDepartement(dep, true);
                                parent.addClass("open").children('UL.Communes').show(); // fadeIn('slow');
                                $(this).addClass("open")
                            }
                        }
                    );
}
