(function() {
    // Private array of chars to use
    var CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');

    Math.uuid = function (len, radix) {
        var chars = CHARS, uuid = [];
        radix = radix || chars.length;

        if (len) {
            // Compact form
            for (var i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
        } else {
            // rfc4122, version 4 form
            var r;

            // rfc4122 requires these characters
            uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
            uuid[14] = '4';

            // Fill in random data.  At i==19 set the high bits of clock sequence as
            // per rfc4122, sec. 4.1.5
            for (var i = 0; i < 36; i++) {
                if (!uuid[i]) {
                    r = 0 | Math.random()*16;
                    uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
                }
            }
        }

        return uuid.join('');
    };

    // A more performant, but slightly bulkier, RFC4122v4 solution.  We boost performance
    // by minimizing calls to random()
    Math.uuidFast = function() {
        var chars = CHARS, uuid = new Array(36), rnd=0, r;
        for (var i = 0; i < 36; i++) {
            if (i==8 || i==13 ||  i==18 || i==23) {
                uuid[i] = '-';
            } else if (i==14) {
                uuid[i] = '4';
            } else {
                if (rnd <= 0x02) rnd = 0x2000000 + (Math.random()*0x1000000)|0;
                r = rnd & 0xf;
                rnd = rnd >> 4;
                uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
            }
        }
        return uuid.join('');
    };

    // A more compact, but less performant, RFC4122v4 solution:
    Math.uuidCompact = function() {
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
            var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
            return v.toString(16);
        }).toUpperCase();
    };
})();


jQuery.fn.limitMaxlength = function(options){

    var settings = jQuery.extend({
        attribute: "maxlength",
        onLimit: function(){},
        onEdit: function(){}
    }, options);

    // Event handler to limit the textarea
    var onEdit = function(){
        var textarea = jQuery(this);
        var maxlength = parseInt(textarea.attr(settings.attribute));

        if(textarea.val().length > maxlength){
            textarea.val(textarea.val().substr(0, maxlength));

            if (textarea.createTextRange) {
                var r = textarea.createTextRange();
                r.collapse(false);
                r.select();
            }
            // Call the onlimit handler within the scope of the textarea
            jQuery.proxy(settings.onLimit, this)();
        }

        // Call the onEdit handler within the scope of the textarea
        jQuery.proxy(settings.onEdit, this)(maxlength - textarea.val().length);
    }

    this.each(onEdit);

    return this.keyup(onEdit)
    .keydown(onEdit)
    .focus(onEdit);
}


$(document).ready(function(){
    if ($.browser.msie){
        $("select")
        .mousedown(function(){
            if($(this).css("position") != "absolute") {
                var width = $(this).width();
                $(this).data("origWidth", $(this).css("width"))
                       .css("width", "auto");
      
                if($(this).width() < width) {
                    $(this).css("width", $(this).data("origWidth"));
                }
                $(this).css("position","absolute");
            }
        })
        
        .blur(function(){
            $(this).css("width", $(this).data("origWidth"));
            $(this).css("position","static");

        })
       
        .change(function(){
            $(this).css("width", $(this).data("origWidth"));
            $(this).css("position","static");

        });

    }

    $(".clickable").attr('style','cursor:pointer;');
    $(".clickable a").click(function(event){
        event.stopPropagation();
        }
    );
    $(".clickable").click(
        function()
        {
            window.location = $(this).attr("url");
        });
    $(".menu-li-1").mouseenter(function(){
        var id = $(this).attr("id");
        id=id.replace('menu-top-','');
        $(".menu-ul-2").hide();
        $(".menu-parent-"+id).show();
    });
    $("#desk-left").mouseover(function(){
        $("#menu-wrapper").stopTime();
    });
    $("#menu-wrapper").mouseleave(function(){
        $("#menu-wrapper").oneTime(1500, "menu-restore", function(){
            $(".menu-ul-2").hide();
            $(".menu-ul-active-2").fadeIn();
        });
    });
    if($.browser.msie){
        $("label").each(function(){
            var forid = this.htmlFor;
            $("img",this).each(function(){
                $(this).click(function(){
                    var e = document.getElementById(forid);
                    switch(e.type){
                        case "radio":
                            e.checked|=1;
                            break;
                        case "checkbox":
                            e.checked=!e.checked;
                            break;
                        case "text": case "password": case "textarea":
                            e.focus();
                            break;
                    }
                });
            });
        });
    }

    var onEditCallback = function(remaining){
        $(this).siblings('.charsRemaining').text("Caractère(s) restant(s) : " + remaining);

        if(remaining > 0){
            $(this).css('background-color', '#FFFFFF');
        }
    }

    var onLimitCallback = function(){
        //$(this).css('background-color', '#FFDDDD');
    }

    $('textarea[maxlength]').limitMaxlength({
        onEdit: onEditCallback,
        onLimit: onLimitCallback
    });

});

