'use strict';

angular.module('istex-widget', []).controller('istexWidgetCtrl', function($scope) {
    $scope.searchField = "";
    $scope.results = [];
    $scope.facets = [];

    $scope.search = function() {
        var query = "document/?q=";
        var fields = [];

        query += this.searchField;
        query += "&size=10";
        query += "&from=0";
        query += "&output=*";
        $("#searchButton").button('loading');
        $("#result").css("opacity", 0.4);

        var request = {
            url: "http://api.istex.fr/" + query,
            jsonp: true,
            crossDomain: true,
            success: function(data) {
            	$scope.results = data.hits;
                $("button").button('reset');
                $("#result").css("opacity", 1);
            },
            error: function(){
            	console.log("Une erreur est survenue!!!!");
            }
        };

        $.ajax(request);

        $("#result").removeClass('hide');
        $("#paginRow").removeClass('hide');
        $("#pageNumber").removeClass('hide');

    };
});