/*global-block-begin*/


function getFeedArtifactPath(artifact) {
    var relationPath = artifact.getRelationPath(),
        path = relationPath ? relationPath.getPath() : null;
    return path;
}
function getFeedHref(path, substituteToken) {
    return path ? barrique.contextPath+'/sx/feed/Feed?path='+path+
            (substituteToken != null ? '&substituteToken='+substituteToken : '') : null;
}

function tagFeedCtrl(args, nestedHtmlGetter) {
    var artifact = args.artifact,
        artifactId = artifact.getArtifactId(),
        html = '',
        onclick = '',
        href = args.path ? getFeedHref(args.path) : getFeedHref(getFeedArtifactPath(artifact));
        
    if (!href) { return ''; }
    href = ' href="'+href+'"';
        
    if (!browserContext.isGuestUser()) {
        var path = args.path? args.path : getFeedArtifactPath(artifact);
        if (path) {
            onclick = ' onclick="createFeedSubstituteToken(\''+path+'\')"';
            href = '';
        }
    }
        
    html += '<span class="feed-ctrl" id="feed-ctrl-element-'+artifactId+
            '"><a'+href+' title="RSS Feed"><img src="'+barrique.contextPath+
            '/pages/'+barrique.buildNumber+'/barrique/module/feed/feed-icon.png" width="16" height="16"'+onclick+
            '/></a></span>';
    return html;
}

function createFeedSubstituteToken(path) {
    if (confirm('The substitute link will give users your rights of all objects on this page.\nDo you want to continue?')) {
        serverManager.asyncRequest('security/CreateSubstitute', null, {onOk:onFeedSubstituteTokenCreated,
        onError: onFeedSubstituteTokenCreatedError}, {path:path});
    }
}

function onFeedSubstituteTokenCreated(targetId, href, substituteToken) {
    var substituteDiv = dojo.byId('feed-ctrl-element-'+targetId);
    
    substituteDiv.innerHTML = '<a title="Copy link location to subscribe to the feed" href="'+
            getFeedHref(getFeedArtifactPath(artifactManager.getArtifact(targetId)), substituteToken)+
            '"><img src="'+barrique.contextPath+'/pages/'+barrique.buildNumber+
            '/barrique/module/feed/feed-icon-substitute.png"/></a>';
}
function onFeedSubstituteTokenCreatedError(exception) {
    console.debug('Cannot create feed substitute link', exception);
    alert('Cannot create feed link.\n');
}

function getHtmlFeedHeader(artifact, path) {
	var href = path ? getFeedHref(path) : getFeedHref(getFeedArtifactPath(artifact));
    if (!href) { return ''; }
    return '<link rel="alternate" type="application/atom+xml" href="'+href+'"/>';
}
 

/*global-block-end*/
/*function-begin (getHeadHtml1u) */
function getHeadHtml1u() {
    var html = '';
    html += '\n\n\n\n';
    return html;
}
/*function-end (getHeadHtml1u) */
/*function-begin (getBodyHtml1u) */
function getBodyHtml1u() {
    var html = '';
    return html;
}
/*function-end (getBodyHtml1u) */
