
/* * Category and Navbar Management {{{ */
function TranslateCategoryToPath(categoryName)
{
	categoryName = categoryName.toLowerCase();
	categoryName = categoryName.replace("[news] ", "").replace("[business] ", "");
	return GetCategoryMapping(categoryName + "/");
}
function GetCategorySpecificity(category)
{
	specificity = category.split("/").length - 1;
	return specificity;
}
function GetCategoryMapping(cat)
{
	if(cat == "/community/neighbors/")
	{
		return "/news/life/neighbors/";
	}
	else if(cat == "/news/nationworld/")
	{
		return "/news/nation-world/";
	}
	else if(cat == "/sports/community sports/")
	{
		return "/news/sports/community/";
	}
	else if(cat == "/sports/prep sports/")
	{
		return "/news/sports/preps/";
	}
	else if(cat == "/opinion/letters to editor/")
	{
		return "/news/opinion/letters/";
	}
	else if(cat.substring(0, 5) != "/news")
	{
		return "/news" + cat;
	}
	else
	{
		return cat;
	}
}
function GetCategoryTranslation(categories)
{
	bestMatch = undefined;
	matchSpecificity = 0;

	catMax = categories.length;
	catCurr = 0;

	while(catCurr < catMax)
	{
		currCategory = categories[catCurr];
		path = TranslateCategoryToPath(currCategory);
		elementMatch = $('#sf-main-navigation a[href="' + path + '"]');
		if(elementMatch.length > 0)
		{
			specificity = GetCategorySpecificity(path);
			if(specificity > matchSpecificity)
			{
				bestMatch = path;
				matchSpecificity = specificity;
			}
		}
		catCurr = catCurr + 1;
	}
	return bestMatch;
}
function CheckForAssignedPath()
{
	var variables = GetUrlVars();
	if(variables["navPath"])
	{
		return variables["navPath"];
	}
	else
	{
		return false;
	}
}
function GetUrlVars()
{
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

	for(var i = 0; i < hashes.length; i++)
	{
		hash = hashes[i].split("=");
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}
function SetNavigationStatus(path)
{	 
	if(path === undefined)
	{
		if(CheckForAssignedPath())
		{
			path = CheckForAssignedPath();
		}
		else if(typeof(ArticleCategories) === "undefined")
		{
			path = window.location.pathname;
		}
		else
		{
			path = GetCategoryTranslation(ArticleCategories);
		}
	}
	currentElement = $('#sf-main-navigation a[href="' + path + '"]');
	if(currentElement.length > 1)
	{
		/* if we match more than one element, let's select the second. */
		currentElement = jQuery(currentElement[1])
	}

	if(currentElement.parent().parent().parent().parent().attr("tagName") == "UL")
	{
		/* Second Level Navigation is selected */
		// Mark current parent LI as hovered
		currentElement.parent().parent().parent().attr("class", "sfHover");
		// Mark current parent UL as visible
		currentElement.parent().parent().css("display", "block").css("visibility", "visible")
		// Mark current LI as current
		currentElement.parent().attr("class", "current");
	}
	else
	{
		/* First Level Navigation is selected */
		// Mark adjacent UL as visible
		currentElement.parent().children("ul").css("display", "block").css("visibility", "visible");
		// Mark self as selected
		currentElement.parent().attr("class", "sfHover");
	}
}
function ClearNavigationStatus(path)
{
	if(path === undefined)
	{
		if(CheckForAssignedPath())
		{
			path = CheckForAssignedPath();
		}
		else if(typeof(ArticleCategories) === "undefined")
		{
			path = window.location.pathname;
		}
		else
		{
			path = GetCategoryTranslation(ArticleCategories);
		}
	}
	var currentElement = $('#sf-main-navigation a[href="' + path + '"]');
	var firstParent = currentElement.parent();
	firstParent.attr("class", "");
	ulChildren = firstParent.children("ul").css("visibility", "hidden").css("display", "none");

	var secondParent = firstParent.parent().parent()
	if(secondParent.attr("tagName") == "LI")
	{
		secondParent.attr("class", "");
		firstParent.parent().css("display", "none").css("visibility", "hidden");
	}
}
jQuery(function(){
	if(typeof(jQuery('ul.sf-menu').superfish) == "function")
	{
		jQuery('ul.sf-menu').superfish({
			pathClass: 'current',
			autoArrows: false,
			onHide: SetNavigationStatus(),
			onBeforeShow: ClearNavigationStatus()
			});
	}
});
$(document).ready(function(){		 
	SetNavigationStatus()	 
});
/* }}} 
 * Ellington Voting {{{ */
function votemod(content_type, object_id, mod, original)
{
	var url = "/ratings/vote/";
	var data = {value: String(mod), object_id: String(object_id), content_type: String(content_type)};
	voting_object = $("#item_score_" + String(content_type) + "_" + String(object_id));
	var current_score = eval('item_vote_original_' + String(content_type) + '_' + String(object_id));
	if(mod == original)
	{
		var new_score = current_score;
	}
	else
	{
		var new_score = current_score + mod;
	}
	if(new_score >= 0)
	{
		var new_score_string = String(new_score);
	}
	else
	{
		var new_score_string = String(new_score);
	}
	if(mod >= 0)
	{
		voting_object.addClass("upvote")
		voting_object.removeClass("downvote");
	}
	else
	{
		voting_object.addClass("downvote")
		voting_object.removeClass("upvote");
	}
	voting_object.html(new_score_string);
	$.post(url, data, function(){}, "json");
}

/* Comment Customizations */
function reveal_comments()
{
	$("div.comment_list").show();
	$("div.comment_gate").hide();
}
/* }}}
 * Google Ads {{{ */
var google_ad_position_queue = new Array();
var DFP_ACCOUNT = "ca-pub-9467325037722181";
function get_ad_placeholder_object(name)
{
	var ga_ad_placeholder = $("#ga_ad_placeholder_" + name);
	var this_ad_spot_div_id = "#google_ads_div_" + name;
	var this_ad_spot_div = $(this_ad_spot_div_id);

	var cache_object = new Object();
	cache_object.placeholder = ga_ad_placeholder;
	cache_object.spot = this_ad_spot_div;
	return cache_object;
}
function post_registrations_to_google()
{
	var curr = 0;
	var queue_length = window.google_ad_position_queue.length;
	while(curr < queue_length)
	{
		this_ad_spot = window.google_ad_position_queue[curr]
		GA_googleAddSlot("ca-pub-9467325037722181", this_ad_spot)
		curr = curr + 1;
	}
}
function load_ads_and_replace()
{
	GA_googleFetchAds();
	var curr = 0;
	var queue_length = window.google_ad_position_queue.length;
	while(curr < queue_length)
	{
		this_ad_spot = window.google_ad_position_queue[curr]
		GA_googleFillSlot(this_ad_spot)
		curr = curr + 1;
	}
}
function debug_announce(message)
{
	/*
	var page = $("#page");
	page.after("<div>" + message + "</div>");
	*/
}
function position_ads()
{
	var curr = 0;
	var queue_length = window.google_ad_position_queue.length;
	while(curr < queue_length)
	{
		var this_ad_spot = window.google_ad_position_queue[curr];
		var cache_object = get_ad_placeholder_object(this_ad_spot);
		this_ad_spot_div = cache_object.spot;
		ga_ad_placeholder = cache_object.placeholder;

		if(! this_ad_spot_div.hasClass("do_not_reposition"))
		{
			if(this_ad_spot_div.length > 0)
			{
				ga_ad_placeholder.addClass("ad");
				ga_ad_placeholder.css("display", "block");
				if(ga_ad_placeholder.css("position") == "fixed")
				{
					this_ad_spot_div.css("position", "fixed");
					this_ad_spot_div.css("left", ga_ad_placeholder.css("left"));
					this_ad_spot_div.css("top", ga_ad_placeholder.css("top"));
					this_ad_spot_div.css("right", ga_ad_placeholder.css("right"));
					this_ad_spot_div.css("bottom", ga_ad_placeholder.css("bottom"));
					this_ad_spot_div.css("width", ga_ad_placeholder.css("width"));
					this_ad_spot_div.css("z-index", ga_ad_placeholder.css("z-index") + 1)
					if($.browser.msie && jQuery.browser.version < 8.0)
					{
						this_ad_spot_div.css("margin-left", ga_ad_placeholder.css("margin-left"));
						this_ad_spot_div.css("left", "50%");
					}
					else
					{
						this_ad_spot_div.css("margin-left", "auto");
						this_ad_spot_div.css("margin-right", "auto");
					}
				}
				else
				{
					this_ad_spot_div.css("position", "absolute");
					this_ad_spot_div.css("left", ga_ad_placeholder.offset().left + "px");
					this_ad_spot_div.css("top", ga_ad_placeholder.offset().top + "px");
				}
				if($.browser.msie == true)
				{
					// For some undetermined reason, IE likes to misplace ads loaded from
					// doubleclick.	 I first check if the advertisement exists where I 
					// expect it to be, and if it doesn't, see if one might be where 
					// IE predictably (and, unless I'm mistaken, wrongly) puts it.
					var this_ad_children = this_ad_spot_div.children();
					if(this_ad_children.length == 1)
					{
						if(this_ad_children[0].nodeName.toUpperCase() == "SCRIPT")
						{
							// Yep-- only one node in this ad position, and it's a script tag.
							// this is the telltale sign of this bug.
							var first_object_child = $("#temporary_placement_holder > object")[0];
							this_ad_spot_div.append(first_object_child);
						}
					}
				}
			}
		}
		curr = curr + 1;
	}
}
function register_ad_position(position_name)
{
	window.google_ad_position_queue.push(position_name);
}
/* }}} 
 * Category Name Googlization {{{ */
function GoogleizeURL(ungooglized)
{
	var valid_characters = /\//;
	var parts = Array();
	var unclean_parts = ungooglized.split(valid_characters);
	var k = 0;
	var ktop = unclean_parts.length;
	while(k < ktop)
	{
		var this_part = unclean_parts[k];
		if(this_part.length > 0)
		{
			parts.push(this_part.toLowerCase());
		}
		k = k + 1;
	}
	return parts.join("_");
}
function GoogleizeCategory(ungooglized)
{
	var split_category = /] /;
	var parts = Array();
	var unclean_major_parts = ungooglized.split(split_category);
	parts.push(unclean_major_parts[0].substr(1).toLowerCase());

	var split_tiers = /\//;
	var unclean_minor_parts = unclean_major_parts[1].split(split_tiers);
	var k = 0;
	var ktop = unclean_minor_parts.length;
	while(k < ktop)
	{
		var this_part = unclean_minor_parts[k];
		if(this_part.length > 0)
		{
			var split_tier_components = /[^a-zA-Z0-9]/;
			var this_part_parts = this_part.split(split_tier_components);
			var m = 0;
			var mtop = this_part_parts.length;
			var this_part_parts_arr = Array();
			while(m < mtop)
			{
				var this_part_parts_part = this_part_parts[m];
				if(this_part_parts_part.length > 0)
				{
					this_part_parts_arr.push(this_part_parts_part);
				}
				m = m + 1;
			}
			this_part_string = this_part_parts_arr.join("-");
			parts.push(this_part_string.toLowerCase())
		}
		k = k + 1;
	}
	return parts.join("_");
}
/* }}}
 * 360Marketplaces {{{ */
function populate_360_promo()
{
	restriction = $("#marketplaces-widget-restriction").text();
	if(restriction.length < 1)
	{
		restriction = "columbian"
	}
	API_URL = "http://www2.columbian.com/api/marketplaces/random/" + restriction + "/?callback=?";
	jQuery.getJSON(API_URL, function(data){
		$("#marketplaces-widget-new div.info div.name").html('<a href="' + data.url + '">' + data.name + '</a>');
		$("#marketplaces-widget-new div.info div.phone").html(data.phone);
		$("#marketplaces-widget-new div.info div.keywords").html(data.keywords);
		$("#marketplaces-widget-new div.info div.address").html(data.address + "<br />" + data.city + ", " + data.state);
		if(data.thumbnail != '')
		{
			info_div = $("#marketplaces-widget-new div.info");
			info_div.append("<div class='thumbnail'><a href='" + data.url + "'><img class='thumb' src='" + data.thumbnail + "' alt='" + data.name + "'/></a></div>");
			thumb_image = $("#marketplaces-widget-new div.info img.thumb");
			thumb_div = $("#marketplaces-widget-new div.thumbnail");
			thumb_div.height(data.thumbnail_height + "px");
			thumb_div.width(data.thumbnail_width + "px"); 
		}
		else
		{
			$("#marketplaces-widget-new div.info div.thumbnail").remove();
		}
		pageTracker._trackEvent('mps360 widget', 'load', data.slug);
	});
}

$(document).ready(function(){
	populate_360_promo();
});
/* }}}

 * Flag Staff Members {{{ */
function post_staff_message(user_id, ip_address, staff_status)
{
	API_URL = "http://www2.columbian.com/authenticate/verify/" + user_id + "/" + ip_address + "/" + staff_status + "/";
	jQuery.getJSON(API_URL, function(data){});
}
/* }}} */

// Detect "Externally" marked links and open them in a new window.

$(document).ready( function() {
	$('a[rel="external"]').attr('target', '_blank');
});
