/**
 * Tifany utility function
 *
 * @author Nicolas Léon <nicolas.leon@omnitic.com>
 *
 * @copyright 2009, Tifany
 */
var seq_child = 1;
var i18n = {
    fr: {
        not_similar_password: "Les mots de passes ne sont pas identiques",
        demo: "c'est une demo"
    },
    uk: {
        not_similar_password: "The passwords are diffrent",
        demo: "This is a demo"
    },
    es: {
        not_similar_password: "mots de passes non similaires",
        demo: "demo"
    }
};

$(document).ready(function(){
    jQuery('#nav-accordion').accordion({
        active: false,
        autoheight: true,
        header: '.accordion-title',
        navigation: true,
        collapsible: true,
        //event: 'mouseover',
        fillSpace: false
    //animated: 'easeslide'
    });
    // Manage form info hints
    $('.required').focus(function(){
        $('div.alert').fadeOut(300);
        $(this).val('');
    });
    $('.required').blur(function(){
        $(this).val( ($(this).val() == '')?$(this).attr('field'): $(this).val() );
    });
    
});


function add_child_date()
{
    $('.alert').html('');
    $('.alert').fadeOut(500);
    //check for a valid day and month
    is_ok_jour = ($('#jour-e').val() != '')?true:false;
    is_ok_mois = ($('#mois-e').val() != '')?true:false;
    if(is_ok_jour && is_ok_mois)
    {
        // Process
        // Ajax alert on succes go to another page;
        parent_data = 'parent_child_data_' + seq_child;
        childs_data = '<p id="' + parent_data + '" style="display:none;"><input id="child_jj' + seq_child + '" name="jj[]" value="'+ $('#jour-e').val() +'" type="hidden"><input id="child_jj' + seq_child + '" name="mm[]" value="'+ $('#mois-e').val() + '" type="hidden"></p>';
        parent_display = 'parent_child_display_' + seq_child;
        childs_display = '<p id="' + parent_display + '" class="parent-class spacer-top-xs">Date de naissance (mm/jj)<input id="child_jj' + seq_child + '" name="jj[]" value="'+ $('#jour-e').val() +'" size="2" class="date" disabled>/<input id="child_jj' + seq_child + '" name="mm[]" value="'+ $('#mois-e').val() + '" size="2" class="date" disabled><img name="'+ seq_child + '" src="/public/images/pictos/user_delete.png" onclick="remove_child_date(this.name);"/></p>';
        seq_child++;
        $('#childs').append(childs_data);
        $('#child-list').append(childs_display);
        parent_data = '#' + parent_data;
        parent_display = '#' + parent_display;
        $(parent_data).animate({
            'opacity': 0.5
        }).animate({
            'opacity': 1
        });
        return true;
    }
    else{
        $('.alert').html('Veuillez indiquer jour et mois de naissance.');
        $('.alert').addClass('error');
        $('.alert').fadeIn('slow');
        return false;

    }
}

function remove_child_date(name)
{
    parent_id = name; //$(item).readAttributes('parent_id');
    //    console.log(parent_id);
    parent_data = '#parent_child_data_' + parent_id;
    parent_display = '#parent_child_display_' + parent_id;
    $(parent_display).fadeOut(300);
    $(parent_data).remove();
}

function fal()
{
    alert('fal');
}

function add_subscriber()
{
    $('.alert').html('');
    $('.alert').fadeOut(500);

    missing = new Array();
    $('.required').each(function(){
        if($(this).val() == $(this).attr('field')) missing.push($(this).attr('field'));
    });
    total_missing = missing.length;
    if(total_missing == 0){
        // Same password?
        if($('#password').val() != $('#confirm').val())
        {
            show_error(__('uk', 'not_similar_password'));
            return false;
        }
        $('#form-account').submit();

    }
    else
    {
        msg = '';
        for(field in missing)
        {
            msg += missing[field] + ', ';
        }
        msg = msg.substring(0, msg.length - 2);
        msg = 'Veuillez compléter les champs suivants:<br/>' + msg;
        show_error(msg);
    }
    return false;

}


function show_error(msg)
{
    $('.alert').html(msg);
    $('.alert').addClass('error');
    $('.alert').addClass('spacer-top-s');
    $('.alert').fadeIn(500);
}

function __(lang, key)
{
    return i18n[lang][key];
}

//--------------------------------------------------------------------/
function ad_subscriber()
{
    $('.alert').html('');
    $('.alert').fadeOut(500);

    missing = new Array();
    $('.required').each(function(){
        //if($(this).val() == $(this).attr('field')) missing.push($(this).attr('field'));
        });
    total_missing = 0;
    if(total_missing == 0)
    {
        // Same password?
        if($('#password').val() != $('#confirm').val())
        {
        //show_error('Les mot de passe ne sont pas identiques.');
        //return false;
        }
        // Ajax alert on succes go to another page;
        //        $('#assets_page').load(frog_root + '/admin/?/plugin/assets/latest/8');

        url = '/admin/?/plugin/tifany/add_child';
        //        url = '/frog/plugins/tifany/add_subscriber.php';
        data = {
            nom: $('#nom').val(),
            prenom: $('#prenom').val(),
            jour: $('#jour').val(),
            mois: $('#mois').val(),
            email: $('#email').val(),
            password: $('#pasword').val()
        }
        $.ajax({
            type: "POST",
            url: url,
            data: data,
            success: function(msg){
                $('#bp-form').fadeOut(500, function(){
                    $('#bp-form').html(msg);
                    $('#bp-form').fadeIn(500);
                });
            },
            error: function(msg){
                alert(msg);
            }
        });
    }
    else
    {
        msg = '';
        for(field in missing)
        {
            msg += missing[field] + ', ';
        }
        msg = msg.substring(0, msg.length - 2);
        msg = 'Veuillez compléter les champs suivants:<br/>' + msg;
        show_error(msg);
    }
    return false;
}
