/* Minification failed. Returning unminified contents.
(1,14): run-time error CSS1031: Expected selector, found '='
(1,14): run-time error CSS1025: Expected comma or open brace, found '='
(9,8): run-time error CSS1030: Expected identifier, found 'defineProperty('
(9,8): run-time error CSS1031: Expected selector, found 'defineProperty('
(9,8): run-time error CSS1025: Expected comma or open brace, found 'defineProperty('
(22,2): run-time error CSS1019: Unexpected token, found ')'
(24,23): run-time error CSS1031: Expected selector, found '='
(24,23): run-time error CSS1025: Expected comma or open brace, found '='
(34,3): run-time error CSS1030: Expected identifier, found 'config('
(34,3): run-time error CSS1031: Expected selector, found 'config('
(34,3): run-time error CSS1025: Expected comma or open brace, found 'config('
(149,23): run-time error CSS1030: Expected identifier, found 'controller('
(149,23): run-time error CSS1031: Expected selector, found 'controller('
(149,23): run-time error CSS1025: Expected comma or open brace, found 'controller('
 */
var MLSPHOTO = MLSPHOTO || {}; // global namespace

/*
Extend Array
void remove(Object);
Removes an object from the array;
*/

Object.defineProperty(Array.prototype, 'remove', {
    value: function (object) {
        if (typeof object === 'function') {
            var obj = this.filter(object).pop();
            return this.remove(obj);
        } 
        var i = this.indexOf(object);
        this.splice(i, 1);
        return i;
    },
    writable: false,
    enumerable: false,
    configurable: false
});

MLSPHOTO.slideshowApp = angular.module('slideshowApp',
[
	'ngRoute',
	'dataServices',
	'boostrapSlideshow',
	'utilitiesModule',
	'MLSUserProvider'
]);

MLSPHOTO.slideshowApp
	.config(['$locationProvider', '$routeProvider', '$httpProvider', function ($locationProvider, $routeProvider, $httpProvider)
	{
		//$locationProvider.html5Mode(
		//{
		//	enabled: true,
		//	requireBase: false
		//});

		$httpProvider.interceptors.push('authInterceptor');

	    $httpProvider.interceptors.push(['$q', '$window', function ($q, $window) {
	        return {
	            'responseError': function (rejection) {
	                if ( rejection.status === 400 || (rejection.status === 404 && rejection.config.url.indexOf('api') != -1))
	                    $window.location.href = '/MLSListingPhoto/listing/standalone/#/not-found';

	                return $q.reject(rejection);
	            }
	        };
	    }]);

	    $routeProvider
	        .when('/:mlsnum/:mode?', {
				templateUrl: MLSPHOTO.GetPath('/Scripts/modules/slideshowApp/views/slideshowAppView.html?' + new Date()),
	            controller: 'slideshowAppController',
	            resolve: {
					property: ['$route', 'photoInfoService', function ($route, photoInfoService) {
						return photoInfoService.getPropertyData($route.current.params.mlsnum);
	                }],
	                cmaPhoto: function ( ) {                    
	                    return null;
	                },
	                photos: ['$route', 'photoInfoService', function ($route, photoInfo) {
						return photoInfo.getPhotosForSlideshow({ mlsnum: $route.current.params.mlsnum, mode: $route.current.params.mode == undefined ? 'all' : $route.current.params.mode }).then(function (photos) {
	                        return photos.filter(function (each) {
	                            return each.Published;
	                        });
	                    });
	                }],
	            }
			})
			.when('/:mlsnum/:mode/:iscommercialpublic?', {
				templateUrl: MLSPHOTO.GetPath('/Scripts/modules/slideshowApp/views/slideshowAppView.html?' + new Date()),
				controller: 'slideshowAppController',
				resolve: {
					property: ['$route', 'photoInfoService', function ($route, photoInfoService) {
						return photoInfoService.getPropertyData($route.current.params.mlsnum);
					}],
					cmaPhoto: function () {
						return null;
					},
					photos: ['$route', 'photoInfoService', function ($route, photoInfo) {
						return photoInfo.getPhotosForSlideshowVideos({
							mlsnum: $route.current.params.mlsnum,
							mode: $route.current.params.mode
						}).then(function (photos) {
							return photos.filter(function (each) {
								return each.Published;
							});
						});
					}],
				}
			})

		    .when('/:mlsnum/:cma', {
				templateUrl: MLSPHOTO.GetPath('/Scripts/modules/slideshowApp/views/slideshowAppView.html?' + new Date()),
	            controller: 'slideshowAppController',
	            resolve: {
	                property: ['$route', 'cmaService', function ($route, cmaService) {
	                    var params = $route.current.params;
	                    return cmaService.getPropertyInfo(params.mlsnum, params.cma).then(function (result) { return result.data; });
	                }],
	                cmaPhoto: ['$route', 'cmaService',function ($route, cmaService) {
	                    var params = $route.current.params;
	                    return cmaService.getPhotoFromCMA(params.mlsnum, params.cma);
	                }],
	                photos: ['$route', 'photoInfoService', function ($route, photoInfo) {
	                    return photoInfo.getPhotosForSlideshow({ mlsnum: $route.current.params.mlsnum }).then(function (photos) {
	                        return photos.filter(function (each) {
	                            return each.Published;
	                        });
	                    });
	                }],
	            }
	        });
	}])

	.run(['$templateCache', '$http', function ($templateCache, $http)
	{
		$http.get(MLSPHOTO.GetPath('/Scripts/modules/bootstrapSlideshow/views/carouselTemplate.html?' + new Date())).then(function (result) {
	       return  $templateCache.put('template/carousel/carousel.html', result.data);
	    });
	}])

	.controller('SlideshowMainController', ['$rootScope', '$scope', '$window', function ($rootScope, $scope, $window)
	{
	    $scope.IsLoading = true;
	    $rootScope.$on('$routeChangeStart', function () {
	        $scope.IsLoading = true;
	    });

	    $rootScope.$on('$routeChangeSuccess', function () {
	        $scope.IsLoading = false;
	    });

		$rootScope.$on('$routeChangeError', function (event, current, previous, rejection) 
		{
	        if (rejection == 'Listing not found')
	            $window.location.href = '/MLSListingPhoto/#/not-found';
	        else if (rejection == 'Not authorized' || rejection == 'Not logged in')
	            $window.location.href = '/MLSListingPhoto/listing/standalone/#/errorocurred';
	        
	        $scope.IsLoading = false;
	    });
	}]);
MLSPHOTO.slideshowApp.controller('slideshowAppController',
	['$scope', 'property', 'photos', 'cmaPhoto', 'browserDetectService', '$window', '$routeParams',
	function ($scope, property, photos, cmaPhoto, browserDetectService, $window, $routeParams) 
	{
        var sortFunction = browserDetectService.isIE() ?
            function (a, b) { return a.DisplayOrder - b.DisplayOrder; } :
            function (a, b) { return a.DisplayOrder - b.DisplayOrder || -1; },
	        urlPrint = "/listing/standalone/#/photoreport/" + $routeParams.mlsnum + '/twocolumn';

		$scope.property = property;
		console.log('$routeParams', $routeParams)
		$scope.isCommercialPublic = $routeParams.iscommercialpublic != null && $routeParams.iscommercialpublic != undefined ? $routeParams.iscommercialpublic.toLowerCase() : 'false';

		if ($routeParams.cma) {
            if (cmaPhoto && cmaPhoto.Url) {
                photos.remove(function(each) { return each.IsMainPhoto; });
                photos.unshift(cmaPhoto);
            }
            urlPrint = "/listing/standalone/#/photoreportCMA/" + $routeParams.mlsnum + "/" + $routeParams.cma + '/twocolumn';      
	    } 

		$scope.photosCollection = photos.sort(sortFunction).map(function (each) {
	        return {
	            image: each.Url,
	            fullImage: each.FullUrl,
	            thumb: each.ThumbUrl,
				text: each.Caption,
				mediaURL: each.MediaURL,
				mediaType: each.MediaType
	        };
	    });

	    $scope.print = function() {
	        $window.open(MLSPHOTO.GetPath(urlPrint));
	    };
	}]
);
