/**
 * FileStreamer
 * Copyright (c) 2006 MakProject.com. All Rights Reserved.
 * http://www.filestreamer.makproject.com
 *
 * Authors:
 *  Toni Rajkovski <rtoni@makproject.com>
 *  Vladimir Jakimov <vjakimov@makproject.com>
 */
 


function getStringFromDocumentFile(xslDocumentSrc, xmlDocument){
    var xslDocument = Sarissa.getDomDocument();
    xslDocument.async = false; 
    xslDocument.load(xslDocumentSrc);
    
    var xsltProc  = new XSLTProcessor();
    xsltProc.importStylesheet(xslDocument);
    fragment = xsltProc.transformToDocument(xmlDocument);

    if (Sarissa.getParseErrorText(fragment) != Sarissa.PARSED_OK) {
        alert ("err = " +   Sarissa.getParseErrorText(fragment));
    };
    var str = new XMLSerializer().serializeToString(fragment);
    return str;
}

function returnXmlDocumentFromStringFile(str){
    var xmlDocument = Sarissa.getDomDocument();
    xmlDocument = (new DOMParser()).parseFromString(str, "text/xml");
    return xmlDocument;
}



 
/**
 *  TabbedBox Class
 */
 
function TabbedBox(instanceNameParam, navDivNameParam, contentDivNameParam, hiddenDivNameParam){
    this.instanceName = instanceNameParam;
    this.navDivName = navDivNameParam;
    this.contentDivName = contentDivNameParam;
    this.hiddenDivName = hiddenDivNameParam;
    this.items = new Array();
};

TabbedBox.prototype.URL = "";
TabbedBox.prototype.instanceName;
TabbedBox.prototype.navDivName;
TabbedBox.prototype.contentDivName;
TabbedBox.prototype.hiddenDivName;
TabbedBox.prototype.items;

TabbedBox.prototype.getItems = function(){
    return this.items;
};

TabbedBox.prototype.addItem = function(item){
    this.items.push(item);
};


TabbedBox.prototype.getNav = function(){
    return "";
};

TabbedBox.prototype.displayNav = function(nav_html){
    var element = document.getElementById(this.navDivName);
    element.innerHTML = nav_html;
};

TabbedBox.prototype.refreshNav = function(){
    var html = this.getNav();
    this.displayNav(html);
};

TabbedBox.prototype.setHiddenDivHtml = function(hidden_html){
    var element = document.getElementById(this.hiddenDivName);
    element.innerHTML = hidden_html;
};






/**
 *  XFTPFile Class
 */
 
function XFTPFile(fileNode, xslTemplateParam, urlParam, isNewFileParam){
    this.template = xslTemplateParam;
    this.isNewFile = isNewFileParam;
    this.URL = urlParam;
	if (!isNewFileParam) this.loadFileFromNode(fileNode);
};

XFTPFile.prototype.tempalte;
XFTPFile.prototype.URL;

XFTPFile.prototype.isNewFile;
XFTPFile.prototype.newFileNumber = "";

XFTPFile.prototype.file_id;
XFTPFile.prototype.filename;
XFTPFile.prototype.file_extension;
XFTPFile.prototype.file_size;
XFTPFile.prototype.date_posted;
XFTPFile.prototype.date_cnaged;
XFTPFile.prototype.icon;

XFTPFile.prototype.getNodeNameElementValue = function(fileNode, elementName){
    var element = fileNode.getElementsByTagName(elementName).item(0);
    if(element.childNodes.length > 0)
        return element.childNodes.item(0).data;
    else return "";
}

XFTPFile.prototype.loadFileFromNode = function(fileNode){
    this.file_id = this.getNodeNameElementValue(fileNode, "file_id");
    this.filename = this.getNodeNameElementValue(fileNode, "filename");
    this.file_extension = this.getNodeNameElementValue(fileNode, "file_extension");
    this.file_size = this.getNodeNameElementValue(fileNode, "file_size");
    this.date_posted = this.getNodeNameElementValue(fileNode, "date_posted");
    this.date_changed = this.getNodeNameElementValue(fileNode, "date_changed");
    this.icon = this.getNodeNameElementValue(fileNode, "icon");
}

XFTPFile.prototype.getHtmlDisplayString = function(){
	if(!this.isNewFile){
		var xmlString = "<root>";
		xmlString += "<file_id>" + this.file_id + "</file_id>";
		xmlString += "<filename>" + this.filename + "</filename>";
		xmlString += "<file_extension>" + this.file_extension + "</file_extension>";
		xmlString += "<file_size>" + this.file_size + "</file_size>";
		xmlString += "<date_posted>" + this.date_posted + "</date_posted>";
		xmlString += "<date_changed>" + this.date_changed + "</date_changed>";
		xmlString += "<icon>" + this.icon + "</icon>";
		xmlString += "<url>" + this.URL + "</url>";
		xmlString += "</root>";
		//alert(xmlString);
		var xmlDoc = parent.xftp.returnXmlDocumentFromString(xmlString);
		return parent.xftp.getStringFromDocument(this.template, xmlDoc);    
	} else {
		return "";
	}
}

XFTPFile.prototype.getDisplayName = function(){
	if(this.isNewFile) return "new file " + (this.newFileNumber + 1);
    else return this.filename;
}

XFTPFile.prototype.setNewFileNumber = function(newFileNumberParam){
	this.newFileNumber = newFileNumberParam;
}

XFTPFile.prototype.showNewFileDiv = function(visible){
	var div = document.getElementById("newFileDiv_" + this.newFileNumber);
	if (visible){ 
		div.style.visibility = "visible";
		div.style.position = "static";
		div.style.overflow = "visible";
	} else {
		div.style.visibility = "hidden";
	  	div.style.position = "absolute";
        div.style.posLeft = "0";
        div.style.posTop = "-100";
		div.style.overflow = "hidden";
	}
}




/**
 *  TabbedFileBox Class
 */
 
function TabbedFileBox(instanceName, navDivName, contentDivName, hiddenDivName, xslContentTemplateParam){
    TabbedBox.call(this, instanceName, navDivName, contentDivName, hiddenDivName);
    this.xslContentTemplate = xslContentTemplateParam;
    this.selectedFile = 0;
	this.newFilesCount = 0;
}

TabbedFileBox.prototype = new TabbedBox();
TabbedFileBox.prototype.xslTemplate;
TabbedFileBox.prototype.filesXmlNode;
TabbedFileBox.prototype.selectedFile;
TabbedFileBox.prototype.newFilesCount;

TabbedFileBox.prototype.loadFilesFromXml = function(filesXmlNodeParam){
    this.filesXmlNode = filesXmlNodeParam;
	var rootElem = this.filesXmlNode.childNodes[0];
    for (var i=0; i < rootElem.childNodes.length; i++) {
        var elem = rootElem.childNodes[i];
        var file = new XFTPFile(elem, this.xslContentTemplate, this.URL, false);
        this.addItem(file);
    }
}

TabbedFileBox.prototype.displayContent = function(content_html){
    var element = document.getElementById(this.contentDivName);
    element.innerHTML = content_html;
};

TabbedFileBox.prototype.refreshContent = function(itemNumber){
    var html = this.getContent(itemNumber);
    this.displayContent(html);
};

TabbedFileBox.prototype.getContent = function(itemNumber){
	if(this.items.length > 0)
		return this.items[itemNumber].getHtmlDisplayString();
	else return "";
}

TabbedFileBox.prototype.getNav = function(){
    TabbedBox.prototype.getNav.call(this);
    return this.getComboNav();
}

TabbedFileBox.prototype.getComboNav = function(){
    var html = "<select style='width=270px;' name='" + this.instanceName + "_file_chooser' onChange='" + this.instanceName + ".selectFile(this.options[this.selectedIndex].value);'>";
    for(var i=0; i < this.items.length; i++){
		var selected = (this.selectedFile == i) ? "selected" : "";
        html += "<option value='" + i + "' " + selected + ">" + this.getItemDisplayName(i) + "</option>";
    };
    html += "</select>";
    return html;
};

TabbedFileBox.prototype.getItemDisplayName = function(itemNumber){
    return this.items[itemNumber].getDisplayName();
}

TabbedFileBox.prototype.selectFile = function(newSelectedFile){
	if(this.items[this.selectedFile].isNewFile){
		this.items[this.selectedFile].showNewFileDiv(false);
	}
	
	this.selectedFile = newSelectedFile;
	
	if(this.items[this.selectedFile].isNewFile){
		this.items[this.selectedFile].showNewFileDiv(true);
	}
	this.refreshContent(newSelectedFile);
	this.refreshNav(newSelectedFile);
}

TabbedFileBox.prototype.newFileClick = function(){
	var file = new XFTPFile(null, "", this.URL, true);
	file.setNewFileNumber(this.newFilesCount++);
	this.addItem(file);
	var tr1 = document.getElementById("row_" + this.instanceName);
	var td = tr1.insertCell(0);
	td.innerHTML = "<br /> <div id='newFileDiv_"+file.newFileNumber+"'> <input type='file' name='file_" + file.newFileNumber + "'></div> <br />";
	var numFilesField = document.getElementById("numNewFiles_" + this.instanceName);
	numFilesField.value = this.newFilesCount;
	this.selectFile(this.items.length - 1);
}


