// file: base.js, Base Functionality Routines for HighMark Capital Group

/*global location, document, $ */

////////////////////////////////////////////////////////////////////
//  TOP MENU DROPDOWN LOGIC
////////////////////////////////////////////////////////////////////

/**
 * TITLE ........: Parse Query Params from the URL
 * PURPOSE.......: 
 * DEPENDENCIES .: n/a
 * USAGE ........: var sMyValue = queryString("MyKey");
 * PARAMETERS ...: 
 * CREDITS ......: http://webborg.blogspot.com/2007/07/querystring-get-parameters-from-url.html
 *
 * HISTORY
 * --------------------------------------------------------------------------
 *  03/02/2009, rwr Initial thievery
 */
function queryString(parameter) {
	var loc = location.search.substring(1, location.search.length);
	var param_value = false;
	var params = loc.split("&");
	var i;

	for (i = 0; i < params.length; i++) {
		var param_name = params[i].substring(0, params[i].indexOf('='));
		if (param_name === parameter) {
			param_value = params[i].substring(params[i].indexOf('=') + 1);
		}
	}
	if (param_value) {
		return param_value;
	} else {
		return false; //Here determine return if no parameter is found
	}
}

/**
 * TITLE ........: Initialize Logic for Top Dropdown Menu's
 * PURPOSE.......: Initialize top dropdown menu's by attaching event handlers
 *                 for on-click, mouseover & mouse-out events to the HTML.
 *                 Also, highlight the correct top-menu item by switching
 *                 to the correct navbar.jpg image.
 * DEPENDENCIES .: JQuery 1.2.26
 * USAGE ........: call function initTopMenu like so --
 *                  $(document).ready( function() { initTopMenu(); });
 * PARAMETERS ...: This function take 1 optional parameter.  The parameter
 *                 should look like "http://server.uboc.com" where 'server'
 *                 is the name of a web server that contains the Tridion
 *                 content of either the Production, Test or Development
 *                 environment.
 *
 * HISTORY
 * --------------------------------------------------------------------------
 *  12/18/2008, rwr Initial creation
 *  01/12/2009, rwr Added optional parm 'sURL' to handle the Search Results
 *                  page from the Google appliance being on a different
 *                  web server than the rest of the Tridion content.
 *  02/27/2009, rwr changed body tag selector to div#body selector
 */
function initTopMenu(sURL) {
	// add onClick event handler and hover effects for top menu
	$("#map_about, #dm_about").mouseover(function() {
		$("#dm_about").addClass("over");
	}).mouseout(function() {
		$("#dm_about").removeClass("over");
	});
	$("#map_institutional, #dm_institutional").mouseover(function() {
		$("#dm_institutional").addClass("over");
	}).mouseout(function() {
		$("#dm_institutional").removeClass("over");
	});
	$("#map_individual, #dm_individual").mouseover(function() {
		$("#dm_individual").addClass("over");
	}).mouseout(function() {
		$("#dm_individual").removeClass("over");
	});
	$("#map_news, #dm_news").click(function() {
		location.href = $(this).attr("href");
	}).mouseover(function() {
		$("#dm_news").addClass("over");
	}).mouseout(function() {
		$("#dm_news").removeClass("over");
	});

	// * OPTIONAL PARAMETER: sURL
	// **** If parameter 'sURL' has a value, prefix every image URL
	// ****  with the value of 'sURL'.
	// **** If parameter 'sURL' doesn't exist, don't prefix the
	// ****  image URL's with anything; just leave them as relative URL's
	//var sUseThisUrl = (sURL === undefined) ? ("") : (sURL);

	// highlight correct top menu item based on the class of the
	// <body> tag
	/*
	var sUseThisImg = "";
	if ($("div#body").hasClass("About")) {
		sUseThisImg = sUseThisUrl + "/images/navbar_about_tcm14-17228.jpg";
	} else if ($("div#body").hasClass("Institutional")) {
		sUseThisImg = sUseThisUrl + "/images/navbar_institutional_tcm14-17230.jpg";
	} else if ($("div#body").hasClass("Individual")) {
		sUseThisImg = sUseThisUrl + "/images/navbar_individual_tcm14-17229.jpg";
	} else if ($("div#body").hasClass("News_n_Commentary")) {
		sUseThisImg = sUseThisUrl + "/images/navbar_news_commentary_tcm14-17231.jpg";
	} else {
		sUseThisImg = sUseThisUrl + "/images/navbar_tcm14-17227.jpg";
	}
	$("div#navbar_bkgnd img:first").attr("src", sUseThisImg);
	*/
	if ($("div#body").hasClass("About")) {
		$('#h1-about').css('color', '#666');
	} else if ($("div#body").hasClass("Institutional")) {
		$('#h1-institutional').css('color', '#666');
	} else if ($("div#body").hasClass("Individual")) {
		$('#h1-individual').css('color', '#666');
	} else if ($("div#body").hasClass("News_n_Commentary")) {
		$('#h1-news').css('color', '#666');
	}
}

////////////////////////////////////////////////////////////////////
//  HOMEPAGE LOGIC FOR INSTITUTIONAL & INDIVIDUAL BOXES
////////////////////////////////////////////////////////////////////

/**
 * TITLE ........: Initialize HomePage Logic for Institutional & Individual boxes
 * PURPOSE.......: Initialize the rollover effects blah blah blah
 * DEPENDENCIES .: JQuery 1.2.26
 * USAGE ........: call function initHomepageSubmenuslike so --
 *                  $(document).ready( function() { initHomepageSubmenus(); });
 *
 * HISTORY
 * --------------------------------------------------------------------------
 *  12/18/2008, rwr Initial creation
 *  01/26/2009, rwr added image rotation logic based on random numbers
 *  02/05/2009, rwr Migrated selecting the random pair of images out of the
 *                  Javascript logic and into the JSP using scriptlet.
 */
function initHomepageSubmenus() {
	// mouse-over effect for the sub-menu's in the body of the page
	$("#sub_menu_1").click(function() {
		location.href = "/institutional/institutional_investors_investment_process.jsp";
	}).hover(function() {
		$("#institutional_mono_img").fadeIn();
	}, function() {
		$("#institutional_mono_img").fadeOut();
	});

	$("#sub_menu_2").click(function() {
		location.href = "/individual/individual_investors_investment_process.jsp";
	}).hover(function() {
		$("#individual_mono_img").fadeIn();
	}, function() {
		$("#individual_mono_img").fadeOut();
	});
}

////////////////////////////////////////////////////////////////////
//  LEFT-SIDE MENU LOGIC
////////////////////////////////////////////////////////////////////

/**
 * TITLE ........: Adjust the Height of the Left Side Menu Appropriately
 * PURPOSE.......: To resize the height of the left-side menu to be as
 *                 tall as the content to the right of the menu.
 *                 If the content on the right is not as tall as the
 *                 left-side menu, then leave the height of the menu alone.
 * DEPENDENCIES .: JQuery 1.2.26
 * USAGE ........: call function adjustLeftSideMenuHeight like so --
 *                  adjustLeftSideMenuHeight();
 *
 * HISTORY
 * --------------------------------------------------------------------------
 *  12/18/2008, rwr Initial creation
 */
function adjustLeftSideMenuHeight() {
	// fixup height of the left column for IE 6, as needed
	var nMax = Math.max(
		$("#right_content").outerHeight(), 
		$("#left_content").outerHeight(), 
		$("#related_content").outerHeight(), 
		$("#container_inner").outerHeight()
	);

	var i = nMax - $("#left_content").outerHeight();
	if (i > 0) {
		var nNewHeight = $("#col_pad_bot").outerHeight() + i;
		$("#col_pad_bot").css("padding-bottom", nNewHeight);
	}
}

/**
 * TITLE ........: Initialize Logic for Left-Side Menu's
 * PURPOSE.......: To Initialize left-side menu by highlighting the
 *                 correct menu item and firing the logic to correctly set
 *                  the height of the left-side menu.
 * DEPENDENCIES .: JQuery 1.2.26
 * USAGE ........: call function initLeftSideMenu like so --
 *                  $(document).ready( function() { initLeftSideMenu(); });
 * PARAMETERS ...: This function take 1 optional parameter.  The parameter
 *                 should look like "http://server.uboc.com" where 'server'
 *                 is the name of a web server that contains the Tridion
 *                 content of either the Production, Test or Development
 *                 environment.
 *
 * HISTORY
 * --------------------------------------------------------------------------
 *  12/18/2008, rwr Initial creation
 *  01/12/2009, rwr Added optional parm 'sURL' to handle the Search Results
 *                  page from the Google appliance being on a different
 *                  web server than the rest of the Tridion content.
 */
function initLeftSideMenu(sURL) {
	// * OPTIONAL PARAMETER: sURL
	// **** If parameter 'sURL' has a value, prefix every left menu URL
	// ****  with the value of 'sURL'.
	// **** If parameter 'sURL' doesn't exist, don't prefix the left menu
	// ****  URL's with anything; just leave them as relative URL's
	var sUseThisUrl = (sURL === undefined) ? ("") : (sURL);

	// highlight selected left menu item ...
	// *** by looking for "a" tags with href attributes that
	// ***  match the last part of the current location.href
	$("#left_menu a").each( function() {
		if (document.location.pathname.indexOf($(this).attr("href")) > -1 ) {
			var cssObj = {
				'color' : '#CC9900',
				'font-weight' : 900
			};
			$(this).css( cssObj );
		}
		// handle the optional parm 'sURL' right here... :-)
		$(this).attr("href", sUseThisUrl + $(this).attr("href"));
	});
	adjustLeftSideMenuHeight();
}

////////////////////////////////////////////////////////////////////
//  HILITE CURRENT PERSON IN MANAGEMENT TEAM PAGE(S)
////////////////////////////////////////////////////////////////////

/**
 * TITLE ........: Hilite the Current Person in the List of Team Members
 * PURPOSE.......: To highlight the correct team member's name in the list
 *                 f team members (either Management Team or Investment Team)
 *                 and to expand the team that contains the currently
 *                 selected team member.
 * DEPENDENCIES .: JQuery 1.2.26
 * USAGE ........: call function hiliteCurrentTeamMember like so --
 *                  $(document).ready( function() { hiliteCurrentTeamMember(); });
 *
 * HISTORY
 * --------------------------------------------------------------------------
 *  12/18/2008, rwr Initial creation
 */
function hiliteCurrentTeamMember() {
	// highlight selected person's name ...
	// *** by looking for "a" tags with href attributes that
	// ***  match the last part of the current location.href
	$("#bio_list a").each(function() {
		if (document.location.pathname.indexOf($(this).attr("href")) > -1) {
			$(this).css("color", "#CC9900");
			$(this).parent().next().css("color", "#666666");
		}
	});
}

/**
 * TITLE ........: Initialize the Management Team List
 * PURPOSE.......: To highlight the correct team member's name in the list
 *                 and to hide & show the correct team member
 * DEPENDENCIES .: JQuery 1.2.26
 * USAGE ........: call function initManagementTeamList like so --
 *                  $(document).ready( function() { initManagementTeamList(); });
 *
 * HISTORY
 * --------------------------------------------------------------------------
 *  01/05/2008, nl Initial creation
 */
function initManagementTeamList() {
	$("#bio_list dt a").click( function() {
		$("#bio div").hide();
		$( $(this).attr("href") ).show();
		$("#bio_list a").css("color", "black");
		$("#bio_list a").parent().next().css("color", "black");
		$(this).css("color", "#CC9900");
		$(this).parent().next().css("color", "#666666");
		initLeftSideMenu();
		return false;
	});

	var sURI = queryString("URI");
	if (sURI) {
		$("[href=#" + sURI + "]").click();
	}
}

////////////////////////////////////////////////////////////////////
//  COLLAPSIBLE INVESTMENT TEAM LIST
////////////////////////////////////////////////////////////////////

/**
 * TITLE ........: Initialize the Investment Team List
 * PURPOSE.......: To highlight the correct team member's name in the list
 *                 and to hide & show the correct team member
 * DEPENDENCIES .: JQuery 1.2.26
 * USAGE ........: call function initManagementTeamList like so --
 *                  $(document).ready( function() { initInvestmentTeamList(); });
 *
 * HISTORY
 * --------------------------------------------------------------------------
 *  01/06/2008, nl Initial creation
 */
function initInvestmentTeamList() {
	// magical expanding bio-list logic...
	// add onClick event handler to every <a> contained inside a <h3> inside
	// id="bio_list" ==> the event handler toggles expanding/collapsing
	// the list of team members
	$("#bio_list h3 a").click(function() {
		// get the <ul> that contains the list of team members
		// based on which link was clicked...
		var oDropDown = $(this).parent().next();

		// is the list of team members visible or hidden?
		if (oDropDown.css("display") === "none") {
			// HIDDEN: In that case, add extra space to the next
			// <h3> element so that it will be seperated from
			// the list of team members and look pretty
			$("h3", oDropDown.parent().next()).css("margin-top", "1em");
		} else {
			// VISIBLE: In that case, undo the extra space added
			// to the following <h3> element.
			$("h3", oDropDown.parent().next()).css("margin-top", "0");
		}

		// now actually toggle the visiblity of the list of team
		// members and then call the function to recalculate the
		// correct height of the left-side menu bar.
		oDropDown.toggle("fast", adjustLeftSideMenuHeight);
	});

	// add onClick event handler to show biographical data by un-hiding
	// ... this gets added to every <a> that is 3 <li> below id="bio_list"
	$("#bio_list li li li a").click(function() {
		$("#bio div").hide();
		$( $(this).attr("href") ).show();

		// first, reset the color of ALL of the links to default black
		$("#bio_list li li li a").css("color", "black");
		$("#bio_list li li li a").parent().next().css("color", "black");
		// then hilite the link that was clicked on
		$(this).css("color", "#CC9900");
		//take to top of the page 
		$('html, body').animate({scrollTop: '0px'}, 600);

		//repaint left menu
		initLeftSideMenu();
		// and lastly return false to prevent the link from
		// firing normally
		return false;
	});

	// trigger the onClick event to display the person's biography
	var sURI = queryString("URI");
	if (sURI) {
		$("[href=#" + sURI + "]").click();
	}
}

////////////////////////////////////////////////////////////////////
//  COLLAPSIBLE ADVISORS LIST
////////////////////////////////////////////////////////////////////

/**
 * TITLE ........: Initialize the Advisors List
 * PURPOSE.......: To highlight the correct team member's name in the list
 *                 and to hide & show the correct team member
 * DEPENDENCIES .: JQuery 1.2.26
 * USAGE ........: call function initAdvisorsList like so --
 *                  $(document).ready( function() { initAdvisorsList();});
 *
 * HISTORY
 * --------------------------------------------------------------------------
 *  01/09/2008, nl Initial creation
 */

function initAdvisorsList() {
	// magical expanding bio-list logic...
	// add onClick event handler to every <a> contained inside a <h3> inside
	// id="bio_list" ==> the event handler toggles expanding/collapsing
	// the list of team members
	$("#bio_list h3 a").click(function() {
		// get the <ul> that contains the list of team members
		// based on which link was clicked...
		var oDropDown = $(this).parent().next();

		// is the list of team members visible or hidden?
		if (oDropDown.css("display") === "none") {
			// HIDDEN: In that case, add extra space to the next
			// <h3> element so that it will be seperated from
			// the list of team members and look pretty
			$("h3", oDropDown.parent().next()).css("margin-top", "1em");
		} else {
			// VISIBLE: In that case, undo the extra space added
			// to the following <h3> element.
			$("h3", oDropDown.parent().next()).css("margin-top", "0");
		}

		// now actually toggle the visiblity of the list of team
		// members and then call the function to recalculate the
		// correct height of the left-side menu bar.
		oDropDown.toggle("fast", adjustLeftSideMenuHeight);
	});

	// add onClick event handler to show biographical data by un-hiding
	// ... this gets added to every <a> that is 3 <li> below id="bio_list"
	$("#bio_list li li li a").click(function() {
		$("#bio div").hide();
		$($(this).attr("href")).show();

		// first, reset the color of ALL of the links to default black
		$("#bio_list li li li a").css("color", "black");
		$("#bio_list li li li a").parent().next().css("color", "black");
		// then hilite the link that was clicked on
		$(this).css("color", "#CC9900");
		//repaint left menu
		initLeftSideMenu();
		// and lastly return false to prevent the link from
		// firing normally
		return false;
	});

	// trigger the onClick event to display the person's biography
	var sURI = queryString("URI");
	if (sURI) {
		$("[href=#" + sURI + "]").click();
	}
}

// search autocomplete for GSA suggest
$(document).ready(function() {
	$("input[name*='q']").autocomplete(
		'/includes/HMC_GSA_Proxy.jsp', 
		{
			dataType: 'json', 
			scroll: true, 
			delay: 0, 
			parse: function(data) {
				var array = [];
				if (data !== null) {
					var i;
					for (i = 0; i < data.results.length; i++) {
						array[i] = {
							data: data.results[i], 
							value: data.results[i].name, 
							result: data.results[i].name
						};
					}
				}
				return array;
			}, 
			formatItem: function(row) {
				return row.name;
			}
		}
	);
});
