
$(document).ready(function() {
    resetSearchForm();
    // events
    $('#wfs1').change(wfs1Search);
    $('#wfs2').change(wfs2Search);
    $('#wfs3').change(doSearch);
    // windows
    $('#interactive').click(function() {
        openWindow();
        return false;
    });
    $('#close').click(function() {
        closeWindow();
        return false;
    });
    // form cancel
    $('#can1').click(function() {
        $('#sector2').hide();
        $('#wfs1').fadeTo(400, 1);
        return false;
    });
    $('#can2').click(function() {
        $('#can1').show();
        $('#sector1').show();
        $('#sector3').hide();
        $('#wfs2').fadeTo(400, 1);
        return false;
    });
    $('#can3').click(function() {
        $('#can2').show();
        $('#can3').hide();
        $('#searchSubmit').hide();
        $('#wfs3').fadeTo(400, 1);
        $('#sector2').show();
        return false;
    });
    // inline form
    $('.favlinksave').click(function() {
        var ref = $(this);
        var paramDocId = escape(ref.attr('id'));
        var paramFlag = escape(ref.attr('flag'));
        $.ajax({
            type: 'GET',
            url: '/members/favs',
            data: {
                docid: paramDocId,
                flag: paramFlag
            },
            success: function(response) {
                if (response == 1) {
                    $(location).attr('href', '/members');
                    return true;
                } else {
                    // fail
                    alert("There was an error saving this workflow to your favourites list. Please refresh the webpage and try again.");
                }
            }
        });
        return false;
    });
});

/* workflows */

function openWindow(action, userid) {
    $('#popup').fadeIn();
    $('#window').fadeIn();
};

function closeWindow() {
    $('#popup').fadeOut();
    $('#window').fadeOut();
};

/* search */

function resetSearchForm() {
    // 1
    $('#sector1').show();
    // 2
    $('#sector2').hide();
    // 3
    $('#sector3').hide();
    // final
    $('#searchSubmit').hide();
    $('#can3').hide();
};

function wfs1Search() {
    var wfs1 = $('#wfs1').val();
    if (wfs1 != "") {
        $.ajax({type: 'GET', url: '/members/search', data: {flag: wfs1},
            success: function(response) {
                $("#wfs2Target").html(response);
                $('#wfs1').fadeTo(400, 0.4);
                $('#sector2').show();
            }
        });
    }
};

function wfs2Search() {
    var wfs2 = $('#wfs2').val();
    if ((wfs2.indexOf('http://') < 0) && (wfs2 != "")) {
        // fetch more containers
        $.ajax({type: 'GET', url: '/members/search', data: {flag: wfs2},
            success: function(response) {
                $("#wfs3Target").html(response);
                $('#wfs2').fadeTo(400, 0.4);
                $('#can1').hide();
                $('#sector3').show();
            }
        });
    } else {
        window.location.replace(wfs2);
    }
};

function doSearch() {
    var wfs3 = $('#wfs3').val();
    if ((wfs3.indexOf('http://') < 0) && (wfs3 != "")) {
        $('#wfs3').fadeTo(400, 0.4);
        $('#can2').hide();
        $('#can3').show();
        $('#searchSubmit').show();
    } else {
        window.location.replace(wfs3);
    }
};


