// lttnetsolutions.com displayFrame.js 2007/05/09
// Copyright (c) 2007 Han Lee lttnetsolutions.com  2007/05/09
// displayFrame.js is freely distributable / modify if this copyright notice remain intact

MyWindow = function (currDocument, url, title, width, height,modal) {
	// member variables
	this.currDocument = currDocument;
	this.modal = modal;
	this.width = 0;
	this.height = 0;
	this.transparent = null;
	this.frame = null;
	this.title = null; 
	this.dragactive = false;
	this.mouseX = 0; this.mouseXY = 0;
	this.target = new Array();
	var pos = 0;
	if(window.LTTWindowFrame)	
	{
		pos = window.LTTWindowFrame.length;
		window.LTTWindowFrame[pos] = this;
	}
	else
	{
		window.LTTWindowFrame = new Array();
		window.LTTWindowFrame[pos] = this;
	}	
	if(modal)
	{
		//create <div> - main container
		var tmpBG = currDocument.createElement("DIV");
  		var styleStr = 'position:absolute;background-color:black;left:0px;top:0px;filter:alpha (opacity=65);-moz-opacity: .65; z-index:'+(200+pos)+';width:100%;height:100%;';
		tmpBG.style.cssText = styleStr; 
		currDocument.body.appendChild(tmpBG);
		this.transparent = tmpBG;
	}	
	//create <div> - main container
    var left = Math.floor( (screen.width - width) / 2);
    var top = 50;
	var tmpDiv = currDocument.createElement("DIV");	
    var styleStr = 'position:absolute;z-index:300;padding:0px;';
	tmpDiv.style.cssText = styleStr; 
	tmpDiv.style.width = width + 'px';
	tmpDiv.style.height = height + 'px';
	tmpDiv.style.left = left + 'px';
	tmpDiv.style.top = top + 'px';
	
	var titleHeight = 18;
	// create <table> inside div
	var tmpTable = currDocument.createElement("TABLE");	
	var tmpBody = currDocument.createElement("TBODY");
	tmpTable.cellSpacing= 0 ;	tmpTable.width= '100%'; tmpTable.cellPadding= 0 ; 
    //Add  1px line for div to move
	var tmpRow = currDocument.createElement("tr");	
	var tmpColumn = currDocument.createElement("td");
	tmpColumn.style.height = '1px';
	tmpColumn.colspan = 2;
	tmpRow.appendChild(tmpColumn); 	
	tmpRow.appendChild(tmpColumn);	
	tmpBody.appendChild(tmpRow);	
	
	if(title)
	{
	//1st row store header
	tmpRow = currDocument.createElement("tr");	
	tmpRow.style.cssText = "height:" + titleHeight+"px;FONT-FAMILY: Tahoma;font-size:9px;color: white;font-weight:bold;";	
	tmpRow.bgColor = "#0080C0";	
	tmpColumn = currDocument.createElement("td");
	tmpColumn.style.width = (width - 25)+'px';
	tmpColumn.align = 'center'; tmpColumn.style.cursor = 'move';
	MyWindow.addEvent(tmpColumn, "mousedown",function(event){ LTTWindowDragActivate(event,pos); });
	tmpRow.appendChild(tmpColumn); 	
	tmpColumn.innerHTML = title;
	this.title=tmpColumn;
	tmpColumn = currDocument.createElement("td");
	tmpColumn.align = 'right';  tmpColumn.style.width = '25px';
	tmpColumn.innerHTML ="<a href='javascript:HideLTTWindowFrame(" + pos+");'><font color='white'>X</font></a>&nbsp; ";
	tmpRow.appendChild(tmpColumn);	
	tmpBody.appendChild(tmpRow);	
	} else titleHeight = 0;
	//create iframe
	var tmpIframe  = currDocument.createElement("iframe"); 
	if(url)
	{
		tmpIframe.setAttribute("src", url);
	} 
	else
	{
		if(document.all)
			tmpIframe.src = "javascript:false;";   // IE fixed long scroll
		else tmpIframe.src = "javascript:;";    // Firefox fixed frame display false
	}
	styleStr = 'border:0px;z-index:301';
	tmpIframe.style.cssText = styleStr;
	tmpIframe.style.width = width + 'px';

	tmpIframe.style.height = (height-titleHeight) + 'px';
	tmpIframe.scrolling = 'auto';	
	tmpIframe.frameBorder=0; 
	this.frame = tmpIframe;
	//attaches the parts together
	tmpTable.appendChild(tmpBody);
	tmpDiv.appendChild(tmpTable);
	tmpDiv.appendChild(tmpIframe);
	
	currDocument.body.appendChild(tmpDiv);
	this.mywindow = tmpDiv;
	//close frame first as default
	this.mywindow.style.visibility = 'hidden';
	if(this.modal)	
		this.transparent.style.display = 'none';	
};
MyWindow.prototype.Hide = function()
{
	if(document.all)
		this.mywindow.style.display = 'none';
	else
		this.mywindow.style.visibility = 'hidden';
	if(this.modal)	
	{	
		this.transparent.style.display = 'none';	
	}
	
};
MyWindow.prototype.Title = function(title)
{
	this.title.innerHTML = title;	
};
MyWindow.prototype.SetTarget = function(obj,index)
{  this.target[index] = obj; };
MyWindow.prototype.SetSize = function(width,height)
{
	this.mywindow.style.width = width + 'px';
	this.mywindow.style.height = height +'px';
	this.frame.style.width = width + 'px';
	this.frame.style.height = (height-18) + 'px';
};
MyWindow.prototype.Open = function()
{
	if(document.all)
	{
		if(this.mywindow.style.visibility == 'hidden')
		   this.mywindow.style.visibility = 'visible';
		this.mywindow.style.display = 'inline';
	}
	else
		this.mywindow.style.visibility = 'visible';
	if(this.modal)
		this.transparent.style.display = 'inline';
};
MyWindow.prototype.Url = function(url)
{
	this.frame.src = url;
};
// assign location
MyWindow.prototype.Location = function(x,y)
{
	 this.mywindow.style.left = x + "px";
	 this.mywindow.style.top = y + "px";	

};
// move x and y
MyWindow.prototype.Move = function(x,y)
{
	var diffX = parseInt(this.mywindow.style.left)+x; var diffY = parseInt(this.mywindow.style.top)+y;
	if(diffX > -1) this.mywindow.style.left = diffX + "px";
	if(diffY > -1) this.mywindow.style.top = diffY + "px";	
};
// HIDE frame general
function HideLTTWindowFrame(pos)
{
	window.LTTWindowFrame[pos].Hide();
}
//scrolling functions
function LTTWindowDragActivate(e,pos)
{
  var evt = e?e:event; // keep event to detect for ctr/alt
  var  mouseXPos=0;var  mouseYPos=0;
  var mouseclick =  navigator.appName=="Netscape"?evt.which:event.button; 
  var targetObj = document.all? evt.srcElement : evt.target;
   // if text use parent
  if (targetObj.nodeType == 3)
        dragObj = dragObj.parentNode;

	MyWindow.addEvent(window.LTTWindowFrame[pos].currDocument, "mouseup", function(event){ LTTWindowDragStop(event,pos); });
	MyWindow.addEvent(window.LTTWindowFrame[pos].currDocument, "mousemove", function(event){ LTTWindowDragMove(event,pos); }); 
   if (document.all) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
 	 }
 	 else
  	  evt.preventDefault();
  
   if(document.all)  //IE
   {
       mouseXPos = parseInt(window.event.x)+parseInt(document.body.scrollLeft);
       mouseYPos = parseInt(window.event.y)+parseInt(document.body.scrollTop);
	}
   else   // Netscape
   {
       mouseXPos = parseInt(evt.clientX )+parseInt(window.scrollX);
       mouseYPos = parseInt(evt.clientY )+parseInt(window.scrollY);
   }
  if(mouseclick)
  {
     window.LTTWindowFrame[pos].dragactive = true;   
     window.LTTWindowFrame[pos].mouseX = mouseXPos;   
     window.LTTWindowFrame[pos].mouseY = mouseYPos;   
  }	

}
function LTTWindowDragMove(e,pos)
{
  var evt = e?e:event; // keep event to detect for ctr/alt
  var  mouseXPos=0;var  mouseYPos=0; 
  var mouseclick =  navigator.appName=="Netscape"?evt.which:event.button; 
  var targetObj = document.all? evt.srcElement : evt.target;
    // if text use parent
  if (targetObj.nodeType == 3)
        dragObj = dragObj.parentNode;  
    if (document.all) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
 	 }
 	 else
  	  evt.preventDefault();

  if(!window.LTTWindowFrame[pos].dragactive ) return;
   if(document.all)  //IE
   {
       mouseXPos = parseInt(window.event.x)+parseInt(document.body.scrollLeft);
       mouseYPos = parseInt(window.event.y)+parseInt(document.body.scrollTop);
	}
   else   // Netscape
   {
       mouseXPos = parseInt(evt.clientX )+parseInt(window.scrollX);
       mouseYPos = parseInt(evt.clientY )+parseInt(window.scrollY);
   }
   var diffX = mouseXPos - window.LTTWindowFrame[pos].mouseX;
   var diffY = mouseYPos - window.LTTWindowFrame[pos].mouseY;
   if(mouseclick)
   {
	  window.LTTWindowFrame[pos].Move(diffX,diffY);	  
	  window.LTTWindowFrame[pos].mouseX = mouseXPos;	  
	  window.LTTWindowFrame[pos].mouseY = mouseYPos;	  
   }
}        

function LTTWindowDragStop(e,pos)
{
   window.LTTWindowFrame[pos].dragactive = false;
	MyWindow.removeEvent(window.LTTWindowFrame[pos].currDocument, "mouseup", function(event){ LTTWindowDragStop(event,pos); });
	MyWindow.removeEvent(window.LTTWindowFrame[pos].currDocument, "mousemove", function(event){ LTTWindowDragMove(event,pos); });    
   if (document.all) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
   }
   else
     e.preventDefault();		
}       
MyWindow.addEvent = function(el, evname, func) {
	if (el.attachEvent) { // IE
		el.attachEvent("on" +evname, func);
	} else if (el.addEventListener) { // Gecko / W3C
		el.addEventListener(evname, func, true);
	} else {
		el[evname] = func;
	}
};
MyWindow.removeEvent = function(el, evname, func) {
	if (el.detachEvent) { // IE
		el.detachEvent("on" +evname, func);
	} else if (el.removeEventListener) { // Gecko / W3C
		el.removeEventListener(evname, func, true);
	} else {
		el[evname] = null;
	}
};
MyWindow.getAbsolutePos = function(el) {
	var SL = 0, ST = 0;
	var is_div = /^div$/i.test(el.tagName);
	if (is_div && el.scrollLeft)
		SL = el.scrollLeft;
	if (is_div && el.scrollTop)
		ST = el.scrollTop;
	var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
	if (el.offsetParent) {
		var tmp = MyWindow.getAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
};