Mediasite = Mediasite || {};
Mediasite.Images = Mediasite.Images || {};

/**
	requires:
		* Mediasite.Filters
		* Mediasite.Color
		* jQuery
 */

(function(module) {
    module.getDominantColor = function(imageUrl, callback) { getColor('topbottom-edges', 'simple', imageUrl, callback); };
    module.getVerticalEdgesColor = function(imageUrl, callback) { getColor('leftright-edges', 'simple', imageUrl, callback); };

    function getColor(samplingStrategy, averagingStrategy, imageUrl, callback) {
        

		var image = document.createElement('img'); 
        image.setAttribute('src', imageUrl);
        if (image.width) {
        	process();
        } else {
        	$(image).on('load', process);
        }


        function process() {
            var pixelData = Mediasite.Filters.getPixels(image, { padding: 0 });
            if (pixelData) {
                var samplePixels = samplingStrategies[samplingStrategy];
                var averageColors = averagingStrategies[averagingStrategy];

                var sampledPixels  = samplePixels(pixelData.imageData);
                var color = averageColors(sampledPixels);
                callback(color);
            } else {
                callback(false);
            }
        }
    }


    var samplingStrategies = {};
    samplingStrategies['topbottom-edges'] = (function() {
    	var pixelsPerEdge = 6;

        return function(imageData) {
        	var pixels = [];

        	for (var x = 0, max = imageData.width, step = Math.floor(max / pixelsPerEdge); x < max; x += step) {
        		pixels.push(getPixel(imageData, x, 0));
        		pixels.push(getPixel(imageData, x, imageData.height - 1));
        	}
            
            return pixels;
        };
    }());

    samplingStrategies['leftright-edges'] = (function() {
    	var pixelsPerEdge = 6;

        return function(imageData) {
        	var pixels = [];

        	for (var y = 0, max = imageData.height, step = Math.floor(max / pixelsPerEdge); y < max; y += step) {
        		pixels.push(getPixel(imageData, 0, y));
        		pixels.push(getPixel(imageData, imageData.width - 1, y));
        	}
            
            return pixels;
        };
    }());

    

    function getPixel(imageData, x, y, array) {
		var start = (y * imageData.width + x) * 4;
		var pixel = [].slice.call(imageData.data, start, start + 4);
		return pixel;
	} 


    var averagingStrategies = {};
    averagingStrategies['simple'] = (function() {
        return function(pixels) {
        	var num = 0;
        	var sum = [0, 0, 0, 0];
        	for (var i = 0, length = pixels.length; i < length; i++) {
        		var pixel = pixels[i];
        		if (!pixel[3]) continue;

        		sum[0] += pixel[0];
        		sum[1] += pixel[1];
        		sum[2] += pixel[2];
        		sum[3] += pixel[3];
        		num++;
        	}

        	if (num) {
        		sum[0] /= num;
        		sum[1] /= num;
        		sum[2] /= num;
        		sum[3] /= num;
        	}
        	sum[3] = sum[3] || 255;

        	return { 
        		r: sum[0],
        		g: sum[1],
        		b: sum[2],
        		a: sum[3] / 256
        	};
        };
    }());




})(Mediasite.Images);