var jsReady = false;
var videoURL = '';
var swfObjectID = 'flashVideoPlayer';
var swfMovie;
var isOpen;
var lastClick = {x: 0, y: 0};


function easeInOut(minValue,maxValue,totalSteps,actualStep,powr) { 
//Generic Animation Step Value Generator By www.hesido.com 
    var delta = maxValue - minValue; 
    var stepp = minValue+(Math.pow(((1 / totalSteps) * actualStep), powr) * delta); 
    return Math.ceil(stepp) 
} 


function animateChange(elem,startX,startY,startW,startH,endX,endY,endW,endH,steps,intervals,powr) { 
	if (elem.changeMemInt)
	window.clearInterval(elem.changeMemInt);
    var actStep = 0;
    elem.changeMemInt = window.setInterval(
	function() { 
	  elem.currentRect.x = easeInOut(startX,endX,steps,actStep,powr);
	  elem.style.left = elem.currentRect.x + "px";
	  elem.currentRect.y = easeInOut(startY,endY,steps,actStep,powr);
	  elem.style.top = elem.currentRect.y + "px"; 
	  elem.currentRect.w = easeInOut(startW,endW,steps,actStep,powr);
	  elem.style.width = elem.currentRect.w + "px";
	  elem.currentRect.h = easeInOut(startH,endH,steps,actStep,powr);
	  elem.style.height = elem.currentRect.h + "px";
	  
	  var videoPlayerBG = document.getElementById('videoPlayerBG'); 
	  videoPlayerBG.style.width = elem.currentRect.w + "px";
	  videoPlayerBG.style.height = elem.currentRect.h + "px";
	  
	  actStep++;
	  if (actStep > steps) { 	
		window.clearInterval(elem.changeMemInt);
		if (isOpen) {
			document.getElementById('videoPlayerContent').innerHTML = swfObjectText();
			document.getElementById('videoPlayer').style.left = "50%";
		} else {
			document.getElementById('videoPlayer').style.visibility = "hidden";
			document.getElementById('videoPlayer').style.left = "50%";
			document.getElementById('videoPlayer').style.top = "75px";
		}
	  }
	} 
	,intervals)
}

function isReady() {
	return jsReady;
}

function swfObjectText() {
	var string = '';
	var reqestedVersionInstalled = DetectFlashVer(9, 0, 115);
	if (reqestedVersionInstalled) {  // hasRequestedVersion is a variable in the html document that loads this script
	string = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" name="flashVideoPlayer" width="660" height="478" id="flashVideoPlayer">'+
    '<param name="movie" value="http://valleynewschool.com/media/videoplayer.swf" />'+
    '<param name="quality" value="high" />'+
    '<param name="allowscriptaccess" value="always" />'+
    '<param name="enablejavascript" value="true" />'+
    '<param name="allownetworking" value="all" />'+
    '<param name="wmode" value="transparent" />'+
    '<embed src="http://valleynewschool.com/media/videoplayer.swf" width="660" height="478" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" allowscriptaccess="always" name="flashVideoPlayer" enablejavascript="true" allownetworking="all" wmode="transparent">'+
	'</embed></object>';
	} else {
		string = '<table cellpadding="75"><tr>'
		+'<td align="center" width="660" height="328">The video player requires Adobe Flash Player.  <a href="http://www.adobe.com/go/getflash/">Get Flash Here</a>. '
		+'If you do not wish to install Adobe Flash Player, <a href="' + videoURL + '">click here</a> to download the video.'
		+ '<br /><br /><a href="javascript:hideVideoPlayer()">Close</a></td>'
		+'</tr></table>';
	}
  return string;
}


function isIE () 
{
	return (navigator.appName.indexOf('Microsoft') != -1) ? true: false;	
}

function getVideoURL ()
{
	return videoURL;
}

function getSWF(movieName) {
	var movieObject;
	
	if (navigator.appName.indexOf('Microsoft') != -1) {
		movieObject = window[movieName];
	} else {
		if (document[movieName]) {
			if(document[movieName].length != undefined){
				movieObject = document[movieName][1];
			}
			movieObject = document[movieName];
		}
	}
	
	if (swfMovie == null) swfMovie = movieObject;
	
	return movieObject;
}

function showVideoPlayer (vURL, e)
{
	videoURL = vURL;
	
	var scrollTop;
	var scrollLeft;
	var docWidth
	var height;
	if (document.documentElement) {
		scrollTop = document.documentElement.scrollTop;
		scrollLeft = document.documentElement.scrollLeft;
		height = document.documentElement.clientHeight;
		docWidth = document.documentElement.clientWidth;
	} 
	if (document.body.scrollTop) {
		scrollTop = document.body.scrollTop;
		scrollLeft = document.body.scrollLeft;
		height = document.body.offsetHeight;
		docWidth = document.body.offsetWidth;
	}
	
	
	//lastClick.x = ((e.clientX+360)/docWidth)*100;
	lastClick.x = e.clientX + scrollLeft;
	lastClick.y = e.clientY + scrollTop;
	
	
	var newY = scrollTop + 75;
	if (newY < 160) newY = 160;
	
	
	var videoPlayer = document.getElementById('videoPlayer');
	
	if (!isOpen) {
		
		
		videoPlayer.style.visibility = "visible";
		
		if (!videoPlayer.currentRect) videoPlayer.currentRect = {x: 0, y: 0, w: 0, h: 0};
		animateChange(videoPlayer,lastClick.x,lastClick.y,0,0,document.body.clientWidth/2,newY,720,538,10,20,1.5);
		
		isOpen = true;
	} else {
		animateChange(videoPlayer,document.body.clientWidth/2,videoPlayer.currentRect.y,videoPlayer.currentRect.w,videoPlayer.currentRect.h,document.body.clientWidth/2,newY,720,538,10,20,1.5);
	}
	
	
	//if (!isIE()) loadQueuedVideo(vURL);
	
}

function hideVideoPlayer ()
{
	if (isOpen) {
		document.getElementById('videoPlayerContent').innerHTML = '';
		
		var videoPlayer = document.getElementById('videoPlayer');
		animateChange(videoPlayer,document.body.clientWidth/2,videoPlayer.currentRect.y,videoPlayer.currentRect.w,videoPlayer.currentRect.h,lastClick.x,lastClick.y,0,0,10,20,1.5);
		isOpen = false;
	}
	
}


function loadQueuedVideo(vURL)
{
	//swfObjectID should contain the id of the swf as is defined in the object tag (see HTML snippet below)
	var mySWFobject = getSWF(swfObjectID);
	if ( mySWFobject != null ) {
		if (mySWFobject.loadVideo != null) {
			mySWFobject.loadVideo(vURL);
		} else {
		setTimeout("loadQueuedVideo('"+vURL+"')",50);
		}
	} else {
		setTimeout("loadQueuedVideo('"+vURL+"')",50);
	}
}



function handleClick (e) {
	if (!e) e = event;
	
	var vURL = this.href;
		
	showVideoPlayer(vURL, e);
	
	//e.cancelBubble is supported by IE - this will kill the bubbling process.
	//e.cancelBubble = true;
	//e.returnValue = false;

	//e.stopPropagation works only in Firefox.
	if (navigator.appName.indexOf('Microsoft') == -1) {
		e.stopPropagation();
		e.preventDefault();
	}
	
	return false;
}

/* define function that adds another function as a DOM event listener */
function myAddListener(obj, evt, handler, captures)
{
	if (document.addEventListener) {
		obj.addEventListener(evt, handler, captures);
    } else {
        // IE
        obj.attachEvent('on' + evt, handler);
	}
}

/* define a single function that registers all listeners to call onload */
function SetupVideoPlayer (obj)
{
	document.getElementById('videoPlayer').style.width = '0px';
	document.getElementById('videoPlayer').style.height = '0px';
	jsReady = true;
	isOpen = false;
	
    var links = document.getElementsByTagName('a');
	
    for(var i = 0; i < links.length; i++){
        var rel = links[i].getAttribute('rel');
		
        if(rel == 'videoPlayer') {
			links[i].onclick = handleClick;
			//myAddListener(links[i], 'click', handleClick, false);
			//xb.addEvent(links[i], 'click', handleClick, false);
		}
	}
	
	//var arVersion = navigator.appVersion.split("MSIE")
	//var version = parseFloat(arVersion[1])

	//if (version < 7) 
	//{
	//	document.getElementById('videoPlayer').style.backgroundImage = 'url(http://valleynewschool.com/images/videoplayer/backgroundIE.png)';
	//	document.getElementById('videoPlayer').style.backgroundPosition = '27px 26px';
	//}
  
}