﻿// Quick initilization variables
var opacity = 75;
var opaque = (opacity / 100);
var bgcolor = '#16242F';
var zindex = 500;
var borderwidth = 10;
var borderstring = 'solid 1px #FFFFFF';
var playertype = "normal";

function showQTVideo(fileID, vidHeight, vidWidth, vidUrl){
	//if(!document.getElementById){
		if(navigator.userAgent.indexOf("Mac") >= 0){
			if(navigator.userAgent.indexOf("Safari") >= 0){
				playertype = "safari";
			}
			else if(navigator.userAgent.indexOf("Firefox") >= 0){
				playertype = "firefox";
			}
			else{
				// Opera has TONS of z-order and drawing issues.
				playertype = "othermac";
			}
		}
		if(playertype == "normal" || playertype == "safari"){
			dhtmlVideo(fileID, vidHeight, vidWidth, vidUrl, false);
			registerHit(fileID);
		}
		else if(playertype == "firefox"){
			dhtmlVideo(fileID, vidHeight, vidWidth, vidUrl, true);
			registerHit(fileID);
		}
		else{
			popupVideo(fileID);
		}
	/*}
	else{
		// Get a modern browser please. 
		location.href='/video/showvideo.aspx?fileID='+fileID;
	}*/
}

function popupVideo(fileID){
	popWindow('/video/videopopup.aspx?fileID='+fileID, 'videoPopup',100,100)
}

function popWindow() 
	{
	// new url,winname, width, height
	args=popWindow.arguments;
	//sizestr='width='+args[2];
	w=args[2];
	h=args[3];
	s=args[4];
	r=args[5];
	if (s=='scroll')
		{scrollbar=",scrollbars=yes";}
	else
		{
		scrollbar="";
		if (w>=screen.width)
			{ 
			w=screen.width-100; 
			scrollbar=",scrollbars=yes";
			}
		if (h>=screen.height)
			{ 
			h=screen.height-150; 
			scrollbar=",scrollbars=yes";
			}
		}
	if (r=='resize')
		{scrollbar+=",resizable=yes";}
	sizestr='width='+w;
	sizestr+=',height='+h;
	midx=(screen.width - w) / 2;
	midy=(screen.height - h) / 2;
	sizestr+=',left='+midx;
	sizestr+=',top='+midy;
	sizestr+=scrollbar;
	popupWin = window.open(args[0], args[1], sizestr)
}

function dhtmlVideo(fileID, vidHeight, vidWidth, vidUrl, fireFudge){
	var ttlWidth = vidWidth;
	var realX =  ((getPageSize()[2] - ttlWidth) / 2) - borderwidth;
	var realY = "50";
	
	var vo;
	var vid;
	
	switch(playertype){
		/*case "safari":
			vo = getSafariVideoPlayer();
			vo.style.display='block';
			vo.style.width=ttlWidth+"px";
			vo.style.height=(vidHeight+30)+"px";
			vo.style.top=realY+"px";
			vo.style.left=realX+"px";
			vid = document.getElementById("videoQTHolder");
			vid.style.width=vidWidth+"px";
			vid.style.height=(vidHeight+16)+"px";
			vid.scrolling='no';
			vid.src="";
			vid.src="/video/macfudge.aspx?fileID="+fileID+"&mode=safari&c=" + new Date().getTime();
			break;*/
		default:
			vo = getVideoPlayer(fireFudge);
			vo.style.display='block';
			vo.style.width=ttlWidth+"px";
			vo.style.height=(vidHeight+30)+"px";
			vo.style.top=realY+"px";
			vo.style.left=realX+"px";
			vid = document.getElementById("videoQTHolder");
			vid.style.width=vidWidth+"px";
			vid.style.height=(vidHeight+16)+"px";
			vid.innerHTML = generateQuickTime(vidWidth, vidHeight, vidUrl);
			break;
	}
	
}

function hideVideo(fireFudge){
	var vo = getVideoPlayer();
	vo.style.display='none';
	var vid = document.getElementById("videoQTHolder");
	vid.innerHTML = '';
}

function generateQuickTime(W, H, FileUrl) {
  var qt = "<object CLASSID=\"clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B\" width=\"" + W + 
     "\" height=\"" + (H+16) + "\" CODEBASE=\"http://www.apple.com/qtactivex/qtplugin.cab\" id=\"popupMovie\">\r\n" +
	 "<param name=\"src\" value=\"" + FileUrl + "\">\r\n" +
	 "<param name=\"autoplay\" value=\"true\">\r\n" +
	 "<param name=\"loop\" value=\"false\">\r\n" +
	 "<param name=\"volume\" value=\"100\">\r\n" +
	 "<param name=\"controller\" value=\"true\">\r\n" +
	 "<embed name=\"popupMovie\" src=\"" + FileUrl + "\" width=\"" + W + 
	 "\" height=\"" + (H+16) + "\" autoplay=\"true\" loop=\"false\" volume=\"100\" controller=\"true\"" +
	 "pluginspage=\"http://www.apple.com/quicktime/\"></embed>\r\n" +
	 "</object>";
  return qt;
}

function getAbsolutePosition(element) {
	var r = { x: element.offsetLeft, y: element.offsetTop };
	if (element.offsetParent) {
		var tmp = getAbsolutePosition(element.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
}

function getVideoPlayer(fireFudge){
	var player = document.getElementById("videoOverlay");
	if(player){
		return player;
	}
	else{
		var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');
        tnode.style.position='absolute';
        tnode.style.top='0px';
        tnode.style.left='0px';
        tnode.style.overflow='hidden';           
        tnode.style.display='none';
        tnode.id='videoOverlay';
				tnode.style.textAlign='center';
				if(!fireFudge){
					tnode.style.opacity=opaque;                      
					tnode.style.MozOpacity=opaque;                   
					tnode.style.filter='alpha(opacity='+opacity+')'; 
				}
				tnode.style.zIndex=zindex;        
				tnode.style.backgroundColor=bgcolor;
				tnode.style.margin=borderwidth+"px";
				tnode.style.border=borderstring;
				tnode.style.padding=borderwidth+'px';
		var vnode = document.createElement('div');
				vnode.id='videoQTHolder';
		tnode.appendChild(vnode);
		vnode = document.createElement('div');	
		vnode.style.textAlign='right';
		vnode.style.margin='5px';
		vnode.className='newstext';
		tnode.appendChild(vnode);
		var buttholder = document.createElement('span');
		buttholder.className='newsheader';
		buttholder.display='block';
		vnode.appendChild(buttholder);
		var closebutt = document.createElement('a');
		closebutt.href = 'javascript:hideVideo(' + fireFudge + ')';
		closebutt.display='block';
		closebutt.className='newsheader';
		closebutt.innerHTML = '[Close Video]';
		buttholder.appendChild(closebutt);
    tbody.appendChild(tnode);
		return tnode;
	}
}

function getSafariVideoPlayer(){
	var player = document.getElementById("videoOverlay");
	if(player){
		return player;
	}
	else{
		var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');
        tnode.style.position='absolute';
        tnode.style.top='0px';
        tnode.style.left='0px';
        tnode.style.overflow='hidden';           
        tnode.style.display='none';
        tnode.id='videoOverlay';
				tnode.style.textAlign='center';
				tnode.style.opacity=opaque;                      
				tnode.style.MozOpacity=opaque;                   
				tnode.style.filter='alpha(opacity='+opacity+')'; 
				tnode.style.zIndex=zindex;        
				tnode.style.backgroundColor=bgcolor;
				tnode.style.margin=borderwidth+"px";
				tnode.style.border=borderstring;
				tnode.style.padding=borderwidth+'px';
		var vnode = document.createElement('iframe');
				vnode.style.opacity=100;                      
				vnode.style.MozOpacity=100;                   
				vnode.style.filter='alpha(opacity=1)'; 
				vnode.id='videoQTHolder';
				vnode.scrolling='no';
		tnode.appendChild(vnode);
		vnode = document.createElement('div');	
		vnode.style.textAlign='right';
		vnode.style.margin='5px';
		vnode.className='newstext';
		tnode.appendChild(vnode);
		var buttholder = document.createElement('span');
		buttholder.className='newsheader';
		buttholder.display='block';
		vnode.appendChild(buttholder);
		var closebutt = document.createElement('a');
		closebutt.href = 'javascript:hideVideo(false)';
		closebutt.display='block';
		closebutt.className='newsheader';
		closebutt.innerHTML = '[Close Video]';
		buttholder.appendChild(closebutt);
    tbody.appendChild(tnode);
		return tnode;
	}
}


function registerHit(fileID){
	var url = "/video/videoincrement.aspx?fileID="+fileID;
	var ajaxRequest = null;
	
	function ajaxBindCallback(){
		if (ajaxRequest.readyState == 4) {
			if (ajaxRequest.status == 200) {
				// do nothing
			}
			else if(ajaxRequest.status == 500){
				alert("statistics update failed. :(");
			}
		}
	}
	
	if (window.XMLHttpRequest) {
		// moz et al
			ajaxRequest = new XMLHttpRequest();
			ajaxRequest.onreadystatechange = ajaxBindCallback;
			ajaxRequest.open("GET", url, true);
			ajaxRequest.send(null);
		} else if (window.ActiveXObject) {
		// ie
			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			if (ajaxRequest) {
			ajaxRequest.onreadystatechange = ajaxBindCallback;
			ajaxRequest.open("GET", url, true);
			ajaxRequest.send();
		}
	}
}

function getPageSize(){
	var pageWidth, pageHeight;
	var xScroll, yScroll;
	if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
		pageWidth = document.body.scrollWidth+'px';
		pageHeight = document.body.scrollHeight+'px';
	} else if( document.body.offsetWidth ) {
		pageWidth = document.body.offsetWidth+'px';
		pageHeight = document.body.offsetHeight+'px';
	} else {
		pageWidth='100%';
		pageHeight='100%';
	}
		
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
		
	var arr = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arr;
}