// Apprise 1.5 by Daniel Raftery
// http://thrivingkings.com/apprise
//
// Button text added by Adam Bezulski
//

function apprise(string, args, callback)
{
    var default_args =
    {
        'confirm'		:	false, 		// Ok and Cancel buttons
        'verify'		:	false,		// Yes and No buttons
        'loading'   	:	false,		// no buttons
        'nobutton'   	:	false,		// no buttons
        'input'			:	false, 		// Text input (can be true or string for default text)
        'animate'		:	false,		// Groovy animation (can true or number, default is 400)
        'textOk'		:	'Ok',		// Ok button default text
        'textCancel'	:	'Cancel',	// Cancel button default text
        'textYes'		:	'Da',		// Yes button default text
        'textNo'		:	'Ne'		// No button default text
    }

    if(args) 
    {
        for(var index in default_args) 
        { if(typeof args[index] == "undefined") args[index] = default_args[index]; } 
    }

    var aHeight = $(document).height();
    var aWidth = $(document).width();
    if ($('#aOverlay').length == 0) {
        $('body').append('<div class="appriseOverlay" id="aOverlay"></div>');
        $('.appriseOverlay').css('height', aHeight).css('width', aWidth).fadeIn(100);
    }
    $('body').append('<div class="appriseOuter"></div>');
    $('.appriseOuter').append('<div class="appriseInner"></div>');
    $('.appriseInner').append(string);
    $('.appriseOuter').css("left", ( $(window).width() - $('.appriseOuter').width() ) / 2+$(window).scrollLeft() + "px");

    if(args)
    {
        if(args['animate'])
        { 
            var aniSpeed = args['animate'];
            if(isNaN(aniSpeed)) { aniSpeed = 400; }
            $('.appriseOuter').css('top', '-200px').show().animate({top:"100px"}, aniSpeed);
        }
        else
        { $('.appriseOuter').css('top', '100px').fadeIn(200); }
    }
    else
    { $('.appriseOuter').css('top', '100px').fadeIn(200); }

    if(args)
    {
        if(args['input'])
        {
            if(typeof(args['input'])=='string')
            {
                $('.appriseInner').append('<div class="aInput"><input type="text" class="aTextbox" t="aTextbox" name="'+args['input']+'" /></div>');
            }
            else
            {
                $('.appriseInner').append('<div class="aInput"><input type="text" class="aTextbox" t="aTextbox" /></div>');
            }
            $('.aTextbox').focus();
        }
    }

    $('.appriseInner').append('<div class="aButtons"></div>');
    if(args)
    {
        if(args['confirm'] || args['input'])
        { 
            $('.aButtons').append('<button name="ok">'+args['textOk']+'</button>');
            $('.aButtons').append('<button name="cancel">'+args['textCancel']+'</button>'); 
        }
        else if(args['verify'])
        {
            $('.aButtons').append('<button name="ok">'+args['textYes']+'</button>');
            $('.aButtons').append('<button name="cancel">'+args['textNo']+'</button>');
        }
        else if(args['loading'])
        {
            $('.aButtons').append('<img src="'+baseUrl+'/gfx/ajax-loader.gif" alt="" />');
        }
        else if(args['nobutton'])
        {
        }
        else
        { 
            $('.aButtons').append('<button name="ok">'+args['textOk']+'</button>');
        }
    }
    else
    { $('.aButtons').append('<button name="ok">Ok</button>'); }

    $(document).keydown(function(e) 
            {
            if($('.appriseOverlay').is(':visible'))
            {
            if(e.keyCode == 13) 
            { $('.aButtons > button[name="ok"]').click(); }
            if(e.keyCode == 27) 
            { $('.aButtons > button[name="cancel"]').click(); }
            }
            });

    var aText = $('.aTextbox').val();
    if(!aText) { aText = false; }
    $('.aTextbox').keyup(function()
            { aText = $(this).val(); });

    if (args && args['nobutton']) {
        setTimeout("removeApprise(1)", 2000);
    }
    else {
        $('.aButtons > button').click(function() {
            removeApprise(1, args, callback, $(this), aText);
        });
    }
}

function removeApprise(removeLayer, args, callback, $this, aText) {
    if (removeLayer) {
        $('.appriseOverlay').remove();
    }
    $('.appriseOuter').remove();
    if(callback) {
        var wButton = $this.attr("name");
        if(wButton=='ok') { 
            if(args) {
                if(args['input']) {
                    callback(aText);
                }
                else {
                    callback(true);
                }
            }
            else {
                callback(true);
            }
        }
        else if(wButton=='cancel') {
            callback(false);
        }
    }
}

