RolloverImage = function(imageID, blurImage, focusImage)
{
	this.imageID = imageID;
	this.blurImage = blurImage;
	this.focusImage = focusImage;	
};

RolloverImage.prototype.init = function()
{
	this.imageElem = dojo.byId(this.imageID);
	dojo.event.connect(this.imageElem, "onmouseover", this, "onMouseOver");
	dojo.event.connect(this.imageElem, "onmouseout", this, "onMouseOut");
};
	
RolloverImage.prototype.onMouseOver = function()
{
  if (document.images)
  {
	this.imageElem.src = this.focusImage;
  }
};
	
RolloverImage.prototype.onMouseOut = function()
{
  if (document.images) 
  {
	this.imageElem.src = this.blurImage;
  }
};
function playerReady(p)
{
	var player = window.document[p.id];
	if (trellios.flashMovies[p.id]) trellios.flashMovies[p.id].player = player;
	trellios.flashMovies[p.id] = player;
}

dojo.event.connect(tapestry, "loadJson", function(type, data, http, kwArgs) {trellios.processMovieLink(type, data, http, kwArgs);});

var trellios = 
{
	rolloverImages : new Array(),
	flashMovies : new Array(),
	
	preloadImage : function(imageURL)
	{
		var img = new Image();
		img.src = imageURL;
	},
	
	addRolloverImage : function(imageID, blurImage, focusImage)
	{
		var rolloverImage = new RolloverImage(imageID, blurImage, focusImage);
		rolloverImage.init();
		this.rolloverImages.push(rolloverImage);
	},	
	
	initFlashMovie : function(movieID, soObject)
	{	
		this.flashMovies[movieID] = soObject;
		if (soObject.getVariable('file') && !this.flashMovies[movieID].isWritten)
		{
			dojo.debug("file: " + soObject.getVariable('file'));
			this.flashMovies[movieID].write(movieID);
			this.flashMovies[movieID].isWritten = true;			
		}
	},	
	
	updateFlashMovie : function(movieID, flvURL)
	{
		if (this.flashMovies[movieID] === undefined)
		{
			return;
		}
		var fm = this.flashMovies[movieID];
		
		fm.addVariable('file', flvURL);		
		fm.addVariable('autostart', 'true');
		if (flvURL)
		{		
			fm.write(movieID);
			fm.isWritten = true;			
		}
	},
	
	processMovieLink : function(type, data, http, kwArgs)
	{
		if (!data.processor)
		{
			return;
		}
		
		if (data.processor == "movieLink")
		{
			this.updateFlashMovie(data.movieID, data.url);			
		} else if (data.processor == "movieDialog")
		{
			this.updateFlashWindow(data);
		}
	},
	
	updateFlashWindow : function(data)
	{
		var movieUrl = data.url;
		var title = data.title || "";
		var dlg = this.movieDialog;
		if (!dlg)
		{
			dlg = new Ext.BasicDialog("movie-dialog", {
				autoCreate : true,
				title : title,
			    height: 422,
			    width: 501,
			    minHeight: 100,
			    minWidth: 150,
			    modal: true,
			    proxyDrag: true,
			    shadow: true,
				resizable : false,
				getPlayer : function()
				{
					if (!this.player) this.player = document.getElementById(this.playerID);
					return this.player;					
				}
			});
			
			
			var playerID = dlg.body.id; 
			dlg.body.name = playerID;
			var swfURL = this.getURL("/resources/player.swf");
			dlg.on("show", function()
			{
				;
				swfobject.embedSWF(
						swfURL,
						playerID,
						"480",
						"378",
						"7",
						false,
						{
							file:movieUrl, 
							autostart:"true"
						},
						{
							allowFullScreen : "true",
							autoStart : "true",
							wmode : "transparent"
						});				
			}, dlg, {single : true})							
			
			dlg.playerID = playerID;
			
			dlg.on("hide", function(){
				var player = this.getPlayer();
				this.getPlayer().sendEvent("STOP");
			}, dlg);			
			
			this.movieDialog = dlg;
		} else
		{	
			var player = dlg.getPlayer();
			if (player.sendEvent) player.sendEvent("LOAD", movieUrl);
			dlg.setTitle(title);			
		}
		dlg.show();			
	},
	
	getMovieLink : function(url)
	{
		tapestry.bind(url, {}, true);
	},
	
	squeezeSEO : function(val)
	{
		return "SO_" + val.replace(/ /g, "_");
	},
	
	squeeze : function(val, type)
	{
		if (type == "id")
		{
			return "l" + val;
		} else if (type == "string")
		{
			return "S" + val;
		}
		
		return val;
	},
	
	fireEvent : function(e)
	{
		//dojo.debug("Event Fired!");
	},
	
	addEventListener : function(eventName, listener)
	{		
		var tmp = 		
		{
			type : eventName, 
			delegate : listener,
			handleEvent : 	function(e) 
			{ 
				if (e.type == this.type) 
					this.delegate.apply(this, e.args);
			}
		}
		
		dojo.event.connect(this, "fireEvent", tmp, "handleEvent")
	},
	
	defineAjaxLink : function(linkID)
	{
		var link = Ext.Element.get(linkID);
		if (!link)
		{
			return;
		}
		
		link.on("click", function(e)
		{
			tapestry.bind(this.dom.href);		
			e.stopEvent();
			return false;			
		}, link);
	},
	
	ajaxLink : function(e)
	{
		var link = e.getTarget();
		tapestry.bind(link.link);		
		e.preventDefault();
	},
	
	getURL : function(contextRelative)
	{
		if (this.contextPath)
		{
			if (this.contextPath != "/")
			{
				return this.contextPath + contextRelative;			
			}
		}
		
		return contextRelative;
	}
};

trellios.ext = {};