// Pop up box to choose from a list of items, such as images.

function chooser() {
	this.hazeLvl = 50;
}

chooser.prototype.box = function() {
var ot = '';
ot += '<style>\
	.chooserBtn {\
	background-color:transparent;\
	position:absolute;\
	font-size:20px;\
	z-index:11;\
	cursor:pointer;\
	}\
	</style>';
ot += myBox.top('chooserWindow');
ot += '<div class="chooserBtn" id="chooserHide" onclick="myChooser.hide()"><img src="'+om+'/img/close15.png" width="15" height="15" border="0" onmouseover="myBox.mouseOn(this)" onmouseout="myBox.mouseOff(this)"></div>';
ot += '<div style="width:1px;height:24px"></div>';
ot += '<b><i>Enter the address of the webpage that has the image on it</i></b><br>';
ot += '<input id="om.input.remotePage" type="text" size="60" name="remotePage" value=""/>';
ot += '<input type="button" name="getRemote" value="get images" onclick="myChooser.get(\'om.input.remotePage\')"/>';
ot += '<div id="chooserBox"></div>';
ot += '<div id="sandBox" style="position:absolute;top:-10000px;left:-10000px;width:1px;height:1px;visibility:hidden"></div>';
ot += myBox.end();
return ot;
}

chooser.prototype.show = function(o,hazeLvl) {
	this.hazeLvl = hazeLvl ? hazeLvl : 50;
	this.ICaller = o;
	myBox.hazeOn(this.hazeLvl);
	if (!this.content) {
		this.content = document.getElementById('chooserBox');
		this.sandbox = document.getElementById('sandBox');
	}
	var d = myBox.dim(this.content);
	var p = myBox.centerPlace(50,d[0]);
	var el = myBox.showById('chooserWindow',p[0],p[1]);
	el.placeGadget('chooserHide',12,12);
}

chooser.prototype.hide = function() {
	myBox.hazeOff();
	myBox.hideById('chooserWindow');
}

chooser.prototype.reposition = function() {
	var d = myBox.dim(this.content);
	var p = myBox.centerPlace(50,d[0]);
	myBox.position('chooserWindow',p[0],p[1]);
}

chooser.prototype.get = function(id) {
	try {
		var el = document.getElementById(id);
		var u = el.value;
		if (u.substr(0,7) != 'http://' && u.substr(0,8) != 'https://') u = 'http://' + u;
		if (el.value.length > 0) myRemote2.get(oa+'/do.browse.php?url=' + escape(u),this);
	} catch (ex) {
		dbg.error("Err:chooser.get: " + ex);
	}
}

chooser.prototype.IGetRemoteCallback = function(txt) {
	try {
		this.sandbox.innerHTML = txt;
		var arr = this.sandbox.getElementsByTagName('img');
		var ot = '';
		ot += '<hr>';
		if (arr.length > 0) {
			ot += '<h3><i>Please click on the image you wish to use.</i></h3>';
			for (var i = 0;i<arr.length;i++) {
				ot += '<div style="padding:1px 0px 5px 1px"><img src="' + arr[i].src + '" onclick="myChooser.setChoice(this)" border="1"/></div>';
			}
		} else {
			ot += "<h3><i>page does not exist or has no images</i></h3>";
		}
		this.sandbox.innerHTML = '';
		this.content.innerHTML = ot;
		this.reposition();
		myBox.hazeOn(this.hazeLvl);
	} catch (ex) {
		dbg.error("Err:chooser.IGetRemoteCallback: " + ex);
	}
}

chooser.prototype.setChoice = function(el) {
	this.hide();
	if (this.ICaller && typeof(this.ICaller.ISetChoice) == 'function') {
		this.ICaller.ISetChoice(el.src);
	}
	else dbg.error("Err:chooser.setChoice: missing ICaller.ISetShoice");
}

if (window.myAll) {
	window.myChooser = new chooser();
	document.write(myChooser.box());
	if (window.dbg) dbg.trace("sucessfully loaded lib.chooser.js");
}

