﻿/*
* Outside the document ready for speed
* this script is loaded just before </form> so most of the dom is there
*/


/*
* Global document ready
*/

$(document).ready(function() {

    // implement facebox on all links using iframe approach
    $('a[rel*=facebox]').click(function(e) {
        $.facebox({ 'iframe': $(this).attr('href') });

        return false;
    });

});


// closes active modal
function closeModalSelf(doParentRefresh) {
    // ensure we are in a modal
    if (parent.$) {
        parent.$('#facebox .close').click(); // we are using facebox
        if (doParentRefresh === true) {
            parent.window.location.reload(true);
        }
    }
}

function callPageMethod(methodUrl, onSuccess) {
    var args = '';
    var l = arguments.length;
    if (l > 2) {
        for (var i = 2; i < l - 1; i += 2) {
            if (args.length != 0) args += ',';
            args += '"' + arguments[i] + '":"' + arguments[i + 1] + '"';
        }
    }
    $.ajax({
        type: "POST",
        url: methodUrl,
        data: "{" + args + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: onSuccess,
        fail: callPageMethodFail
    });
}

/*
global error handler for callPageMethod fail
*/
function callPageMethodFail(response) {
    alert("An error occurred calling a page method: " + response.d);
}
