/**
 * 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;

/**
 * XftpFrontend Class
 */

function XftpFrontend(url){
	XftpCore.call(this, url, "frontend");
	this.initialize();
	this.setElementsVariables();
}

XftpFrontend.prototype = new XftpCore();
XftpFrontend.prototype.tabbedFiles = null;
XftpFrontend.prototype.activePage = "";

XftpFrontend.prototype.setElementsVariables = function(){
    this.clientBoxFrame = document.getElementById("categories_frame");
    this.folderBoxFrame = document.getElementById("subcategories_frame");
    this.itemBoxFrame = document.getElementById("items_frame");
    this.setElements(this.isCollapsed);
    this.refreshHeader();
}


XftpFrontend.prototype.checkForHideClientBox = function(){
	if(this.clientBoxFrame.contentWindow.categoryToShowAndHideTheBox != null){
		this.hideClientBox();
		this.categoryClick(this.clientBoxFrame.contentWindow.categoryToShowAndHideTheBox);
	}
}


/*
 *  PAGES
 */
XftpFrontend.prototype.refreshClientFrame = function(){
    this.clientBoxFrame.src = this.URLFrontEnd + "category/tree.php";
} 
 
XftpFrontend.prototype.refreshFolderFrame = function(){
    //this.folderBoxFrame.src = this.URLFrontEnd + "subcategory/tree.php";
    //subCategoryFrame.createSubCategoryTree(this.selectedCategoryId);
} 

XftpFrontend.prototype.refreshItemFrame = function(){
    if(this.itemBoxFrame != null)
        this.itemBoxFrame.src = this.URLFrontEnd + "item/list.php";
} 

XftpFrontend.prototype.refreshFrames = function(){
    this.refreshClientFrame();
    this.refreshFolderFrame();
    this.refreshItemFrame();
};

XftpFrontend.prototype.refreshHeader = function(){
    xmlDoc = this.getBasicXMLDocument();
    this.displayHtml(this.URLFrontEnd + "header_links.xsl", xmlDoc, "contentHeader");
}

XftpFrontend.prototype.displayHomePage = function(){
    xmlDoc = this.getBasicXMLDocument();
    this.displayHtml(this.URLFrontEnd + "home.xsl", xmlDoc, this.contentElementId);
    this.activePage = "HomePage";
};

XftpFrontend.prototype.displayLogin = function(loginDoc){
    var xmlDoc = this.getBasicXMLDocument();
    if (typeof(loginDoc) != "string"){
        xmlDoc = this.mergeXMLDocuments(xmlDoc, loginDoc);
    }
    this.displayHtml(this.URLFrontEnd + "login.xsl", xmlDoc, this.contentElementId);
    this.activePage = "Login";
};

XftpFrontend.prototype.displayItem = function(){
    xmlDoc = this.getBasicXMLDocument();
    this.displayHtml(this.URLFrontEnd + "item/display_item.xsl", xmlDoc, this.contentElementId);
    this.itemBoxFrame = document.getElementById("items_frame");
    this.displayHtml(this.URLFrontEnd + "item/view_empty.xsl", xmlDoc, "contentItem"); 
    this.activePage = "Item";
};

XftpFrontend.prototype.displayViewItem = function(){
    if(this.activePage != 'Item') this.displayItem();
    xmlBasicDoc = this.getBasicXMLDocument();
    query = "?operation=get_item_row&item_id=" + this.selectedItemId + "&page=frontend";
    xmlDoc = this.getDocumentFromServer(this.URLPublicService + query);
    xmlDoc = this.mergeXMLDocuments(xmlDoc, xmlBasicDoc);
    var item_type = this.getTagValue("/root/item/type", xmlDoc);
    if(item_type == "1"){ 
        this.displayHtml(this.URLFrontEnd + "item/view_file_upload.xsl", xmlDoc, "contentItem"); 
        this.tabbedFiles = new TabbedFileBox('parent.xftp.tabbedFiles', 'fileNavDiv', 'fileContentDiv', 'fileHiddenDiv', this.URLFrontEnd + 'item/file_view_display.xsl');
        this.tabbedFiles.URL = this.URL;
        query = "?operation=get_files&item_id=" + this.selectedItemId + "&page=frontend";
        var xmlDocFiles = this.getDocumentFromServer(this.URLPublicService + query);
		this.tabbedFiles.loadFilesFromXml(xmlDocFiles);
		this.tabbedFiles.refreshContent(0);
		this.tabbedFiles.refreshNav(0);
    }
    else if(item_type == "2") this.displayHtml(this.URLFrontEnd + "item/view_message.xsl", xmlDoc, "contentItem"); 
    else this.displayHtml(this.URLFrontEnd + "item/view.xsl", xmlDoc, "contentItem"); 
};

XftpFrontend.prototype.displayModifyItem = function(){
    if(this.activePage != 'Item') this.displayItem();
    xmlDoc = this.getBasicXMLDocument();
    this.displayHtml(this.URLFrontEnd + "item/modify_frame.xsl", xmlDoc, "contentItem");
};

XftpFrontend.prototype.displayInsertItem = function(){
    if(this.activePage != 'Item') this.displayItem();
    this.selectedItemId = 0;
    xmlDoc = this.getBasicXMLDocument();
    this.displayHtml(this.URLFrontEnd + "item/modify_frame.xsl", xmlDoc, "contentItem");
};

XftpFrontend.prototype.displayModifyItemFileUpload = function(){
    if(this.activePage != 'Item') this.displayModifyItem();
	document.getElementById("modify_item_frame").src = this.URLFrontEnd + "item/modify.php?item_id=" + this.selectedItemId + "&template_type=1";
};

XftpFrontend.prototype.displayModifyItemMessage = function(){
    if(this.activePage != 'Item') this.displayModifyItem();
	document.getElementById("modify_item_frame").src = this.URLFrontEnd + "item/modify.php?item_id=" + this.selectedItemId + "&template_type=2";
};

XftpFrontend.prototype.deleteSelectedItem = function(){
    query = "?operation=delete_item&item_id=" + this.selectedItemId + "&page=frontend";
    var xmlDoc = this.getDocumentFromServer(this.URLPublicService + query);
    this.displayItem();
};

XftpFrontend.prototype.sendLogin = function(username, password){
    query = "?operation=login&username=" + username + "&password=" + password + "&page=frontend";
    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.refreshFrames();
        this.sendVariables("frontend");
    } else{
        this.displayLogin(xmlDoc);
        this.refreshFrames();
    }
    return false;
};

XftpFrontend.prototype.sendLogout = function(){
    query = "?operation=logout&page=frontend";
    xmlDoc = this.getDocumentFromServer(this.URLPublicService + query);
    if(this.getTagValue("/root/success", xmlDoc) == 'true'){
        this.displayLogin("");
        this.refreshClientFrame();
        this.refreshFolderFrame();
    }
};

XftpFrontend.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());
	if(new_selected_link != null) {
		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.URLFrontEnd + "info.xsl", xmlDoc, this.contentElementId);
	//this.sendVariables("frontend");
	
	//alert(subCategoryFrame);
	subCategoryFrame.createSubCategoryTree(this.selectedCategoryId);
}

XftpFrontend.prototype.subCategoryClick = function(subCategoryId){
    this.selectedSubCategoryId = subCategoryId;
    this.selectedItemId = 0;
    this.sendVariables("frontend");
    this.refreshHeader();
    this.displayItem();
}

XftpFrontend.prototype.itemClick = function(itemId){
    this.selectedItemId = itemId;
    this.sendVariables("frontend");
    this.displayViewItem();
}

XftpFrontend.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_1.jpg)";
        document.getElementById('button_collapse').src = this.URL + "images/skins/" + this.skin + "button_expand.jpg";
        document.getElementById('button_collapse').hspace = 1;
        if(document.getElementById('items_box') != null){
            document.getElementById('items_box').width = 390;
            if(document.getElementById('item_info_2') != null)
                document.getElementById('item_info_2').width = 150;
        }
    }
    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_1.jpg)";
        document.getElementById('button_collapse').src = this.URL + "images/skins/" + this.skin + "button_collapse.jpg";
        document.getElementById('button_collapse').hspace = 2;
        if(document.getElementById('items_box') != null){
            document.getElementById('items_box').width = 310;
            if(document.getElementById('item_info_2') != null)
                document.getElementById('item_info_2').width = 1;
        }
    }
}

XftpFrontend.prototype.revertSite = function(){
    if (this.isCollapsed == "true") this.setElements("false");
    else this.setElements("true");
    this.sendVariables("frontend");
    this.refreshItemFrame();
    if(document.getElementById('item_image_preview') != null)
        this.displayViewItem();
}