var branchlocate = function(){
	var humegpselement;
	var humegpsscrollto;
	return {
		nearesthume: function (elmt,scrt)
		{
			humegpselement = elmt;
			humegpsscrollto = scrt;
			if( $(humegpselement).hasClass('active') )
			{
				$('html,body').animate({scrollTop: $('#main').offset().top}, 700, function(){
					$(humegpselement).slideUp('slow');
					$(humegpselement).removeClass('active');
				});
			}
			else
			{
				$(humegpselement).html('<p style="text-align: center"><img src="img/ajaxloading.gif" /> Please Wait</p>');
				$(humegpselement).slideDown('slow', function() {
					$(humegpselement).addClass('active');
					if(navigator.geolocation)
					{
						navigator.geolocation.getCurrentPosition(branchlocate.nearesthumegpssuccess,branchlocate.failure,{maximumAge:600000});
					}
					else
					{
						$(humegpselement).html('<p style="text-align: center">GPS not support in browser</p>');
					}
				});
			}
		},
		nearesthumegpssuccess: function (position)
		{
			try {
				var lat = position.coords.latitude;
				var lng = position.coords.longitude;
				$.getJSON('pages/nearesthumebranch.aspx?lat=' + lat + '&lng=' + lng + '&max=3', function(data) {
					var items = [];
					var out = "<table class=\"branchlist\" border=\"0\" width=\"100%\" cellspacing=\"3\" cellpadding=\"3\">";
					$.each(data.branches.branch, function(key, val) {
						items.push(new google.maps.Marker({ position: new google.maps.LatLng(val.lat,val.lng), title: "'" + val.addr + "'" }));
						out += "<tr class=\"branchlistitem\"><td>" + val.addr + "</td><td style=\"text-align: right;\" width=\"20%\">" + val.dist + "km</td></tr>";
					});
					if( items.length > 0 )
					{
						gmaps.sethumebranchesnearest(items);
					}
					out += "</table>";
					out += "<p style=\"text-align: center\"><a id=\"nearhumebranchgpsbtn\" class=\"button\" href=\"javascript:void(0);\" onclick=\"gmaps.displaymapnearesthumebranches('displaymap','overlay','.maplabel','.findbranchlabel');\">Show MAP</a></p>";
					$(humegpselement).slideUp('slow',function(){
						$(humegpselement).html(out);
						$(humegpselement).slideDown('slow',function(){
							$('#nearhumebranchgpsbtn').mousedown(function(){
								$(this).addTemporaryClass("buttonclicked", 200);
							});
							$(humegpselement).addClass('active');
							$('html,body').delay(200).animate({scrollTop: $(humegpsscrollto).offset().top}, 700);
						});
					});	
				})
				.success(function() {})
				.error(function() {
					$(humegpselement).slideUp('slow',function(){
						$(humegpselement).html("<p style=\"text-align: center\">Error getting nearest branches</p>");
						$(humegpselement).slideDown('slow',function(){
							$('#nearhumebranchgpsbtn').mousedown(function(){
								$(this).addTemporaryClass("buttonclicked", 200);
							});
							$(humegpselement).addClass('active');
							$('html,body').delay(200).animate({scrollTop: $(humegpsscrollto).offset().top}, 700);
						});	
					});
				})
				.complete(function() {});
			}
			catch(err)
			{
				$(humegpselement).slideUp('slow',function(){
					$(humegpselement).html("<p style=\"text-align: center\">Error getting nearest branches</p>");
					$(humegpselement).slideDown('slow',function(){
						$('#nearhumebranchgpsbtn').mousedown(function(){
							$(this).addTemporaryClass("buttonclicked", 200);
						});
						$(humegpselement).addClass('active');
						$('html,body').delay(200).animate({scrollTop: $(humegpsscrollto).offset().top}, 700);
					});
				});
			}
		},
		listallhume: function ( elmt, scrlto )
		{
			if( $(elmt).hasClass('active') )
			{
				$('html,body').animate({scrollTop: $('#main').offset().top}, 700, function(){
					$(elmt).slideUp('slow',function(){
						$(elmt).html = "";
					});
					$(elmt).removeClass('active');			
				});
			}
			else
			{
				$(elmt).html('<p style="text-align: center"><img src="img/ajaxloading.gif" /> Please Wait</p>');
				$(elmt).slideDown('slow',function(){
					$.getJSON('pages/listallbrancheshume.aspx', function(data) {
						var items = [];
						var out = "<table class=\"branchlist\" border=\"0\" width=\"100%\" cellspacing=\"3\" cellpadding=\"3\">";
						$.each(data.branches.branch, function(key, val) {
							items.push(new google.maps.Marker({ position: new google.maps.LatLng(val.lat,val.lng), title: "'" + val.addr + "'" }));
							out += "<tr class=\"branchlistitem\"><td>" + val.addr + "</td></tr>";
						});
						if( items.length > 0 )
						{
							gmaps.sethumebranchesall(items);
						}
						out += "</table>";
						out += "<p style=\"text-align: center\"><a id=\"listhumebranchgpsbtn\" class=\"button\" href=\"javascript:void(0);\" onclick=\"gmaps.displaymapallhumebranches('displaymap','overlay','.maplabel','.findbranchlabel');\">Show MAP</a></p>";
						$(elmt).slideUp('slow',function(){
							$(elmt).html(out);
							$(elmt).slideDown('slow',function(){
								$('#listhumebranchgpsbtn').mousedown(function(){
									$(this).addTemporaryClass("buttonclicked", 200);
								});
								$(elmt).addClass('active');
								$('html,body').delay(200).animate({scrollTop: $(scrlto).offset().top}, 700);
							});
						});	
					})
					.success(function() {})
					.error(function() {
						$(elmt).slideUp('slow',function(){
							$(elmt).html("<p style=\"text-align: center\">Error getting branches list</p>");
							$(elmt).slideDown('slow',function(){
								$('#listhumebranchgpsbtn').mousedown(function(){
									$(this).addTemporaryClass("buttonclicked", 200);
								});
								$(elmt).addClass('active');
								$('html,body').delay(200).animate({scrollTop: $(scrlto).offset().top}, 700);
							});
						});	
					})
					.complete(function() {});
				});
			}
		}
	};
}();

var atmlocate = function(){
	var humegpselement;
	var humegpsscrollto;
	var affilgpselement;
	var affilgpsscrollto;
	return {
		nearesthume: function (elmt,scrt)
		{
			humegpselement = elmt;
			humegpsscrollto = scrt;
			if( $(humegpselement).hasClass('active') )
			{
				$('html,body').animate({scrollTop: $('#main').offset().top}, 700, function(){
					$(humegpselement).slideUp('slow');
					$(humegpselement).removeClass('active');
				});
			}
			else
			{
				$(humegpselement).html('<p style="text-align: center"><img src="img/ajaxloading.gif" /> Please Wait</p>');
				$(humegpselement).slideDown('slow', function(){
					$(humegpselement).addClass('active');
					if(navigator.geolocation)
					{
						navigator.geolocation.getCurrentPosition(atmlocate.nearesthumegpssuccess,atmlocate.nearesthumefailure,{maximumAge:600000});
					}
					else
					{
						$(humegpselement).html('<p style="text-align: center">GPS not support in browser</p>');
					}
				});
			}
		},
		nearesthumefailure: function(err)
		{
			$(humegpselement).html('<p style="text-align: center">Failed finding location. Please check handset and/or allow GPS from browser.</p>');
		},
		nearestaffil: function (elmt,scrt)
		{
			affilgpselement = elmt;
			affilgpsscrollto = scrt;
			if( $(affilgpselement).hasClass('active') )
			{
				$('html,body').animate({scrollTop: $('#main').offset().top}, 700, function(){
					$(affilgpselement).slideUp('slow');
					$(affilgpselement).removeClass('active');
				});
			}
			else
			{
				$(affilgpselement).html('<p style="text-align: center"><img src="img/ajaxloading.gif" /> Please Wait</p>');
				$(affilgpselement).slideDown('slow', function(){
					$(affilgpselement).addClass('active');
					if(navigator.geolocation)
					{
						navigator.geolocation.getCurrentPosition(atmlocate.nearestaffilgpssuccess,atmlocate.nearestaffilfailure,{maximumAge:600000});
					}
					else
					{
						$(affilgpselement).html('<p style="text-align: center">GPS not support in browser</p>');
					}
				});
			}
		},
		nearestaffilfailure: function(err)
		{
			$(humegpselement).html('<p style="text-align: center">Failed finding location. Please check handset and/or allow GPS from browser.</p>');
		},
		nearesthumegpssuccess: function (position)
		{
			try {
				var lat = position.coords.latitude;
				var lng = position.coords.longitude;
				$.getJSON('pages/nearesthumeatm.aspx?lat=' + lat + '&lng=' + lng + '&max=5', function(data) {
					var items = [];
					var out = "<table class=\"atmlist\" border=\"0\" width=\"100%\" cellspacing=\"3\" cellpadding=\"3\">";
					$.each(data.atms.atm, function(key, val) {
						items.push(new google.maps.Marker({ position: new google.maps.LatLng(val.lat,val.lng), title: "'" + val.addr + "'" }));
						out += "<tr class=\"atmlistitem\"><td>" + val.addr + "</td><td style=\"text-align: right;\" width=\"20%\">" + val.dist + "km</td></tr>";
					});
					if( items.length > 0 )
					{
						gmaps.sethumeatmsnearest(items);
					}
					out += "</table>";
					out += "<p style=\"text-align: center\"><a id=\"nearhumeatmgpsbtn\" class=\"button\" href=\"javascript:void(0);\" onclick=\"gmaps.displaymapnearesthumeatms('displaymap','overlay','.maplabel','.findatmlabel');\">Show MAP</a></p>";
					$(humegpselement).slideUp('slow',function(){
						$(humegpselement).html(out);
						$(humegpselement).slideDown('slow',function(){
							$('#nearhumeatmgpsbtn').mousedown(function(){
								$(this).addTemporaryClass("buttonclicked", 200);
							});
							$(humegpselement).addClass('active');
							$('html,body').delay(200).animate({scrollTop: $(humegpsscrollto).offset().top}, 700);
						});
					});	
				})
				.success(function() {})
				.error(function() {
					$(humegpselement).slideUp('slow',function(){
						$(humegpselement).html("<p style=\"text-align: center\">Error getting nearest hume atms</p>");
						$(humegpselement).slideDown('slow',function(){
							$('#nearhumeatmgpsbtn').mousedown(function(){
								$(this).addTemporaryClass("buttonclicked", 200);
							});
							$(humegpselement).addClass('active');
							$('html,body').delay(200).animate({scrollTop: $(humegpsscrollto).offset().top}, 700);
						});
					});	
				})
				.complete(function() {});
			}
			catch(err)
			{
				$(humegpselement).slideUp('slow',function(){
					$(humegpselement).html("<p style=\"text-align: center\">Error getting nearest hume atms</p>");
					$(humegpselement).slideDown('slow',function(){
						$('#nearhumeatmgpsbtn').mousedown(function(){
							$(this).addTemporaryClass("buttonclicked", 200);
						});
						$(humegpselement).addClass('active');
						$('html,body').delay(200).animate({scrollTop: $(humegpsscrollto).offset().top}, 700);
					});
				});	
			}
		},
		nearestaffilgpssuccess: function (position)
		{
			try {
				var lat = position.coords.latitude;
				var lng = position.coords.longitude;
				$.getJSON('pages/nearestaffilatm.aspx?lat=' + lat + '&lng=' + lng + '&max=5', function(data) {
					var items = [];
					var out = "<table class=\"atmlist\" border=\"0\" width=\"100%\" cellspacing=\"3\" cellpadding=\"3\">";
					$.each(data.atms.atm, function(key, val) {
						items.push(new google.maps.Marker({ position: new google.maps.LatLng(val.lat,val.lng), title: "'" + val.addr + "'" }));
						out += "<tr class=\"atmlistitem\"><td>" + val.addr + "</td><td style=\"text-align: right;\" width=\"20%\">" + val.dist + "km</td></tr>";
					});
					if( items.length > 0 )
					{
						gmaps.setaffilatmsnearest(items);
					}
					out += "</table>";
					out += "<p style=\"text-align: center\"><a id=\"nearaffilatmgpsbtn\" class=\"button\" href=\"javascript:void(0);\" onclick=\"gmaps.displaymapnearestaffilatms('displaymap','overlay','.maplabel','.findatmlabel');\">Show MAP</a></p>";
					$(affilgpselement).slideUp('slow',function(){
						$(affilgpselement).html(out);
						$(affilgpselement).slideDown('slow',function(){
							$('#nearaffilatmgpsbtn').mousedown(function(){
								$(this).addTemporaryClass("buttonclicked", 200);
							});
							$(affilgpselement).addClass('active');
							$('html,body').delay(300).animate({scrollTop: $(affilgpsscrollto).offset().top}, 700);
						});
					});	
				})
				.success(function() {})
				.error(function() {
					$(affilgpselement).slideUp('slow',function(){
						$(affilgpselement).html("<p style=\"text-align: center\">Error getting nearest affiliates list</p>");
						$(affilgpselement).slideDown('slow',function(){
							$('#nearaffilatmgpsbtn').mousedown(function(){
								$(this).addTemporaryClass("buttonclicked", 200);
							});
							$(affilgpselement).addClass('active');
							$('html,body').delay(300).animate({scrollTop: $(affilgpsscrollto).offset().top}, 700);
						});
					});	
				})
				.complete(function() {});
			}
			catch(err)
			{
				$(affilgpselement).slideUp('slow',function(){
					$(affilgpselement).html("<p style=\"text-align: center\">Error getting nearest affiliates list</p>");
					$(affilgpselement).slideDown('slow',function(){
						$('#nearaffilatmgpsbtn').mousedown(function(){
							$(this).addTemporaryClass("buttonclicked", 200);
						});
						$(affilgpselement).addClass('active');
						$('html,body').delay(300).animate({scrollTop: $(affilgpsscrollto).offset().top}, 700);
					});
				});	
			}
		},
		listallhume: function ( elmt, scrlto )
		{
			if( $(elmt).hasClass('active') )
			{
				$('html,body').animate({scrollTop: $('#main').offset().top}, 700, function(){
					$(elmt).slideUp('slow',function(){
						$(elmt).html = "";
					});
					$(elmt).removeClass('active');			
				});
			}
			else
			{
				$(elmt).html('<p style="text-align: center"><img src="img/ajaxloading.gif" /> Please Wait</p>');
				try {
					$(elmt).slideDown('slow',function(){
						$.getJSON('pages/listallatmshume.aspx', function(data) {
							var items = [];
							var out = "<table class=\"atmlist\" border=\"0\" width=\"100%\" cellspacing=\"3\" cellpadding=\"3\">";
							$.each(data.atms.atm, function(key, val) {
								items.push(new google.maps.Marker({ position: new google.maps.LatLng(val.lat,val.lng), title: "'" + val.addr + "'" }));
								out += "<tr class=\"atmlistitem\"><td>" + val.addr + "</td></tr>";
							});
							if( items.length > 0 )
							{
								gmaps.sethumeatmsall(items);
							}
							out += "</table>";
							out += "<p style=\"text-align: center\"><a id=\"listhumeatmgpsbtn\" class=\"button\" href=\"javascript:void(0);\" onclick=\"gmaps.displaymapallhumeatms('displaymap','overlay','.maplabel','.findatmlabel');\">Show MAP</a></p>";
							$(elmt).slideUp('slow',function(){
								$(elmt).html(out);
								$(elmt).slideDown('slow',function(){
									$('#listhumeatmgpsbtn').mousedown(function(){
										$(this).addTemporaryClass("buttonclicked", 200);
									});
									$(elmt).addClass('active');
									$('html,body').delay(300).animate({scrollTop: $(scrlto).offset().top}, 700);
								});
							});	
						})
						.success(function() {})
						.error(function() {
							$(elmt).slideUp('slow',function(){
								$(elmt).html("<p style=\"text-align: center\">Error getting Hume ATM listing</p>");
								$(elmt).slideDown('slow',function(){
									$('#listhumeatmgpsbtn').mousedown(function(){
										$(this).addTemporaryClass("buttonclicked", 200);
									});
									$(elmt).addClass('active');
									$('html,body').delay(300).animate({scrollTop: $(scrlto).offset().top}, 700);
								});
							});	
						})
						.complete(function() {});
					});
				}
				catch(err)
				{
					$(elmt).slideUp('slow',function(){
						$(elmt).html("<p style=\"text-align: center\">Error getting Hume ATM listing</p>");
						$(elmt).slideDown('slow',function(){
							$('#listhumeatmgpsbtn').mousedown(function(){
								$(this).addTemporaryClass("buttonclicked", 200);
							});
							$(elmt).addClass('active');
							$('html,body').delay(300).animate({scrollTop: $(scrlto).offset().top}, 700);
						});
					});	
				}
			}
		}
	};
}();

var gmaps = function(){
	var initialLocation;
	var browserSupportFlag =  new Boolean();
	var map;
	var myOptions = {
	    disableDefaultUI: true,
	    zoomControl: true,
	    zoom: 14,
	    noClear: false,
	    mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var element;
	var overlay;
	var initscroll;

	var gpswatch;
	var memarker;
	
	var humeatmsall;
	var humeatmsnearest;
	var humebranchesall;
	var humebranchesnearest;
	
	var affilatmsnearest;

	return {
		closemap: function ()
		{
			$('html,body').delay(300).animate({scrollTop: $(returnscroll).offset().top}, 700, function(){
				//$('#'+element).fadeOut('slow');
				$('#'+element).hide();
			});
			this.clearGPSwatch();
		},
		sethumeatmsall: function (list)
		{
			humeatmsall = list;
		},
		sethumeatmsnearest: function (list)
		{
			humeatmsnearest = list;
		},
		setaffilatmsnearest: function (list)
		{
			affilatmsnearest = list;
		},
		sethumebranchesall: function (list)
		{
			humebranchesall = list;
		},
		sethumebranchesnearest: function (list)
		{
			humebranchesnearest = list;
		},
		clearGPSwatch: function()
		{
			navigator.geolocation.clearWatch(gpswatch);
			$('#maploadingimg').fadeOut('slow');
		},
		resetPosition: function()
		{
			map.setCenter(initialLocation);
		},
		placeMeMarker: function()
		{
			if(navigator.geolocation)
			{
				browserSupportFlag = true;
				gpswatch = navigator.geolocation.watchPosition(
				function(position) {
					initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
					if( memarker != null )
					{
						memarker.setMap(null);
					}
					map.setCenter(initialLocation);
					memarker = new google.maps.Marker({ position: new google.maps.LatLng(position.coords.latitude,position.coords.longitude), title:'You', icon: 'img/green-dot.png' });
					memarker.setMap(map);
					$('#gpsaccuracy').html('A: ' + Math.round(position.coords.accuracy) + 'm');
					}, function() {
							$('#mapmessage').html('- Unable to locate using GPS. Please check device settings. Default location used');
							setTimeout("$('#mapmessage').fadeOut('slow')",5000);
							// Default action if to center on Head Office
							initialLocation = new google.maps.LatLng(-36.08159740543335,146.9184896349907);
							map.setCenter(initialLocation);
					});
				$('#maploadingimg').delay(700).fadeIn('slow');
				setTimeout('gmaps.clearGPSwatch()',30000);
			}
			else
			{
				$('#mapmessage').html('- Your device doesn\'t support GPS.');
				setTimeout("$('#mapmessage').fadeOut('slow')",5000);
				this.closemap();
			}
		},
		placeAllHumeATMMarkers: function()
		{
			var marker;
			for( i = 0; i < humeatmsall.length; i++ )
			{
				marker = humeatmsall[i];
				marker.setIcon('img/red-dot.png');
				marker.setMap(map);
			}
		},
		placeNearestHumeATMMarkers: function()
		{
			var marker;
			for( i = 0; i < humeatmsnearest.length; i++ )
			{
				marker = humeatmsnearest[i];
				marker.setIcon('img/red-dot.png');
				marker.setMap(map);
			}
		},
		placeNearestAffilATMMarkers: function()
		{
			var marker;
			for( i = 0; i < affilatmsnearest.length; i++ )
			{
				marker = affilatmsnearest[i];
				marker.setIcon('img/red-dot.png');
				marker.setMap(map);
			}
		},
		removeAllHumeATMMarkers: function()
		{
			var marker;
			if( humeatmsall != null )
			{
				for( i = 0; i < humeatmsall.length; i++ )
				{
					marker = humeatmsall[i];
					marker.setMap(null);
				}
			}
		},
		removeNearestHumeATMMarkers: function()
		{
			var marker;
			if( humeatmsnearest != null )
			{
				for( i = 0; i < humeatmsnearest.length; i++ )
				{
					marker = humeatmsnearest[i];
					marker.setMap(null);
				}
			}
		},
		removeNearestAffilATMMarkers: function()
		{
			var marker;
			if( affilatmsnearest != null )
			{
				for( i = 0; i < affilatmsnearest.length; i++ )
				{
					marker = affilatmsnearest[i];
					marker.setMap(null);
				}
			}
		},
		placeAllHumeBranchMarkers: function()
		{
			var marker;
			for( i = 0; i < humebranchesall.length; i++ )
			{
				marker = humebranchesall[i];
				marker.setIcon('img/blue-dot.png');
				marker.setMap(map);
			}
		},
		placeNearestHumeBranchMarkers: function()
		{
			var marker;
			for( i = 0; i < humebranchesnearest.length; i++ )
			{
				marker = humebranchesnearest[i];
				marker.setIcon('img/blue-dot.png');
				marker.setMap(map);
			}
		},
		removeAllHumeBranchMarkers: function()
		{
			var marker;
			if( humebranchesall != null )
			{
				for( i = 0; i < humebranchesall.length; i++ )
				{
					marker = humebranchesall[i];
					marker.setMap(null);
				}
			}
		},
		removeNearestHumeBranchMarkers: function()
		{
			var marker;
			if( humebranchesnearest != null )
			{
				for( i = 0; i < humebranchesnearest.length; i++ )
				{
					marker = humebranchesnearest[i];
					marker.setMap(null);
				}
			}
		},
		displaymap: function(item,item2,item3,item4) {
			element = item;
			overlay = item2;
			initscroll = item3;
			returnscroll = item4;
			$('#'+element).slideDown('slow', function(){
				$('html,body').animate({scrollTop: $(initscroll).offset().top}, 700);
			});
			if( !$('#'+element).data('displayed') )
			{
				map = new google.maps.Map(document.getElementById(overlay), myOptions);
				google.maps.event.trigger(map, 'resize') ;
				this.placeMeMarker();
				this.placeATMMarkers();
				$('#'+element).data('displayed',true);
			} else {
				google.maps.event.trigger(map, 'resize') ;
				this.placeMeMarker();
			}
		},
		displaymapnearesthumeatms: function(item,item2,item3,item4) {
			element = item;
			overlay = item2;
			initscroll = item3;
			returnscroll = item4;

			this.removeNearestAffilATMMarkers();
			this.removeNearestHumeATMMarkers();
			this.removeAllHumeATMMarkers();
			this.removeAllHumeBranchMarkers();
			this.removeNearestHumeBranchMarkers();
			
			//$('#'+element).slideDown('slow', function(){
			$('#'+element).show(0, function(){
				$('html,body').animate({scrollTop: $(initscroll).offset().top}, 700);
			});
			if( !$('#'+element).data('displayed') )
			{
				map = new google.maps.Map(document.getElementById(overlay), myOptions);
				google.maps.event.trigger(map, 'resize') ;
				this.placeMeMarker();
				this.placeNearestHumeATMMarkers();
				$('#'+element).data('displayed',true);
			} else {
				google.maps.event.trigger(map, 'resize');
				this.placeNearestHumeATMMarkers();
				this.placeMeMarker();
			}
		},
		displaymapnearesthumebranches: function(item,item2,item3,item4) {
			element = item;
			overlay = item2;
			initscroll = item3;
			returnscroll = item4;

			this.removeNearestAffilATMMarkers();
			this.removeNearestHumeATMMarkers();
			this.removeAllHumeATMMarkers();
			this.removeAllHumeBranchMarkers();
			this.removeNearestHumeBranchMarkers();
			
			//$('#'+element).slideDown('slow', function(){
			$('#'+element).show(0, function(){
				$('html,body').animate({scrollTop: $(initscroll).offset().top}, 700);
			});
			if( !$('#'+element).data('displayed') )
			{
				map = new google.maps.Map(document.getElementById(overlay), myOptions);
				google.maps.event.trigger(map, 'resize') ;
				this.placeMeMarker();
				this.placeNearestHumeBranchMarkers();
				$('#'+element).data('displayed',true);
			} else {
				google.maps.event.trigger(map, 'resize');
				this.placeNearestHumeBranchMarkers();
				this.placeMeMarker();
			}
		},
		displaymapnearestaffilatms: function(item,item2,item3,item4) {
			element = item;
			overlay = item2;
			initscroll = item3;
			returnscroll = item4;
			
			this.removeNearestAffilATMMarkers();
			this.removeNearestHumeATMMarkers();
			this.removeAllHumeATMMarkers();
			this.removeAllHumeBranchMarkers();
			this.removeNearestHumeBranchMarkers();
			
			//$('#'+element).slideDown('slow', function(){
			$('#'+element).show(0, function(){
				$('html,body').animate({scrollTop: $(initscroll).offset().top}, 700);
			});
			if( !$('#'+element).data('displayed') )
			{
				map = new google.maps.Map(document.getElementById(overlay), myOptions);
				google.maps.event.trigger(map, 'resize') ;
				this.placeMeMarker();
				this.placeNearestAffilATMMarkers();
				$('#'+element).data('displayed',true);
			} else {
				google.maps.event.trigger(map, 'resize') ;
				this.placeNearestAffilATMMarkers();
				this.placeMeMarker();
			}
		},
		displaymapallhumebranches: function(item,item2,item3,item4) {
			element = item;
			overlay = item2;
			initscroll = item3;
			returnscroll = item4;
			
			this.removeNearestAffilATMMarkers();
			this.removeNearestHumeATMMarkers();
			this.removeAllHumeATMMarkers();
			this.removeAllHumeBranchMarkers();
			this.removeNearestHumeBranchMarkers();
			
			//$('#'+element).slideDown('slow', function(){
			$('#'+element).show(0, function(){
				$('html,body').animate({scrollTop: $(initscroll).offset().top}, 700);
			});
			if( !$('#'+element).data('displayed') )
			{
				map = new google.maps.Map(document.getElementById(overlay), myOptions);
				google.maps.event.trigger(map, 'resize') ;
				this.placeMeMarker();
				this.placeAllHumeBranchMarkers();
				$('#'+element).data('displayed',true);
			} else {
				google.maps.event.trigger(map, 'resize') ;
				this.placeAllHumeBranchMarkers();
				this.placeMeMarker();
			}
		},
		displaymapallhumeatms: function(item,item2,item3,item4) {
			element = item;
			overlay = item2;
			initscroll = item3;
			returnscroll = item4;
			
			this.removeNearestAffilATMMarkers();
			this.removeNearestHumeATMMarkers();
			this.removeAllHumeATMMarkers();
			this.removeAllHumeBranchMarkers();
			this.removeNearestHumeBranchMarkers();
			
			//$('#'+element).slideDown('slow', function(){
			$('#'+element).show(0, function(){
				$('html,body').animate({scrollTop: $(initscroll).offset().top}, 700);
			});
			if( !$('#'+element).data('displayed') )
			{
				map = new google.maps.Map(document.getElementById(overlay), myOptions);
				google.maps.event.trigger(map, 'resize') ;
				this.placeMeMarker();
				this.placeAllHumeATMMarkers();
				$('#'+element).data('displayed',true);
			} else {
				google.maps.event.trigger(map, 'resize') ;
				this.placeAllHumeATMMarkers();
				this.placeMeMarker();
			}
		}
	};
}();

// When page loads lets do some stuff
$(document).ready(function() {

	// Setup TABS and there onclick fucntion
	$('#tabs li').click(function (){
		$('#tabs li').removeClass('selected');
		$(this).addClass('selected');
		window.scrollTo(0, 1);
		// Hide the existing page
		$('#'+$('body').data('activepage')).hide();
		// Set the active page to this page
		$('body').data('activepage',$(this).attr('page'));
		// Reset faq page
		if( $(this).attr('page') == 'faq' )
		{
			$('#detail').data('faqselected',true);	// Flag FAQ selected
			
			// Hide all the faq dialogs. Reset
			$('#gettingstarted_faq').hide();
			$('#security_faq').hide();
			$('#accessing_faq').hide();
			$('#transacting_faq').hide();
			// Remove all selected from the faq list
			$('#faq ul.list li.item a').removeClass('faqselected')
			// Show all FAQ section headers to allow selection
			$('#faq ul.list li.item').show();
			// Reset selected items
			$('#faq li.faqitem').children('a').removeClass('faqselected');
			$('#faq li.faqitem').children('p').hide();
		}
		if( $(this).attr('page') == 'locateus' )
		{
			// Hide the page elements
			$('#findatm').hide();
			$('#findbranch').hide();
			$('#findheader').show();
		}
		if( $(this).attr('page') == 'news' )
		{
			var nowtm = new Date();
			var lastloaded = $('#news').data('lastloaded');
			if( lastloaded == null | lastloaded == undefined )
			{
				populateNews();
			}
			else
			{
				// If 10 minutes has passed then reload the news
				if( (lastloaded + (((60)*10)*1000)) < nowtm.getTime() )
				{
					populateNews();
				}
			}
			$('#newsdetail').hide();
			$('#newslist').show();
		}
		// Display the page. FadeIn
		//$('#'+$('body').data('activepage')).fadeIn();
		$('#'+$('body').data('activepage')).show();
	});

	$('#atmbutton').click(function(){
		$('#findheader').delay(300).hide(0,function(){
			// Reset ATM options
			$('#nearesthumeatm').hide();
			$('#nearesthumeatm').removeClass('active');
			$('#listhumeatms').hide();
			$('#listhumeatms').removeClass('active');
			$('#nearestaffiliateatm').hide();
			$('#nearestaffiliateatm').removeClass('active');
			$('#findatm').show(0,function(){
				$('html,body').delay(300).animate({scrollTop: $('#main').offset().top}, 500);
			});
		});

	});
	$('#branchbutton').click(function(){
		$('#findheader').delay(300).hide(0,function(){
			// Reset Branch options
			$('#nearesthumebranch').hide();
			$('#nearesthumebranch').removeClass('active');
			$('#listhumebranches').hide();
			$('#listhumebranches').removeClass('active');
			$('#findbranch').show(0,function(){
				$('html,body').delay(300).animate({scrollTop: $('#main').offset().top}, 500);
			});
		});
	});
	
	$('#branchclosebutton').click(function(){
		$('html,body').animate({scrollTop: $('body').offset().top}, 500, function(){
			$('#findbranch').hide(0,function(){
				$('#findheader').show(0);
			});
		});
	});
	
	$('#atmclosebutton').click(function(){
		$('html,body').animate({scrollTop: $('body').offset().top}, 500, function(){
			$('#findatm').hide(0,function(){
				$('#findheader').show(0);
			});
		});
	});
	
	// Add onclick events
	$('#faq li.faqitem').click(function(){
		if( $(this).data('selected') )
		{
			$(this).children('a').removeClass('faqselected');
			$(this).data('selected',false);
			$(this).children('p').slideUp('slow');
			$('#detail').data('faqselected',true);
		}
		else
		{
			$('#faq li.faqitem').each(function(){
				if( $(this).data('selected') )
				{
					$(this).children('p').slideUp('slow',function(){
						try {
							$('html,body').animate({scrollTop: $('#faq li.faqitem .faqselected').offset().top}, 500);
						}
						catch (err)
						{
							//alert('Error');
						}
					});
					$(this).children('a').removeClass('faqselected');
					$(this).data('selected',false);
				}
			});
			$(this).children('a').addClass('faqselected');
			$(this).data('selected',true);
			$(this).children('p').slideToggle();
			if( $('#detail').data('faqselected') )
			{
				$('html,body').animate({scrollTop: $(this).offset().top}, 500);
			}
			$('#detail').data('faqselected',false);
		}
	});

	// Always attempt to hide the address bar. If user attempts 2x in 5 seconds we will stop catching
	$(window).scroll(function(){
		var nowtm = new Date();
		var prevreset = $('body').data('scrollreset');
		if( $(this).scrollTop() == 0 )
		{	
			if( prevreset == null | prevreset == undefined )
			{
				window.scrollTo(0, 1);
				$('body').data('scrollreset',nowtm.getTime());
			}
			else
			{
				if( prevreset + 5000 < nowtm.getTime() )
				{
					window.scrollTo(0, 1);
					$('body').data('scrollreset',nowtm.getTime());
				}
			}
		}
	});

	// Populate any of our dynamic pages i.e. News
	populateNews();
	
	// Add a temporary highlight for all mousedowns on list items
	$('ul.list li.item a').mousedown(function(){
		$(this).addTemporaryClass("itemselected", 200);
	});

	$('.button').mousedown(function(){
		$(this).addTemporaryClass("buttonclicked", 200);
	});
	
	$('#atmbutton').mousedown(function(){
		$(this).addTemporaryClass("buttonclicked", 200);
	});
	
	$('#branchbutton').mousedown(function(){
		$(this).addTemporaryClass("buttonclicked", 200);
	});
	
	// Initalise the home page i.e. Default page
	$('#home').delay(300).fadeIn('slow', function(){
		setTimeout('window.scrollTo(0, 1)',1000);	// Scroll to top when animation has finished
	});
	$('body').data('activepage','home');
	$('#map').data('displayed',false);
	
	// We are a web app so place notification bubble if required
	setTimeout('placeAddToHomeScreen()',3500);
	
	$(function() {
	    setInterval( 'toggleAdverts()', 10000 );
	});
});

// Create basic jQuery extensions for click highlight
(function($){

    $.fn.extend({ 

        addTemporaryClass: function(className, duration) {
            var elements = this;
            setTimeout(function() {
                elements.removeClass(className);
            }, duration);

            return this.each(function() {
                $(this).addClass(className);
            });
        }
    });

})(jQuery);


function placeAddToHomeScreen()
{
	// Handle iPhone
	if(navigator.userAgent.match(/iPhone/i))
	{
		if( !window.navigator.standalone )
		{
			if( typeof(localStorage) != 'undefined' )	// Local storage supported
			{
				//localStorage.removeItem('waprompt');
				if( !localStorage.getItem('waprompt') )
				{
					// We need to center align and place at the bottom with teh arrow pointing where to press
					$('#addtohomescreen').css('top',(window.innerHeight-($('#addtohomescreen').height()+40)));
					$('#addtohomescreen').css('left',((window.innerWidth-$('#addtohomescreen').width())/2)-10);
					// Fade the box in
					$('#addtohomescreen').delay(700).fadeIn('slow');
					// Add close handler
					$('#addtohomescreen').click(function(){	
						if( typeof(localStorage) != 'undefined' )
						{
							try {
								localStorage.setItem('waprompt', true);
							} catch (e) {
							 	if (e == QUOTA_EXCEEDED_ERR) {
				 	 		 		alert('Quota exceeded!'); //data wasn't successfully saved due to quota exceed so throw an error
								}
							}
						}
						$(this).fadeOut('slow');
					});
				}
			}
		}
	}
}

function selectNewsItem(dbid)
{
	// Change the ad to reflect that we have clicked!!! Some nicities for touch screen users
	var $active = $('#advert div.active');
	if ( $active.length == 0 ) $active = $('#advert div:last');
	$active.css('opacity',0.3);
	$active.delay(300).animate({opacity: 1.0});
	// Let atems to get the news item
	try {
		$.getJSON('pages/newsitem.aspx?item='+dbid, function(data) {
			var out = "";
			$.each(data.news.item, function(key, val) {
				// Start building up the temporary page for the news item
				out += "<ul class='list'><li class='item'><a class='label' href='javascript:void(0);'>" + unescape(val.label) + "</a></li></ul><div style='padding: 0px 20px;'>" + unescape(val.detail) + "</div>";
				$('#tabs li').removeClass('selected');
				// Add news as selected tabe
				$('#tabs li').each(function(index){
					if( $(this).attr('page') == 'news' )
					{
						$(this).addClass('selected');
					}
				});
				// Change the pages
				$('#'+$('body').data('activepage')).hide();					// Hide the active page so no flickering when we populate
				$('body').data('activepage','news');						// Activate the news page
				$('#newslist').hide();										// Hide the list of items as we are displaying a news item
				$('#'+$('body').data('activepage')).show();					// Show the active page
				$('#newsdetail').hide();									// Hide the detail
				$('#newsdetail').html(out);									// Populate the news item
				$('#newsdetail').show();									// Show the news item
				// Scroll to item
				$('html,body').delay(100).animate({scrollTop: $('#newsdetail').offset().top}, 500);
				// Add click to return to news page i.e. Hide this and show previous
				$('#newsdetail ul.list li.item a').click(function(){
					$('html,body').animate({scrollTop: $('#advert').offset().top}, 500, function(){
						$('#newsdetail').delay(300).hide(1); 
						$('#newslist').delay(300).show(1);
					});
				});
				// Add onclick highlight
				$('#news ul.list li.item a').mousedown(function(){
					$(this).addTemporaryClass("itemselected", 300);
				});
			});			
		})
		.success(function() {
		})
		.error(function(xhr, ajaxOptions, thrownError) {
			$('#news').html("<p style=\"padding: 10px;\">Error obtaining news. Click <a href=\"javascript:void;\" onclick=\"populateNews()\">here</a> to try again.");
		})
		.complete(function() {});
	}
	catch(err)
	{
	}
}

function populateNews()
{
	try {
		$.getJSON('pages/news.aspx?items=10', function(data) {
			var nowtm = new Date();
			var items = [];
			var out = "<div id='newslist'><ul class='list'>";
			$.each(data.news.item, function(key, val) {
				out += "<li class='item'><a class='label' href='javascript:void(0);' dbid='" + val.dbid + "' label='" + val.label + "' detail='" + val.detail + "'>" + unescape(val.label) + "</a></li>";
			});
			out += "</ul></div><div id='newsdetail'></div>";
			// Add built up markup to element
			$('#news').html(out);
			// Add onclick highlight
			$('#news ul.list li.item a').mousedown(function(){
				$(this).addTemporaryClass("itemselected", 200);
			});
			// Set the last loaded time
			$('#news').data('lastloaded',nowtm.getTime());
		})
		.success(function() {
			// Add onclick events
			$('#news li.item a.label').click(function(){
				var det = unescape($(this).attr('detail'));	// Remove any encoding
				var lab = unescape($(this).attr('label'));	// Remove any encoding
				// Start building up the temporary page for the news item
				$('#newsdetail').hide();
				$('#newsdetail').html("<ul class='list'><li class='item'><a class='label' href='javascript:void(0);'>" + lab + "</a></li></ul><div style='padding: 0px 20px;'>" + det + "</div>");
				$('#newslist').delay(300).hide(0,function(){
					$('#newsdetail').delay(200).show();
					$('html,body').animate({scrollTop: $('#newsdetail').offset().top}, 500);
				});
				// Add click to return to news page i.e. Hide this and show previous
				$('#newsdetail ul.list li.item a').click(function(){
					$('html,body').animate({scrollTop: $('#advert').offset().top}, 500, function(){
						$('#newsdetail').delay(300).hide(1); 
						$('#newslist').delay(300).show(1);
					});
				});
				// Add onclick highlight
				$('#news ul.list li.item a').mousedown(function(){
					$(this).addTemporaryClass("itemselected", 300);
				});
			});
		})
		.error(function(xhr, ajaxOptions, thrownError) {
			$('#news').html("<p style=\"padding: 10px;\">Error obtaining news. Click <a href=\"javascript:void;\" onclick=\"populateNews()\">here</a> to try again.");
		})
		.complete(function() {});
	}
	catch(err)
	{
	}
}


function toggleAdverts() {

	var adcount = $('#advert div');
	// Only switch if there is something to switch to!!!
	if( adcount.length > 1 )
	{
		var $active = $('#advert div.active');
		if ( $active.length == 0 ) $active = $('#advert div:last');
		var $next =  $active.next().length ? $active.next() : $('#advert div:first');
		$active.addClass('last-active');
		$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 700, function() {
				$active.removeClass('active last-active');
			});
	}
}

function loadmBank()
{
	window.open('https://ibank.humebuild.com.au/mobile/SignOn/Login.aspx','_blank');
}

function loadfullsite()
{
	window.open('http://www.humebuild.com.au','_blank');
}

function faqGettingStarted()
{
	if( $('#gettingstarted_header li a').hasClass('faqselected') ) 
	{
		$('html,body').animate({scrollTop: $('body').offset().top}, 700, function(){
			$('#gettingstarted_faq').hide();
			$('#faq ul.list li.item').delay(200).show(); 
			$('#gettingstarted_header li a').removeClass('faqselected');
		});
	} 
	else 
	{
		$('#gettingstarted_header li a').addClass('faqselected');
		//$('#faq ul.list li.item').hide();
		$('#faq ul.list li.item').slideUp();
		//$('#gettingstarted_header li a').parent().show(); 
		$('#gettingstarted_header li a').parent().delay(300).slideDown(function(){
			$('#gettingstarted_faq').show();
			$('html,body').delay(500).animate({scrollTop: $('#gettingstarted_header').offset().top}, 500);
		});
	}
}

function faqSecurity()
{
	if( $('#security_header li a').hasClass('faqselected') ) 
	{
		$('html,body').animate({scrollTop: $('body').offset().top}, 700, function(){
			$('#security_faq').hide(); 
			$('#faq ul.list li.item').show(); 
			$('#security_header li a').removeClass('faqselected');
		});
	} 
	else 
	{
		$('#security_header li a').addClass('faqselected');
		//$('#faq ul.list li.item').hide();
		$('#faq ul.list li.item').slideUp();
		$('#security_header li a').parent().delay(300).slideDown(function(){
			$('#security_faq').show();
			$('html,body').delay(500).animate({scrollTop: $('#security_header').offset().top}, 500);
		}); 
		//$('#security_header li a').parent().show(); 
	}
}

function faqAccessing()
{
	if( $('#accessing_header li a').hasClass('faqselected') ) 
	{
		$('html,body').animate({scrollTop: $('body').offset().top}, 700, function(){
			$('#accessing_faq').hide(); 
			$('#faq ul.list li.item').show(); 
			$('#accessing_header li a').removeClass('faqselected');
		});
	} 
	else 
	{
		$('#accessing_header li a').addClass('faqselected');
		//$('#faq ul.list li.item').hide();
		$('#faq ul.list li.item').slideUp();
		//$('#accessing_header li a').parent().show(); 
		$('#accessing_header li a').parent().delay(300).slideDown(function(){
			$('#accessing_faq').show();
			$('html,body').delay(500).animate({scrollTop: $('#accessing_header').offset().top}, 500);
		}); 
	}
}

function faqTransacting()
{
	if( $('#transacting_header li a').hasClass('faqselected') ) 
	{
		$('html,body').animate({scrollTop: $('body').offset().top}, 700, function(){
			$('#transacting_faq').hide(); 
			$('#faq ul.list li.item').show(); 
			$('#transacting_header li a').removeClass('faqselected');
		});
	} 
	else 
	{
		$('#transacting_header li a').addClass('faqselected');
		$('#faq ul.list li.item').hide();
		//$('#transacting_header li a').parent().show(); 
		$('#transacting_header li a').parent().delay(300).slideDown(function(){
			$('#transacting_faq').show();
			$('html,body').delay(500).animate({scrollTop: $('#transacting_header').offset().top}, 500);
		}); 
	}
}

