$(function()
{
    $('div.customCheckbox b').live('click',function()
    {
        var input = $('input',$(this).parent().toggleClass('selected'));
        
        input[0].value = input.val()=='0' ? 1 : 0;
        
    });
    $('div.customCheckbox span').live('click',function()
    {
        $(this).prev().click();
    });
    
    $('button.reset').live('click',function()
    {
        if(!confirm('Reset all infos ?'))
        {
            return false;
        }
    });
    
    // COLRO PICKER
    //$('body').delegate('input.colorpicker', 'focus', function()
    //{
    //    var e = $(this);
    //    
    //    e.ColorPicker({
    //        color: e.val(),
    //        onChange: function (hsb, hex, rgb) {
    //            
    //            e.val(hex).css('backgroundColor', '#' + hex);
    //        }
    //    });
    //    
    //});
    
    // CALENDAR 
    $('body').delegate('input.calendar', 'focus', function()
    {
        var e = $(this);
        
        var dateFormat = $.getParamValue('dateFormat',e); 
        
        var date = $.getParamValue('date',e); 
        
        if(!date) date = '-25y';
        
        if(dateFormat)
        {
            dateFormat = (dateFormat[0]+dateFormat[0]+'-'+dateFormat[2]+dateFormat[2]+'-'+dateFormat[4]+dateFormat[4]).toLowerCase();
        }
        else
        {
            dateFormat = 'yy-mm-dd';
        }

        e.datepicker(
        {
            changeYear: true,
            yearRange: '1910:2010',
            defaultDate: date,
            dateFormat: dateFormat,
            closeText: 'X',
            showAnim: 'slideDown'
        });
    });
    
    
    
    
    
});

var form = {};

form.init = function()
{
    form.placeholder();
    
    form.textarea();
    form.autocomplete();
    form.counter();
    //$('form').ketchup();
    form.sortable();
    $.fn.formLabels();
};

form.placeholder = function()
{
    if(!html5.input.placeholder)
    {
        var e = $('input[placeholder]:empty');
        
        e.each(function()
        {
            var input = $(this);
            
            var val = input.attr('placeholder');
            
            input.focusin(function()
            {
                $.clearTxt(this,val);
                
            }).focusout(function()
            {
                $.resetTxt(this,val);
                
            }).val(val);
        });
    
    }
};

form.counter = function()
{
    $('input.counter,textarea.counter').bind('textchange focus', function()
    {
        var counter = $(this).parent().find('.charLeft').show();

        counter.html(parseInt($(this).attr('maxlength'),10) - parseInt($(this).val().length,10));
    });
};

form.textarea = function()
{
    $('textarea',$.e.div_content).elastic();
};

form.autocomplete = function()
{
    $('input.autocomplete').each(function()
    {
        var e = $(this);
        
        if(!e.next('[type=hidden]')[0]) return;
        
        var source = e.next('[type=hidden]').val().split(',');
        
        e.autocomplete({
            source: source
        });
    });
};
form.sortable = function()
{
    $('ul.adminList.sortable').sortable(
    {
        update: function()
        {
            //alert($('ul.adminList.sortable').sortable('serialize'));
            var ajaxClass = $(this).attr('id')+'_updateOrder';
            ajaxClass.ajax($(this).sortable('serialize'));
        },
        handle:'.move',
        axis:'y'
    });
};


