	/*
	************************************************************************

	Default scripts
		General scripts

	@Homepage:		http://www.internotredici.com
	@Author:			Fabrizio Menghini Calderón <fabrizio@internotredici.com>
	@License:		Creative Commons Attribution-Noncommercial-Share Alike 
						3.0 Unported License
	@Copyright:		2009 (c) Interno Tredici. 
						Some rights reserved.

	************************************************************************
	*/





	/*
	========================================================================
		JQuery ready function
	========================================================================
	*/

	function getInternetExplorerVersion()
	{
		var rv = -1; // Return value assumes failure.
		if(navigator.appName == 'Microsoft Internet Explorer')
		{
			var ua = navigator.userAgent;
			var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
			if (re.exec(ua) != null)
				rv = parseFloat( RegExp.$1 );
		}
		return rv;
	}

	$(document).ready
	(
		function()
		{
			// Opening ready function



			$.fn.wait = function(time, type) 
			{
				time = time || 4000;
				type = type || "fx";
				return this.queue(type, function() 
				{
					var self = this;
					setTimeout(function() 
					{
						$(self).dequeue();
					}, time);
				});
			};



			/*
			==================================================================
				Instructions
			==================================================================
			*/

			// IE - See the conditional include of the CSS
			if(getInternetExplorerVersion() == 6)
			{
				$(".filter_row label").click(function() 
				{
					$(".filter_row label").css("background-image","url(/images/radio_off.png)");
					$(this).css("background-image","url(/images/radio_on.png)");
				});

				$(".filter_row input").each(function(index)
				{
					if($(this).attr("checked"))
						$(this).next("label").css("background-image","url(/images/radio_on.png)");
				});
			}

			// Disable autocomplete for inputs
			$("input").attr("autocomplete","off");

			// Select input field contents
			$("input").focus(function()
			{
				this.select();
			});

			// Overlay panel
			if(getInternetExplorerVersion() == -1)
			{
				if($("#overlay").length)
				{
					$("#overlay").show();

					// Remove scrollbars
					// $("body").css("overflow","hidden");

					// Get the window height
					var top_message_box = Math.round(($(window).height() - ($("#overlay .message").height() + 85)) / 2);
					$("#overlay .message").css("margin-top",top_message_box);

					$("#overlay").wait().fadeOut("slow",function() 
						{
							$("#wrapper.first_time").show();				
							sIFR.replace(anonimround,
								{
									selector: 'span.sifr_price', 
									css: '.sIFR-root { background-color: transparent; color: #f2711d; font-size: 66px; width: 215px; }'
								});
						});
				}
			}
			else
			{
				if($("#overlay").length)
				{
					$("#wrapper.first_time").show();
				}				
			}
				


			/*
			==================================================================
				Filters
			==================================================================
			*/

			$("#filter_everyone").click(function () { document.getElementById("filter_form").submit(); });
			$("#filter_men").click(function () { document.getElementById("filter_form").submit(); });
			$("#filter_women").click(function () { document.getElementById("filter_form").submit(); });



			$("#frequency dt a").click(function() { $("#frequency dd ul").toggle(); });
			$("#sex dt a").click(function() { $("#sex dd ul").toggle(); });
			$("#age dt a").click(function() { $("#age dd ul").toggle(); });
			$("#county dt a").click(function() { $("#county dd ul").toggle(); });
			$("#kids dt a").click(function() { $("#kids dd ul").toggle(); });

			$("#frequency dd ul li a").click(function() 
			{
				var text = $(this).html();

				$("#frequency dt a span").html(text);
				$("#frequency dd ul").hide();
				$("#frequency_value").val(getSelectedValue("frequency"));
			});
			$("#sex dd ul li a").click(function() 
			{
				var text = $(this).html();
				$("#sex dt a span").html(text);
				$("#sex dd ul").hide();
				$("#sex_value").val(getSelectedValue("sex"));

				if($("#filter_sex").length)
				{
					$(".skinned").attr("checked","");
					$("#filter_sex").attr("checked","checked");
					$("#filter_sex").val(getSelectedValue("sex"));
					$("#filter_drop").val("sex");

					$(".filter_row label").css("background-image","url(/images/radio_off.png)");
					$("#filter_drop_county").css("background-image","url(/images/radio_off_drop.png)");
					$("#filter_drop_age").css("background-image","url(/images/radio_off_drop.png)");
					$("#filter_drop_kids").css("background-image","url(/images/radio_on_drop.png)");
					$('#filter_form').submit();
				}
			});
			$("#age dd ul li a").click(function() 
			{
				var text = $(this).html();
				$("#age dt a span").html(text);
				$("#age dd ul").hide();
				$("#age_value").val(getSelectedValue("age"));

				if($("#filter_age").length)
				{
					$(".skinned").attr("checked","");
					$("#filter_age").attr("checked","checked");
					$("#filter_age").val(getSelectedValue("age"));
					$("#filter_drop").val("age");

					$(".filter_row label").css("background-image","url(/images/radio_off.png)");
					$("#filter_drop_county").css("background-image","url(/images/radio_off_drop.png)");
					$("#filter_drop_kids").css("background-image","url(/images/radio_off_drop.png)");
					$("#filter_drop_age").css("background-image","url(/images/radio_on_drop.png)");
					$('#filter_form').submit();
				}
			});
			$("#county dd ul li a").click(function() 
			{
				var text = $(this).html();
				$("#county dt a span").html(text);
				$("#county dd ul").hide();
				$("#county_value").val(getSelectedValue("county"));

				if($("#filter_county").length)
				{
					$(".skinned").attr("checked","");
					$("#filter_county").attr("checked","checked");
					$("#filter_county").val(getSelectedValue("county"));
					$("#filter_drop").val("county");

					$(".filter_row label").css("background-image","url(/images/radio_off.png)");
					$("#filter_drop_age").css("background-image","url(/images/radio_off_drop.png)");
					$("#filter_drop_kids").css("background-image","url(/images/radio_off_drop.png)");
					$("#filter_drop_county").css("background-image","url(/images/radio_on_drop.png)");
					$('#filter_form').submit();
				}
			});
			$("#kids dd ul li a").click(function() 
			{
				var text = $(this).html();
				$("#kids dt a span").html(text);
				$("#kids dd ul").hide();
				$("#kids_value").val(getSelectedValue("kids"));

				if($("#filter_kids").length)
				{
					$(".skinned").attr("checked","");
					$("#filter_kids").attr("checked","checked");
					$("#filter_kids").val(getSelectedValue("kids"));
					$("#filter_drop").val("kids");

					$(".filter_row label").css("background-image","url(/images/radio_off.png)");
					$("#filter_drop_county").css("background-image","url(/images/radio_off_drop.png)");
					$("#filter_drop_age").css("background-image","url(/images/radio_off_drop.png)");
					$("#filter_drop_kids").css("background-image","url(/images/radio_on_drop.png)");
					$('#filter_form').submit();
				}
			});

			$(document).bind('click', function(e) 
			{
				var $clicked = $(e.target);
				if(!$clicked.parents().is("#frequency"))
					$("#frequency dd ul").hide();
				if(!$clicked.parents().is("#sex"))
					$("#sex dd ul").hide();
				if(!$clicked.parents().is("#age"))
					$("#age dd ul").hide();
				if(!$clicked.parents().is("#county"))
					$("#county dd ul").hide();
				if(!$clicked.parents().is("#kids"))
					$("#kids dd ul").hide();
			});

			function getSelectedValue(id) 
			{
				return $("#" + id).find("dt a span.value").html();
			}



			$("#filter_drop_sex").click(function() 
			{
				$(".skinned").attr("checked","");
				$("#filter_sex").attr("checked","checked");

				$(this).css("background-image","url(/images/radio_on_drop.png)");
				$("#filter_drop_age").css("background-image","url(/images/radio_off_drop.png)");
				$("#filter_drop_county").css("background-image","url(/images/radio_off_drop.png)");
				$("#filter_drop_kids").css("background-image","url(/images/radio_off_drop.png)");
			});
			$("#filter_drop_age").click(function() 
			{
				$(".skinned").attr("checked","");
				$("#filter_age").attr("checked","checked");

				$(this).css("background-image","url(/images/radio_on_drop.png)");
				$("#filter_drop_sex").css("background-image","url(/images/radio_off_drop.png)");
				$("#filter_drop_county").css("background-image","url(/images/radio_off_drop.png)");
				$("#filter_drop_kids").css("background-image","url(/images/radio_off_drop.png)");
			});
			$("#filter_drop_county").click(function() 
			{
				$(".skinned").attr("checked","");
				$("#filter_county").attr("checked","checked");

				$(this).css("background-image","url(/images/radio_on_drop.png)");
				$("#filter_drop_sex").css("background-image","url(/images/radio_off_drop.png)");
				$("#filter_drop_age").css("background-image","url(/images/radio_off_drop.png)");
				$("#filter_drop_kids").css("background-image","url(/images/radio_off_drop.png)");
			});
			$("#filter_drop_kids").click(function() 
			{
				$(".skinned").attr("checked","");
				$("#filter_kids").attr("checked","checked");

				$(this).css("background-image","url(/images/radio_on_drop.png)");
				$("#filter_drop_sex").css("background-image","url(/images/radio_off_drop.png)");
				$("#filter_drop_age").css("background-image","url(/images/radio_off_drop.png)");
				$("#filter_drop_county").css("background-image","url(/images/radio_off_drop.png)");
			});
			$(".skinned").click(function() 
			{
				$(".skinned").attr("checked","");
				$(this).attr("checked","checked");
				$("#filter_drop_sex").css("background-image","url(/images/radio_off_drop.png)");
				$("#filter_drop_age").css("background-image","url(/images/radio_off_drop.png)");
				$("#filter_drop_county").css("background-image","url(/images/radio_off_drop.png)");
				$("#filter_drop_kids").css("background-image","url(/images/radio_off_drop.png)");
			});




			$('.bubble').each(function()
			{
				$(this).click(function()
				{
					var bubble_id = '#' + $(this).attr('rel');
					var bubble_content = $(bubble_id).html();

					// Position the bubble
					$(bubble_id).css(
						{
							position: 'absolute',
							top: $(this).offset().top - 175,
							left: $(this).offset().left - 50
						});

					// Add the close button
					$(bubble_id).html("<p class=\"close_button\"><a href=\"#\">Close X</a></p>" + bubble_content);

					$(bubble_id + " .close_button a").click(function()
					{
						// Revert the original content
						$(bubble_id).empty();
						$(bubble_id).html(bubble_content);
						$(bubble_id).hide();

						// Disable the href
						return false;
					});

					// Show the bubble
					$(bubble_id).show();

					// Disable the href
					return false;
				});
			});




			// Closing ready function
		}
	);

	function show_bookmark_panel()
	{
		if($("#bookmark_panel").length)
		{
			// Remove scrollbars
			$("body").css("overflow","hidden");
			$("#bookmark_panel").show();

			// Get the window height
			var top_message_box = Math.round(($(window).height() - ($("#bookmark_panel .message").height() + 85)) / 2);
			$("#bookmark_panel .message").css("margin-top",top_message_box);

			$("#bookmark_panel").wait().fadeOut("slow",function() 
				{
					$("body").css("overflow","visible");
					window.scroll(0,1);						
				});
		}
	}

	function gotoptsb(level)
	{
		if(level == "above")
			pageTracker._trackPageview("/virtual/custom/above-average-savings-CTA");
		else
			pageTracker._trackPageview("/virtual/custom/under-average-savings-CTA");
		document.location.href = "https://www.permanenttsb.ie/whatweoffer/savingsandinvesting/savingsaccounts/21daynoticeregularsaveraccount/";
	}
