// -----------------------------------------------------------------------------
// Globals
// Major version of Flash required
var requiredMajorVersion = 9;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Minor version of Flash required
var requiredRevision = 0;
// -----------------------------------------------------------------------------

var mIsVisible        = false;
var mVideoDataRequest = null;

var videoHashToLoad   = null;
var videoIdToLoad     = null;
var thumbIdToLoad     = null;
var loadedBefore      = false;
var videoListOffset   = 0;
var videoOffset       = 0;

function watchVideo(aVideoHash, aVideoId, aVideoViews, aThumbId, aBabeName, aBabePageLink, aTime)
{
	videoHashToLoad = aVideoHash;
	videoIdToLoad   = aVideoId;
	thumbIdToLoad   = aThumbId;
	
	initialOffset   = $('videothumb' + videoIdToLoad).cumulativeOffset().top - ($('videothumb' + videoIdToLoad).getHeight() + parseInt($('videothumb' + videoIdToLoad).getStyle('marginTop')) + parseInt($('videothumb' + videoIdToLoad).getStyle('marginBottom')));
	videoListOffset = $('videolist').cumulativeOffset().top;

	if (!mIsVisible)
	{
		$('freeOnesVideoPlayer').hide();
		Effect.BlindDown('videocontainer', 
			{
				duration: mBlindSpeed, 
				afterUpdate: function()
				{
					$('videolist').setStyle({height: $('tubecontent').getHeight() - 43 - $('videocontainer').getHeight() + "px"});
					$('videolist').scrollTop = initialOffset - videoListOffset;
				},
				afterFinish: function()
				{
					$('freeOnesVideoPlayer').show();
					$('videolist').scrollTop = initialOffset - videoListOffset;
				} 
			});
		mIsVisible = true;
	}
	
	$('videobabename').update(aBabePageLink.length > 0 ? "<a href='"+aBabePageLink+"'>"+aBabeName+"</a>" : aBabeName);
	$('affiliatename').update(aBabeName);
	
	$('videoviews').update(aVideoViews);
	$('videotime').update(aTime);
	
 	mVideoDataRequest = new Ajax.Request("/video/"+aVideoId+'.html', 
 	{
		method    : 'get',
		evalJSON  : true,
		onSuccess : function(aTransport)
		{
			if (aTransport.statusText == "OK") 
			{
				var lVideoData = aTransport.responseJSON;
				$('screenshotsHolder').update('');

				$('downloadlink').href = lVideoData['downloadlink'];
				$('websitelink').href  = lVideoData['websitelink'];

				for (var i = 0, j = lVideoData['screenshots'].length; i < j; i++)
				{
					var lImagePath = m110ThumbnailPath.replace("%gh%", mGHPrefix + (Math.round(Math.random() * mGHAmount) + 1));

					var lScreenshotHolder = new Element("div");
					var lScreenshot       = new Element("img", { title: aBabeName + ' Video Screenshots', border: 0, src: lImagePath + '/' + aVideoId + lVideoData['screenshots'][i], height: 83, width: 110 } );

					lScreenshotHolder.insert(lScreenshot);
					$('screenshotsHolder').insert(lScreenshotHolder);
				}
			}
		}
	});
	if (function_exists($('freeOnesVideoPlayer').setVideoThumb)) { 
		setProperties(videoHashToLoad, videoIdToLoad, thumbIdToLoad);
	}
}

function function_exists (function_name) {
    if (typeof function_name == 'string'){
        return (typeof this.window[function_name] == 'function');
    } else{
        return (function_name instanceof Function);
    }
}

function setProperties(aVideoHash, aVideoId, aThumbId)
{
	$('freeOnesVideoPlayer').setVideoThumb(aVideoId, aThumbId);
	$('freeOnesVideoPlayer').setVideoId(aVideoHash);
}

function playerLoaded()
{
	loadedBefore = true;
	setProperties(videoHashToLoad, videoIdToLoad, thumbIdToLoad);
}

function closeVideo()
{
	$('freeOnesVideoPlayer').setStyle({display: 'none'});
	Effect.BlindUp('videocontainer', 
		{
			duration: mBlindSpeed,
			afterUpdate: function(){
				$('videolist').setStyle({height: ($('tubecontent').getHeight() - 43) + $('videocontainer').getHeight() + "px"});
			}
		});
	mIsVisible   = false;
	loadedBefore = false;
}