// cBlog.js - Custom routines for Blog site definition
// Copyright 2004, Collutions, Inc.

// Revisions:
// 12/10/04 - Added Menus options to go straight to lists: cBlogSettingsMenu 0.2



// Persistence
// These routines persist information to the user's computer
// kind of like cookies. Here persistence is used to 'remember'
// which days posts have been collapsed and restore that state
// when the visitor returns to the site. These routines rely on
// modifications to the <Views> in the SCHEMA.XML file for the
// Blog list. For more information on using persistence to store
// information on a visitors computer, see the following blog post:
// http://dev.collutions.com/blog/pages/persistence.aspx

function calcGroupName(oCell)
{
// calcGroupName version 0.1
// calculates the group name to store in UserData

	// Remove or replace illegal characters from the cell contents
	groupname = oCell.innerText.replace(/ /g,""); // Spaces
	groupname = groupname.replace(/,/g,""); // Commas
	groupname = groupname.replace(/:/g,"_"); // Colons
	// Find open parentheses
	index = groupname.lastIndexOf("(");
	// Trim off item count
	return(groupname.slice(0,index-1));
}

function cBlogSave()
{
// cBlogSave version 0.1
// saves expand/collapse state of groups

	// check for divUserData element which is used to store UserData
	if (document.getElementById('divUserData') == null) return;

	// collect all images that are children of the div tag
	oImgs = divUserData.getElementsByTagName('IMG');

	// loop through collection
	for (i=0; i<oImgs.length;i++) {
		oImg = oImgs[i];
		// make sure the image's id contains 'img'
		if (oImg.id.indexOf('img') != -1)
		{
			// find the table cell that contains the image
			oCell = oImg.parentElement;
			// calculate the group name from the cell
			groupname = calcGroupName(oCell);
			// get the row that the cell lives in
			oParentRow = oCell.parentElement;
			// the group type will be either "Parent" or "Child"
			// this supports views with more than one grouping
			// the group type is set in the <GroupHeader> element in the view
			if (oParentRow.getAttribute("groupType") == "Parent") {
				parentGroupName = groupname;
			} else {
				groupname = parentGroupName + "_" + groupname;
			}
			
			// check the image source and respond accordingly
			if (oImg.src.indexOf('minus.gif') != -1) {
				divUserData.setAttribute(groupname, 'expand');
			} else {
				divUserData.setAttribute(groupname, 'collapse');
			}
		}
	}
	// save the UserData element
	divUserData.save("cBlogUserData");
}

function cBlogLoad()
{
// cBlogLoad version 0.1
// loads expand/collapse states of groups
// and adds items to the Modify Shared Page menu

	// set up the steeings menu
	cBlogSettingsMenu();

	// ensure that the page contains a divUserData element
	if (document.getElementById('divUserData') == null) return;

	// load the information from UserData
	divUserData.load("cBlogUserData");	

	// collect all images that are children of the div tag
	oImgs = divUserData.getElementsByTagName('IMG');

	// loop through the collection
	for (i=0; i<oImgs.length;i++) {
		oImg = oImgs[i];
		// make sure the image's id contains 'img'
		if (oImg.id.indexOf('img') != -1) {
			// find the table cell that contains the image
			oCell = oImg.parentElement;
			// calculate the group name from the cell
			groupname = calcGroupName(oCell);
			// get the row that the cell lives in
			oParentRow = oCell.parentElement;
			// the group type will be either "Parent" or "Child"
			// this supports views with more than one grouping
			// the group type is set in the <GroupHeader> element in the view
			if (oParentRow.getAttribute("groupType") == "Parent") {
				parentGroupName = groupname;
			} else {
				groupname = parentGroupName + "_" + groupname;
			}
			// check to see if the croup was collapsed when the user left
			// the page and click the cell if needed
			if(divUserData.getAttribute(groupname)== 'collapse') {
				oCell.parentElement.click();
			}
		}
	}
}

function cBlogSettingsMenu()
{
// cBlogSettingsMenu version 0.2
// adds items to the Modify Shared Page menu

	// check to see if the page actually contains a menu
	if(typeof(L_Menu_BaseUrl) == "undefined") return;
	// get reference to menu element
	var m = document.all["MSOMenu_SettingsMenu"];
	// proceed only if the menu exists
	if(null != m)
	{
		// add seperator
		CAMSep(m);
		// add items to go straight to lists (v0.2 revision)
		CAMOpt(m,"View/Edit Blog Posts","javascript:STSNavigate(L_Menu_BaseUrl + '/Lists/Blog/AllItems.aspx')","/_layouts/images/ittxtbox.gif","");
		CAMOpt(m,"View/Edit Comments","javascript:STSNavigate(L_Menu_BaseUrl + '/Lists/Comments/AllItems.aspx')","/_layouts/images/itdisc.gif","");
		CAMOpt(m,"View/Edit Links","javascript:STSNavigate(L_Menu_BaseUrl + '/Lists/Links/AllItems.aspx')","/_layouts/images/itlink.gif","");
		CAMOpt(m,"Blog/Feed Settings","javascript:STSNavigate(L_Menu_BaseUrl + '/_layouts/1033/cbSettings.aspx')","","");
		// add seperator
		CAMSep(m);
		// add items usually found in top navigation bar
		CAMOpt(m,"Documents and Lists","javascript:STSNavigate(L_Menu_BaseUrl + '/_layouts/1033/viewlsts.aspx')","","");
		CAMOpt(m,"Create","javascript:STSNavigate(L_Menu_BaseUrl + '/_layouts/1033/create.aspx')","","");
		CAMOpt(m,"Site Settings","javascript:STSNavigate(L_Menu_BaseUrl + '/_layouts/1033/settings.aspx')","","");
		CAMOpt(m,"Help","javascript:HelpWindowKey('NavBarHelpHome')","/_layouts/images/helpicon.gif","Help");
	}
}

// Utility function to add an event listener
function addEvent(o,e,f)
{
	if (o)
	{
		if (o.addEventListener)
		{
			o.addEventListener(e,f,true);
			return true;
		}
		else if (o.attachEvent)
		{
			return o.attachEvent("on"+e,f);
		} else {
			return false;
		}
	}

}

// add onload and onunload event handlers
if ((browseris.ie4up) && !browseris.mac) 
{
	// Automatically attach a listener to the body onload and body onunload to load/save persistent information
	try{
		if (document.body.onload != null)
		{
			addEvent(document.body,"load",cBlogLoad);
		} else {
			document.body.onload = cBlogLoad;
		}

		if (document.body.onunload != null)
		{
			addEvent(document.body, "unload", cBlogSave);
		} else {
			document.body.onunload = cBlogSave;
		}
	} catch(e) {}
}

