'use strict';

function SearchCtrl($scope, $location, $route, $http, $filter)
{
    $scope.endyear = 2013;
    $scope.year = ($route.current.params.year ? $route.current.params.year : null);
    $scope.period = ($route.current.params.period ? $route.current.params.period : null);
    $scope.conceptMaps = ['FundamentalAccountingConcepts' ];
    $scope.conceptMap = $scope.conceptMaps[0];
    $scope.conceptMapKey = '';
    $scope.conceptMapKeys = ['fac:Assets', 'fac:Revenues' , 'fac:Equity'];
    $scope.entities = [];
    $scope.factValue = '';
    $scope.units = '';
    $scope.name = '';
    $scope.cik = '';
    $scope.ticker = '';

    $scope.change = function (year, period)
    {
        $location.path("/search/" + year + "/" + period);
        $scope.safeApply();
    };

    $scope.getEntities = function ()
    {
        $http({method: 'GET', url: '/data/entities.json'})
            .success(function (data, status, headers, config)
            {
                $scope.entities = data.entityNameTickerSymbolCikTuples;

            })
            .error(function (data, status, headers, config)
            {
                $scope.entities = [];
            });

    }

    $scope.getValue = function ()
    {
        if ($scope.cik != '' && $scope.period && $scope.year && $scope.conceptMap != null && $scope.conceptMapKey != '')
        {
            $http({method: 'GET', url: '/data/factValue.json'})
                .success(function (data, status, headers, config)
                {
                    $scope.factValue = Number(data.fact.value['#text']).toLocaleString();
                    $scope.units = data.fact.value['-unit'].split(':')[1];
                })
                .error(function (data, status, headers, config)
                {
                    $scope.factValue = '';
                    $scope.units = '';
                });

        }
        else
            alert('Complete all parameters!');


    }

    if (!$route.current.params.year)
    {
        $scope.change($scope.endyear - 1, "FY");
    }

    $scope.SetCik = function ()
    {
        if (event.keyCode == 13 && $scope.entity)
            $filter('filter')($scope.entities, function (elem)
            {
                if ($scope.entity == elem.name)
                {
                    $scope.Cik = elem.cik;
                    return;
                }

            });


    }

    $scope.getEntities();

    $scope.update = function (element)
    {
        $scope.entities.filter(function (e)
        {
            if (e[element] == $scope[element])
            {
                $scope.name = e.name;
                $scope.cik = e.cik;
                $scope.ticker = e.ticker;
                return;
            }
        })

    };

    /* $scope.$watch('name', function (value)
     {
     $scope.entities.filter(function (e)
     {
     if (e.name == value)
     {
     $scope.name = e.name;
     $scope.cik = e.cik;
     $scope.ticker = e.ticker;
     return;
     }
     })

     });

     $scope.$watch('cik', function (value)
     {
     $scope.entities.filter(function (e)
     {
     if (e.cik == value)
     {
     $scope.name = e.name;
     $scope.cik = e.cik;
     $scope.ticker = e.ticker;
     return;
     }
     })

     });

     $scope.$watch('ticker', function (value)
     {
     $scope.entities.filter(function (e)
     {
     if (e.ticker == value)
     {
     $scope.name = e.name;
     $scope.cik = e.cik;
     $scope.ticker = e.ticker;
     return;
     }
     })

     });*/
}

@@ -64,17 +64,8 @@
 http
-, $filter
 )%0A%7B%0A
@@ -2039,354 +2039,8 @@
  %7D%0A%0A
-    $scope.SetCik = function ()%0A    %7B%0A        if (event.keyCode == 13 && $scope.entity)%0A            $filter('filter')($scope.entities, function (elem)%0A            %7B%0A                if ($scope.entity == elem.name)%0A                %7B%0A                    $scope.Cik = elem.cik;%0A                    return;%0A                %7D%0A%0A            %7D);%0A%0A%0A    %7D%0A%0A
     
@@ -2400,773 +2400,6 @@
 %7D;%0A%0A
-    /* $scope.$watch('name', function (value)%0A     %7B%0A     $scope.entities.filter(function (e)%0A     %7B%0A     if (e.name == value)%0A     %7B%0A     $scope.name = e.name;%0A     $scope.cik = e.cik;%0A     $scope.ticker = e.ticker;%0A     return;%0A     %7D%0A     %7D)%0A%0A     %7D);%0A%0A     $scope.$watch('cik', function (value)%0A     %7B%0A     $scope.entities.filter(function (e)%0A     %7B%0A     if (e.cik == value)%0A     %7B%0A     $scope.name = e.name;%0A     $scope.cik = e.cik;%0A     $scope.ticker = e.ticker;%0A     return;%0A     %7D%0A     %7D)%0A%0A     %7D);%0A%0A     $scope.$watch('ticker', function (value)%0A     %7B%0A     $scope.entities.filter(function (e)%0A     %7B%0A     if (e.ticker == value)%0A     %7B%0A     $scope.name = e.name;%0A     $scope.cik = e.cik;%0A     $scope.ticker = e.ticker;%0A     return;%0A     %7D%0A     %7D)%0A%0A     %7D);*/%0A
 %7D%0A

remove unused code