/**
 * 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>
 */
 
var xftp = null;

function XftpBackend(urlParam){
	XftpCore.call(this, urlParam, "backend");	
	this.initialize();
	this.setElementsVariables();
}

XftpBackend.prototype = new XftpCore();
XftpBackend.prototype.activePage = "";

XftpBackend.prototype.setElementsVariables = function(){
    this.clientBoxFrame = document.getElementById("categories_frame");
    this.folderBoxFrame = document.getElementById("subcategories_frame");
    this.mainFrame = document.getElementById("main_frame");
    this.setElements(this.isCollapsed);
    this.refreshHeader();
}

/*
 *  PAGES
 */
XftpBackend.prototype.refreshClientFrame = function(){
    this.clientBoxFrame.src = this.URLBackEnd + "category/tree.php";
} 
 
XftpBackend.prototype.refreshFolderFrame = function(){
    this.folderBoxFrame.src = this.URLBackEnd + "subcategory/tree.php";
} 

XftpBackend.prototype.refreshMainFrame = function(){
    if (this.mainFrame != null){
        if(this.isIE) this.mainFrame.src = this.mainFrame.contentWindow.location;
        else this.mainFrame.src = this.mainFrame.contentDocument.location;
    }
} 

XftpBackend.prototype.refreshFrames = function(){
    this.refreshClientFrame();
    this.refreshFolderFrame();
    this.refreshMainFrame();
};

XftpBackend.prototype.refreshHeader = function(){
    xmlDoc = this.getBasicXMLDocument();
    this.displayHtml(this.URLBackEnd + "header_links.xsl", xmlDoc, "contentHeader");
}

XftpBackend.prototype.displayHomePage = function(){
    xmlDoc = this.getBasicXMLDocument();
    this.displayHtml(this.URLBackEnd + "home.xsl", xmlDoc, this.contentElementId);
    this.activePage = "HomePage";
};

XftpBackend.prototype.displayLogin = function(loginDoc){
    var xmlDoc = this.getBasicXMLDocument();
    if (typeof(loginDoc) != "string"){
        xmlDoc = this.mergeXMLDocuments(xmlDoc, loginDoc);
    }
    this.displayHtml(this.URLBackEnd + "login.xsl", xmlDoc, this.contentElementId);
    this.activePage = "Login";
};

XftpBackend.prototype.displayMainFrame = function(src){
    var xmlStr = "<root><frame_src>"+src+"</frame_src></root>"
	var xmlDoc = this.returnXmlDocumentFromString(xmlStr);
	xmlDoc = this.mergeXMLDocuments(xmlDoc, this.getBasicXMLDocument());
    this.displayHtml(this.URLBackEnd + "main_frame.xsl", xmlDoc, this.contentElementId);
    this.mainFrame = document.getElementById("main_frame");
};

XftpBackend.prototype.displayInfo = function(info){
	xmlStr = this.createInfoXml(info);
	var xmlDoc = this.returnXmlDocumentFromString(xmlStr);
    xmlDoc = this.mergeXMLDocuments(xmlDoc, this.getBasicXMLDocument());
	this.displayHtml(this.URLBackEnd + "info.xsl", xmlDoc, this.contentElementId);
}

XftpBackend.prototype.sendLogin = function(username, password){
    query = "?operation=login&username=" + username + "&password=" + password + "&page=backend";
    var xmlDoc = this.getDocumentFromServer(this.URLPublicService + query);
    if(this.getTagValue("/root/success", xmlDoc) == 'true'){
        this.loggedUser = this.getTagValue("/root/logged_user", xmlDoc);
        this.loggedUserId = this.getTagValue("/root/logged_user_id", xmlDoc);
        this.loggedUserDisplay = this.getTagValue("/root/logged_user_display", xmlDoc);
        this.displayHomePage();
        this.refreshHeader();
        this.refreshClientFrame();
        this.sendVariables("backend");
    } else{
        this.displayLogin(xmlDoc);
        this.refreshHeader();
        this.refreshClientFrame();
    }
    return false;
};

XftpBackend.prototype.sendLogout = function(){
    query = "?operation=logout&page=backend";
    xmlDoc = this.getDocumentFromServer(this.URLPublicService + query);
    if(this.getTagValue("/root/success", xmlDoc) == 'true'){
        this.displayLogin("");
        this.refreshClientFrame();
        this.refreshFolderFrame();
    }
};

XftpBackend.prototype.categoryClick = function(categoryId){
	if(this.selectedCategoryId != 0){
		old_selected_link = this.clientBoxFrame.contentWindow.document.getElementById("link_category_" + this.selectedCategoryId.toString());
		old_selected_link.className = "client_link";
	}
	new_selected_link = this.clientBoxFrame.contentWindow.document.getElementById("link_category_" + categoryId.toString());
	new_selected_link.className = "client_link_selected";
	
	this.selectedCategoryId = categoryId;
	this.selectedSubCategoryId = 0;
	this.selectedItemId = 0;

	xmlStr = this.createInfoXml("Please select " + this.subCategoryDisplayName + ".");
	var xmlDoc = this.returnXmlDocumentFromString(xmlStr);
	
    xmlDoc = this.mergeXMLDocuments(xmlDoc, this.getBasicXMLDocument());
	
	this.displayHtml(this.URLBackEnd + "info.xsl", xmlDoc, this.contentElementId);
	//this.SendVariables("backend");
	
	//alert(subCategoryFrame);
	subCategoryFrame.createSubCategoryTree(this.selectedCategoryId);
	
	/*
	this.RefreshFrames();
	this.RefreshHeader();
	*/
}

XftpBackend.prototype.subCategoryClick = function(subCategoryId){
    this.selectedSubCategoryId = subCategoryId;
    this.selectedItemId = 0;
    this.sendVariables("backend");
    this.refreshHeader();
    this.displayMainFrame('item/list.php');
}

XftpBackend.prototype.itemClick = function(itemId){
    this.selectedItemId = itemId;
    this.sendVariables("backend");
    // display middle with item/list.php
    this.displayViewItem();
}

XftpBackend.prototype.setElements = function(value){
    if(value == "false"){
        this.isCollapsed = "false";
        document.getElementById('CategoryBox').style.visibility = 'hidden';
        document.getElementById('SubCategoryBox').style.visibility = 'hidden';
        document.getElementById('left_td').width = 20;
        document.getElementById('right_td').width = 700;
        document.getElementById('background_td').style.backgroundImage = "url(" + this.URL + "images/skins/" + this.skin + "back_collapsed.jpg)";
        document.getElementById('button_collapse').src = this.URL + "images/skins/" + this.skin + "button_expand.jpg";
        document.getElementById('button_collapse').hspace = 1;
    }
    else{
        this.isCollapsed = "true";
        document.getElementById('CategoryBox').style.visibility = 'visible';
        document.getElementById('SubCategoryBox').style.visibility = 'visible';
        document.getElementById('left_td').width = 253;
        document.getElementById('right_td').width = 463;
        document.getElementById('background_td').style.backgroundImage = "url(" + this.URL + "images/skins/" + this.skin + "back_expanded.jpg)";
        document.getElementById('button_collapse').src = this.URL + "images/skins/" + this.skin + "button_collapse.jpg";
        document.getElementById('button_collapse').hspace = 2;
    }
}

XftpBackend.prototype.revertSite = function(){
    if (this.isCollapsed == "true") this.setElements("false");
    else this.setElements("true");
    this.sendVariables("backend");
    this.refreshMainFrame();
}
