$(document).ready(function() {
    $('.warning_box, .error_box, .valid_box, .loading_box').each(function(){$(this).animate({'opacity': 100}, 4000, function(){$(this).animate({'opacity': 0}, 1000, function(){$(this).remove()})})});
    $(document).ajaxSend(function(){
        if ($('.loading_box').length == 0)
            showMessage('Thông tin đang xử lý. Vui lòng đợi ...','loading');
    }).ajaxStop(function() {
        $('.loading_box').slideUp(function(){$(this).remove()});
    });
    
    //Snow Fall
    
    //Start the snow default options you can also make it snow in certain elements, etc.
    /*options = {
        flakeCount : 500,        // number
        flakeColor : '#ffffff', // string
        flakeIndex: 999999,     // number
        minSize : 1,            // number
        maxSize : 3,            // number
        minSpeed : 2,           // number
        maxSpeed : 3,           // number
        round : true,          // bool
        shadow : false,         // bool
        collection : '.corner-left-top'          // string
    };

    $(document).snowfall(options);*/
});

function showMessage(message, type)
{
   if (type != "loading")
       $('.loading_box').slideUp(function(){$(this).remove()});
   
   $("div.messages_container").prepend($("<div>").addClass(type + "_box").html(message));
   $('.warning_box, .error_box, .valid_box').each(function(){$(this).animate({'opacity': 100}, 4000, function(){$(this).animate({'opacity': 0}, 1000, function(){$(this).remove()})})});
}

function parseAjaxJson(data)
{
    if (data.type == 'valid')
    {
        showMessage (data.message, data.type);
        if (data.url && data.url != '')
            location.href = base_url + data.url;
    }
    else
        showMessage (data.message, data.type);
    if (data.type != 'valid') return false;

    return true;
}
