var currentlyPlaying;
var currentPlaceHolder;
var autoClick;

var PreviewPlayer = Class.create(
{
	initialize: function(id, index)
	{
		this.id = id; 
		this.type = null; 
		this.index = index; 
		this.iteratorIndex = null; 
		
		this.autoClickID = null; 
		this.autoPlay = false; 
		
		this.currentRow = null; 
		this.originalRowHeight = null; 
		
		this.playerContainer = null; 
		this.originalContentHeight = null; 
		
		this.patterns = { 
			'numeric': new RegExp('^(\\d+)$', 'i'), 
			'trailingID': new RegExp('.*\\/([\\w|\\+]+)\\/$', 'i'), 
			'numericPageType': new RegExp('^.*\\/(\\w+)\\/\\d+\\/$', 'i'), 
			'doubleNumericPageType': new RegExp('^.*\\/(\\w+)\\/\\d+\\/\\d+\\/$', 'i'), 
			'stringPageType': new RegExp('^.*\\/(\\w+)\\/([a-zA-Z]+)\\/$', 'i'), 
			'trackID': new RegExp('.*\\/\\d{5,}\\/(\\d{5,})(\\/|)$', 'i'), 
			'screencastID': new RegExp('.*\\/\\w+\\/(\\d{1,})(\\/|)$', 'i') 
		}; 
		
		this.setPlayerContainer(); 
		if (this.playerContainer) 
		{
			this.setPageType(); 
			this.setIteratorIndex(); 
			this.performAutoClick(); 
		}
	},
	
	setPlayerContainer: function() 
	{
		if ($(this.id).down('.playerContainer')) 
		{
			this.playerContainer = $(this.id).down('.playerContainer'); 
		}
	},
	
	setPageType: function() 
	{
		if (window.location.href.replace(this.patterns.trailingID, '$1').match(this.patterns.numeric)) 
		{
			if (window.location.href.replace(this.patterns.numericPageType, '$1').match(this.patterns.numeric)) 
			{
				this.type = window.location.href.replace(this.patterns.doubleNumericPageType, '$1'); 
			}
			else this.type = window.location.href.replace(this.patterns.numericPageType, '$1'); 
		}
		else this.type = window.location.href.replace(this.patterns.stringPageType, '$1'); 
	}, 
	
	setIteratorIndex: function() 
	{
		if (dataIterators) 
		{
			dataIterators.each(function(item, index) 
			{
				if (!this.iteratorIndex && item.id == this.id) 
				{
					this.iteratorIndex = index; 
				}
			}, this); 
		}
	},
	
	performAutoClick: function() 
	{
		this.autoClickID = window.location.href.replace(this.patterns.trackID, '$1'); 
		if (this.autoClickID.match('http://')) 
		{
			this.autoClickID = window.location.href.replace(this.patterns.screencastID, '$1'); 
			
			if (this.autoClickID.match('http://') || this.type != 'screencast') 
			{
				this.autoClickID = null; 
			}
		}
		
		if (this.autoClickID != null && this.autoClickID != undefined && 
			$(this.autoClickID + 'Preview')) 
		{
			this.clickHandler(this.autoClickID + 'Preview', true); 
		}
	}, 
	
	clickHandler: function(itemID, autoScroll) 
	{
		KillTipPopup();
		playerIndex = this.index; 
		
		if ($(itemID)) 
		{
			var item = $(itemID); 
			var mediaType = item.className; 
			var mediaID = item.href.replace(this.patterns.trailingID, '$1'); 

			var row = item.up('tr'); 
			
			var buttonTops = row.select('td div.content div.buttonTop'); 
			if (buttonTops == null || buttonTops == undefined || buttonTops.length == 0) 
			{
				row.select('td div.content').each(function(itemContent)
				{
					itemContent.insert('<div class="buttonTop" />');
				});
			}
			
			var playerHeight = (mediaType == 'audio' ? 36 : 368);
			
			var rowHeight = row.getHeight(); 
			var newRowHeight = (rowHeight + playerHeight); 
			
			var content = row.up('div'); 
			var contentHeight = content.getHeight(); 
			var contentHeightDiff = (playerHeight + rowHeight) > contentHeight ? (playerHeight + rowHeight) - contentHeight : 0; 
			var newContentHeight = (contentHeight + contentHeightDiff); 
			
			var scrollbar = content.up('div').down('.scrollbarContainer'); 
			var scrollbarModifier = -30; 
			
			var useScrollbar = this.iteratorIndex && dataIterators[this.iteratorIndex] && dataIterators[this.iteratorIndex].scrollbar ? true : false; 
			
			var effects = new Array(); 
			if (this.currentRow && this.currentRow.id) 
			{
				var oldRow = this.currentRow; 
				if (this.currentRow.id != row.id) 
				{
					effects.push(new Effect.Morph(this.currentRow, { 'style': { 'height': this.originalRowHeight + 'px' } })); 
					effects.push(new Effect.Morph(this.currentRow.firstDescendant(), { 'style': { 'height': this.originalRowHeight + 'px' } })); 
					effects.push(new Effect.Morph(row, { 'style': { 'height': newRowHeight + 'px' } })); 
					effects.push(new Effect.Morph(row.firstDescendant(), { 'style': { 'height': newRowHeight + 'px' } })); 
					if (useScrollbar) 
					{
						effects.push(new Effect.Morph(scrollbar, { 'style': { 'height': (useScrollbar ? (newContentHeight + scrollbarModifier) : 0) + 'px' } })); 
						effects.push(new Effect.Morph(content, { 'style': { 'height': newContentHeight + 'px' } })); 
					}
					
					new Effect.Parallel(effects, 
					{
						duration: 0.3,
						beforeStart: function() { previewPlayers[playerIndex].unloadPlayer(oldRow); },
						afterFinish: function()
						{
							contentHeight = content.getHeight(); 
							previewPlayers[playerIndex].loadPlayer(row, rowHeight, contentHeight, contentHeightDiff, playerHeight, mediaID, mediaType); 
							previewPlayers[playerIndex].resetIteratorScrollbar(row); 
						}
					});
				}
				else
				{
					effects.push(new Effect.Morph(row, { 'style': { 'height': this.originalRowHeight + 'px' } }));  
					effects.push(new Effect.Morph(row.firstDescendant(), { 'style': { 'height': this.originalRowHeight + 'px' } })); 
					if (useScrollbar) 
					{
						effects.push(new Effect.Morph(content, { 'style': { 'height': this.originalContentHeight + 'px' } })); 
						effects.push(new Effect.Morph(scrollbar, { 'style': { 'height': (useScrollbar ? (this.originalContentHeight + scrollbarModifier) : 0) + 'px' } })); 
					}
					
					new Effect.Parallel(effects, 
					{
						duration: 0.3,
						beforeStart: function()
						{
							previewPlayers[playerIndex].unloadPlayer(oldRow); 
						},
						afterFinish: function() 
						{
							previewPlayers[playerIndex].resetIteratorScrollbar(oldRow); 
						}
					});
				}
			}
			else 
			{
				effects.push(new Effect.Morph(row, { 'style': { 'height': newRowHeight + 'px' } })); 
				effects.push(new Effect.Morph(row.firstDescendant(), { 'style': { 'height': newRowHeight + 'px' } })); 
				if (useScrollbar) 
				{
					effects.push(new Effect.Morph(content, { 'style': { 'height': newContentHeight + 'px' } })); 
					effects.push(new Effect.Morph(scrollbar, { 'style': { 'height': (useScrollbar ? (newContentHeight + scrollbarModifier) : 0) + 'px' } })); 
				}
				
				new Effect.Parallel(effects, 
				{
					duration: 0.3,
					afterFinish: function() 
					{ 
						previewPlayers[playerIndex].loadPlayer(row, rowHeight, contentHeight, contentHeightDiff, playerHeight, mediaID, mediaType); 
						previewPlayers[playerIndex].resetIteratorScrollbar(row); 
					}
				});
			}
		}
	}, 
	
	loadPlayer: function(row, rowHeight, contentHeight, contentHeightDiff, playerHeight, mediaID, mediaType) 
	{
		row.addClassName('selected'); 
		
		this.currentRow = row; 
		this.originalRowHeight = rowHeight; 
		if (!this.originalContentHeight) this.originalContentHeight = contentHeight; 
		
		var contentContainer = this.playerContainer.up('div'); 
		var contentContainerHeight = contentContainer.getHeight(); 
		
		var contentContainerOffset = contentContainer.cumulativeOffset(); 
		var rowOffset = row.cumulativeOffset(); 
		
		var playerPadding = { 'top': (mediaType == 'video' ? 12 : 2), 'left': 14 }; 
		var playerOffset = { 'top': 0, 'left': 0 }; 
		
		playerOffset.top = rowOffset.top - contentContainerOffset.top + rowHeight + playerPadding.top; 
//		playerOffset.left = rowOffset.left - contentContainerOffset.left + playerPadding.left; 
		playerOffset.left = playerPadding.left; 
		
		this.playerContainer.setStyle({
			'display': 'block', 
			'position': 'absolute', 
			'top': playerOffset.top + 'px', 
			'left': playerOffset.left + 'px', 
			'height': playerHeight 
		}); 
		
		this.playerContainer.update(new Element('div', { id: 'playerObject' + mediaID })); 
		
		flashvars = {
			productID: mediaID,
			productType: mediaType,
			previewAutoPlay: 'true', 
			screencast: (this.type == 'screencast' ? 'true' : 'false') 
		};
		
		params = {
			quality: 'high',
			menu: 'none',
			wmode: 'transparent',
			scale: 'noscale',
			salign: 'lt'
		};
		
		var playerWidth = ''; 
		playerWidth += (this.type == 'screencast' ? '550' : (contentContainer.getWidth() ? (contentContainer.getWidth() - (playerPadding.left * 2)) : '600')); 

		swfobject.embedSWF(
			'/w/flash/player.swf', 
			'playerObject' + mediaID,
			playerWidth, 
			(mediaType == 'video' ? '340' : '18'), 
			'9.0.0', '/w/flash/installer.swf',
			flashvars, params
		);
		
		getPageSize(); 
		var currentPosition = (window.pageYOffset ? window.pageYOffset : (document.documentElement.scrollTop ? document.documentElement.scrollTop : 1)); 
		
		var rowTop = (row.cumulativeOffset().top - row.cumulativeScrollOffset().top + document.viewport.getScrollOffsets().top); 
		var contentTop = contentContainer.cumulativeOffset().top; 
		
		var rowBottom = (row.cumulativeOffset().top - row.cumulativeScrollOffset().top + row.getHeight() + document.viewport.getScrollOffsets().top); 
		var contentBottom = contentContainer.cumulativeOffset().top + contentContainer.getHeight(); 
		
		if (this.iteratorIndex && dataIterators[this.iteratorIndex].scrollbar) 
		{
			if (rowTop < contentTop) 
			{
				dataIterators[this.iteratorIndex].scrollbar.scrollTo(row.positionedOffset().top); 
			}
			else if (rowBottom > contentBottom) 
			{
				var scrollBottomTo = Prototype.Browser.IE ? row.positionedOffset().top : row.positionedOffset().top + contentContainer.getHeight() - row.getHeight(); 
				dataIterators[this.iteratorIndex].scrollbar.scrollTo(scrollBottomTo); 
			}
		}
		
		rowBottom = (row.cumulativeOffset().top - row.cumulativeScrollOffset().top + row.getHeight() + document.viewport.getScrollOffsets().top); 
		contentBottom = contentContainer.cumulativeOffset().top + contentContainer.getHeight(); 
		
		if (rowBottom > (currentPosition + pageHeight)) 
		{
			window.scrollTo(0, currentPosition + (rowBottom - (currentPosition + pageHeight))); 
		}
	}, 
	
	resetIteratorScrollbar: function(row) 
	{
		if (this.iteratorIndex && dataIterators[this.iteratorIndex] && dataIterators[this.iteratorIndex].scrollbar) 
		{
			var contentContainer = row.up('div'); 
			var content = contentContainer.firstDescendant(); 
			
			if (contentContainer && content && 
				contentContainer.getHeight() >= content.getHeight()) 
			{
				dataIterators[this.iteratorIndex].scrollbar.disable(); 
			}
			else dataIterators[this.iteratorIndex].scrollbar.recalculateLayout(); 
		}
	},
	
	unloadPlayer: function(row) 
	{
		this.playerContainer.update(''); 
		this.playerContainer.setStyle({ 'display': 'none' }); 
		
		row.removeClassName('selected'); 
		this.currentRow = null; 
		this.originalRowHeight = null; 
	}
}); 

function playPreview(itemID) 
{
	previewPlayers[0].clickHandler(itemID, true); 
}

var previewPlayers = new Array(); 
Event.observe(window, 'load', function()
{
	var previewPlayerID = 'previewPlayer'; 
	if ($(previewPlayerID)) previewPlayers[0] = new PreviewPlayer(previewPlayerID, 0); 
});
