<!-- Copyright [2008] Adobe Systems Incorporated.  All rights reserved. -->
<html xmlns:MMString="http://www.macromedia.com/schemes/data/string/">

<head>
<title><MMString:loadString id="Menus/MM/ccBlogEntryCategories/title" /></title>
<SCRIPT LANGUAGE="javascript" SRC="../../Shared/MM/Scripts/CMN/ccBlogMetaDataStrings.js"></SCRIPT>
<script language="JavaScript">

var defaultPage = dw.loadString("BlogEntryToolbar/main page");
var connectTo = dw.loadString("BlogEntryToolbar/connect to get list");
var selectEntryID = "CT_SELECT_ENTRY_ID";
var CurrentItem = "CT_SELECT_ENTRY_ID";
var showItem = true;
var newPageId = "newPage#*#*";
var Marker = dw.loadString("BlogEntryToolbar/original parent for this page");

function showIf()
{
    var entryType = CCWorkspaceManager.getManager(dw.getDocumentDOM()).getCurBlogEntryType(); 
    if(entryType == 'pages')
    {
        return true;
    }
	return false;
}

function receiveArguments()
{
    if(CurrentItem == arguments[0])
    {
        return;
    }
        
	CurrentItem = arguments[0];
	var dom = dw.getDocumentDOM();
	if (dom)
	{
		var wm = CCWorkspaceManager.getManager(dom);
		if (wm)
		{
		    // return if not a blog site!!
			if(!wm.isBlogSite())
				return null;
			// get the meta tags	
            metatags = dom.getElementsByTagName("meta");
            count = metatags.length;
            var bTitleUpdated = false;
            var bIdUpdated = false;
            
            // find for the TEMPLATE_PARENT_ID meta tag and change the content in it!
            for (i = 0; i < count; i++)
            { 
                if (metatags[i].name == TEMPLATE_PARENT_ID)
                {
                    metatags[i].content = CurrentItem;
                    bIdUpdated = true;
                }
                else if(metatags[i].name == TEMPLATE_PARENT_TITLE)
                {                
                    // get the parent page list and update the Title matching the ID.                                            
			        var recentPages = wm.getBlogEntryList('pages');
			        var k;
			        
			        // inserted as title followed by page id
			        for(k = 0; k < recentPages.length; k += 3)
			        {
			            if(recentPages[k + 1] == CurrentItem)
			            {
			                metatags[i].content = recentPages[k];
			                break;
			            }
			        }			           
			        bTitleUpdated = true;
                }
                // we can exit this loop after we have updated both title and Id of the parent.
                if(bTitleUpdated && bIdUpdated)
                    break;
            } // for - loop
        }
        CCWorkspaceManager.getManager(dom).updateUI();
    } // if dom
	return;	
}

function canAcceptCommand()
{
    var dom = dw.getDocumentDOM();
	var wm = CCWorkspaceManager.getManager(dom);
	var bAccept = (dom != null &&	dw.getFocus() == 'document' &&
	        wm.isBlogSite() && wm.isParentPageSupported());
	if(bAccept)
	{	
	    // Check if the Page count is more. If so, Commands accepted, else NO
		var recentPages = wm.getValidParentList();	    
	    if(recentPages.length > 0)
	    {
	        // check if we need to update the currentItem
	        if(CurrentItem == selectEntryID)
	        {
	            // get the meta tagged elements        
                metatags = dom.getElementsByTagName("meta");
                count = metatags.length;
		        var i;
		                		
		        // The command has been accepted.. so check if we have the meta tag 
		        // TEMPLATE_ORIGINAL_PARENT_ID - if so, update the Template 
                for (i = 0; i < count; i++)
                {
                    if(metatags[i].name == TEMPLATE_ORIGINAL_PARENT_ID)
                    {
                        OriginalParentId = metatags[i].content;
                        CurrentItem = OriginalParentId;
                    }
                }
             }
	        return true;
	    }
	}
	return false;
}

function getCurrentValue()
{
	return CurrentItem;		
}

function getDynamicContent()
{
    var dom = dw.getDocumentDOM();
	var wm = CCWorkspaceManager.getManager(dom);
    var OriginalParentTitle = "";
    var OriginalParentId = "";
	if (wm)
	{
	    // return-  if this is not a blog site
		if(!wm.isBlogSite())
			return null;
			
        var bFindParent = true;
        
        //get the meta tagged elements        
        metatags = dom.getElementsByTagName("meta");
        count = metatags.length;
		var i;
		
		// The Original Parent needs to be remembered in order to MARK it for the user.
        for (i = 0; i < count; i++)
        {
            if (metatags[i].name == TEMPLATE_PARENT_ID)
            {
                CurrentItem = metatags[i].content;
            }
            else if(metatags[i].name == TEMPLATE_ORIGINAL_PARENT_ID)
            {
                OriginalParentId = metatags[i].content;
            }
        }                
	    // if there is no parent (new page)... OriginalParentId remains "" if there is no entry in the meta-data
	    // OriginalParentId will be 0, if there parent is Main Page
        if(OriginalParentId == "0")
        {
            OriginalParentId = selectEntryID;
        }
        if(CurrentItem == "" || CurrentItem == "0")
        {
            CurrentItem = selectEntryID;
        }
        
        // New Page handler
        if(OriginalParentId == "")
        {
            // we Haven't found TEMPLATE_ORIGINAL_PARENT_ID
            // A case of missing tags - New Page - Add Corresponding Tags
            // insert the tags - TEMPLATE_PARENT_ID, TEMPLATE_ORIGINAL_PARENT_ID
            // and TEMPLATE_PARENT_TITLE
            dom.insertBlogMetaTag(TEMPLATE_PARENT_ID, "0");
            dom.insertBlogMetaTag(TEMPLATE_PARENT_TITLE, defaultPage);
            // Still Keep TEMPLATE_ORIGINAL_PARENT_ID as "" so that (*) doesn't get added
            dom.insertBlogMetaTag(TEMPLATE_ORIGINAL_PARENT_ID, newPageId);        
            CurrentItem = selectEntryID;
            CCWorkspaceManager.getManager(dom).updateUI();
        } // if OriginalParentId == ""
        else if(OriginalParentId != newPageId)
        {                
            // Just set the Parent title to marker.
            // The actual "Parent Title" text will be extracted based on OriginalParentId
            OriginalParentTitle = Marker;
        }
            
        
        // get all the valid entries for the providing parent selection. 
        // - All pages except its children and itself can be parent to the 
        // current page
		var items = new Array;
		var recentPages = wm.getValidParentList();		
		if(recentPages.length == 0)
		{
		    if(!dw.isPageEnabled())
		    {
		        items[0] = connectTo + ";id='" + selectEntryID + "'";
		        CurrentItem = selectEntryID;
		        return items;
		    }		    
		}
		
		var item = 0;
		
		if(OriginalParentId == selectEntryID)
		{
		    items[item++] = defaultPage + OriginalParentTitle + ";id='" + selectEntryID + "'";		    
		}
		else
		{
		    items[item++] = defaultPage + ";id='" + selectEntryID + "'";
		}
		
		// inserted as title followed by page id..
		for (i = 0; i < recentPages.length; i += 2)
		{
		    if(OriginalParentId == recentPages[i + 1])
		    {
	            items[item++] = recentPages[i] + OriginalParentTitle + ";id='" + recentPages[i + 1] + "'";
		    }
		    else
		    {
	            items[item++] = recentPages[i] + ";id='" + recentPages[i + 1] + "'";
	        }
	    }						
		return items;
	}
	return null;
}

function isCommandChecked()
{
	var value = getCurrentValue();
	if(value.length == 0)
		return false;

	if(value.search(arguments[0]) == -1)
	    return false;
	else
	    return true;
}


</script>
</head>

<body>
</body>
</html>

