'use strict';

var ArticleDetailsController = angular.module('articles.controllers.details', [
    'system'
])
    .controller('ArticleDetailsController', [
        '$scope',
        '$route',
        '$location',
        'System',
        'ArticleService',
        'DocumentService',
        'ImageService',
        'KartaService',
        '$sce',
        '$window',
function ($scope, $route, $location, System, ArticleService, DocumentService, ImageService, KartaService, $sce, $window) {
    var id = $route.current.params["id"];
    $scope.DisplayPodaci = false;
    $scope.DisplayPodatak = false;
    $scope.DisplayKarta = false;


    $scope.refresh = function () {
        ArticleService.get(id)
            .then(function (data) {
                $scope.article = data;
                System.Page.title($scope.article.Title);
                if (data.Library != null) {
                    var libimage = data.Library[0];
                    if (libimage != "" && libimage != null) {
                        ImageService.query({ LibraryID: $scope.article.Library[0] })
                         .then(function (data) {
                             $scope.mojeslike = data.Images;
                         });
                    }
                }

                if (data.Karta != null) {
                    var karta = data.Karta;
                    if (karta != "" && karta != null) {
                        $scope.DisplayKarta = true;
                        KartaService.get({ KartaID: $scope.article.Karta })
                      .then(function (data) {
                          $scope.karta = data;
                      });
                    }
                }
                if (data.LibraryDocument != null) {
                    var libID = data.LibraryDocument[0];
                    if (libID != "" && libID != null) {
                        $scope.DisplayPodaci = true;
                        $scope.LibraryID = libID;
                        DocumentService.getSettings()
                       .then(function (data) {
                           $scope.settings = data;
                       });
                        DocumentService.query({ LibraryID: $scope.LibraryID })
                       .then(function (data) {
                           $scope.documents = data.documents;
                           $scope.predicateNaziv = 'Title';
                       });
                    }
                }
                if (data.Document != null) {
                    var docID = data.Document[0];
                    if (docID != "" && docID != null) {
                        $scope.DisplayPodatak = true;
                        DocumentService.get(docID)
                    .then(function (data) {
                        $scope.documentP = data;
                    });

                    }
                }
            });
    }


    $scope.Ext = function (value) {
        return System.Ext.icon(value);
    };


    $scope.fo = function () {
        $window.open('//facebook.com/profile.php?id=100007286538022');
    };


    $scope.renderHtml = function (html_code) {
        return $sce.trustAsHtml(html_code);
    };

    $scope._Index = 0;

    $scope.isActive = function (index) {

        return $scope._Index === index;
    };

    $scope.showPhoto = function (index) {
        $scope._Index = index;
    };

    $scope.search = function (q, reset) {
        reset ? $scope.query = q : angular.extend($scope.query, q);
        $location.search($scope.query);
        $scope.refresh();
    };


    $scope.$watch("pager.page", function () {
        if (!$scope.pager || !$scope.pager.page) return;
        $scope.query.page = $scope.pager.page;
        $scope.search($scope.query, false);
    });

    $scope.selectLibrary = function (tip) {
        if (tip != null)
            $location.url('/documentst/' + tip);
        else
            $location.url('/documentst');
    };

    $scope.selectLibraryJ = function (tip) {
        $location.url('/javnenabavke/' + tip);
    };

    $scope.selectLibraryG = function (tip) {
        $location.url('/sluzbeniglasnici/' + tip);
    };

    $scope.selectLibraryPristup = function (id) {
        $location.url('/documents?LibraryID=' + id);
    };

    $scope.selectLibraryZasjedanja = function (tip) {
        if (tip != null)
            $location.url('/skupstinska/' + tip);
        else
            $location.url('/documentst/');
    };



    /**
        *@method pregleda mape
    */

    $scope.refresh();

    $scope.initializemap = function (la, lo) {
        var mes = $scope.karta.Title;
        var styles = [
          {
              featureType: 'road.highway',
              elementType: 'geometry',
              stylers: [
                { hue: '#ff0022' },
                { saturation: 60 },
                { lightness: -20 }
              ]
          }, {
              featureType: 'road.arterial',
              elementType: 'all',
              stylers: [
                { hue: '#2200ff' },
                { lightness: -40 },
                { visibility: 'simplified' },
                { saturation: 30 }
              ]
          }, {
              featureType: 'road.local',
              elementType: 'all',
              stylers: [
                { hue: '#f6ff00' },
                { saturation: 50 },
                { gamma: 0.7 },
                { visibility: 'simplified' }
              ]
          }, {
              featureType: 'water',
              elementType: 'geometry',
              stylers: [
                { saturation: 40 },
                { lightness: 40 }
              ]
          }, {
              featureType: 'road.highway',
              elementType: 'labels',
              stylers: [
                { visibility: 'on' },
                { saturation: 98 }
              ]
          }, {
              featureType: 'administrative.locality',
              elementType: 'labels',
              stylers: [
                { hue: '#0022ff' },
                { saturation: 50 },
                { lightness: -10 },
                { gamma: 0.90 }
              ]
          }, {
              featureType: 'transit.line',
              elementType: 'geometry',
              stylers: [
                { hue: '#ff0000' },
                { visibility: 'on' },
                { lightness: -70 }
              ]
          }
        ];
        var options = {
            mapTypeControlOptions: {
                mapTypeIds: [google.maps.MapTypeId.HYBRID, 'Styled']
            },
            center: new google.maps.LatLng(la, lo),
            zoom: 16,
            disableDefaultUI: false,
            mapTypeId: 'Styled'
        };
        var div = document.getElementById('maparticle_canvas');
        var map = new google.maps.Map(div, options);
        var styledMapType = new google.maps.StyledMapType(styles, { name: 'Styled' });
        map.mapTypes.set('Styled', styledMapType);
        var iconBase = 'http://www.laktasi.net/Areas/cms/Style/images/';
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(la, lo),
            map: map,
            icon: iconBase + 'pin.png',
            shadow: iconBase + 'pin.png',
            size: new google.maps.Size(5, 5)

        });

        var message = '<h2 style="padding:0px 0px 10px 0px !important;">' + mes + '</h2>' +
            '<p>Web: <a class="see-all" href="http://www.laktasi.net/">' +
           '<h2>http://www.laktasi.net/>/h2></a> ' +
            '</p>' +
            '</div>';
        var infowindow = new google.maps.InfoWindow({ content: message });
        google.maps.event.addListener(marker, 'click', function () {
            infowindow.open(marker.get('map'), marker);
            google.maps.event.trigger(map, 'resize')
        });
    };

    $scope.mapa = function (value) {
        $scope.dialogArticleMap.dialog("open");
        KartaService.get(value)
           .then(function (data) {
               $scope.karta = data;
               var la = data.Latitude;
               var lo = data.Longitude
               $scope.initializemap(la, lo);
           });

    };

  

}
    ]);