(function () {
	"use strict"
	angular
		.module('myApp.GalleryModule')
		.controller('GalleryController', ['$http',
			GalleryController
		]);

	/**
	 * Main Controller for the Photo Pride Voting page (view2)
	 */
	function GalleryController($http) {
		var self = this;

		$http.get('photos/randomPhotos.json').then(function (res) {
			// returns a list of JSON objects:
			// {
			// 	"id": 1,
			// 	"name": "random1",
			// 	"imageUrl": "photos/random/random1.jpg"
			// }
			self.canvasGrid = buildGrid(res.data);
		});

		function buildGrid(items) {
			// grid has a number of columns within only one row
			var cols = 5;
			var grid = [];
			var listOfItems = items;
			// prepare cols
			for (var i = 0; i < cols; i++) {
				grid.push([]);
			};
			// apend more images to the list don't do more than 1000
			var more = 100;
			var newId = items.length;
			for (var i = 0; i < more; i++) {
				var randomIndex = Math.floor(Math.random() * (items.length - 1));
				var newItem = items[randomIndex];
				newItem.id = newId + i;
				listOfItems.push(newItem);
			};
			// propagate cols with img data
			while (listOfItems.length != 0) {
				for (var col = 0; col < cols; col ++) {
					if (listOfItems.length != 0) {
						grid[col].push(listOfItems.pop());
					} else {
						break;
					}
				}
			}
			return grid
		};
	}

})();