// Function added to control Expat banner resizing, triggered from Flash banner
function resizeFlashHeight(newHeight, animSecs) {
	console.log('#flashContent');
	$('#flashContent').animate( {height:newHeight}, animSecs * 1000 );
}

(function($){  
	$(document).ready(function() {
		var $carouselItems = $('#hero').find('li');
		$carouselItems.each(function(i){
			//don't want to call customSelect on cloned items
			if(i !== 0 && i !== $carouselItems.length-1){
				$(this).find('.whatStageForm .selectWrapper').customSelect({
					active: 'on',
					startValue: 'Select your situation'
				});	 
			}
		});
		
		$('div.accordion span.accordionHeading').each(function() {
			var text = $(this).text();
			$(this).replaceWith('<a href="#" class="accordionHeading" title="' + text + '">' + text + '</a>');			
		});
		
		if($.fn.accordion){
			$('div.accordion').accordion({
				node: 'div.section',
				heading: '.accordionHeading',
				content: '.accordionContent',
				open: 'selected',	
				overlap: true,
				collapsible: true
			}).bind('onopenaccordion', function(e,node) {
				$(node).find('.accordionContent').find('a, input').first().focus();
				$(node).find('.accordionContent').attr('style', 'display: block; overflow: hidden;');
			});
		}
		
		if($.address){
			$.address.crawlable(true);
		}

// Flash and XML on Rackspace - doesn't work
// src: 'https://www.hsbcoffshoreinteractive.com/dailyupdates/mediacontent/flashresize.swf?xmlPath=https://www.hsbcoffshoreinteractive.com/dailyupdates/mediacontent/expat-banner-data.xml',
// Flash on Rackspace XML on our server - doesn't work
// src: 'https://www.hsbcoffshoreinteractive.com/dailyupdates/mediacontent/flashresize.swf?xmlPath=/1/PA_ES_Content_Mgmt/content/hsbc_expat/tools/why_expat_flash_intro/expat-banner-data.xml',
// Flash on Rackspace XML on our server (full internal url ) - doesn't work
// src: 'https://www.hsbcoffshoreinteractive.com/dailyupdates/mediacontent/flashresize.swf?xmlPath=http://international-55-bde.hk.hsbc/1/PA_ES_Content_Mgmt/content/hsbc_expat/tools/why_expat_flash_intro/expat-banner-data.xml',
// Both files on our site - works a treat !
											
//								// Load flash content
//								$('#flashContent').flash({
//									// this line works internally
//									src: '/1/PA_ES_Content_Mgmt/content/hsbc_expat/tools/why_expat_flash_intro/flashresize.swf?xmlPath=/1/PA_ES_Content_Mgmt/content/hsbc_expat/tools/why_expat_flash_intro/expat-banner-data.xml',
//									// this line is the external RS path that needs to go into live
//									// src: 'https://www.hsbcoffshoreinteractive.com/dailyupdates/mediacontent/flashresize.swf?xmlPath=https://www.hsbcoffshoreinteractive.com/dailyupdates/mediacontent/expat-banner-data.xml',			
//									width: '100%',
//									height: '100%',
//									version: 9
//									},
//									{
//										wmode:'transparent',
//										scale: 'noscale',
//										salign: 'lt',
//										allowFullScreen: 'true',
//										allowScriptAccess:'always'
//									}
//								);
	});
	
	
	TaxNavigator = {
		opts: {
			//css selectors
			taxNav: 		'.taxNav',		//css selector for the nav panes
			countryMenu:	'.taxNav .menu',		//css selector for the country menu
			countryMenuCol:	'.taxNav .menu .js_menuCol ul', //css selector for each menu column
			directionMenu:	'.taxNav .directionMenu',
			//Ajax vars
			leavingXML:		'/1/PA_ES_Content_Mgmt/content/hsbc_expat/tools/global_tax_navigator/tax-navigator-leaving-information.xml',	//path to leaving XML file
			arrivingXML:	'/1/PA_ES_Content_Mgmt/content/hsbc_expat/tools/global_tax_navigator/tax-navigator-arriving-information.xml',	//path to arriving XML file
			xmlData:		new Array(2),
			//other global vars
			currCountry:	null,
			currDirection:	'0'		//0 for arriving, 1 for leaving
		},
		
		init: function(){
			//load arriving data xml file
			var obj = this,
				success = function(){
					//toggle to JS version of the tool
					$(obj.opts.taxNav).toggle();
					
					obj.populateCountryMenu();
					var queryString = $.address.value().split('/');
					
					
					//if query string is set for arriving or leaving
					if (queryString[1] != null && queryString[1] == 'leaving'){
						obj.changeDirection()
					}
					//if query string for country code is set
					if (queryString[2] != null){
						obj.changeCountry(queryString[2]);
					}else{
						obj.changeCountry($(obj.opts.countryMenu + ' ul li a').eq(0).attr('class'));
					}
					
					$(obj.opts.countryMenu+' a.'+obj.opts.currCountry).parent().addClass('selected');
					
					$(obj.opts.countryMenu + ' ul li a').live('click', function(e){
						e.preventDefault();
						if (!$(this).hasClass(obj.opts.currCountry)){
							obj.changeCountry($(this).attr('class'));
						}
					});
					
					$(obj.opts.directionMenu + ' ul li a').click(function(e){
						e.preventDefault();	
						if ((obj.opts.currDirection === 0 && $(this).hasClass('js_arriving')) || (obj.opts.currDirection === 1 && $(this).hasClass('js_leaving'))){
							return;
						}else{
							obj.changeDirection($(this).attr('class'));
						}
					});
					$(obj.opts.taxNav+' .taxNavButton').click(function(e){
						e.preventDefault();
						obj.openMenu($(this));
					});
					
					//click outside the countrty menu
					$(document).click(function(e){
						if ($('.countryBtn').hasClass('open') && (!$.contains($(obj.opts.countryMenu)[0], e.target) && !$.contains($('.countryBtn')[0], e.target))){
							obj.openMenu('.countryBtn');
						}
						if ($('.directionBtn').hasClass('open') && (!$.contains($(obj.opts.directionMenu)[0], e.target) && !$.contains($('.directionBtn')[0], e.target))){
							obj.openMenu('.directionBtn');
						}
					});
					//bind key clicks
					var key_code;
					$('.directionBtn, .directionMenu').keydown(function(e){
						key_code = e.keyCode;
						//alt and down (open menu)
						if (key_code === 40 && e.altKey){
							obj.openMenu($('.directionBtn'));
						//alt and up, or esc (close menu)
						}else if(((key_code === 38) && (e.altKey)) || (key_code === 27)){
							obj.openMenu($('.directionBtn'));
						//down or right (next country)
						}else if (key_code === 39 || key_code === 40 || key_code === 37 || key_code === 38){
							e.preventDefault();
							obj.changeDirection();
						}
					});
					$('.countryBtn, .menu').keydown(function(e){
						key_code = e.keyCode;
						//alt and down (open menu)
						if (key_code === 40 && e.altKey){
							e.preventDefault();
							obj.openMenu($('.countryBtn'));
						//alt and up, or esc (close menu)
						}else if(((key_code === 38) && (e.altKey)) || (key_code === 27)){
							e.preventDefault();
							obj.openMenu($('.countryBtn'));
						//end or page down (select last in list) 
						}else if (key_code === 34 || key_code === 35){
							e.preventDefault();
							var $countries =  $(obj.opts.countryMenu+' ul li a');
							obj.changeCountry($($countries[$countries.length-1]).attr('class'));
						//home or page up (select last in list) 
						}else if (key_code === 33 || key_code === 36){
							e.preventDefault();
							var $countries =  $(obj.opts.countryMenu+' ul li a');
							obj.changeCountry($($countries[0]).attr('class'));
						//down or right (next country)	
						}else if (key_code === 39 || key_code === 40){
							e.preventDefault();
							var $countries =  $(obj.opts.countryMenu+' ul li a');
							$countries.each(function(index){
								if ($(this).hasClass(obj.opts.currCountry)){
									if (index == $countries.length-1){
										obj.changeCountry($($countries[0]).attr('class'));
									}else{
										obj.changeCountry($($countries[index+1]).attr('class'));
									}
									return false;
								}
							});
						// up or left (next country)
						}else if (key_code === 37 || key_code === 38){ 
							e.preventDefault();
							var $countries =  $(obj.opts.countryMenu+' ul li a');
							$countries.each(function(index){
								if ($(this).hasClass(obj.opts.currCountry)){
									if (index == 0){
										obj.changeCountry($($countries[$countries.length-1]).attr('class'));
									}else{
										obj.changeCountry($($countries[index-1]).attr('class'));
									}
									return false;
								}
							});
						}
					});
				};

			$.ajax({
				url: obj.opts.arrivingXML,
				type: 'GET',
				dataType: 'xml',
				success: function(arrData){
					$.ajax({
						url: obj.opts.leavingXML,
						type: 'GET',
						dataType: 'xml',
						success: function(leaveData){
							obj.opts.xmlData[0] = arrData;
							obj.opts.xmlData[1] = leaveData;
							success();
						}
					});
				}
			});
		},
		
		openMenu: function(source){
			$this = $(source);
			if (!$this.hasClass('open')){
				$this.addClass('open');
			}else{
				$this.removeClass('open');
			}
			//controls for country menu
			if ($this.hasClass('countryBtn') && $this.hasClass('open')){
				pos = $this.position();
				$(this.opts.directionMenu).hide();
				$(this.opts.countryMenu).css({
					'left': pos.left + 10,
					'top': pos.top + ($this.height()-4)
				}).show().find(' li a:first').focus();
				
			}else if ($this.hasClass('countryBtn') && !$this.hasClass('open')){
				$(this.opts.countryMenu).hide();
				$this.find('a').focus();
			}
			//control direction menu
			else if ($this.hasClass('directionBtn') && $this.hasClass('open')){
				pos = $this.position();
				$(this.opts.countryMenu).hide();
				$(this.opts.directionMenu).css({
					'left': pos.left + 10,
					'top': pos.top + ($this.height()-4)
				}).show().find(' li a:first').focus();;
			}else if ($this.hasClass('directionBtn') && !$this.hasClass('open')){
				$(this.opts.directionMenu).hide();
				$this.find('a').focus();
			}
		},
		
		//change direction
		changeDirection: function(){
			$(this.opts.directionMenu + ' ul li.selected').removeClass('selected');
			if (this.opts.currDirection === 1){
				this.opts.currDirection = 0;
				$(this.opts.taxNav + ' .directionBtn span a').html('Going to');
				$(this.opts.directionMenu +' .js_arriving').parent().addClass('selected');
				$.address.value('arriving/'+this.opts.currCountry);
			}else{
				this.opts.currDirection = 1;	
				$(this.opts.taxNav + ' .directionBtn span a').html('Leaving');
				$(this.opts.directionMenu +' .js_leaving').parent().addClass('selected');
				$.address.value('leaving/'+this.opts.currCountry);
			}
			this.populateCountryMenu();
			this.replaceMarkup(this.opts.currCountry, true);
		},
		
		//change current country
		changeCountry: function(country){
			$(this.opts.countryMenu + ' ul li.selected').removeClass('selected');
			$(this.opts.countryMenu + ' ul li a.'+country).parent().addClass('selected');
			this.replaceMarkup(country, false);
			this.opts.currCountry = country;
			if (this.opts.currDirection === 1){
				$.address.value('leaving/'+country);
			}else{
				$.address.value('arriving/'+country);
			}
		},
		
		//populate the country menu from the XML data loaded
		populateCountryMenu: function(){
			$menu = $(this.opts.countryMenuCol);
			$menu.empty();

			$xmlSelector = this.opts.xmlData[this.opts.currDirection];
			$countries = $($xmlSelector).find('countries').children();
			$.each($countries, function(index){
				$($menu[Math.floor((index/$countries.length)*$menu.length)]).append('<li><a class="'+$(this).attr('code')+'"href="#">'+$(this).attr('name')+'</a></li>');
			});
			$(this.opts.countryMenu).find('.'+this.opts.currCountry).parent().addClass('selected');
		},
			
		//replace markup of the tool
		replaceMarkup: function(country, changeDir){
			//return if already on specified country
			if (country == this.opts.currCountry && !changeDir){
				return;	
			}	
			//find xml for country specified
			$wrapper = $(this.opts.taxNav);
			
			//select XML data of the specified country
			var $xml = $(this.opts.xmlData[this.opts.currDirection]).find('country[code=\''+country+'\']');
			
			//change h3
			var direction_text = 'Going to ';
			if (this.opts.currDirection === 1){
				direction_text = 'Leaving ';	
			}
			$(this.opts.taxNav).find('h3').html(direction_text+$xml.attr('name'));
			//change map image
			$('.countryDisplay').css('background-position', -$xml.attr('imageX')*415+'px '+ -$xml.attr('imageY')*214+'px');
			$(this.opts.taxNav).find('.countryBtn span a').html($xml.attr('name'));
			
			//populate local info
			$('.js_localInfo').empty();
			this.buildTable('', $('.js_localInfo'), $xml.find('localInfo'));
			
			//populate tax rates
			$('.js_taxRates').empty();
			$title = $xml.find('taxRateTitle');
			if ($title != null){
				$('.js_taxRates').siblings('h4').empty().text($title.text())
			}
			for (var i = 0; i < $xml.find('taxRates').length; i++){
				this.buildTable('<tr class="odd"><th>Taxable Income Band <span class="currency"></span></th><th class="center">National Income Tax Rates</th></tr>', $('.js_taxRates'), $xml.find('taxRates').eq(i));
			}
			
			//populate notes
			$page_element = $wrapper.find('.js_notes');
			$page_element.empty();
			$.each($xml.find('notes').children(), function(){
				$page_element.append('<p>'+$(this).text()+'</p>')											   
			});
			
			//populate QA
			$page_element = $wrapper.find('.js_QA');
			$page_element.empty();
			var col_a_height = 0, col_b_height = 0;
			var $element_a = $($page_element[0]), $element_b = $($page_element[1])
			
			$.each($xml.find('QA').children(), function(){
				if (col_a_height == col_b_height){
					$element_a.append($(this).text());
					col_a_height = $element_a.height();
				}else if (col_a_height < col_b_height){
					$element_a.append($(this).text());
					col_a_height = $element_a.height();
				}else if (col_a_height > col_b_height){
					$element_b.append($(this).text());
					col_b_height = $element_b.height();
				}
			});
			
			$(this.opts.taxNav).find('span.currency').html($xml.attr('currency'));
		},
		
		buildTable: function($th, $wrapper, $xml_data){
			if ($xml_data.find('title').length){
				$wrapper.append('<h5>'+$xml_data.find('title').text()+'</h5>');
			}
			$table = $('<table></table>').appendTo($wrapper);
			
			$table.append($th);
			var cssClass = '';
			$.each($xml_data.find('item'), function(index){
				if (index % 2 != 0){
					cssClass = ' class="odd"';
				}else{
					cssClass = '';
				}
				$table.append('<tr '+cssClass+'><td>'+$(this).attr('title')+'</td><td>'+$(this).text()+'</td></tr>');
			});
			if ($xml_data.find('notes')){
				$wrapper.append($xml_data.find('notes').text());
			}
		},
		
		//recurses xml tree with unique nodes that have HTML nodes with the same class name in the markup and populates from the XML node text data
		recurseAndReplace: function($element){
			obj = this;
			//if this is true this is a root node
			if ($element.children().length < 1) {
				$('.js_'+$element.context.nodeName).html($element.text());
				return;
			}else{
				$.each($element.children(), function(){
					obj.recurseAndReplace($(this));
				});	
			}
		}
		
	}
})(jQuery);




var AUDTMD07D = new Array();
var AUDTMD14D = new Array();
var AUDTMD01M = new Array();
var AUDTMD02M = new Array();
var AUDTMD03M = new Array();
var AUDTMD06M = new Array();
var AUDTMD01Y = new Array();
var CADTMD07D = new Array();
var CADTMD14D = new Array();
var CADTMD01M = new Array();
var CADTMD02M = new Array();
var CADTMD03M = new Array();
var CADTMD06M = new Array();
var CADTMD01Y = new Array();
var CHFTMD07D = new Array();
var CHFTMD14D = new Array();
var CHFTMD01M = new Array();
var CHFTMD02M = new Array();
var CHFTMD03M = new Array();
var CHFTMD06M = new Array();
var CHFTMD01Y = new Array();
var EURTMD07D = new Array();
var EURTMD14D = new Array();
var EURTMD01M = new Array();
var EURTMD02M = new Array();
var EURTMD03M = new Array();
var EURTMD06M = new Array();
var EURTMD01Y = new Array();
var GBPTMD01D = new Array();
var GBPTMD07D = new Array();
var GBPTMD14D = new Array();
var GBPTMD01M = new Array();
var GBPTMD02M = new Array();
var GBPTMD03M = new Array();
var GBPTMD06M = new Array();
var GBPTMD01Y = new Array();
var GBPTMD02Y = new Array();
var GBPTMD03Y = new Array();
var GBPTMD04Y = new Array();
var GBPTMD05Y = new Array();
var HKDTMD07D = new Array();
var HKDTMD01M = new Array();
var HKDTMD02M = new Array();
var HKDTMD03M = new Array();
var HKDTMD06M = new Array();
var HKDTMD01Y = new Array();
var JPYTMD07D = new Array();
var JPYTMD01M = new Array();
var JPYTMD02M = new Array();
var JPYTMD03M = new Array();
var JPYTMD06M = new Array();
var JPYTMD01Y = new Array();
var USDTMD01D = new Array();
var USDTMD07D = new Array();
var USDTMD14D = new Array();
var USDTMD01M = new Array();
var USDTMD02M = new Array();
var USDTMD03M = new Array();
var USDTMD06M = new Array();
var USDTMD01Y = new Array();
var AUDCDP = new Array();
var AUDMCQ = new Array();
var CADCDP = new Array();
var CADMCQ = new Array();
var CHFCDP = new Array();
var CHFMCQ = new Array();
var EURCDP = new Array();
var EURMCQ = new Array();
var EURNDA = new Array();
var EURNDO = new Array();
var EURUBA = new Array();
var GBPCDP = new Array();
var GBPMCQ = new Array();
var GBPNDA = new Array();
var GBPNDO = new Array();
var GBPOFA = new Array();
var HKDCDP = new Array();
var HKDMCQ = new Array();
var JPYCDP = new Array();
var JPYMCQ = new Array();
var USDCDP = new Array();
var USDMCQ = new Array();
var USDNDA = new Array();
var USDNDO = new Array();
var USDUBA = new Array();
var USDUFA = new Array();
var GBPMIA06M = new Array();
var GBPMIA01Y = new Array();
var GBPMIA02Y = new Array();
var GBPMIA03Y = new Array();
var GBPMIA04Y = new Array();
var GBPMIA05Y = new Array();



 
var XDTrue = "XD";
var XDFalse = "";
 
function writeit(writeText) {
	if (writeText!="0")
		document.write(writeText);
	else 
		document.write(" ");
}

function writecompare(writeText,comparetext,TrueText,FalseText) { 
	if (writeText!=comparetext) 
		document.write(FalseText); else document.write(TrueText); 
}

// Nicks new function please leave

function writeoutto2dp(rate,n)
{
	var that = rate,a;
	n = n?n:0;
	that = (Math.floor(that*Math.pow(10,n)))/Math.pow(10,n);
	a = that.toString().split('.');
	a[1] = a[1]?a[1]:'';
	while(a[1].length < n) {
		a[1] = a[1]+'0';
	}
	return n>0?a.join('.'):a[0];
}

// MG link into showhide code below.  Tag showhide with id="name" and link to #name
var ShowHideUrl = window.location;
var urlString = escape(ShowHideUrl)
var showhidename = urlString.split('%23');
if (showhidename[1] != null);
{
$("#showhidename\\[1\\]").show();
}
