// Copyright 2001, 2002, 2003 Macromedia, Inc. All rights reserved.

var defaultNumberOfBlogEntries = 5;
var selectEntryID = "CT_SELECT_ENTRY_ID";
var LastEntry = "ID_LAST_INDEX";

function receiveArguments()
{
	var itemID = arguments[0];
	
	if(itemID == selectEntryID)
		return;

	var dom = dw.getDocumentDOM();
	if (dom)
	{	   		
		var wm = CCWorkspaceManager.getManager(dom);
		
	    if(itemID == "ID_SHOW_POSTS")
	    {
	        wm.setBlogEntryTypeToShow("posts");
	    }
	    else if(itemID == "ID_SHOW_PAGES")	    
	    {
	        wm.setBlogEntryTypeToShow("pages");
	    }
	    else if(itemID == "ID_SHOW_BOTH")
	    {
	        wm.setBlogEntryTypeToShow("all");
	    }
	    else if (itemID == 'ID_SPACER' || itemID == 'ID_LAST_INDEX')
	    {
		    // don't do anything for the spacers
		}
		else 
		{ 
		    // browse to the url mapped to by the itemID
		    if (wm)
		  	    wm.setURLAndState(itemID, 'browse');
		  
		  //	var browser = dw.getBrowser();
		  //	browser.openURL(itemID);

		}
		if(wm)
		    wm.updateUI();
	}	
}

function canAcceptCommand()
{
	if(!dw.isCCOnline() ||( dw.getDocumentDOM() != null && CCWorkspaceManager.getManager(dw.getDocumentDOM()) != null && (!CCWorkspaceManager.getManager(dw.getDocumentDOM()).isBlogSite())))
		return false;
	else
	{
		var itemID = arguments[0];
		var dom = dw.getDocumentDOM();
		var wm = null;
		if (dom)
			wm = CCWorkspaceManager.getManager(dom);
		var temp = CCWorkspaceManager.getManager(dw.getDocumentDOM()).getBlogEntryList('all');
		if(temp.length > 0)
			return true;
		else
			return false;
	}
}

function showIf()
{
	return (dw.getDocumentDOM()!=null && CCWorkspaceManager.getManager(dw.getDocumentDOM()) != null && CCWorkspaceManager.getManager(dw.getDocumentDOM()).isBlogSite());
}

function getCurrentValue()
{
	var dom = dw.getDocumentDOM();
	if (dom)
	{
		var wm = CCWorkspaceManager.getManager(dom);
		if (wm)
		{
			if(!wm.isBlogSite())
				return selectEntryID;
				
			var browser = dw.getBrowser();
			var curURL;
			if(browser)
				curURL = browser.getURL();
			    
			var blogEntryList = wm.getBlogEntryList(wm.getBlogEntryTypeToShow());
			var i;

			for (i = 0; i < blogEntryList.length; i += 2)
			{
				if(blogEntryList[i+1] == curURL)
				{
					return blogEntryList[i+1];
				}
			}
		}
	}
	return selectEntryID;		
}


function getDynamicContent()
{
 	var dom = dw.getDocumentDOM();
	if (dom)
	{
		var wm = CCWorkspaceManager.getManager(dom);
		if (wm)
		{
			if(!wm.isBlogSite())
			{
				return null;
			}
			
			// get the count of the number of Pages and Posts.
			var nPageCnt = wm.getEntriesCount('pages');
			var nPostCnt = wm.getEntriesCount('posts');
            
            // get the entries of the type user wants to see
            var curSelGroup = wm.getBlogEntryTypeToShow();               
			var recentPages = wm.getBlogEntryList(curSelGroup);
			
            var selectEntryText = "";
            
            if(curSelGroup == "posts")
            {
                if(nPostCnt)
                    selectEntryText = "<" + dw.loadString("BlogEntryToolbar/select post entry") + ">";
                else
                {
                    curSelGroup = "all";
                    var bSuccess  = wm.setBlogEntryTypeToShow(curSelGroup);
                }
            }
            else if(curSelGroup == "pages")
            {
                if(nPageCnt)
                    selectEntryText = "<" + dw.loadString("BlogEntryToolbar/select page entry") + ">";
                else
                {
                    curSelGroup = "all";
                    var bSuccess  = wm.setBlogEntryTypeToShow(curSelGroup);
                }
            }
            
            if(curSelGroup == "all")
            {
                // now choose the string to be displayed based on the case how we arrived here..
                if(nPageCnt == 0 && nPostCnt == 0)
                    selectEntryText = "<" + dw.loadString("BlogEntryToolbar/select blog entry") + ">";
                else if(nPageCnt == 0)
                    selectEntryText = "<" + dw.loadString("BlogEntryToolbar/select post entry") + ">";
                else if(nPostCnt == 0)
                    selectEntryText = "<" + dw.loadString("BlogEntryToolbar/select page entry") + ">";
                else // none of them are 0
                    selectEntryText = "<" + dw.loadString("BlogEntryToolbar/select blog entry") + ">";
			}
			
			// now get the entries in 'curSelGroup' into an array and return the same!
			var i;
			var item = 0;
            
			var items = new Array;
			
			items[item++] = selectEntryText + ";id='" + selectEntryID + "'";
			
			// If there are no entries, just return the list with description.
			if(nPageCnt == 0 && nPostCnt == 0)
			    return items;
			
			var PostsOnly  = dw.loadString("BlogEntryToolbar/show blog entries only");
			var PagesOnly = dw.loadString("BlogEntryToolbar/show blog pages only");
			var BothPostsNPages = dw.loadString("BlogEntryToolbar/show both blog entries and pages");
			
			var bAddedGroups = true;
			
			// Add the Groups!
			if(curSelGroup == "posts")
			{
                items[item++] = PagesOnly + ";id='ID_SHOW_PAGES'";
                items[item++] = BothPostsNPages + ";id='ID_SHOW_BOTH'";                
            }
            else if(curSelGroup == "pages")
            {
                items[item++] = PostsOnly+ ";id='ID_SHOW_POSTS'";
                items[item++] = BothPostsNPages + ";id='ID_SHOW_BOTH'"; 
            }
            else if(nPageCnt && nPostCnt)
            {
                items[item++] = PostsOnly + ";id='ID_SHOW_POSTS'";
                items[item++] = PagesOnly + ";id='ID_SHOW_PAGES'";
            }
            else
                bAddedGroups = false;
            
			// Add the spacer
            if(bAddedGroups)
            {
			    items[item++] = "------------------------------------------;id='ID_SPACER'";	
			}
			
			// Add the entries into the array!
			for (i = 0; i < recentPages.length; i += 2)
			{
				items[item++] = recentPages[i] + ";id='" + recentPages[i + 1] + "'";
			}
			return items;
		}
	}
	return null;
}
