
$(function() {

	//global variable
	window.geo = (location.hostname=="www.pwc.com")? geoloc : {code:"uk"};

	initContact();
}); 


function isArray(val){
	
	return val &&
		typeof(val) === 'object' &&
		typeof(val.length) === 'number' &&
		typeof(val.splice) === 'function' &&
		!(val.propertyIsEnumerable('length'));
}

function printContact(contact, cell) {  
	
	//find the table cell specified by the "cell" argument
	
	var $ctable = $("#learn_more"), 		
	    $cells = $("#learn_more td");

	//if we have to make a new row, do so
	if ($cells.length - cell < 2) {
		$("tr:last",$ctable).before("<tr><td></td><td></td></tr>");
	}

	var $cell = $("#learn_more td").eq(cell-1),

		
	//print out all of the available contact information.
	    details =  "<div class='contact'><div class='contact_name'>"
				+ feedbackURL(contact.name,contact.email,geo.code)
				+ "</div><div class='contact_role'>"
				+ contact.title
				+ "</div><div class='contact_phone'>"
				+ contact.num
				+ "</div></div>";
	
	$cell.html(details);
	
}
		   	
function ChangeRegion(item)
{



	$("div.box  div.contact").fadeOut(200, function(){

	//clear old region contacts:
	var $cells = $("#learn_more td");

	for(var i = geo.numgc; i< $cells.length - 2; i++){  //hint: geo.numgc is set in initContact()
		$cells.eq(i).html("");
	}

	if(item.value == "zz") return;
	var code = item.value;
	
	if (typeof(tc[code][practice]) =="undefined")
		tc[code][practice] = { "site" : "", "name" : "", "title": "", "email": "", "num" : "" };
		
	var contact = tc[code][practice];

		
		
	if(!$.isArray(contact) && contact.name == "") contact = tc[code].leader;
	
	if ($.isArray(contact)){
		for(var i = 0; i<contact.length; i++){
			printContact(contact[i],geo.numgc+i+1);	
		}

	} else{
		printContact(contact,geo.numgc+1);	
	}

	
	var sitelink = tc[code][practice].site || tc[code].country_site;		
			
	
			$("#territory_name").html(tc[code].country_name);
			$("#practice_name").html(tc[code].practice_in);
			$("#terr_practice_link").attr("href",sitelink);
			$("div.box div.contact").fadeIn(500);	
	});
	
}
		   	
var initContact = function(){
       	// relies on |practice| and |tc| global variables.
	    // also geoloc global variable if on production server.
	        
	var $list = $("#combo_for_region");
	
	//set up some defaults for the country if it doesn't have the practice defined:
	
	if (typeof(tc[geo.code][practice]) =="undefined")
		tc[geo.code][practice] = { "site" : "", "name" : "", "title": "", "email": "", "num" : "" };

	
	

	
	
	var globalcontact = tc.gx[practice];

	var localcontact = contact = tc[geo.code][practice];
	if(localcontact.name == "") localcontact = tc[geo.code].leader;
    var sitelink = "";

	//print global network leader(s)

	if($.isArray(globalcontact)){
		geo.numgc=0;   //we will use this variable to track the number of global contacts for this network
		for(var i = 0; i<globalcontact.length; i++){
			geo.numgc++;
			printContact(globalcontact[i],geo.numgc);	
		}

	} else{
		printContact(globalcontact,1);
		geo.numgc = 1;
	}	

	//print the user's local network information, if it exists
	if(geo.code in tc){
		
		if($.isArray(localcontact)){
			for(var i = 0; i<localcontact.length; i++){
				printContact(localcontact[i],geo.numgc+i+1);	
				sitelink = ( localcontact[i].site =="") ? tc[geo.code].country_site : localcontact[i].site;
			}

		} else{
			printContact(localcontact,geo.numgc+1);
			sitelink = localcontact.site;
		}
				//print local network leader
		$("#practice_name").html(tc[geo.code].practice_in);		//print local country name 
		$("#terr_practice_link").show().attr("href",sitelink);		//print local practice link
		$("#territory_name").html(tc[geo.code].country_name);		//print local territory name
	}else{
		$("#terr_practice_link").hide();
	}		
	
	// initialize country drop-down list
	$list.find("option").remove();
	$list.append($("<option value='zz' selected>Change territory</option>"));
	for(co in tc){
		if(co != "gx" && co != "zz")
			$list.append($("<option value='"+co+"'>"+tc[co].country_name + "</option>"));
	}
	$list.show();	
	$("div.contact").show();
			
	$("#learn_more tr:last td:last a").attr("href","/gx/en/tax/specialists.jhtml");

	

};
