/*
     $Revision$
     $Id$

     File:          mobile.js
     Description:   clientside functions for pda/*
*/

window.fcltmobile = 1;

$(document).bind("mobileinit", function(){
 // Enhancement to use history.replaceState in supported browsers,
 // to convert the hash-based Ajax URL into the full document path.
 // Note that we recommend disabling this feature if Ajax is disabled
 // or if extensive use of external links are used.
 //$.mobile.pushStateEnabled = false;
 if (window.FcltMgr)
    FcltMgr.fixActiveelementError();
});
$(document).bind("pageinit", function(){
    $("i.dateklikker,i.timeklikker").each(function() {
        $(this).appendTo($(this).prev());
    });
});

function McltCallbackAndThen(afterAction)
{
    return function (json, textStatus)
    {
        if (json.message) alert(json.message); // Normaal door FcltMgr.closeDetail
        if (json.warning) alert(json.warning);
        json.message = null;
        json.warning = null;
        if (json.success)
        {
            if (afterAction) afterAction(json);
        }
    }
};

function McltCallbackAndThenAlways(afterAction)
{
    return function (json, textStatus)
    {
        if (json.message) alert(json.message);
        if (json.warning) alert(json.warning);
        json.message = null;
        json.warning = null;
        if (afterAction) afterAction(json);
    }
};

function jqToast (msg)
{
	$("<div class='ui-loader ui-overlay-shadow ui-body-e ui-corner-all'><h3>"+msg+"</h3></div>")
    	.css({ display: "block",
    		opacity: 0.90,
    		position: "absolute",
    		padding: "7px",
    		"text-align": "center",
    		width: "270px",
    		left: (($(window).width() - 284)/2)+"px",
    		top: ($(window).scrollTop() + $(window).height()/2)+"px" })
    	.appendTo( $.mobile.pageContainer ).delay( 2500 )
    	.fadeOut( 400, function(){
		    $(this).remove();
    	});
}

var McltCallbackSaved = McltCallbackAndThen(function (json)
    {
      if (json.success)
        jqToast(L("lcl_mobile_data_saved"));
    });

var McltCallbackRefresh = McltCallbackAndThen(function (json)
    {
      window.location.href = window.location.href;
    });

var McltCallbackClose = McltCallbackAndThen(function (json)
    {
        window.history.back(1);
    });

var McltCallbackHome = McltCallbackAndThen(function (json)
    {
        // window.history.back(1); doet geen refresh als je bijvoorbeeld net een reservering hebt verwijderd
        window.location.href = "./facilitor.asp"
    });

function onBijlagenMobile(formurl, // protected
               saveUrl, // protected
               multi,
               objButton)
{
    // Vorige eventueel opruimen
    $("#bijlagepopup").trigger( "destroy" ).remove(); // eventuele vorige opruimen

    var html = '<iframe class="mfcltmodal"'
                +' frameborder="0"'
                +' id="fcltmodal" name="fcltmodal" scrolling="no" style="padding: 0px;"'
                +' src="../shared/empty.html">'
             +'</iframe>';

    html = '<div id="bijlagepopup" data-role="popup" class="ui-content" data-theme="c">'
         +    '<a href="#" data-rel="back" data-role="button" data-theme="c" data-icon="delete" data-iconpos="notext" class="ui-btn-right">Close</a>'
         + html
         + '</div>';
    var $html = $(html);
    $( "body" ).append ( $html );
    $html.trigger( "create" );

    $("#bijlagepopup").on("popupafterclose",
        function(event, ui)
        {
                if (window.return_data) // gezet door BijlagenForm.asp
                {
                    // Overkill alert(window.return_data.fileName + " is toegevoegd");
                    if (window.return_data.fileName)
                    {
                        if ($(objButton).is("[src]"))
                        {
                            afterUploadForm(window.return_data.fileName);
                        }
                        else
                        {
                            $(objButton).val(window.return_data.fileName)
                                        .show()
                                        .attr("onclick", "")
                                        .attr("readonly", "1")
                                        .parent().next().hide(); // de 'Add' knop
                        }
                    }
                }
                // Als we dit weglaten krijgen we obscure clientside jQuery errors ('Array not defined etc')
                $("iframe.mfcltmodal").attr("src", "../shared/empty.html");
        });

    $("#bijlagepopup").popup().popup( "open" );

    $html.find("iframe").attr("src", formurl);

}
