$(document).ready(function() {
    $('button.button, a.button').set_red_button();
    $('button.button2, a.button2').set_grey_button();

    var elem_logo = $('#logo');
    $('a', elem_logo).hide();
    elem_logo.click(function() {
        self.location = $('a', $(this)).attr('href');
    });

    var dialog_buttons;

    dialog_buttons = $('<div></div>', {
        id: "dialog_general_message"
    }).appendTo($('body')).dialog({
        autoOpen: false,
        modal: true,
        resizable: false,
        buttons: {
            'Continuer': function() {
                $(this).dialog('close');
            }
        }
    }).siblings('.ui-dialog-buttonpane').find('button');
    dialog_buttons.children('span').wrapInner('<span>');
    dialog_buttons.set_red_button();

    dialog_buttons = $('<div></div>', {
        id: "dialog_general_confirm"
    }).appendTo($('body')).dialog({
        autoOpen: false,
        modal: true,
        resizable: false,
        buttons: {
            'Confirmer': function() {
                $(this).dialog('close');
            },
            'Annuler': function() {
                $(this).dialog('close');
            }
        }
    }).siblings('.ui-dialog-buttonpane').find('button');
    dialog_buttons.children('span').wrapInner('<span>');
    dialog_buttons.set_red_button();

    $.fn.label_display = function() {
        $(this).addClass('label_display').each(function() {
            $(this).bind('click focus', function() {
                if ($.trim($(this).val()) == $(this).attr('label_display')) {
                    /*
                    if ($(this).data('has_password')) {
                        $(this).attr('type', 'password');
                    }
                    */
                    $(this).val('').removeClass('label_display_on');
                }
            }).bind('blur change', function() {
                if ($.trim($(this).val()) == '') {
                    /*
                    if ($(this).attr('type') == 'password') {
                        $(this).attr('type', 'text').data('is_password', true);
                    }
                    */
                    $(this).addClass('label_display_on').val($(this).attr('label_display'));
                }
            }).trigger('blur');
            $(this).parents('form').submit(function(event) {
                $(this).find('.label_display').each(function() {
                    if ($.trim($(this).val()) == $(this).attr('label_display')) {
                        $(this).val('');
                    }
                });
            });
        });
    }
    $('[label_display]').label_display();
    
    $('[maxlength]').show_max_length();


    var dialog_contact = $('<div></div>', {
        id: "dialog-contact"
    }).appendTo($('body')).dialog({
        width: 750,
        height: 530,
        autoOpen: false,
        modal: true,
        resizable: false
    });
    $('.contact_link').click(function(event) {
        event.preventDefault();
        $.ajax({
            url: site_domain + 'elements/contact_form',
            type: "GET",
            dataType: "html",
            cache: false,
            success: function(data){
                dialog_contact.html(data).dialog('open');
            }
        });
    });

    $('#global_navigation form button').button({
        label: '&nbsp;'
    }).css({
        border: 'none',
        width: 14,
        height: 22,
        background: "url(" + site_domain + "images/pictos/boutons/bouton_login.png) 5px center no-repeat transparent"
    });

    $('[error_tip][error_tip!=""]').show_error_tips();
});

$.fn.set_red_button = function() {
    $(this).button().css({
        background: '#992c56 url(' + site_domain + 'images/fonds/bouton2_on.png) repeat-x scroll 50% 50%',
        height: 29,
        border: '1px solid #992c56',
        fontFamily: 'Arial,Verdana,sans-serif',
        color: '#ffffff',
        fontSize: 15,
        marginTop: '4px',
        marginBottom: '4px',
        'border-radius': 6,
        '-moz-border-radius': 6,
        '-webkit-border-radius': 6,
        '-khtml-border-radius': 6,
        'box-shadow': '0 0 5px #404040',
        '-webkit-box-shadow': '0 0 5px #404040',
        '-moz-box-shadow': '0 0 5px #404040',
        '-khtml-box-shadow': '0 0 5px #404040',
        padding: 0,
        textAlign: 'left',

        cursor: 'pointer',
        display: 'inline-block',
        overflow: 'visible',
        position: 'relative',
        textDecoration: 'none'
    }).hover(function() {
        $(this).css({
            backgroundImage: 'url(' + site_domain + 'images/fonds/bouton2.png)',
            border: '1px solid #992c56'
        });
    }, function() {
        $(this).css({
            backgroundImage: 'url(' + site_domain + 'images/fonds/bouton2_on.png)',
            border: '1px solid #992c56'
        });
    }).find('span').css({
        padding: '0 10px',
        lineHeight: '29px',
        color: '#e8ae31'
    }).find('span').css({
        padding: 0,
        color: '#ffffff'
    });
}

$.fn.set_grey_button = function() {
    $(this).button().css({
        background: '#adacab url(' + site_domain + 'images/fonds/bouton3.gif) repeat-x scroll 50% 50%',
        /*width: 160,*/
        height: 29,
        border: '1px solid #adacab',
        fontFamily: 'Arial,Verdana,sans-serif',
        color: '#ffffff',
        fontSize: 15,
        marginTop: '4px',
        marginBottom: '4px',
        'border-radius': 6,
        '-moz-border-radius': 6,
        '-webkit-border-radius': 6,
        '-khtml-border-radius': 6,
        'box-shadow': '0 0 5px #404040',
        '-webkit-box-shadow': '0 0 5px #404040',
        '-moz-box-shadow': '0 0 5px #404040',
        '-khtml-box-shadow': '0 0 5px #404040',
        padding: 0,
        textAlign: 'left',

        cursor: 'pointer',
        display: 'inline-block',
        overflow: 'visible',
        position: 'relative',
        textDecoration: 'none'
    }).hover(function() {
        $(this).css({
            border: '1px solid #909090'
        });
    }, function() {
        $(this).css({
            border: '1px solid #adacab'
        });
    }).find('span').css({
        padding: '0 10px',
        lineHeight: '29px',
        color: '#697a86'
    }).find('span').css({
        padding: 0,
        color: '#992c56'
    });
}

$.fn.show_error_tips = function() {
    var msg = new Array();
    $(this).addClass('ui-state-error').each(function() {
        $(this).attr('title', $(this).attr('error_tip'));
        if ($(this).is('label')) {
            msg.push($(this).text());
        } else if ($(this).attr('label_display') != undefined) {
            msg.push($(this).attr('label_display'));
        } else {
            msg.push($('label[for=' + $(this).attr('id') + ']').text());
        }
    });
    if (msg.length) {
        $('#dialog_general_message').html('<h2>Merci de vérifier<br/>le' + ((msg.length > 1) ? 's' : '') + ' champ' + ((msg.length > 1) ? 's' : '') + ' suivant' + ((msg.length > 1) ? 's' : '') + '&nbsp;:</h2>' + msg.join('<br/>')).dialog('open').css('z-index', 3999);
    }
}
$.fn.update_error_tips = function(errors) {
    $('.error_tip', $(this)).remove();
    $('.ui-state-error').removeClass('ui-state-error');
    for (var field_id in errors) {
        $('[name="' + field_id + '"]', $(this)).attr('error_tip', errors[field_id]).show_error_tips();
    }
}

$.fn.show_max_length = function() {
    $(this).each(function() {
        var elem = $(this);
        var maxlength = parseInt($(this).attr('maxlength'));
        if (maxlength > 0 && maxlength < 500000) {
            $(this).data('remaining_elem', $('<span/>', {
                "id": elem.attr('id') + '_remaining_chars'
            }).insertAfter(elem));
            $(this).keyup(function() {
                var value_length = $(this).val().length;
                $(this).data('remaining_elem').text(maxlength - value_length);
            }).keypress(function(event) {
                var kc = event.keyCode;
                if (kc != 8 && kc != 46 && (kc < 33 || kc > 40)) {
                    if ($(this).val().length >= maxlength) {
                        event.preventDefault();
                    }
                }
            });
            $(this).trigger('keyup');
        }
    });
}

$.fn.reload_captcha = function() {
    var src = $(this).attr('src') + '?';
    src = src.substring(0, src.indexOf('?')) + '?' + Math.random();
    $(this).attr('src', src);
}

$.fn.update_datatable = function() {
    $(this).each(function() {
        var first_th = $('thead th:first-child', $(this));
        first_th.css('font-weight', 'bold').css('font-size', 13)

        var last_th = $('thead th:last-child', $(this));
        var padding = parseInt(last_th.css('padding-right'), 10);
        if (last_th.hasClass('sorting') || last_th.hasClass('sorting_desc') || last_th.hasClass('sorting_asc')) {
              last_th.css('background-position', (last_th.width() + padding + 2 - 16) + 'px 50%').css('padding-right', 16);
        } else {
            last_th.css('background-image', 'none').css('padding-right', 0)
        }
    });
}
