﻿
var HfAjax = {
    getRoute: function(action, controller, routeValues, callback) {
        routeValues.routeAction = action;
        routeValues.routeController = controller;
        $.get(
            '/HfAjax/GetRoute', $.param(routeValues),
            function(responseText, textStatus, XMLHttpRequest) {
                callback(responseText);
            }
        );
    },
    get: function(action, controller, routeValues, callback) {
        HfAjax.getRoute(action, controller, routeValues,
            function(responseText) {
                $.get(responseText, null, callback);
            }
        );
    },
    goTo: function(url) {
        //        var hpos = url.indexOf('#') > -1 ? url.indexOf('#') : url.length;
        //        var qpos = url.indexOf('?') > -1 ? url.indexOf('?') : hpos;

        //        var p = url.substr(0, qpos);
        //        var q = url.substr(qpos, hpos);
        //        var h = url.substr(hpos, url.length);
        //        alert(
        //        window.location.pathname + '|' +
        //        window.location.search + '|' +
        //        window.location.hash);

        //        alert(p + '|' + q + '|' + h);


        //        window.location.search = q;
        //        window.location.hash = h;
        window.location = url;
    },
    goToSearch: function(form) {
        var data = $(form).serialize();
        $.post(
            $(form).attr('action'), data,
            function(jsonObj, textStatus) {
                if (jsonObj.redirectUrl)
                    HfAjax.goTo(jsonObj.redirectUrl);
            }, 'json');
    },
    getSelectItems: function(dropdown, action, controller, routeValues, callback) {
        HfAjax.get(action, controller, routeValues, function(responseText, textStatus, XMLHttpRequest) {
            $(dropdown).html(responseText);
            if (callback)
                callback(dropdown, $(responseText));
        })
    },

    handleJson: function(data, callback) {
        $('.validatorMessage').empty();
        $('.validatorIcon').hide();

        if (data.script) {
            eval(data.script);
        }

        if (data.validation) {
            if (!data.validation.isValid) {
                var top = Infinity;

                jQuery.each(data.validation.results, function(idx, o) {
                    if (o.isValid) return true;

                    var key = o.key.replace('.', '_');
                    if (o.messages) {
                        jQuery.each(o.messages, function(idx, m) {
                            var ele = $(document.createElement('div'));
                            ele.text(m);
                            $('#_validator_' + key + '_Msg').append(ele);
                        });
                    }
                    $('#_validator_' + key + '_Icon').show();

                    var offset = $('#' + key).offset();

                    if (offset.top < top)
                        top = offset.top;
                });


                $(window).scrollTop(top - 16); // Eh, random margin. :-P Hush.
            }
            else if (data.validation.redirectUrl) {
                HfAjax.goTo(data.validation.redirectUrl);
                return;
            }
        }

        if (callback) callback(data);
    },
    submit: function(ev, url, callback) {
        //alert($(this).serialize());
        $.post(url, $(this).serialize(), function(data) { { HfAjax.handleJson(data, callback); } }, 'json');
        return false;
    }
};