function TDVViewerAPI()
{
	this.__init__ = function()
	{		
		this._clear();
	}
	
	this._clear = function()
	{
		this._viewer = null;
		this._isViewerReady = false;
		this._viewerReadyList = [];
		this._listeners = {};
		this._listeners[TDVViewerAPI.MEDIA_INDEX_CHANGE_EVENT] = [];
		this._listeners[TDVViewerAPI.MOVEMENT_CHANGE_EVENT] = [];	
	}
	
	this.getVersion = function()
	{
		return "0.4";
	}

	this.init = function(name)
	{
		if (typeof(name) == "undefined")
			name = "Viewer";
		this._name = name;
		
		var self = this;
		this._onViewerReady(function()
		{
			self._callViewerReadyList();
			self._isViewerReady = true;
		});			
	}
	
	this.finish = function()
	{
		this._clear();
	}
	
	this._executeOnViewerReady = function(f)
	{
		if (this._isViewerReady)
			f();
		else
			this._viewerReadyList.push(f);
	}	

	this._callViewerReadyList = function()
	{
		while (this._viewerReadyList.length > 0)
		{
			var call = this._viewerReadyList[0];
			this._viewerReadyList.splice(0, 1);
			call();
		}
	}		
	
	this._onViewerReady = function(callback)
	{
		var self = this;
		this._onViewerAvailable(function()
		{
			self._onViewerStatusReady(function()
			{			
				callback();
			});
		});
	}
	
	this._onViewerAvailable = function(callback)
	{
		var container = ((navigator.appName.indexOf("Microsoft") != -1)? window : document); 

		var self = this;
		var intervalId = setInterval(function()
		{
			try
			{
				if (typeof(container[self._name]) != "undefined")
				{
					self._viewer = container[self._name];
					clearInterval(intervalId);
					callback();
				}
			}
			catch (e) { }						
		}, 100);
    }
	
	this._onViewerStatusReady = function(callback)
	{
		var self = this;
		var intervalId = setInterval(function()
		{
			try
			{
				if (self._viewer.tdvv_status() == "ready") 
				{
					clearInterval(intervalId);
					callback();
				}
			}
			catch (e) { }						
		}, 1000);
	}
	
	this.dispatchEvent = function(event, params)
	{
		if (typeof(params) == "undefined")
			params = [];
		for (var i = 0; i < this._listeners[event].length; i++) 
		{
			try
			{
				var listener = this._listeners[event][i]; 
				listener.apply(listener, params);
			}
			catch (e) {}
		}
	}
		
	this.addListener = function(event, listener)
	{
		this._listeners[event].push(listener);		
	}

	this.removeListener = function(event, listener)
	{
		var index = this._listeners[event].indexOf(listener);
		if (index >= 0)
			this._listeners[event].splice(index, 1);		
	}
			
	this.setMediaByIndex = function(index)
	{
		var self = this;
		this._executeOnViewerReady(function()
		{
			self._viewer.tdvv_setMediaByIndex(index);
		});
	}

	this.setMediaByName = function(name)
	{
		var self = this;
		this._executeOnViewerReady(function()
		{
			self._viewer.tdvv_setMediaByName(name);
		});
	}
	
	this.play = function()
	{
		var self = this;
		this._executeOnViewerReady(function()
		{
			self._viewer.tdvv_play();
		});			
	}
	
	this.stop = function()
	{
		var self = this;
		this._executeOnViewerReady(function()
		{
			self._viewer.tdvv_stop();
		});			
	}	
	
	this.setMovement = function(value)
	{
		var self = this;
		this._executeOnViewerReady(function()
		{
			self._viewer.tdvv_setMovement(value);
		});	
	}	
	
	this.beginZooming = function(value)
	{
		var self = this;
		this._executeOnViewerReady(function()
		{
			self._viewer.tdvv_beginZooming(value);
		});	
	}
	
	this.endZooming = function(value)
	{
		var self = this;
		this._executeOnViewerReady(function()
		{
			self._viewer.tdvv_endZooming(value);
		});	
	}		
		
	this.__init__();
}

TDVViewerAPI.MOVEMENT_STOP = "stop";
TDVViewerAPI.MOVEMENT_LEFT = "left";
TDVViewerAPI.MOVEMENT_RIGHT = "right";
TDVViewerAPI.MOVEMENT_UP = "up";
TDVViewerAPI.MOVEMENT_DOWN = "down";

TDVViewerAPI.ZOOMING_IN = "zooming in";
TDVViewerAPI.ZOOMING_OUT = "zooming out";

TDVViewerAPI.MEDIA_INDEX_CHANGE_EVENT = "mediaIndexChange";
TDVViewerAPI.MOVEMENT_CHANGE_EVENT = "movementChange";

tdvViewerAPI = new TDVViewerAPI();
