
/*
 * legal_homepage.js
 * 
 * Written by Brendon Hicks (brendon@elegantthought.com) for PricewaterhouseCoopers,LLP.
 * Code to handle interactions on the PwC.com legal site homepage.
 * 
 * Requires:
 *  - jQuery to be already loaded.
 *  - function from /gx/assets/widgets/widget.js
 * 
 */

//  jQuery version of onReady function:
$(function(){
	
	// Set up "In your region" contacts box:
	initContact();
	
	// Make the Our Service links display pop-up sub-menus on hover:
	$("div.link").hover(
			function(){
				$("div.active").removeClass("active");
				$(this).parent().addClass("active");
				$(this).find("div.popup").show();
			},
			function(){
				$(this).find("div.popup").hide();
			}
	);
	
	// Make the three box title function as links:
	$('#our_expertise .box_title').css('cursor','pointer').click(function(){
		window.location = '/gx/en/legal/expertise.jhtml';
	});
	$('#our_people .box_title').css('cursor','pointer').click(function(){
		window.location = '/gx/en/legal/people.jhtml';
	});
	$('#in_your_region .box_title').css('cursor','pointer').click(function(){
		window.location = '/gx/en/legal/countries.jhtml';
	});

});			


function loadDropDown(){
	var temp = "";
	jQuery.each(legalData, function(i,val) {
		if(val.contacts){
				temp+="<option value="+i+">"+val.countryName+"</option>";
			}
		}); 
	var $list = $("#combo_for_region");
	$list.append(temp);
	$list.show();	
}		


function initContact(){
	var temp = "";
	
	// Load up the dropdown list of countries that we have contact information for.
	// A try/catch used because IE6 sometimes executes this script before the DOM is ready
	// even though this function is called from the jQuery document.ready function.
	try {
		loadDropDown();
	}
	catch(ex){
		setTimeout("loadDropDown()",1);
	}
	
	// Get the user's country code from the geoloc variable.
	// The variable is set by some script pulled in via the LD functionality.
	// We'll use the user's country if we have data for it, or their region if not (default Europe).
	
	// But first, re-map the Americas together:
	if(geoloc.region == "North America" || geoloc.region == "South America") geoloc.region = "Americas";

	var geo = (geoloc.code in legalData) ? geoloc.code : ( (geoloc.region in regions) ? geoloc.region : "Europe");
	
	ChangeRegion(geo);

};


/*
 * ChangeRegion(code)
 * Function that is called when a user selects a region from the 
 * select box in the "In you region" seciton. The selected option's value is passed in
 * as an argument.  Also called to initially set up the page.
 */
function ChangeRegion(code){

	

	var temp="";
	
	// Compute out contact information
	
	// if the region is a country in legalData.js:
	if(legalData[code]){
		$("#territory_name").html(legalData[code].countryName);
		for(var i=0;legalData[code].contacts && i<legalData[code].contacts.length;i++){
			var contactId=legalData[code].contacts[i];
			var contact=people[contactId.id];
			var leadertitle = (contact.hasOwnProperty("leader_title")) ? contact.leader_title : (legalData[code].countryName+" legal leader");
			
			
			temp+="<td><div class=\"contact\">";
			temp+="<div class=\"contact_name\">"+feedbackURL(contact.name,contact.email,code);+"</div>";
/*exception for China*/ temp+= (code == "cn") ? "" : "<div class=\"contact_role\">"+leadertitle + "</div>";
			temp+="<div class=\"contact_role\">"+legalData[code].practiceName+"</div>";
			temp+="<div class=\"contact_phone\">"+contact.tel+"</div>";
			temp+="</div></td>";
			
			temp = (i%2==0) ? ("<tr>" + temp ) : (temp + "</tr>"); 
		}
		
		$("#practice_name").html(legalData[code].countryName);
		var href='/gx/en/legal/countries/index.jhtml?co='+code;
		$("#terr_practice_link").attr('href',href).show();
	} 

	else if(regions[code]){
		$("#territory_name").html(code);
		var region = regions[code];
		
		for(var i=0; i< region.length;i++){
			var contact=region[i];
			temp+="<td><div class=\"contact\">";
			temp+="<div class=\"contact_name\">"+feedbackURL(contact.name,contact.email,"gx");+"</div>";
			temp+= "<div class=\"contact_role\">"+contact.title+"</div>";
			temp+="<div class=\"contact_role\">"+contact.firmname+"</div>";
			temp+="<div class=\"contact_phone\">"+contact.phone+"</div>";
			temp+="</div></td>";
			
			temp = (i%2==0) ? ("<tr>" + temp ) : (temp + "</tr>"); 
		}
		
		$("#terr_practice_link").hide();
	}

	// Now show it nicely:
	
	$("#learn_more_table").fadeOut(200, function(){
		$("#learn_more_table").html(temp);
		$("#learn_more_table").fadeIn(500);	
	});	
	
}

/***********************
 Retired functionality
************************/

//People search stuff:
/*  This is not used anymore now that people pages are hard-coded
 * 
	for(var x in legalData){
		if(legalData[x].countryName!='Hungary')
			$("#territory_search_list").append("<option value='"+x+"'>"+legalData[x].countryName+"</option>");
	}

	$("#territory_search_list").change(function(){
		location.href = "/gx/en/legal/people.jhtml?t="+this.value;
	});

	$("#specialty_search_list").change(function(){
		location.href = "/gx/en/legal/people.jhtml?s="+this.value;
	});
*/

/*
 * old news banner functionality
 * 
function loop(){				
	$("div.news_title:visible").fadeOut(1000, function(){
		if ($(this).next("div.news_title").length > 0) {
			$(this).next().fadeIn(1000);
		}
		else
			$("div.news_title:first").fadeIn(1000);
	});
}

var timer;
$(function(){
	$("#sliding_part img.left_arrow").click(function(){
		clearInterval(timer);
		var $current = $("#sliding_part div.news_title:visible");
		if($current.prev().length > 0){
			$current.fadeOut(1000,function(){
				$current.prev().fadeIn();	
			});	
		}	
	});
	$("#sliding_part img.right_arrow").click(function(){
		clearInterval(timer);
		var $current = $("#sliding_part div.news_title:visible");
		if($current.next().length > 0){
			$current.fadeOut(1000,function(){
				$current.next().fadeIn();	
			});	
		}		
	});					
}

*/


