// JavaScript Document
//WIth help from the following sites:
//http://alexle.net/archives/169

var globalScopePrompt;
var globalTarget;

function customPrompt(type, divFront, divBack) {
	globalScopePrompt = this;
	this.dimDiv = divBack;
	this.txtDiv = divFront;
	this.type = type;
	this.timeCount = 0;
	this.t = 500;
	this.maxOpacity = 80;
	this.gotValue = null;
	this.doNotShowButton = null;
}
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}
function ieIntervalHandler()
{
	var scope = globalScopePrompt;
	var fade = 'fade';
	eval( "scope.fade()" );
}
function floatHandler(method, value)
{
	var scope = globalScopePrompt;
	eval( "scope.timeCount = 0" );
	eval( "scope.dir = 1" );
	(value)? eval( "scope."+method+"('"+value+"')" ) :eval( "scope."+method+"()" );
}



// Browser Window Size and Position
// copyright Stephen Chapman, 3rd Jan 2005, 8th Dec 2005
// you may copy these functions but please keep the copyright notice as well
function pageWidth() {return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ?       document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;} function pageHeight() {return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;} function posLeft() {return typeof window.pageXOffset != 'undefined' ? window.pageXOffset :document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;} function posTop() {return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;} function posRight() {return posLeft()+pageWidth();} function posBottom() {return posTop()+pageHeight();}


customPrompt.prototype.setFadeTime = function(milliseconds) { this.t = milliseconds; }
customPrompt.prototype.setOpacity = function(o) { (o <= 1)? this.maxOpacity = o*100 : this.maxOpacity = o; }
customPrompt.prototype.setFadeColor = function(hex) { this.dimDiv.style.background = hex; this.bgColorSet = true; }

customPrompt.prototype.init = function() {
	if(!this.bgColorSet) this.setFadeColor('#000');
	switch(this.type) {
		case 'link': //this will ask the user to input a string
			if(!this.title) this.title = 'Create a link';
			if(!this.message) this.message = 'Please enter address here!';
			innerHTML = '<table width="100%" border="0" cellspacing="0" cellpadding="5" style="opacity: 1;">' +
'  <tr>' +
'    <td colspan = "2" align="center"><h2>'+this.title+'</h2></td>' +
'  </tr>' +
'  <tr>' +
'    <td colspan = "2"><input type="text" size="45" value="'+this.message+'"  onfocus="this.value=\'\'" name="userInput" id="userInput"/>'+
'		<select name="target" onchange="globalTarget = this.options[this.selectedIndex].text">'+
'		<option value="">Same Window</option>'+
'		<option value"blank">New Window</option>'+
'		</select></td>' +
'  </tr>' +
'  <tr>' +
'    <td><input type="button" name="cancel" value="Cancel" onclick="floatHandler(\'fade\')" /></td>' +
'    <td align="right"><input type="button" name="accept" value="Accept" onclick="floatHandler(\'acceptLink\',document.getElementById(\'userInput\').value)" /></td>' +
'  </tr>' +
'</table>';
		break;
		case 'alert': //this will present information to a user
			if(!this.title) this.title = 'Wait!';
			innerHTML = '<table border="0" cellspacing="0" cellpadding="4">'
+'  <tr>'
+'    <td id="alertDragHandler" style="cursor:move;"><h2>'+this.title+'</h2></td>'
+'  </tr>'
+'  <tr>'
+'    <td>'+this.message+'</td>'
+'  </tr>'
+'  <tr>'
+'    <td>'+this.showOkButton()+'</td>'
+'  </tr>'
+'</table>';
			var iga = new Array(this.txtDiv, this.dimDiv);
		break;
		default: //this will be an alert where you have-to accept decline
			innerHTML = this.innerHTML;
		break;
	}
	this.userInput = document.getElementById('userInput');
	this.txtDiv.innerHTML = innerHTML;
	this.buildDimmerDiv();
	if(this.type == 'alert') makeDraggable(iga, document.getElementById('alertDragHandler'));
	this.fade();
}

customPrompt.prototype.validateInput = null;

customPrompt.prototype.getToolbarFloater = function() {
	return new Array(this.txtDiv, this.dimDiv);
}

customPrompt.prototype.setInnerHTML = function(html) {
	this.innerHTML = html;
}
customPrompt.prototype.setLifeTime = function(time) {
	this.lifetime = time;
}
customPrompt.prototype.resetFade = function(dir) {
	(dir)? this.dir = 1:this.dir = 0;
	this.timeCount = 0;
	this.innerHTML = null;
}

customPrompt.prototype.acceptLink = function(str) {
	target = (globalTarget == "New Window")? 'blank': null;
	(this.userInput)? null : this.userInput = document.getElementById('userInput');
	if(this.userInput.value.length) {
		//check to see if validation has been set
		if(this.validateInput) this.validateInput();
		str = this.gotValue = this.userInput.value;
		try { 
			if ((str!=null) && (str!="http://")) {
				if (mytext.document.selection.type=="None") {    	
					var sel=mytext.document.selection.createRange();
					sel.pasteHTML("<A HREF=\""+str+"\">"+str+"</A> ");
					sel.select();
				}		else 
				//If user had selected/blocked the string  just pass this command.
					RunCom("CreateLink",str);
			}			else 
			//If nothing entered just Focust in our IFRAME.
			mytext.focus();
		} catch(e) {
			//(sel)? null: alert("Sorry, but this feature works best in IE...");
			RunCom("CreateLink",str);
		}
	}
	this.fade();
}

customPrompt.prototype.showOkButton = function() {
	if(!this.doNotShowButton) return '<input type="button" name="accept" value="Ok">';
	return ' ';
}

customPrompt.prototype.buildDimmerDiv = function()
{
	this.dimOpacity = 0;
	this.txtOpacity = 100;
	if(document.all) {
		this.dimDiv.style.filter ="progid:DXImageTransform.Microsoft.Alpha(opacity="+this.dimOpacity+")";
		this.txtDiv.style.filter ="progid:DXImageTransform.Microsoft.Alpha(opacity="+this.txtOpacity+")";
		try { 
			this.dimDiv.filters.item("DXImageTransform.Microsoft.Alpha").opacity = this.dimOpacity;
			this.txtDiv.filters.item("DXImageTransform.Microsoft.Alpha").opacity = this.txtOpacity;
		} catch(e) {}
		setTimeout( 'ieIntervalHandler()', 75);
	} else {
		this.dimDiv.style.opacity = this.dimOpacity/100;
		this.txtDiv.style.opacity = this.txtOpacity/100;
		setTimeout(function(thisObj) { thisObj.fade(); }, 75, this);
	}
	offset = getScrollXY();
	
	if(!this.halign)  this.dimDiv.style.left = ((pageWidth() - this.txtDiv.offsetWidth)/2 + offset[0]) + 'px';
	else {
		if(this.halign == 'right') {
			this.dimDiv.style.left = ((pageWidth() - this.txtDiv.offsetWidth) - 25 - offset[0]) + 'px';
		} else if(this.halign == 'left') {
			this.dimDiv.style.left = (25 + offset[0]) + 'px';
		} else this.dimDiv.style.left = ((pageWidth() - this.txtDiv.offsetWidth)/2 + offset[0]) + 'px';
	}
	
	if(!this.valign) this.dimDiv.style.top = ((pageHeight() - this.txtDiv.offsetHeight)/2 + offset[1]) + 'px';
	else {
		if(this.valign == 'top') {
			this.dimDiv.style.top = (25 + offset[1]) + 'px';
		} else if(this.valign == 'bottom') {
			this.dimDiv.style.top = ((pageHeight() - this.txtDiv.offsetHeight) + 25 + offset[1]) + 'px';
		} else this.dimDiv.style.top = ((pageHeight() - this.txtDiv.offsetHeight)/2 + offset[1]) + 'px';
	}
	
	this.txtDiv.style.left = this.dimDiv.style.left; 
	this.txtDiv.style.top = this.dimDiv.style.top;
	this.dimDiv.style.width = this.txtDiv.offsetWidth + 'px'; 
	this.dimDiv.style.height = this.txtDiv.offsetHeight + 'px';
	this.dimDiv.style.visibility = 'visible';
	this.txtDiv.style.visibility = 'visible';
	this.dir = 0;
}



customPrompt.prototype.fade = function() {
	if(this.timeCount >= this.t) {
		(this.dir)? this.dimDiv.style.visibility = this.txtDiv.style.visibility = 'hidden' : null;
		if(this.lifetime) {
			this.dir = 1;
			this.timeCount = 0;
			if(document.all) {
				setTimeout( 'ieIntervalHandler()', this.lifetime);
			} else {
				setTimeout(function(thisObj) { thisObj.fade(); }, this.lifetime, this);
			}
			this.lifetime = null;
			return;
		}
		return;
	}
	this.timeCount += 75;
	if(!this.dir) {
		this.txtOpacity = (this.timeCount/this.t);
		this.dimOpacity = this.maxOpacity * this.txtOpacity;
	} else {
		this.txtOpacity = 1 - (this.timeCount/this.t);
		this.dimOpacity = this.maxOpacity * this.txtOpacity;
	}
	this.txtOpacity *= 100;
	if(document.all) {
		this.dimDiv.style.filter ="progid:DXImageTransform.Microsoft.Alpha(opacity="+this.dimOpacity+")";
		this.txtDiv.style.filter ="progid:DXImageTransform.Microsoft.Alpha(opacity="+this.txtOpacity+")";
		try { 
			this.dimDiv.filters.item("DXImageTransform.Microsoft.Alpha").opacity = this.dimOpacity;
			this.txtDiv.filters.item("DXImageTransform.Microsoft.Alpha").opacity = this.txtOpacity;
		} catch(e) {}
		setTimeout( 'ieIntervalHandler()', 75);
	} else {
		this.dimDiv.style.opacity = this.dimOpacity/100;
		this.txtDiv.style.opacity = this.txtOpacity/100;
		setTimeout(function(thisObj) { thisObj.fade(); }, 75, this);
	}
}

customPrompt.prototype.setTitle = function(str) {
	this.title = str;
}
customPrompt.prototype.setMsg = function(str) {
	this.message = str;
}
customPrompt.prototype.openFolder = function(dir) {
	if(!dir) return false;
	/*try //Internet Explorer
	  {
	  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	  xmlDoc.async="false";
	  try { xmlDoc.overrideMimeType('text/xml'); } catch(e) { }
	  xmlDoc.loadXML("backbone/files.php?url="+dir);
	  
	  }
	catch(e)
	  {
	  parser=new DOMParser();
	  try { xmlDoc.overrideMimeType('text/xml'); } catch(e) { }
	  xmlDoc=parser.parseFromString("backbone/files.php?url="+dir,"text/xml");
	  return xmlDoc;
	  }*/
}


