function _generateBaseLink( dest, blogId, type, resourceName, resourceDesc, preview )
{
    if( type == 2 ) {
	    if( preview == 1 ) {
	        htmlCode = plogBaseUrl+'/resserver.php?blogId='+blogId+'&resource='+encodeURIComponent(resourceName)+'&mode=preview';
	    }
		else if( preview == 2) {
			htmlCode = plogBaseUrl+'/resserver.php?blogId='+blogId+'&resource='+encodeURIComponent(resourceName)+'&mode=medium';
		}
	    else {
	        htmlCode = plogBaseUrl+'/resserver.php?blogId='+blogId+'&resource='+encodeURIComponent(resourceName);
	    }
    }
    else {
	    // if not an image, there is not much we can do
	    htmlCode = '<a title="'+resourceDesc+'" href="'+plogBaseUrl+'/resserver.php?blogId='+blogId+'&amp;resource='+encodeURIComponent(resourceName)+'">'+resourceName+'</a>';
    }
  
    return htmlCode;
}

function _generateResourceLink( dest, blogId, type, resourceName, resourceDesc, preview, mimeType, resId )
{
    var htmlCode = '';
    if( type == 1 ) {
	    // if the resource is an image, check the second parameter to see wether we'd like
	    // to show the preview with a link to the real size image or the real image
	    // inlined with the text
	    if( preview == 1 ) {
		    htmlCode = '<a id="res_' + resId+ '" href="'+plogBaseUrl+'/resserver.php?blogId='+blogId+'&amp;resource='+encodeURIComponent(resourceName)+'" type="'+mimeType+'">';
		    htmlCode += '<img style="margin: 5px;" border="0" alt="'+resourceDesc+'" src="'+plogBaseUrl+'/resserver.php?blogId='+blogId+'&amp;resource='+encodeURIComponent(resourceName)+'&amp;mode=preview" />';
		    htmlCode += '</a>';
	    }
		else if ( preview == 2 ) {
		    htmlCode = '<a id="res_' + resId+ '" href="'+plogBaseUrl+'/resserver.php?blogId='+blogId+'&amp;resource='+encodeURIComponent(resourceName)+'" type="'+mimeType+'">';
		    htmlCode += '<img style="margin: 5px;" border="0" alt="'+resourceDesc+'" src="'+plogBaseUrl+'/resserver.php?blogId='+blogId+'&amp;resource='+encodeURIComponent(resourceName)+'&amp;mode=medium" />';
		    htmlCode += '</a>';
		}
	    else {
		    htmlCode = '<img style="margin: 5px;" alt="'+resourceDesc+'" src="'+plogBaseUrl+'/resserver.php?blogId='+blogId+'&amp;resource='+encodeURIComponent(resourceName)+'" />';
	    }
    }
    else {
	    // if not an image, there is not much we can do
	    htmlCode = '<a id="res_'+resId+'" title="'+resourceDesc+'" href="'+plogBaseUrl+'/resserver.php?blogId='+blogId+'&amp;resource='+encodeURIComponent(resourceName)+'" type="'+mimeType+'">'+resourceName+'</a>';
    }
  
    return htmlCode;

}

function addResourceLink( dest, reqFormat, blogId, type, resourceName, resourceDesc, preview, mimeType, resId ) 
{
    // generate the link
    var htmlCode = _generateResourceLink( dest, reqFormat, blogId, type, resourceName, resourceDesc, preview, mimeType, resId );
  
    // and now send the value to the text area where we are typing the post
    if( dest == 1 ) {
	    //parent.opener.document.newPost.postText.value += htmlCode;
	    addText( parent.opener.document.newPost.postText, htmlCode );
    }
    else {
	    //parent.opener.document.newPost.postExtendedText.value += htmlCode;
	    addText( parent.opener.document.newPost.postExtendedText, htmlCode );
    }	

    // finally close this window since we don't need it anymore
    //window.close();
}

function _generateAlbumLink( dest, blogId, albumId, albumName, albumDesc ) 
{
    var htmlCode = '';
    
    // if no other global variable called 'indexPage' is defined, use the
    // default one
    if( indexPage == '' )
        indexPage = 'index.php';

    htmlCode = '<a title="'+albumDesc+'" href="'+plogBaseUrl+'/'+indexPage+'?op=ViewAlbum&amp;blogId='+blogId+'&amp;albumId='+albumId+'">'+albumName+'</a>';
	
    return htmlCode;      
}

/**
 * generates and adds a link to an album to the textarea
 *
 * @param dest
 * @param blogId
 * @param albumId
 * @param albumName
 * @param albumDesc
 */
function addAlbumLink( dest, blogId, albumId, albumName, albumDesc ) 
{
    albumLink = _generateAlbumLink( dest, blogId, albumId, albumName, albumDesc );

    // and now send the value to the text area where we are typing the post
    if( dest == 1 ) {
        addText( parent.opener.document.newPost.postText, albumLink );
    }	
    else {
		addText( parent.opener.document.newPost.postExtendedText, albumLink );
    }
}

// ------
//
// htmlarea-related code
//
// -------
//

function addHtmlareaLink(dest, blogId, type, resourceName, resourceDesc, preview, mimeType, resId ) {
    var htmlCode = _generateResourceLink( dest, blogId, type, resourceName, resourceDesc, preview, mimeType, resId );
    
    tinyMCE.execCommand("mceInsertContent",true,htmlCode);
	tinyMCE.selectedInstance.repaint();

	// Close the dialog
	tinyMCE.closeWindow(window);
};

function addHtmlareaAlbumLink( dest, blogId, albumId, albumName, albumDesc ) 
{
    var htmlCode = _generateAlbumLink( dest, blogId, albumId, albumName, albumDesc );

    tinyMCE.execCommand("mceInsertContent",true,htmlCode);
	tinyMCE.selectedInstance.repaint();

	// Close the dialog
	tinyMCE.closeWindow(window);
}

function onCancel() {
	tinyMCE.closeWindow(window);
};
