
function makeRequest(link) {
	url = link.href;
	var items = url.split('?id=');
	var vid_id = items[1];
	
	var id_array = vid_id.split('&vid=');
	var vim_id = id_array[0];
	var video_id = id_array[1];
	var domain = url.split('/')[2];
	var img_url = 'http://'+domain+'/xml_video.php?id='+vim_id+'&vid='+video_id+'';
	
	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		unLink();
		return false;
	}
	http_request.onreadystatechange = function() { alertContents(http_request, vid_id); };
	http_request.open('GET', img_url, true);
	http_request.send(null);
	return false;
}

function alertContents(http_request, vid_id){
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var xmldoc = http_request.responseXML;
			video_width = xmldoc.getElementsByTagName('width')[0].firstChild.data;
			video_height = xmldoc.getElementsByTagName('height')[0].firstChild.data;
			if (xmldoc.getElementsByTagName('description')[0].firstChild){
				video_description = xmldoc.getElementsByTagName('description')[0].firstChild.data;
			} else {
				video_description = '';
			}
			
			showVid(video_width, video_height, video_description, vid_id);
		}
	}	
}

function wfhRequest(link) {
	url = link.href;
	var items = url.split('?id=');
	var vid_id = items[1];
	
	var id_array = vid_id.split('&vid=');
	var vim_id = id_array[0];
	var video_id = id_array[1];
	var domain = url.split('/')[2];
	if ( domain.match('sixstair.com') == null ){
		var img_url = 'http://'+domain+'/sixstair/xml_video.php?id='+vim_id+'&vid='+video_id+'';
	} else {
		var img_url = 'http://'+domain+'/xml_video.php?id='+vim_id+'&vid='+video_id+'';
	}
	
	
	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		unLink();
		return false;
	}
	http_request.onreadystatechange = function() { wfhContents(http_request, vid_id); };
	http_request.open('GET', img_url, true);
	http_request.send(null);
	
	return false;
}


function wfhContents(http_request, vid_id){
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var xmldoc = http_request.responseXML;
			video_title = xmldoc.getElementsByTagName('title')[0].firstChild.data;
			video_width = xmldoc.getElementsByTagName('width')[0].firstChild.data;
			video_height = xmldoc.getElementsByTagName('height')[0].firstChild.data;
			if (xmldoc.getElementsByTagName('description')[0].firstChild){
				video_description = xmldoc.getElementsByTagName('description')[0].firstChild.data;
			} else {
				video_description = '';
			}
			
			workForHire(video_title, video_width, video_height, video_description, vid_id);
		}
	}	
}

function vimeoVid(width, height, description, vid_id)
{
	var video = document.createElement('object');
	video.setAttribute('width', width);
	video.setAttribute('height', height);
	video.setAttribute('type', 'application/x-shockwave-flash');
	video.setAttribute('data', 'http://vimeo.com/moogaloop.swf?clip_id='+vid_id+'&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=ffffff&fullscreen=1');
	
	var fullscreen = document.createElement('param');
	fullscreen.setAttribute('name', 'allowfullscreen');
	fullscreen.setAttribute('value', 'true');
	
	var scriptaccess = document.createElement('param');
	scriptaccess.setAttribute('name', 'allowscriptaccess');
	scriptaccess.setAttribute('value', 'always');
	
	var movie = document.createElement('param');
	movie.setAttribute('name', 'movie');
	movie.setAttribute('value', 'http://vimeo.com/moogaloop.swf?clip_id='+vid_id+'&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=ffffff&fullscreen=1');
	
	video.appendChild(fullscreen);
	video.appendChild(scriptaccess);
	video.appendChild(movie);
	
	return video;
}

function showVid(width, height, description, vid_id)
{

	var overlay = makeOverlay();

	var video = vimeoVid(width, height, description, vid_id);
	
	var holder = document.createElement('div');
	holder.setAttribute('id', 'video_content');
	
	holder.style.width = 640+'px';
	
	var close_img = new Image();
	close_img.setAttribute('title', 'Close');
	close_img.setAttribute('src', 'images/x.png');
	close_img.setAttribute('alt', 'Close');
	close_img.src = 'images/x.png';

	var close = document.createElement('p');
	close.setAttribute('id', 'close');
	close.appendChild(close_img);
	close.onclick = function(){
		document.body.removeChild(overlay);
		document.body.style.overflowY = 'auto';
	}
	holder.appendChild(close);
	
	holder.appendChild(video);
	
	//caption
	
	var captionText = document.createTextNode(description);
	var caption = document.createElement('p');
	caption.appendChild(captionText);
	holder.appendChild(caption);
	
	
	overlay.appendChild(holder);
	
	return false;
	
}

function workForHire(title, width, height, description, id)
{
	
	var overlay = makeOverlay();
	
	var div = document.createElement('div');
	div.setAttribute('id', 'wfh_vid');
	
	var close_img = new Image();
	close_img.setAttribute('id', 'close');
	close_img.setAttribute('title', 'Close');
	close_img.setAttribute('src', 'images/x.png');
	close_img.setAttribute('alt', 'Close');
	close_img.src = 'images/x.png';
	
	close_img.onclick = function(){
		document.body.removeChild(overlay);
	}
	
	div.appendChild(close_img);
	
	//
	var tab_img = new Image();
	tab_img.setAttribute('title', 'Work for Hire');
	tab_img.setAttribute('src', 'images/tabs/workhire.png');
	tab_img.setAttribute('alt', 'Work For Hire');
	tab_img.src = 'images/tabs/workhire.png';
	var para = document.createElement('p');
	para.setAttribute('id', 'tab');
	para.appendChild(tab_img)
	div.appendChild(para);
	
	var h1 = document.createElement('h1');
	h1.appendChild( document.createTextNode(title) );
	div.appendChild(h1);
	
	
	//the video
	var video = vimeoVid(width, height, description, id);
	var main_film = document.createElement('div');
	main_film.setAttribute('id', 'main_film');
	main_film.appendChild(video);
	div.appendChild(main_film);
	
	//the text
	var desc = document.createElement('div');
	desc.setAttribute('id', 'description');
	
	//make newlines into <br>
	var paras = description.split("\n\n");
	
	var p_num = paras.length;
	for ( var x=0; x < p_num; x++ ){
		var pgh = document.createElement('p');
		var pgh_text = paras[x];
		var lines = pgh_text.split("\n");
		line_num = lines.length
		if (line_num > 1){
			pgh_text = '';
			for ( var n=0; n < line_num; n++ ){
				pgh.appendChild( document.createTextNode(lines[n]) );
				pgh.appendChild( document.createElement('br') );
			}
		} else {
			pgh.appendChild( document.createTextNode(pgh_text) );	
		}
		
		desc.appendChild(pgh);
	}
	
	div.appendChild(desc);
	
	//clearing div
	var clearing = document.createElement('div');
	clearing.setAttribute('class', 'clearing');
	div.appendChild(clearing);
	
	overlay.appendChild(div);
	return false;
}