// -------------------------------------------------------------------
// This script has been developed by Solar Flare Studios.
//		http://www.solarflarestudios.com
// Unlike most scripts you will find on the web it is well documented so feel free to learn from it.
// If you wish to use it wholly or in part please do not remove this message.
// -------------------------------------------------------------------


// get the DIV that holds the large thumbnails
var playerDiv = document.getElementById('videoPlayerDIV');
var playerAncor = document.getElementById('videoPlayerAnchor');


function startVideo(videoName, videoCaption)
{

	playerDiv.style.left = findPosX(playerAncor);

	// figure out where to put the player so that it approximatley lines up with the top of the page without covering the nav bar
	var scrollHeight = getVPos();
	if (scrollHeight == 0)
	{
		playerDiv.style.top = findPosY(playerAncor);
	}
	else
	{
		playerDiv.style.top = getVPos() + 100;
	}
	

	
	// create the video player HTML
	var videoPlayer = 
		'<object type="application/x-shockwave-flash" data="FLVPlayer_Progressive.swf" width="320" height="240">'+
		'<param name="movie" value="FLVPlayer_Progressive.swf" />'+
		'<param name="bgcolor" value="#000000" />'+

		'<param name="salign" value="lt" />'+
		'<param name="quality" value="high" />'+
		'<param name="scale" value="noscale" />'+
		'<param name="FlashVars" value="&MM_ComponentVersion=1&skinName=Clear_Skin_3&streamName=videos/'+videoName+'.flv&autoPlay=true&autoRewind=true" />'+

		'<img src="images/flash_required.gif" width="320" height="240" alt="Flash Required To Play" />'+
		'</object>';
	
	
	playerDiv.innerHTML = 
		'<a href="#" onClick="closeVideo(); return false;">'+
		'<img src="images/close_OFF.gif" onMouseOver="document.playerClose.src=\'images/close_ON.gif\'" onMouseOut="document.playerClose.src=\'images/close_OFF.gif\'" name="playerClose" class="playerClose" border="0" /></a>'+
		'<p class="playerCaption">'+videoCaption+'</p>'+videoPlayer;
	
	return !fadeInV('videoPlayerDIV');
}

function closeVideo()
{
	var callback = function() { playerDiv.innerHTML = ''; }
		
	fadeOutV('videoPlayerDIV', callback);
}


/**
 * Fade in and object.
 * @param objId The ID of the object to fade in.
 * @param callback A function to call when finished.
 */
function fadeInV(objId, callback)
{
	var result = false;
	
    var obj = document.getElementById(objId);
	
	if (obj)
	{
		obj.style.visibility = 'visible';
		
		if (obj.interval != null)
		{
			window.clearInterval(obj.interval);
			obj.interval = null;
			if (obj.callback != null)
			{
				obj.callback();	
			}
		}
		
		obj.callback = callback;
		
		if (!obj.opacity)
		{
			obj.opacity = 0;
			setOpacityV(obj);
		}

		obj.hide = false;
		
		obj.interval = window.setInterval( function() { doFade(obj, 40); }, 50);
		
		result = true;
	}
	else
	{
		if (callback != null)
		{
			callback();
		}
		
		result = true;
	}
	
	return result;
}

/**
 * Fade out an object.
 * @param objID The ID of the object to fade out.
 * @param callback A function to call when finished.
 * @param hide true to hide the object when finished.
 */
function fadeOutV(objId, callback, hide)
{
	var result = false;
	
    var obj = document.getElementById(objId);
	
	if (obj)
	{
		if (obj.interval != null)
		{
			window.clearInterval(obj.interval);
			obj.interval = null;
			if (obj.callback != null)
			{
				obj.callback();	
			}			
		}
		
		obj.callback = callback;
		if (obj.hide != null)
		{
			obj.hide = hide;
		}
		else
		{
			obj.hide = false;	
		}
		
		if (obj.opacity)
		{
			obj.interval = window.setInterval( function() { doFade(obj, -20); }, 50);
		}
		
		result = true;
	}
	else
	{
		if (callback != null)
		{
			callback();
		}
		
		result = true;
	}
	
	return result;
}


/**
 * Function on an interval to perform the fade operation.
 * @param obj The object to fade.  Must have the properties: "opacity", "interval"
 * @param amount The amount to fade by.
 */
function doFade(obj, amount)
{
	obj.opacity += amount;
	
	if (obj.opacity > 100)
	{
		window.clearInterval(obj.interval);
		obj.interval = null;

		obj.opacity = 100;
	}
	else if (obj.opacity < 0)
	{
		window.clearInterval(obj.interval);
		obj.interval = null;				
		
		obj.opacity = 0;
	}
	
	if (obj.interval == null)
	{
		if (obj.callback != null)
		{
			obj.callback();	
		}
		
		if (obj.hide)
		{
			obj.style.visibility = 'hidden';
		}
		
	}
		
	setOpacityV(obj);
}

/**
 * Sets the opacity of the provided DOM object.
 * @param obj The object to set the opacity of.
 */
function setOpacityV(obj)
{
	var opacity = obj.opacity;
  
	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";

	// prevent divide by zero errors
	opacity = (obj.opacity == 100)?99.999:obj.opacity;

	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}


// locates the X positionn of an object based off of the top left corner
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

// locates the Y positionn of an object based off of the top left corner
function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function getVPos() {
	var scrolled;
	if (document.documentElement && document.documentElement.scrollTop) { scrolled = document.documentElement.scrollTop; } //Sniffing for IE5
	else if (document.body) { scrolled = document.body.scrollTop; } //Sniffing for IE6
	else { scrolled = window.pageYOffset; } //Sniffing for Netscape
	return scrolled; //Returning the variable
}
function getHPos() {
	var scrolled;
	if (document.documentElement && document.documentElement.scrollLeft) { scrolled = document.documentElement.scrollLeft; } //Sniffing for IE5
	else if (document.body) { scrolled = document.body.scrollLeft; } //Sniffing for IE6
	else { scrolled = window.pageXOffset; } //Sniffing for Netscape
	return scrolled; //Returning the variable
}

