/*/////////////////////////////////////////////
This script copyright 2006 Steven Albert / Pulaski Networks, LLC

Usage:
launch('absolute/or/relative/path/to/filename.ext','width in pixels','height in pixels')

Examples of usage:
<a href="javascript:launch('file.ext','320','240')">link</a>
<a href="javascript:launch('../file.ext')">link</a>
<a href="javascript:launch('http://www.example.com/file.ext','640','480')">link</a>
<a href="#" onRelease="javascript:launch('file.ext','800','600')">link</a>

Filetypes accommodated:
AVI, GIF, JPEG, JPG, MID, MIDI, MP2, MP3, MPG, MPEG, MOV, PDF, PNG, WAV, WMV
* All other filetypes will be launched in the window in which the link was clicked, but any other filetype should simply prompt the user to Open/Save/Cancel.

Notes:
- Width and height must be specified in pixels if you do not want the window to be full-screen.
- The only exception to the above note is if you are launching an image file. The window will automatically resize to the dimensions of the image.
- The filetype PDF relies on the installation of Adobe Reader or other PDF viewer and its Web browser plug-in for in-browser viewing.
/////////////////////////////////////////////*/


function fittopicture(windowname) {
	if (window.innerWidth){
		iWidth = window.innerWidth;
		iHeight = window.innerHeight;
	} else {
		iWidth = document.body.clientWidth;
		iHeight =document.body.clientHeight;
	}
	iWidth = windowname.document.images[0].width - iWidth;
	iHeight = windowname.document.images[0].height - iHeight;
	windowname.resizeBy(iWidth, iHeight);
}


function launch(filepath,w,h) {
	var ext3 = filepath.substring(filepath.length - 3).toLowerCase();
	var ext4 = filepath.substring(filepath.length - 4).toLowerCase();
	var a = filepath.lastIndexOf("/") + 1;
	var filename = filepath.substring(a);
	delete a;

	if (ext3 == "mp2" || ext3 == "mp3" || ext3 == "wav" || ext3 == "mid") {
		var filetype = "audio";
	}

	if (!w) {
		if (filetype == "audio") {
			var w = '350';
		} else {
			var w = screen.width.toString();
			
		}
	} else {
		var w = parseInt(w).toString();
	}

	if (!h) {
		if (filetype == "audio") {
			var h = '200';
		} else {
			var h = screen.height.toString();
		}
	} else {
		var h = parseInt(h).toString();
	}




	if (ext3 == "avi" || ext3 == "wmv" || ext3 == "mpg" || ext4 == "mpeg" || filetype == "audio") {
		var h = parseInt(h) + 46;
		if (avwindow) {
			avwindow.close();
		}
		var avwindow = window.open('/blank.html','avwindow','width=' + w + ',height=' + h + ', toolbar=0,location=0,menubar=0,resizable=0');
		if (window.focus) {
			avwindow.focus();
		}
		avwindow.document.write('<html><head><title>' + filename + '</title></head><body><div style="width:100%;height:100%;margin:0px;padding:0px;text-align:center"><object id="media" CLASSID="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsm p2inf.cab#Version=6,4,7,1112" standby="Loading Microsoft Windows® Media Player components..." type="application/x-oleobject" width="' + w + '" height="' + h + '"><param name="fileName" value="' + filepath + '" /><param name="animationatStart" value="true" /><param name="transparentatStart" value="true" /><param name="autoStart" value="true" /><param name="showControls" value="true" /><param name="volume" value="-450" /><embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" src="' + filepath + '" name="media" width="' + w + '" height="' + h + '" autostart="true" showcontrols="true" volume="-450" /></object></div></body></html>');
		avwindow.document.body.style.marginTop = 0;
		avwindow.document.body.style.marginLeft = 0;
	}


	else if (ext3 == "pdf") {
		if (pdfwindow) {
			pdfwindow.top.frames['content'].location = "/blank.html";
			pdfwindow.close();
		}
		var pdfwindow = window.open('/blank.html','pdfwindow','width=' + w + ',height=' + h + ', toolbar=0,location=0,menubar=0,resizable=0');
		pdfwindow.document.write('<html><head><title>' + filename + '</title></head><frameset rows="0,*"><frame src="/blank.html" name="empty"><frame src="' + filepath + '" name="content"></frameset><noframes></noframes></html>');
		pdfwindow.document.body.style.marginTop = 0;
		pdfwindow.document.body.style.marginLeft = 0;
		if (window.focus) {
			pdfwindow.focus();
		}
		pdfwindow.moveTo(0,0);
	}


	else if (ext3 == "mov") {
		if (quicktimewindow) {
			quicktimewindow.close();
		}
		var quicktimewindow = window.open('/blank.html','quicktimewindow','width=' + w + ',height=' + h + ', toolbar=0,location=0,menubar=0,resizable=0');
		if (window.focus) {
			 quicktimewindow.focus();
		}
		quicktimewindow.document.write('<html><head><title>' + filename + '</title></head><body>');
		quicktimewindow.document.write('<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" height="' + h + '" width="' + w + '">');
		quicktimewindow.document.write('<param name="src" value="' + filepath + '" />');
		quicktimewindow.document.write('<param name="autoplay" value="true" />');
		quicktimewindow.document.write('<param name="type" value="video/quicktime" height="' + h + '" width="' + w + '" />');
		quicktimewindow.document.write('<embed src="' + filepath + '" height="' + h + '" width="' + w + '" autoplay="true" type="video/quicktime" pluginspage="http://www.apple.com/quicktime/download/" />');
		quicktimewindow.document.write('</object></body></html>');
		quicktimewindow.document.body.style.marginTop = 0;
		quicktimewindow.document.body.style.marginLeft = 0;
	}


	else if (ext3 == "jpg" || ext3 == "jpe" || ext4 == "jpeg" || ext3 == "gif" || ext3 == "png") {
		usescrollbars = false;
		if (imagewindow) {
			imagewindow.close();
		}
		if (!w || w == 0 || w > screen.width) {
			w = screen.width;
			usescrollbars = true;
		}
		if (!h || h== 0 || h > screen.height) {
			h = screen.height;
			usescrollbars = true;
		}
		if (usescrollbars) {
			var imagewindow = window.open('/blank.html','imagewindow','width=' + w + ',height=' + h + ',toolbar=0,location=0,menubar=0,resizable=1,scrollbars=1');
		} else {
			var imagewindow = window.open('/blank.html','imagewindow','width=' + w + ',height=' + h + ',toolbar=0,location=0,menubar=0,resizable=1,scrollbars=0');
		}
		if (window.focus) {
			imagewindow.focus();
		}
		var img = new Image();
		img.src = filepath;
		imagewindow.document.write('<html><head><title>' + filename + '</title></head><body><img src="' + filepath + '" border="0"></body></html>');
		imagewindow.document.body.style.marginTop = 0;
		imagewindow.document.body.style.marginLeft = 0;
		if (w == screen.width || h == screen.height) {
			imagewindow.moveTo(0,0);
		}
		//fittopicture(imagewindow);
	}


	else {
		window.location.href = filepath;
	}
}