/*********************************************************************************
Copyright (C) 2007  Ryan Bowman

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

For any questions or comments contact ryan at fiddlerelf dot com
**********************************************************************************/

listViewId = 0;
uniqueId = 0;

function WXListView()
{

this.LVPREFIX = "LV";

this.htmlRoot;
this.xmlDoc;
this.listViewId = listViewId++;
this.xmlDocImages = getDocument("xml/imageicons.xml");
this.uniqueIdName = "id";
this.collapsedFolderImage = "folder_closed.png";
this.unknownFileImage = "unknown.png";
this.lookAndFeel = MODERN;

this.populateListFromNode = LVpopulateListFromNode;
this.setHtmlListRoot = LVsetHtmlListRoot;
this.createListNode = LVcreateListNode;
this.getListViewId = LVgetListViewId;
this.listNodeDoubleClicked = LVlistNodeDoubleClicked;
this.listNodeDoubleClickedExt = LVlistNodeDoubleClickedExt;
this.getImageIcon = LVgetImageIcon;
this.getNormalizedNodeId = LVgetNormalizedNodeId;
this.convertIdToListViewId = LVconvertIdToListViewId;
this.getXmlElementById = LVgetXmlElementById;
this.setXmlDocument = LVsetXmlDocument;
this.getToolTipInfo = LVgetToolTipInfo;
this.setListViewId = LVsetListViewId;
this.getFileNameFromNode = LVgetFileNameFromNode;
this.buildToolTipHtml = LVbuildToolTipHtml;
this.getResizeImageDimensions = LVgetResizeImageDimensions;
this.clearList = LVclearList;
this.getCollapsedFolderImage = LVgetCollapsedFolderImage;
this.setLookAndFeel = FCsetLookAndFeel;
this.getImageDirectory = FCgetImageDirectory;

}

/*
function LVsetLookAndFeel(lookAndFeel)
{
	this.lookAndFeel = lookAndFeel;
}
*/

function LVgetCollapsedFolderImage()
{
	return this.getImageDirectory() + this.collapsedFolderImage;
}

/*
function LVgetImageDirectory()
{
	if (this.lookAndFeel == MODERN)
		return MODERNLOOKANDFEELDIRECTORY;
	else
		return OLDSCHOOLLOOKANDFEELDIRECTORY;
}
*/

function LVsetXmlDocument(xmlDoc, uniqueIdName)
{
	this.xmlDoc = xmlDoc;
	this.uniqueIdName = (!uniqueIdName ? "id" : uniqueIdName);
}

function LVgetXmlElementById(nodeId)
{
	var nodes = this.xmlDoc.selectNodes("//*[@id = '" + nodeId + "']");
	return nodes[0];
}

function LVgetNormalizedNodeId(htmlNode)
{
	var id = htmlNode.getAttribute("id");
	var prefix = this.listViewId + "_" + this.LVPREFIX;
	return id.subtring(prefix.length, id.length);
}

function LVconvertIdToListViewId(id)
{
	return this.listViewId + "_" + this.LVPREFIX + id;
}

function LVgetListViewId()
{
	return this.listViewId;
}

function LVsetListViewId(id)
{
	this.listViewId = id;
}

function LVpopulateListFromNode(xmlNode)
{
	this.clearList();
	nodeId = xmlNode.getAttribute(this.uniqueIdName);
	var currentXmlNode = xmlNode;
	var children = currentXmlNode.childNodes;
	for (var i = 0; i < children.length; i++)
	{
		if (children[i].nodeType == 1)
		{
			var listNode = this.createListNode(children[i]);
			this.htmlRoot.appendChild(listNode);
		}	
	}
}

function LVclearList()
{
	while (this.htmlRoot.firstChild) 
	{
		this.htmlRoot.removeChild(this.htmlRoot.firstChild);
	}
}

function LVsetHtmlListRoot(htmlNode)
{
	this.htmlRoot = htmlNode;
}

function LVgetFileNameFromNode(xmlNode)
{
	var name = xmlNode.getAttribute("name");
	if (name.length > 25)
		name = name.substr(0, 22) + "...";
	return name;
}

function LVcreateListNode(xmlNode)
{
	var nodeId = xmlNode.getAttribute(this.uniqueIdName);

	// <div>
	var divEl = document.createElement("div");
	divEl.setAttribute("id", this.convertIdToListViewId(nodeId));
	divEl.className = "listnode";
	
	// <nobr>
	var noBREl = document.createElement("nobr");
	
	// <img>
	var imgIconEl = document.createElement("img");
	imgIconEl.className = "listnode";
	var imgIconFile
	if (xmlNode.nodeName == "directory")
		imgIconFile = this.getCollapsedFolderImage();
	else
		imgIconFile = this.getImageIcon(xmlNode);
		//imgIconFile = "images/3GP.png";
	imgIconEl.setAttribute("src", imgIconFile);
	imgIconEl.setAttribute("id", this.listViewId + "_LVI" + nodeId);
	
	// <span>
	var spanEl = document.createElement("span");
	spanEl.className = "listnode";
	//spanEl.innerHTML = xmlNode.getAttribute("name");
	spanEl.innerHTML = this.getFileNameFromNode(xmlNode);
	spanEl.setAttribute("id", this.listViewId + "_LVS" + nodeId);
		
	// <a>
	var aEl = document.createElement("a");
	aEl.className = "listnode";
	aEl.setAttribute("target", "_blank");
	aEl.setAttribute("id", this.listViewId + "_LVL" + nodeId);
	if (xmlNode.nodeName != "directory")
	{
		aEl.href = xmlNode.getAttribute("link");
	}
	else
	{
		aEl.href = "";
		var returnFalseFx = returnFalse;
		var addClickedFxBody = "listNodeDoubleClicked(" + nodeId + ", " + this.listViewId + ", '" + "test" + "');";
		var addClickedFx = new Function(addClickedFxBody);
		normAddEvent(aEl, "click", returnFalseFx);
		normAddEvent(aEl, "click", addClickedFx);	
	}
	var showToolTipFx = showToolTip;
	var hideToolTipFx = hideToolTip;
	var setToolTipInfo = setToolTipInfo;
	var setToolTipInfoFxBody = "setToolTipInfo(" + nodeId + ", " + this.listViewId + ", 'listview');";
	var setToolTipInfoFx = new Function(setToolTipInfoFxBody);
	var stopPropFx = clickedStopPropogation;
	normAddEvent(aEl, "mouseover", clickedStopPropogation);
	normAddEvent(imgIconEl, "mouseover", clickedStopPropogation);
	normAddEvent(spanEl, "mouseover", clickedStopPropogation);
	normAddEvent(aEl, "mouseout", clickedStopPropogation);
	normAddEvent(imgIconEl, "mouseout", clickedStopPropogation);
	normAddEvent(spanEl, "mouseout", clickedStopPropogation);
	
	normAddEvent(divEl, "mouseover", clickedStopPropogation);
	normAddEvent(spanEl, "mouseover", setToolTipInfoFx);
	normAddEvent(spanEl, "mouseover", showToolTipFx);
	normAddEvent(spanEl, "mouseout", clickedStopPropogation);
	normAddEvent(spanEl, "mouseout", hideToolTipFx);
	normAddEvent(imgIconEl, "mouseover", setToolTipInfoFx);
	normAddEvent(imgIconEl, "mouseover", showToolTipFx);
	normAddEvent(imgIconEl, "mouseout", clickedStopPropogation);
	normAddEvent(imgIconEl, "mouseout", hideToolTipFx);
	
	// rest of stuff...
	aEl.appendChild(imgIconEl);
	aEl.appendChild(spanEl);
	noBREl.appendChild(aEl);
	divEl.appendChild(noBREl);
	
	return divEl;
	
}

function LVcreateListNodeDiv(nodeId)
{
	//to do
}

function LVcreateListNodeImg(nodeId)
{
	//to do
}

function LVcreateListNodeA(nodeId)
{
	//to do
}

function LVcreateListNodeSpan(nodeId)
{
	//to do
}

function LVcreateListNodeToolTip(nodeId)
{
	//to do
}

function LVgetImageIcon(currentXmlElement)
{
	var fileExt = currentXmlElement.getAttribute("fileext");
	if (fileExt != '')
	{
		var nodes = this.xmlDocImages.selectNodes("//*[@fileext = '" + fileExt + "']");
		if (nodes.length > 0)
		{
			return this.getImageDirectory() + nodes[0].getAttribute("name");
		}
	}
	
	return this.getImageDirectory() + this.unknownFileImage;
}

function LVlistNodeDoubleClicked(nodeId, clickedType)
{
	this.listNodeDoubleClickedExt(nodeId, clickedType);
}

function LVlistNodeDoubleClickedExt(nodeId, clickedType)
{

}

function LVgetToolTipInfo(nodeId)
{
	var fileInfo = new WXFileInfo();
	var xmlNode = this.getXmlElementById(nodeId);
	
	fileInfo.fileName = xmlNode.getAttribute("name");
	fileInfo.fileSize = xmlNode.getAttribute("size");
	fileInfo.dateLastModified = xmlNode.getAttribute("lastmodified");
	fileInfo.dateLastAccessed = xmlNode.getAttribute("lastaccessed");
	fileInfo.fileType = xmlNode.getAttribute("type");
	fileInfo.imageType = xmlNode.getAttribute("imagetype");
	fileInfo.imageWidth = xmlNode.getAttribute("imagewidth");
	fileInfo.imageHeight = xmlNode.getAttribute("imageheight");
	
	return this.buildToolTipHtml(xmlNode, fileInfo);	
}

function LVbuildToolTipHtml(xmlNode, fileInfo)
{	
	var info = "<div class=\"ttfilename\">" + fileInfo.fileName + "</div>";
	if (fileInfo.imageType != '')
	{
		var link = xmlNode.getAttribute("link");
		var imageDimensions = this.getResizeImageDimensions(fileInfo);
		info += "<div class=\"ttfileimage\"><img class=\"ttfileimage\" src=\"" + link + "\" ";
		info += "width=\"" + imageDimensions[0] + "\"";
		info += "height=\"" + imageDimensions[1] + "\"";
		info += "</div>";
	}
	if (fileInfo.fileType == "file")
		info += "<div class=\"ttfilesize\">Size: " + fileInfo.fileSize + " bytes</div>";
	info += "<div class=\"ttdatelastmodified\">Last Modified: " + fileInfo.dateLastModified + "</div>";
	info += "<div class=\"ttdatelastaccessed\">Last Accessed: " + fileInfo.dateLastAccessed + "</div>";
		
	return info;
}

function LVgetResizeImageDimensions(fileInfo)
{
	var width = fileInfo.imageWidth;
	var height = fileInfo.imageHeight;
	var limit = 200;
	var percentage;
	
	if (width > 200 || height > 200)
	{
		if (width > height) 
		{
			percentage = (limit / width);
		} 
		else 
		{
			percentage = (limit / height);
		} 
		
		width = Math.round(width * percentage);
		height = Math.round(height * percentage); 
	}
	
	var dimensions = new Array();
	dimensions.push(width);
	dimensions.push(height);
	
	return dimensions;
}
