
function initImageBrowser()
{
	objFile = new customFile();
	makeToolbarImage();
	addEvent(window, "resize", resizeBrowser);
}

function getNodeByTagName(domObj, strType) 
{
	child = domObj.childNodes;	
	for (var i = 0; i < child.length; i++) {
		if (child[i].nodeType != 3 &&
			child[i].tagName.toLowerCase() == strType)
				return child[i];
	}
}

function getNodeByClassName(domObj, strClass) 
{
	child = domObj.childNodes;	
	
	for (var i = 0; i < child.length; i++)
		if (child[i].nodeType != 3 &&
			child[i].className == strClass)
				return child[i];
}

function resizeBrowser()
{
	browser = getNodeByClassName(document.body, "browser");

	if (document.body.clientHeight)
		browser.style.height = document.body.clientHeight - 28;
	else if (window.innerHeight)
		browser.style.height = window.innerHeight - 28;
}

function customFile()
{
	var divs = document.getElementsByTagName("div");
	for (var i = 0; i < divs.length; i++) {
	
		switch (divs[i].className) {
			case "file":				
				inputFile = getNodeByTagName(divs[i], "input");
				addEvent(inputFile, "change", this.update);
				
				image = getNodeByTagName(divs[i], "img");
				
				if (navigator.appName == "Netscape")
					image.style.left = inputFile.clientWidth - image.width;
				else
					image.style.left = inputFile.clientWidth;
				
				addEvent(inputFile, "mouseover", this.iconOver);
				addEvent(inputFile, "mouseout", this.iconOver);
				addEvent(inputFile, "mousedown", this.iconOver);
			break;
			
			case "browser":
				resizeBrowser();
			break;
		}
	}	
}

customFile.prototype.update = function(e) {
	if (e.target)
		var target = e.target;
	else if (e.srcElement)
		var target = e.srcElement;

	form = getNodeByTagName(document.body, "form");
	form.submit();
}

customFile.prototype.iconOver = function(e) {

	if (e.target)
		var target = e.target;
	else if (e.srcElement)
		var target = e.srcElement;

	if (!target)
		return 0;
		
	image = getNodeByTagName(target.parentNode, "img");
	var	type = e.type;	
	
	if (!image)
		return 0;

	if (type.match("mouseover"))
		opacity(image, 0.5)
	else if (type.match("mouseout"))
		opacity(image, 1.5)
	else if (type.match("mousedown"))
		opacity(image, 0.3)
}

function wisiHideHeader(objImg, divId)
{
	if (wisi) {
		var divHeader = document.getElementById(divId);
	
		if (divHeader.style.display != "none") {
			divHeader.style.display = "none";
			objImg.src = "img/wisi/icons/bottom.png";
			
			var h = parseInt(divHeader.style.height) + parseInt(wisi.height)			
			wisi.height = h;
			
	 	} else {
			divHeader.style.display = "block";
			objImg.src = "img/wisi/icons/top.png";
			wisi.height = "250px";
		}			
	}
}

function setValueCommand(objSelect)
{
	var index = objSelect.selectedIndex;
	var size = objSelect.options[index].value;

	if (wisi &&
		size != 0 &&
		objSelect.name)
	{
		wisi.contentWindow.document.execCommand(objSelect.name, false, size);
		objSelect.selectedIndex = 0;	
	}	
	wisi.contentWindow.focus();
}

function wisiShowBrowser(intId)
{
  	pupupHeight = 400;
  	popupWidth = 320;
  
	var top = (screen.height / 2) - (pupupHeight / 2);
	var left = (screen.width / 2) - (popupWidth / 2);
  
	var attrs  = "top=" + top + ",";
		attrs += "left=" + left + ",";
		attrs += "width=" + popupWidth + ",";
		attrs += "height=" + pupupHeight + ",";
		attrs += "resizable=no,";
		attrs += "scrollbars=no,";
		attrs += "toolbar=no,";
		attrs += "location=no,";
		attrs += "directories=no,";
		attrs += "statusbar=no,";
		attrs += "menubar=no,";
		attrs += "copyhistory=no";

	window.open("lib/browser.php?id=" + intId, "browser", attrs);
}

function wisiSyncEdit(strInputId)
{
	if (wisi) {
		var content = document.getElementById(strInputId);
		alert(content.value);
	}
}

function wisiAddImage(strSrc)
{
	parentDocument = window.opener.document;
	wisi = parentDocument.getElementById("wisi");

	wisi.contentWindow.focus();
	wisi.contentWindow.document.execCommand("InsertImage", false, strSrc);
	window.close();
}

function wisiGetSelection()
{
	alert(wisi.contentWindow.getSelection());
}

function wisiDelImage(intId, strFile)
{
	form = getNodeByTagName(document.body, "form");
	form.action = "?id=" + intId + "&delete=" + strFile;
	form.submit();
}

function wisiBasicAction(cmd)
{
	if (wisi){
		wisi.contentWindow.document.execCommand(cmd, false, null);
		wisi.contentWindow.focus();
	}
}

function wisiSubmit(strInputId, strFormId)
{
	var content = document.getElementById(strInputId);
	var form = document.getElementById(strFormId);

	if (wisi && content) {
		var html = wisi.contentWindow.document.body.innerHTML;
		content.value = html;
	}

	if (form)
		form.submit();
}



