﻿// meta data functions
var processingMetaDataRequest = false;
var currMetaDataBox = null;
var currMetaDataBoxSender = null;

function ShowMetaData_Request(sender, mdPath) {
    
    if (!processingMetaDataRequest) {
        
        if (currMetaDataBox) currMetaDataBox.destroy(); 
        if (currMetaDataBoxSender) {
            currMetaDataBoxSender.setText('View');
            currMetaDataBoxSender.setStyle('text-decoration', 'underline');
        }
        
        processingMetaDataRequest = true;
        
        var progressTxt = $(document.createElement('div'));
        progressTxt.setText('Loading...');
        
        sender.parentNode.appendChild(progressTxt);
        $(sender).setStyle('display','none');
        
        // show the loading meta data screen
        FS.Fscms.Modules.DocumentFinder.Webservices.FileMetaDataProvider.GetFileDetailsHTML(mdPath, 'MetaDataTemplate', function(response) {
            ShowMetaData_Callback(response, progressTxt, sender);
        });
    }
}

function ShowMetaData_Callback (r, progressTxt, sender) {
    if (r) {
        // hide and destrony the prograss txt
        $(progressTxt).destroy();
        
        // show the sender
        sender = currMetaDataBoxSender = $(sender);
        sender.setStyle('display','block');
        sender.setText('Visible');
        sender.setStyle('text-decoration', 'none');
        currMetaDataBox = GenerateMetaDataInfoBox(r, sender);                  
        processingMetaDataRequest = false;      
    }
}

function GenerateMetaDataInfoBox(contentHtml, sender) {

    // create the main containing object and its content
    var mdbox = $(document.createElement('div'));
    mdbox.id = 'metaDataContainer';
    
    var content = $(document.createElement('div'));
    //content.addClass('content');
    content.setText(contentHtml);
    
    var tab = $(document.createElement('table'));
    var tabBody = $(document.createElement('tbody'));
    var tabBodyRow = $(document.createElement('tr'));
    var tabHeadRowCell1 = $(document.createElement('td'));
    var tabHeadRowCell2 = $(document.createElement('td'));
    
    tab.cellPadding = 0;
    tab.cellSpacing = 0;
    tab.addClass('header');
    tab.appendChild(tabBody);    
    tabBody.appendChild(tabBodyRow);    
    tabBodyRow.appendChild(tabHeadRowCell1);
    tabBodyRow.appendChild(tabHeadRowCell2);
    
    var hdrLbl = $(document.createElement('div'));
    hdrLbl.setText('Details');
    hdrLbl.addClass('title');
    
    tabHeadRowCell1.appendChild(hdrLbl);    
    
    var closeBtn = $(document.createElement('div'));
    closeBtn.addClass('closeBtn');
    closeBtn.setText('Close X');
    closeBtn.addEvent('click', function() {
        mdbox.setStyle('display','none');
        mdbox.destroy();
        sender.setText('View');
        sender.setStyle('text-decoration', 'underline');
        currMetaDataBoxSender = null;
    });
    
    tabHeadRowCell2.appendChild(closeBtn);
    
    mdbox.appendChild(tab);
    mdbox.appendChild(content);
    
    // add the element to the page
    $(document).body.appendChild(mdbox);    
    
    var pos = $(sender).getPosition();
    
    // position the element by the sender
    mdbox.setStyle('top', pos.y + 'px');
    mdbox.setStyle('left', pos.x - mdbox.getSize().x - 20 + 'px');
    
    var wHeight = window.getHeight();
    var mbBottom = (pos.y + mdbox.getHeight() - window.getScrollTop());
    var posYDiff = wHeight - mbBottom;
        
    // reposition the element if is off the viewport
    if (posYDiff < 0) {
        mdbox.setStyle('top', mdbox.getPosition().y + posYDiff + 'px');
    }
    
    // apply the drag within the browser window
    var dragMdbox = new Drag(mdbox, {
                handle: tab,
                limit: {
		            x: [
			            window.getScrollLeft, 
			            function() { return window.getWidth() + window.getScrollLeft() - mdbox.offsetWidth;	}
		            ], 
		            y: [
			            window.getScrollTop,
			            function() { return window.getHeight() + window.getScrollTop() - mdbox.offsetHeight; }
		            ]
	            }
            });
    
    return mdbox;
}

function HideMetaDataWindow() {
    // hide the metadata window if available
    var mdWindow = $('metaDataContainer');
    if (mdWindow) mdWindow.destroy();
}
