
var FeedViewer = function() {};

FeedViewer.altWriter = 
	function (title) {
		return "";
	};

FeedViewer.createThumbByUrlImageCreator =
	function (thumbFc) {
		return function(url, title, thumbDimension) {
			return '<img class="image" src="' + thumbFc(url) + '" alt="' + FeedViewer.altWriter(title) + '" />';
		};
	};

FeedViewer.createThumbByStyleImageCreator =
	function (width, height) {
		return function(url, title, thumbDimension) {
			return '<img class="image" style="' + (width ? "width: " + width + "px;" : "") + (height ? "height: " + height + "px;" : "") + '" src="' + url + '" alt="' + FeedViewer.altWriter(title) + '" />';
		};
	};

FeedViewer.createThumbByUrlAndStyleImageCreator =
	function (thumbFc, width, height) {
		return function(url, title, thumbDimension) {
			return '<img class="image" src="' + thumbFc(url) + '" alt="' + FeedViewer.altWriter(title) + '" style="' + (width ? "width: " + width + "px;" : "") + (height ? "height: " + height + "px;" : "") + '" />';
		};
	};

FeedViewer.createScaledThumbByUrlAndStyleImageCreator =
	function(serviceUrl, maxWidthOrHeight) {
		return function(url, title, thumbDimension) {
			var maxThumbDim = thumbDimension ? Math.max(thumbDimension.width, thumbDimension.height) : maxWidthOrHeight;
			var scaledThumbDim = Math.min(maxThumbDim, maxWidthOrHeight);
			var scale = 1.0 * scaledThumbDim / maxThumbDim;
			
			var thumbDimsToThumbFc = {
				width: null,
				height: null
			};
			var scaledDims = {
				width: null,
				height: null
			};

			if (thumbDimension == null) {
				thumbDimsToThumbFc.width = scaledThumbDim;
				scaledDims.width = scaledThumbDim;
				scaledDims.height = scaledThumbDim;
			} 
			else if (thumbDimension.width > thumbDimension.height) { // width
				thumbDimsToThumbFc.width = scaledThumbDim;
				scaledDims.width = scaledThumbDim;
				scaledDims.height = parseInt(thumbDimension.height * scale);
			}
			else { // height
				thumbDimsToThumbFc.height = scaledThumbDim;
				scaledDims.width = parseInt(thumbDimension.width * scale);
				scaledDims.height = scaledThumbDim;
			}
			
			var marginX = parseInt( Math.abs(maxWidthOrHeight - scaledDims.width) / 2);
			var marginY = parseInt( Math.abs(maxWidthOrHeight - scaledDims.height) / 2);
			
			var thumbFc = FeedViewer.createThumbFc(serviceUrl, thumbDimsToThumbFc.width, thumbDimsToThumbFc.height, false);
			var maxWidthOrHeightHtml = maxWidthOrHeight + 2;
			var html = "";
			html += '<div class="scaledImage" style="width: '+maxWidthOrHeightHtml+'px; height: '+maxWidthOrHeightHtml+'px;">';
			html += '<img class="image" src="' + thumbFc(url) + '" alt="' + FeedViewer.altWriter(title) + '" style="' + ("margin-left: " + marginX + "px;") + ("margin-top: " + marginY + "px;") + '" />';
			html += '</div>';
			return html;
		};
	};

FeedViewer.createDebugThumbByStyleImageCreator =
	function (width, height) {
		return function(url, title, thumbDimension) {
			url = "./images/logo-digmap.jpg";
			return '<img class="image" style="' + (width ? "width: " + width + "px;" : "") + (height ? "height: " + height + "px;" : "") + '" src="' + url + '" alt="' + FeedViewer.altWriter(title) + '" />';
		};
	};

FeedViewer.createThumbFc = 
	function(serviceUrl, width, height, noWaitCompletion) {
		return function(url) {
			noWaitCompletion = typeof noWaitCompletion == "undef" ? true : noWaitCompletion;
			return serviceUrl + "?url=" + encodeURIComponent(url) + (width ? "&width="+width : "") + (height ? "&height="+height : "") + "&nowait=" + noWaitCompletion;
		};
	};

FeedViewer.defaultThumbFc = 
	function() {
		return function(url) {
			return url;
		};
	};

FeedViewer.prototype.createList = 
	function(feedJson, mode, imageCreator, useTitle) {
		var entries = this.getList(feedJson.feed.entry);
		useTitle = useTitle || false;
		var html = '';
		html += '<div class="itemsContainer itemsContainer-' + mode + '">';
		if (useTitle) {
			html += 	'<div class="itemsHeader">';
			html += 		'<h2 class="green allcaps">';
			html += 			(this.getValue(feedJson.feed.title));
			html += 		'</h2>';
			html += 	'</div>';
		}
		
		html += '<div class="itemList">';
		if (mode == "thumbs") {
			html += '<div class="item">';
			html += '<table><tr><td style="height: 88px; vertical-align: middle;">';
			html += 	'<a href="javascript:previousFeatures()">';
			html += 		'<img src="'+appRelPath+'/images/arrow-left.png" style="margin: 0 auto; vertical-align: middle;" alt="[Previous]" />';
			html += 	'</a>';
			html += '</td></tr></table>';
			html += '</div>';
		}
		for (var i in entries) {
			var entry = entries[i];
			if (i > 0) { // add separator to modes it makes sense
				if (mode == "records" || mode == "news") {
					html += '<div class="separator">';
					html += 	'<hr class="light"/>';
					html += '</div>';
				}
			}
			html += this.createItem(entry, mode, imageCreator);
		}
		if (mode == "thumbs") {
			html += '<div class="item">';
			html += '<table><tr><td style="height: 88px; vertical-align: middle;">';
			html += 	'<a href="javascript:nextFeatures()">';
			html += 		'<img src="'+appRelPath+'/images/arrow-right.png" style="margin: 0 auto;" alt="[Previous]" />';
			html += 	'</a>';
			html += '</td></tr></table>';
			html += '</div>';

			html += '<div class="clear"></div>';
		}
		html += '</div>';
		
		html += '</div>';
		
		return html;
	};

FeedViewer.prototype.getValue =
	function(element) {
		return element ? (element["#text"] || element) : "";
	};

FeedViewer.prototype.createItem =
	function(entry, mode, imageCreator) {
		var links = this.getLinks(entry);
		var thumbDimension = null;
		if (mode == "thumbs") {
			thumbDimension = this.getThumbDimension(entry);
		}
		
		var imageHtml = imageCreator(links.thumbnail["@href"], this.getValue(entry.title), thumbDimension);
		
		var html = '';
		if (mode == "thumbs") {
			html += '<div class="item">';
			html += 	'<div class="itemLink">';
			html += 		'<a target="blank" href="' + links.self["@href"] + '" title="' + this.getValue(entry.title) + '">';
			html += 			imageHtml;
			html += 		'</a>';
			html += 	'</div>';
			html += '</div>';
		} 
		else if (mode == "news") {
			html += '<div class="item">';
			html += 	'<div class="itemLink">';
			html += 		'<a target="blank" class="menulink" href="' + links.self["@href"] + '">';
			html += 			this.getValue(entry.title);
			html += 		'</a>';
			html += 	'</div>';
			html += 	imageHtml;
			html += 	'<div>' + this.getValue(entry.summary) + '</div>';
			html += '</div>';
		}
		else if (mode == "records") {
			html += '<div class="item">';
			html += '<table>';
			html += '<tr>';
			html += '<td class="itemCell itemCell-image">';
			html += 	'<div class="imageHighlight">';
			html += 		imageHtml;
			html += 	'</div>';
			html += '</td>';
			html += '<td class="itemCell">';
			html += 	'<div class="itemLink">';
			html += 		'<a target="blank" class="menulink bold" href="' + links.record["@href"] + '">';
			html += 			this.getValue(entry.title);
			html += 		'</a>';
			html += 	'<div>' + this.getValue(entry.summary) + '</div>';
			html += 	'<a target="_blank" title="' + links.collection["@label"] + '" href="' + links.collection["@href"] + '">' + links.collection["@label"] + '</a>';
			html += 	'</div>';
			html += '</td>';
			html += '</tr>';
			html += '</table>';
			/*
			html +=		'<div class="subBox radius right">';
			html += 		'<a target="_blank" title="View Record" href="' + links.self["@href"] + '" class="meta">';
			html +=				'View Record';
			html +=			'</a>';
			html +=		'</div>';
			html += '</div>';
			*/
			html += '<div style="clear:both;"></div>';
		}
		
		return html;
	};

FeedViewer.prototype.getList =
	function(obj) {
		if (typeof obj == "undefined" || obj == null) {
			return [];
		}
		return (obj instanceof Array ? obj : [obj]);
	};

FeedViewer.prototype.getUnique =
	function(obj) {
		if (typeof obj == "undefined" || obj == null) {
			return null;
		}
		return (obj instanceof Array ? null : obj);
	};

FeedViewer.prototype.getLinks =
	function(entry) {
		var links = {};
		var entryLinks = this.getList(entry.link);
		for (var i in entryLinks) {
			var link = entryLinks[i];
			if (link["@rel"] == null) {
				links["self"] = link;
			} else {
				links[link["@rel"]] = link;
			}
		}
		return links;
	};

FeedViewer.prototype.getThumbDimension =
	function(entry) {
		var thumbDimension = null;
		var metadata = this.getUnique(entry.metadata);
		
		if (metadata) {
			var format = this.getUnique(metadata["dc:format"]);
			if (format) {
				var formatRE = /^(\d+) x (\d+).+$/i;
				var result = formatRE.exec(format);
				if (result && result.length >= 3) {
					thumbDimension = {
						width:  parseInt( result[1] ),
						height: parseInt( result[2] )
					};
				}
			}
		}
		return thumbDimension;
	};

	
// image creators

var getImageCreator = function(id, thumbDimensions, thumbServiceUrl, thumbsMode) {
	var imageCreator = null;
	if (thumbsMode == "style") {
		imageCreator = FeedViewer.createThumbByStyleImageCreator(
			thumbDimensions[id].width, 
			thumbDimensions[id].height
		);
	} else if (thumbsMode == "debug") {
		imageCreator = FeedViewer.createDebugThumbByStyleImageCreator(
			thumbDimensions[id].width, 
			thumbDimensions[id].height
		);
	} else if (thumbsMode == "thumbService") {
		imageCreator = FeedViewer.createThumbByUrlImageCreator(
			FeedViewer.createThumbFc(
				thumbServiceUrl,
				thumbDimensions[id].width,
				thumbDimensions[id].height
			)
		);
	} else if (thumbsMode == "thumbService+style") {
		imageCreator = FeedViewer.createThumbByUrlAndStyleImageCreator(
			FeedViewer.createThumbFc(
				thumbServiceUrl,
				thumbDimensions[id].thumbDefault.width,
				thumbDimensions[id].thumbDefault.height
			),
			thumbDimensions[id].width,
			thumbDimensions[id].height
		);
	} else if (thumbsMode == "scaled") {
		var w = thumbDimensions[id].width;
		var h = thumbDimensions[id].height;
		var maxWidthOrHeight;
		if (w == null) {
			maxWidthOrHeight = h;
		} else if (h == null) {
			maxWidthOrHeight = w;
		} else {
			maxWidthOrHeight = Math.min(w, h);
		}
		
		imageCreator = FeedViewer.createScaledThumbByUrlAndStyleImageCreator(thumbServiceUrl, maxWidthOrHeight);
	}
	return imageCreator;
};
