/* JavaScript Document */

function getHTTPObject() {
	var xhr = false;
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				xhr = false;
			}
		}
	}
	return xhr;
}


function grabFile(file,viewer) {
	var request = getHTTPObject();
	if (request) {
		request.onreadystatechange = function() {
			parseResponse(request,viewer);
		};
		request.open("GET", file, true);
		request.send(null);
		return true;
	} else {
		return false;
	}
}


function parseResponse(request,viewer) {
	if (request.readyState == 4) {
		if (request.status == 200 || request.status == 304) {

			while (viewer.hasChildNodes()) {
				 viewer.removeChild(viewer.lastChild);
			}

			viewer.innerHTML = request.responseText;
		}
	}
}


function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}


function insertFlash() {	
	if (document.getElementById("flash_content")) {
		var flash_div = document.getElementById("flash_content");
	
		while (flash_div.hasChildNodes()) {
			flash_div.removeChild(flash_div.lastChild);
		}
	
		flash_div.innerHTML = "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0\" width=\"345\" height=\"174\"><param name=\"movie\" value=\"pic_viewer_small.swf\" /><param name=\"loop\" value=\"false\" /><param name=\"quality\" value=\"high\" /><embed src=\"pic_viewer_small.swf\" quality=\"high\" loop=\"false\" pluginspage=\"http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\" width=\"345\" height=\"174\"></embed></object>";
	}
}


function prepVideoLinks() {
  if (!document.getElementById || !document.getElementsByTagName) {
    return;
  }

  var i = 1;

  while (document.getElementById("vid_"+i)) {
	  document.getElementById("vid_"+i).getElementsByTagName("a")[0].onclick = function() {
		 var query = this.getAttribute("href").split("?")[1];
		 var url = "video_viewer.php?"+query;
		 return !grabFile(url,this.parentNode);
	  };
	  i++;
  }
 
}


addLoadEvent(insertFlash);
addLoadEvent(prepVideoLinks);
