// This file uses functions from the blogDataObjects.js file in order to match values for Omniture Variables, Author and Fray Links, and Blog Styles
// Keep in mind that whenever a new USER and/or BLOG is created/updated in Community Server, the data in these files must be updated to match 
var blogName = getBlogName();

function mapAnchors()
{
	var ul = document.getElementById("blog_posts_list");
	var lis = ul.getElementsByTagName("li");
	var links = new Array();
	var span;
	
	var hostname = window.location.hostname
	//create fray host name
	if (hostname.indexOf("www") >=0 )
	{
		hostname = hostname.replace(/www./, "")
	}	
	var frayhost = "fray." + hostname;
	
	for (var i=0; i<lis.length; i++)
	{
		links = SlateDom.getElementsByClassName(lis[i].getElementsByTagName("a"), "postTitleAnchor");
		links[1].href = links[0].href + "#add-comment";		
		SlateDom.getElementsByClassName(lis[i].getElementsByTagName("a"), "js_kit_comments")[0].href = links[1].href;
		
		span = SlateDom.getElementsByClassName(lis[i].getElementsByTagName("span"), "js-kit-comments-count")[0];
		span.setAttribute("uniq", links[0].href.substr(links[0].href.indexOf(".com/")+5))

		SlateDom.getElementsByClassName(lis[i].getElementsByTagName("a"), "fray-comments-link")[0].href = "http://" + frayhost + "/discuss/forums/" + getOmniVars()['frayId'] + "/ShowForum.aspx";
	}
}

function getOmniVars()
{
	var blog;
	var blogs = getBlogData(blogName); //-- array of blogs to track with Omniture
	
	//MAS: 11/24/2008: Checkes to see if the object comming back is either a blog or an array of blogs.
	if(blogs.length > 0) 
	{
		if (!window.location.toString().match('searchresults.aspx')) //-- if this is the default blog listing
		{
			for (var i=0;i<blogs.length;i++)
			{
				if (window.location.toString().match(blogs[i].name))
				{
					blog = getBlogData(blogs[i].name);
				}
			}
		}
	}
	else
	{
		blog = blogs;
	}

	//MAS: 11/24/2008: If the object is null then send an empty object, else send the object that was selected form either of the logic tests.
	if(blog == null)
	{
		var OmniVars = new Object();
			OmniVars.pstax = "";
			OmniVars.frayId = "";
			OmniVars.dept = "";
			OmniVars.sect = "";
			OmniVars.auth = "";
			OmniVars.comNode = "";
			
		return OmniVars;
	}
	else
	{
		var OmniVars = new Object();
			OmniVars.pstax = blog.pstax;
			OmniVars.frayId = blog.frayId;
			OmniVars.dept = blog.dept;
			OmniVars.sect = blog.sect;
			OmniVars.auth = blog.auth;
			OmniVars.comNode = blog.commercialNode;

		return OmniVars;
	}
}

function setFrayLinks()
{
	document.getElementById('blog_discuss').href = "http://fray.slate.com/discuss/forums/" + getOmniVars()['frayId'] + "/ShowForum.aspx";
	//MAS: 11/21/2008: commented out until I can find out if this link is needed in the re-design site.
	document.getElementById('fray_article_discussion_read').href = "http://fray.slate.com/discuss/forums/" + getOmniVars()['frayId'] + "/ShowForum.aspx";
	document.getElementById('fray_article_discussion_post').href = "http://fray.slate.com/discuss/forums/AddPost.aspx?ForumID=" + getOmniVars()['frayId'];
}

if(window.location.toString().match('SearchResults.aspx'))this.onload=setStyles();
function setStyles()
{		
	var stylesheet = document.createElement('link');
	stylesheet.setAttribute('type','text/css');
	stylesheet.setAttribute('rel','stylesheet');
	stylesheet.href =  '/blogs/blogs/'+ blogName +'/customcss.ashx';
	document.documentElement.firstChild.appendChild(stylesheet);
}

function setHeader()
{
	var title = document.getElementById('blog_head_title'); 
	var subhead = document.getElementById('blog_subhead');
	
	title.href = '/blogs/blogs/' + blogName;
	title.innerHTML = getBlogData(blogName).title;
	subhead.innerHTML = getBlogData(blogName).subhead;
}

function setAction()
{
	var form = document.getElementById('blog_quick_search_form');
	form.action = "/blogs/blogs/" + blogName + "/search.aspx"
}

function getBlogName()
{
	var blog;
	var url;
	var regex = /blogs\/blogs\/\w*/i;

	if(regex.test(document.location))
	{
		url = document.location;
	}
	else if(regex.test(document.referrer))
	{
		url = document.referrer;
	}
	else
	{
		url = null;
	}

	var blogPath = regex.exec(url);	

	if(blogPath != null)
	{
		blogPath = blogPath.toString();
		blog = blogPath.substr(blogPath.lastIndexOf("/") + 1);
	}
	else
	{
		try
		{
			var userBlogs = getUserData().blog;
		}
		catch(err)
		{
			var userBlogs = "";
		}
		
		if(typeof(userBlogs) == "string")
		{
			blog = userBlogs; 
		}
		else if(typeof(userBlogs) == "object")
		{
			blog = userBlogs[parseInt(Math.random()*userBlogs.length)];
		}
	}
	return blog;
}
