﻿// VW - 11.15.2011 (INC209833)
// VW - 11.23.2011 (INC211583)
// VW - 12.15.2011 (INC215241)
// AJP - 12.22.2011 (FEAT12171)
// VW - 01.03.2012 (FEAT12171)
var departmentNavigationID = null;
var currentLocation = null;
var dcpAddress = "/depts/dcp/";
var ecoAddress = "/depts/eco_county/";
var commissionerAddress = "/depts/countycommissioners/";
var electionFilipinoAddress = "/depts/election_fil/";

var commissionerRenames = 
{
	"about us": "Biography"
};
			
var electionRenames = 
{
	"services": "Mga Serbisyo",
	"contact us": "Tawagan o Sulatan Kami",
	"about us": "Tungkol sa Amin"
};

var compatibilityView = false;
var ignoreText = ["quick look-up"];
var multiLineID = {"id_ML": ["id", "_ML"]};
var multiLineClass = {"class_ML": ["class", "_ML"]};
var compatibilityViews = ["CC_FeaturedEvents_ViewAllEvents", "CC_FeaturedEventsNoScroll_ViewAllEvents"];

var electionWebParts = 
{
	"CC_Explore_Header": 
	[
		{"explore": "Esplorahin o Siyasatin"}
	],
	"spotlightWrapper": 
	[
		multiLineClass,
		{"service spotlight": "Serbisyo na may Ilaw ng Lente"},
		{"read more": "Magbasa Pa"}
	],
	"CC_FeaturedEvents": 
	[
		multiLineClass,
		{"upcoming events": "Mga Nalalapit na Kaganapan"}
	],
	"CC_FeaturedEvents_ViewAllEvents":
	[
		multiLineClass,
		{"view all events": "Tingnan ang Lahat ng mga Kaganapan"}
	],
	"CC_FeaturedEventsNoScroll": 
	[
		multiLineClass,
		{"upcoming events": "Mga Nalalapit na Kaganapan"}
	],
	"CC_FeaturedEventsNoScroll_ViewAllEvents": 
	[
		multiLineClass,
		{"view all events": "Tingnan ang Lahat ng mga Kaganapan"}
	],
	"leftnavHeading": 
	[
		multiLineClass
	],
	"CC_FeaturedEvents_HeaderCalIcon": 
	[
		multiLineClass
	],
	"CC_DeptNav": 
	[
		multiLineID
	]
};

var electionMenuItems = ["CC_NavItem", "CC_NavItem_Flyout", "CC_NavSubItem"];

$(document).ready
(
	function()
	{
		//Get the current location.
		currentLocation = getLocation();
		
		//Perform Comp Planning left navigation menu item moves.
		//See INC numbers above.
		updateItems(dcpAddress);
		updateItems(ecoAddress);
		
		//Perform department/division left navigation menu item and web part item renames.
		//See INC numbers above.
		updateItems(commissionerAddress);
		updateItems(electionFilipinoAddress);
	}
);

function getLocation()
{
	//Returns the current web location.
	return $(location).attr("href").toLowerCase();
}

function parseLocation(fullyQualifiedLocation, locationMatch)
{
	//Parse the current location to get a match on the desired location.
	var index = fullyQualifiedLocation.indexOf(locationMatch);
	
	return (index > 0) ? locationMatch : null;
}

function updateItems(locationAddress)
{
	//Get the partial location.
	var partialLoc = parseLocation(currentLocation, locationAddress);
	
	//Process the renames.
	switch (partialLoc)
	{
		case dcpAddress:
		case ecoAddress:
			compPlanningMenuItemMoves();
		
			break;
	
		case commissionerAddress:
			renameMenuItems(commissionerRenames);
			
			break;
			
		case electionFilipinoAddress:
			getCompatibilityView();
			renameMenuItems(electionRenames);
			renameWebPartItems(electionWebParts);
			updateWebPartStyles(electionWebParts);
			updateMenuItemStyles(electionMenuItems);
			
			break;
			
		default:
			break;
	}
}

function compPlanningMenuItemMoves()
{
	$("ul[id*='_deptNav_']").each
	(
		function()
		{
			departmentNavigationID = $(this).attr("id");

			$(getSelector("Department Home")).prependTo("#" + departmentNavigationID);
			$(getSelector("Services")).insertAfter(getSelector("Divisions"));
		}
	);
}

function getSelector(pText)
{
	return "#" + departmentNavigationID + " li:contains('" + pText + "')";
}

function renameMenuItems(menuItems)
{
	//Loop through and find items to rename.
	$("ul[id*='_deptNav_']").each
	(
		function()
		{
			$("#" + $(this).attr("id") + " a").each
			(
				function()
				{
					var hyperLink = $(this);
					
					hyperLink.html(menuItems[hyperLink.html().toLowerCase()]);
				}
			);
		}
	);
}

function getCompatibilityView()
{
	if (document.documentMode) compatibilityView = (document.documentMode < 8) ? true : false;
}

function renameWebPartItems(webParts)
{
	for (var webPart in webParts)
	{
		$("div." + webPart).each
		(
			function()
			{
				var div = $(this);
				var divChildren = div.find("div");
				var webPartRenames = webParts[webPart];

				if (divChildren.length == 0)
				{
					replaceSearchText(div, webPartRenames);
				}
				else
				{
					divChildren.each
					(
						function()
						{
							div = $(this);

							replaceSearchText(div, webPartRenames);
						}
					);
				}
			}
		);
	}
}
	
function replaceSearchText(pObject, pRenames)
{
	for (var x = 0; x < pRenames.length; x++)
	{
		for (var rename in pRenames[x])
		{
			var searchText = rename;

			if (!(eval(/_ML$/gi)).test(searchText))
			{
				var replaceText = pRenames[x][rename];

				pObject.html(pObject.html().replace(eval("/" + searchText + "/gi"), replaceText));
			}
		}
	}
}

function updateWebPartStyles(webParts)
{
	for (var webPart in webParts)
	{
		var objects = getObjects(webPart);

		if (objects != null)
		{
			objects.each
			(
				function()
				{
					var firstItem = webParts[webPart][0];
					var firstItemArray = (firstItem["id_ML"] == null) ? firstItem["class_ML"] : firstItem["id_ML"];

					if (firstItemArray != null)
					{
						var object = $(this);

						if (!isIgnoreText(object.html())) object.attr(firstItemArray[0], webPart + firstItemArray[1] + checkCompatibilityView(webPart));
					}
				}
			);
		}
	}
}

function getObjects(pID)
{
	var tags = ["#", "div.", "span."];

	for (var x = 0; x < tags.length; x++)
	{
		var objects = $(tags[x] + pID);

		if (objects.length > 0) return objects;
	}

	return null;
}

function isIgnoreText(pText)
{
	for (var x = 0; x < ignoreText.length; x++)
	{
		if (pText.toLowerCase().indexOf(ignoreText[x].toLowerCase()) >= 0) return true;
	}

	return false;
}

function checkCompatibilityView(pWebPart)
{
	if (compatibilityView) if ($.inArray(pWebPart, compatibilityViews) >= 0) return "CV";

	return "";
}

function updateMenuItemStyles(menuItems)
{
	for (var x = 0; x < menuItems.length; x++)
	{
		$("li." + menuItems[x] + " a").each
		(
			function()
			{
				var listItemAnchor = $(this);

				if (listItemAnchor.html().length <= 22)
				{
					var listItemAnchorParent = listItemAnchor.parent();
					var flyoutArrowClass = "CC_NavItem_Flyout_Arrow";
					var flyoutArrow = listItemAnchorParent.find("span." + flyoutArrowClass);

					listItemAnchorParent.css("height", "22px");

					if (flyoutArrow.length > 0) flyoutArrow.attr("class", flyoutArrowClass + "_ML");
				}
			}
		);
	}
}
