// Window sizing to content:
//
// htmlArea v3.0 - Copyright (c) 2002, 2003 interactivetools.com, inc.
// This copyright notice MUST stay intact for use (see license.txt).
//
// Portions (c) dynarch.com, 2003
//
// A free WYSIWYG editor replacement for <textarea> fields.
// For full source code and docs, visit http://www.interactivetools.com/
//
// Version 3.0 developed by Mihai Bazon.
//   http://dynarch.com/mishoo
//
// $Id: popup.js,v 1.8 2003/12/05 09:19:01 mishoo Exp $


function getAbsolutePos(el) {
	var r = { x: el.offsetLeft, y: el.offsetTop };
	if (el.offsetParent) {
		var tmp = getAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
};


// Rescale to content!

function RescaleToContent(bottom) {
	var body = document.body;
	var body_height = 0;
	if (typeof bottom == "undefined") {
		var div = document.createElement("div");
		body.appendChild(div);
		var pos = getAbsolutePos(div);
		body_height = pos.y;
	} else {
		var pos = getAbsolutePos(bottom);
		body_height = pos.y + bottom.offsetHeight;
	}
	if (body_height > 700) body_height = 700;
	if (!document.all) {
		window.sizeToContent();
		window.sizeToContent();	// for reasons beyond understanding,
								// only if we call it twice we get the
								// correct size.
		// center on parent
		var x = opener.screenX + (opener.outerWidth - window.outerWidth) / 2;
		var y = opener.screenY + (opener.outerHeight - window.outerHeight) / 2;
		window.moveTo(x, y);
		var body_width;
		body_width = body.offsetWidth + 5;
		if (body_width > 900) body_width = 900;
		window.innerWidth = body_width;
		window.innerHeight = body_height + 50;
	} else {
		var height,width;
		if (document.body.scrollHeight > document.body.offsetHeight) {
			width = document.body.scrollWidth;
			height = document.body.scrollHeight;
			} else {
			width = document.body.offsetWidth;
			height = document.body.offsetHeight;
			}
		window.resizeTo(width, height+55);		
		var W = body.offsetWidth;
		var H = 2 * body_height - height;
		var x = (screen.availWidth - W) / 2;
		var y = (screen.availHeight - H) / 2;
		window.moveTo(x, y);
	}    
}

//
// Get x- or y-coordinate of element
//

function elX(id) {
	var el = document.getElementById(id);	
	var pos = getAbsolutePos(el);
	return(pos.x);
	}
	
function elY(id) {
	var el = document.getElementById(id);
	var pos = getAbsolutePos(el);
	return(pos.y);
	}	


//
// Display a popup window
//

function NewWindow(mypagenm,myname,w,h,scroll){
  var win = null;
  LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
  TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
  settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable=yes';
  re = /(http:\/\/[^\/]+\/)([^?]+)(.*)/;
// split
  if (re.exec(mypagenm) === null) {
          parts = re.exec(window.location);
          if (parts !== null) {
                mypagenm = parts[1] + mypagenm;
                }
          }
  mypagenm = mypagenm.replace(/\/\//g, "/");            // remove double backslashes
  mypagenm = mypagenm.replace(/^http:\//, "http://");   // except the one near http...
  wn = window.open(mypagenm,myname,settings);
  if (wn.window.focus){wn.window.focus();}
  }


//
// Display video
//
function InsertMovie(image,movie,width,height)
{
  var id = Math.random()+'';  
  document.write("<div id='" + id + "'></div>");
  var host = "http://" + window.location.hostname;
  var so = new SWFObject('/scripts/player.swf','mpl',width,height,'9');
  so.addParam('allowscriptaccess','always');
  so.addParam('allowfullscreen','true');
  so.addParam('flashvars','&file=' + host + movie + '&image=' + host + image);
  so.write(id);
}
