function getPosition(obj) {
	var curleft = curtop = 0;
	var curwidth = obj.offsetWidth;
	var curheight = obj.offsetHeight;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return {"x":curleft,"y":curtop,"width":curwidth,"height":curheight};
}

function ajaxinit () {
  xmlobj = null;
  try {
    xmlobj = new XMLHttpRequest();
  } catch (e) {
    try {
      xmlobj = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      xmlobj = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlobj;
}

/*function playVideo(id) {
	if (videoWindow != null && !videoWindow.closed) {
		videoWindow.close();
	}
	videoWindow = window.open("/video.php?id="+id,'videoWindow','width=150,height=150,scrollbars=no');
}
var videoWindow;*/



function playVideo(id,refNode,e) {
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
	if (!myVideo.playing) {
		targ.innerHTML = "Stop video";
		myVideo.open(id,refNode);
	} else {
		targ.innerHTML = "Play video";
		myVideo.close();
	}
}
function stopVideo() {
	if (myVideo != null) {
		myVideo.close();
	}
	document.onmousedown = null;
}


var myVideo = new video();
var baseURL = "http://www.angellgallery.com/video_files/";

function video() {
	this.playing = false;
	this.player = null;
}
video.prototype.open = function(id,refNode) {
	this.playing = true;
	this.ref = refNode;
	
	var xmlobj = ajaxinit();
	if (xmlobj == null) return;
	var posi = getPosition(refNode);
	var plr;
	var thisObj = this;
	xmlobj.onreadystatechange = function () {
		if (xmlobj.readyState == 4) {
			var resp = xmlobj.responseXML.documentElement;
			if (resp && resp.getElementsByTagName("error").length == 0) {
				plr.src(baseURL+resp.getElementsByTagName("src")[0].firstChild.nodeValue);
				plr.dimensions(resp.getElementsByTagName("width")[0].firstChild.nodeValue,resp.getElementsByTagName("height")[0].firstChild.nodeValue);
				plr.open(posi.x,posi.y);
				/*closebutton.style.left = (Number(resp.getElementsByTagName("width")[0].firstChild.nodeValue) + 30)+"px";
				console.info("closebutton.style.left = "+closebutton.style.left);*/
			}
		}
	};
	var url = "/video/getDimensions.php?sid="+Math.random()+"&id="+id;
	if (this.player != null) {
		this.player.closePlayer();
	}
	this.player = new QTPlayer();
	plr = this.player;
	document.onmousedown = stopVideo;
	plr.element.onmousedown = stopPlayerBubble;
	/*var closebutton = document.createElement("button");
	closebutton.appendChild(document.createTextNode("x"));
	closebutton.onclick = stopVideo;
	plr.element.appendChild(closebutton);*/
	xmlobj.open("GET",url,true);
	xmlobj.send(null);
}
function stopPlayerBubble(e) {
	if (!e) var e = window.event;
	if (e.stopPropagation) {
		e.stopPropagation();
	} else if (e.cancelBubble) {
		e.cancelBubble();
	}
}
video.prototype.close = function() {
	this.playing = false;
	if (this.player != null) {
		this.player.closePlayer();
	}
	/*if (this.player != null && this.player.element != null && this.player.element.parentNode != null && this.ref != null) {
		this.player.element.parentNode.replaceChild(this.ref,this.player.element);
	}*/
}



function QTPlayer() {
	this.element = document.createElement("div");
	this.element.className = "videoPlayer";
	this.objectelement = document.createElement("object");
	this.element.appendChild(this.objectelement);
	this.objectelement.setAttribute("classid","clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B");
	this.objectelement.setAttribute("codebase","http://www.apple.com/qtactivex/qtplugin.cab");
	this.srcparam = document.createElement("param");
	this.srcparam.setAttribute("name","src");
	this.objectelement.appendChild(this.srcparam);
	var param2 = document.createElement("param");
	param2.setAttribute("name","autoplay");
	param2.setAttribute("value","true");
	this.objectelement.appendChild(param2);
	var param3 = document.createElement("param");
	param3.setAttribute("name","controller");
	param3.setAttribute("value","true");
	this.objectelement.appendChild(param3);
	this.embed = document.createElement("embed");
	this.embed.setAttribute("autoplay","true");
	this.embed.setAttribute("controller","true");
	this.embed.setAttribute("pluginspage","http://www.apple.com/quicktime/download/");
	this.objectelement.appendChild(this.embed);
}
QTPlayer.prototype.open = function(x,y) {
	this.element.style.left = x+"px";
	this.element.style.top = y+"px";
	document.getElementsByTagName("body")[0].appendChild(this.element);
}

QTPlayer.prototype.closePlayer = function() {
	if (this.element != null && this.element.parentNode != null) {
		this.element.parentNode.removeChild(this.element);
	}
}
QTPlayer.prototype.src = function(file) {
	this.srcparam.setAttribute("value",file);
	this.embed.setAttribute("src",file);
}
QTPlayer.prototype.dimensions = function(w,h) {
	this.objectelement.setAttribute("width",w);
	this.embed.setAttribute("width",w);
	this.objectelement.setAttribute("height",h);
	this.embed.setAttribute("height",h);
}


window.onload = function() {
	addPlayButtons();
	window.onresize = function() {
		for (var i=0; i<playButtons.length; i++) {
			playButtons[i].parentNode.removeChild(playButtons[i]);
		}
		addPlayButtons();
	}
}
function addPlayButtons() {
	playButtons = new Array();
	var imgs = document.getElementsByTagName("img");
	for (var i=0; i<imgs.length; i++) {
		if (imgs[i].hasAttribute("video")) {
			 playButtons.push(addPlayButton(imgs[i]));
		}
	}
}
function addPlayButton(refNode) {
	var id = refNode.getAttribute("video");
	var btn = document.createElement("button");
	var w = 100;
	var h = 30;
	btn.onclick = function(e) {
		if (!myVideo.playing) {
			myVideo.open(id,refNode);
		}
	};
	btn.appendChild(document.createTextNode("Play video"));
	btn.style.width = w+"px";
	btn.style.height = h+"px";
	btn.style.position = "absolute";
	var pos = getPosition(refNode);
	/*btn.style.left = (pos.x + (refNode.width/2 - w/2))+"px";
	btn.style.top = (pos.y + (refNode.height/2 - h/2))+"px";*/
	
	// Position the button in the top-right corner instead of centre
	btn.style.left = (pos.x + refNode.width - w)+"px";
	btn.style.top = pos.y+"px";
	
	document.getElementsByTagName("body")[0].appendChild(btn);
	return btn;
}
var playButtons = new Array();
