﻿/**
* Wrapper for Seadragon Ajax
* Copyright Softadvert GmbH http://www.softadvert.com
*/

var SeadragonWrapperCreator;

(function () {
	var viewer;
	var infoControl = null;
	var pictures = [];
	var aspectRatio = 1.0;

	var makeDescriptionViewControl = function () {
		$c = $('<div id="infoControl"><div id="outputText"></div></div>');
		$c.css({ opacity: 0.6 });
		infoControl = $c;
		infoControl.hide();
		return $c[0];
	}

	var toFloat = function (s) {
	  if(s){
		  s = s.replace(",", ".");
		  return parseFloat(s);
		}
		else {
		  return 0;
		}
	}

	var getNodeText = function (node){
	  if(node.firstChild && node.firstChild.nodeValue){
		  return (node.firstChild.nodeValue);
		}
		else{
		  return "";
		}
	}

	var setupTileArray = function (xml) {
		$(xml).find('AspectRatio').each(function () {
			aspectRatio = toFloat(getNodeText($(this)[0]));
		});
		$(xml).find('SceneNode').each(function () {
			var theFileName = justFileName(getNodeText($(this).find('FileName')[0]));
	  
			var TheTitle = $(this).find('Title')[0];
			var TheDescription = $(this).find('Description')[0];
			var TheLink = $(this).find('Link')[0];
			var tTitle = "";
			var tDescription = "";
			var tLink = "";
			if(TheTitle){
				tTitle	=getNodeText(TheTitle);
      }
			if(TheDescription){
			  tDescription	=getNodeText(TheDescription);
		  }
			if(TheLink){
			  tLink	=getNodeText(TheLink);
		  }
	
			var tileInfo = {
				fileName: theFileName,
				x: toFloat(getNodeText($(this).find('x')[0])),
				y: toFloat(getNodeText($(this).find('y')[0])),
				width: toFloat(getNodeText($(this).find('Width')[0])),
				height: toFloat(getNodeText($(this).find('Height')[0])),
				zOrder: toFloat(getNodeText($(this).find('ZOrder')[0])),
				title: tTitle,
				description : tDescription,
				link : tLink
			};
			pictures.push(tileInfo);
		});
	}

	var readSparseImageXml = function (sparseImageXml) {
		if (sparseImageXml) {
			$.ajax({
				type: "GET",
				url: sparseImageXml,
				dataType: "xml",
				success: function (xml) {
					setupTileArray(xml);
				},
  			error : function(jqXHR, textStatus, errorThrown){
	  		}

			});
		}
	}

	var justFileName = function (fileName, separator) {
		var nameSeparator = "\\";
		if (separator) {
			nameSeparator = separator;
		}
		var fileNameSplit = fileName.split(nameSeparator);
		return fileNameSplit.pop();
	}

	var initWrapper = function (dzcOutputXml, imagesPath, configImagesPath, sparseImageXml, container) {
	  setStrings();
		if (!(imagesPath)) {
			imagesPath = dzcOutputXml;
		}

		var output = document.getElementById("outputText");
		Seadragon.Config.imagePath = configImagesPath;
		
		if(!container){
		  container = "seadragonContainer";
		}
		
		viewer = new Seadragon.Viewer(container);
		viewer.addControl(makeDescriptionViewControl(), Seadragon.ControlAnchor.TOP_RIGHT);

		viewer.addEventListener("animationstart", onAnimationStart);
		viewer.addEventListener("animationfinish", onAnimationFinish);
		viewer.addEventListener("animation", onAnimation);

		$.ajax({
			type: "GET",
			url: dzcOutputXml,
			dataType: "text",
			success: function (xml) {
				viewer.openDzi(imagesPath, xml);
				readSparseImageXml(sparseImageXml);
			},
			error : function(jqXHR, textStatus, errorThrown){
			}
		});
	}

  var setStrings = function(){
   Seadragon.Strings.Tooltips.FullPage = "Umschalten Vollbild.";
   Seadragon.Strings.Tooltips.Home = "Zurück Setzen.";
   Seadragon.Strings.Tooltips.ZoomIn = "Hinein zoomen. (Sie können auch das Scrollrad Ihrer Maus verwenden.";
   Seadragon.Strings.Tooltips.ZoomOut = "Heraus zoomen. (Sie können auch das Scrollrad Ihrer Maus verwenden.";
  };

	var getSceneNumber = function (x, y) {
		if (pictures) {
			for (var i = pictures.length - 1; i >= 0; i--) {
				var check = pictures[i];
				var right = check.x + check.width;
				var bottom = check.y + check.height;
				// get the picture num which is shown at current center position
				zy = y * aspectRatio;
				if ((x > check.x) && (x < right)) {
					if ((zy > check.y) && (zy < bottom)) {
						return i;
					}
				}
			}
		}
		return -1;
	};

	function getZoomLevel() {
		if (viewer.viewport) {
			return viewer.viewport.getZoom();
		}
		else {
			return 0;
		}
	}

	var show = function (msg) {
		$("#outputText").html(msg);
	}

	var onAnimationStart = function (viewer) {
		show("Animation started");
	}

	var onAnimationFinish = function (viewer) {
		updateInfotext(viewer, 'finished ');
	}

	var onAnimation = function (viewer) {
		updateInfotext(viewer, 'animating ');
	}

	var recentSceneNumber = -1;

	var updateInfotext = function (viewer, state) {
		if (viewer.viewport) {
			var zoomLevel = getZoomLevel();
			var center = viewer.viewport.getCenter();
		}
		var s = state + '<br/>';
		if (infoControl) {
			if (zoomLevel >= 2) {
				var num = getSceneNumber(center.x, center.y);
				if(num===recentSceneNumber){
			//	  return;
				}
				recentSceneNumber=num;
				if (viewer.source) {
					s += "Source<br/>";
					if (num > -1) {
					  s = getImageDescription(pictures[num], viewer);
            if(s){
							show(s);
							infoControl.show(500);
							return;
            }
					}
				}
			}
			infoControl.hide(500);
			recentSceneNumber=-1;
		}
	}

	var getImageDescription=function(picture, viewer)
	{
	  var s = "";
		if (picture) {
			if (picture.title) {
				s += "<h2>" + picture.title + "</h2>";
			}
			if (picture.description) {
				s += "<p>" + picture.description + "</p>";
			}
	  }
	  return s;
	}

	var SeadragonWrapper = function() {
		this.init = function (dzcOutputXml, imagesPath, configImagesPath, sparseImageXml, container) {
			initWrapper(dzcOutputXml, imagesPath, configImagesPath, sparseImageXml, container);
		}
		this.setImageDescriptionFunction = function(func)
		{
		  getImageDescription = func;
		}
	}

	SeadragonWrapperCreator = function () {
		return new SeadragonWrapper();
	}

})();


