/**********************************************************************

Scripts
	This file provide all javascript to support operations in the 
	website

@Package:		Aviva
@Link: 		http://www.iqcontent.com
@Author: 		Fabrizio Menghini Calderon <fabrizio.menghini@iqcontent.com>
@Copyright:		Copyright (c) 2009 Fabrizio Menghini Calderon
@Change Log:

	[R001] ~ 2009-12-14: Creation

**********************************************************************/





	function enableTooltips(id)
	{
		var links,i,h;
		if(!document.getElementById || !document.getElementsByTagName) return;
		h = document.createElement("span");
		h.id = "btc";
		h.setAttribute("id","btc");
		h.style.position = "absolute";
		document.getElementsByTagName("body")[0].appendChild(h);
		if(id == null) 
			links = document.getElementsByTagName("a");
		else 
			links = document.getElementById(id).getElementsByTagName("a");
		for(i = 0; i < links.length; i++)
		{
			var link = links[i];
			if(link.getAttribute("rel") == "bubble")
				Prepare(links[i]);
		}
	}

	function Prepare(el)
	{
		var tooltip,t,b,s,l;
		t = el.getAttribute("title");
		if(t == null || t.length == 0) 
			t = "link:";
		el.removeAttribute("title");
		tooltip = CreateEl("span","tooltip");
		s = CreateEl("span","top");
		s.appendChild(document.createTextNode(t));
		tooltip.appendChild(s);
		b = CreateEl("b","bottom");
		tooltip.appendChild(b);
		setOpacity(tooltip);
		el.tooltip = tooltip;
		el.onmouseover = showTooltip;
		el.onmouseout = hideTooltip;
		el.onmousemove = Locate;
	}

	function showTooltip(e)
	{
		document.getElementById("btc").appendChild(this.tooltip);
		Locate(e);
	}

	function hideTooltip(e)
	{
		var d = document.getElementById("btc");
		if(d.childNodes.length > 0) 
			d.removeChild(d.firstChild);
	}

	function setOpacity(el)
	{
		el.style.filter = "alpha(opacity:95)";
		el.style.KHTMLOpacity = "0.95";
		el.style.MozOpacity = "0.95";
		el.style.opacity = "0.95";
	}

	function CreateEl(t,c)
	{
		var x = document.createElement(t);
		x.className = c;
		x.style.display = "block";
		return(x);
	}

	function Locate(e)
	{
		var posx = 0, posy = 0;
		if(e == null) 
			e = window.event;
		if(e.pageX || e.pageY)
		{
			posx = e.pageX; 
			posy = e.pageY;
		}
		else if(e.clientX || e.clientY)
		{
			if(document.documentElement.scrollTop)
			{
				posx = e.clientX + document.documentElement.scrollLeft;
				posy = e.clientY + document.documentElement.scrollTop;
			}
			else
			{
				posx = e.clientX + document.body.scrollLeft;
				posy = e.clientY + document.body.scrollTop;
			}
		}
		document.getElementById("btc").style.top = (posy + 20) + "px";
		document.getElementById("btc").style.left = (posx - 35) + "px";
	}

$(document).ready
(
	function()
	{
		/*
		==============================================
			Bubble tooltips
		==============================================
		*/

		enableTooltips();



		/*
		==============================================
			Banner buttons
		==============================================
		*/

		if($("#banner_button").length > 0)
		{
			var bnr_button = $("#banner_button").attr("src");
			var bnr_hover = $("#banner_button_hover").attr("src");
		
			/* Change the banner button on mouse hover */
			$("#banner_button").hover ( function () { $("#banner_button").attr("src", bnr_hover); }, function () { $("#banner_button").attr("src", bnr_button); } );
			/* Change the banner button on mouse hover */
			$("#banner_button").focus ( function () { $("#banner_button").attr("src", bnr_hover); } );
			/* Change the banner button on mouse hover */
			$("#banner_button").blur ( function () { $("#banner_button").attr("src", bnr_button); } );
		}



		/*
		==============================================
			Fix the height for driver doormat
		==============================================
		*/	

		function fixDriver()
		{
			var max_height = 0;
			$("#upper_columns .teaser .SF_content").each
			(
				function (i)
				{
					if($(this).height() > max_height)
						max_height = $(this).height();
				}
			);
			$("#teaser_driving .SF_content").height(max_height);
		}
		fixDriver();

		/*
		==============================================
			Spring content
		==============================================
		*/

		/* Bind a click event */
		$(".trigger").click ( function () 
		{
			var target = $(this).attr("rel");
			if($("#" + target).css("display") == "block")
			{
				$("#" + target).slideUp("slow");
				$(this).parent().find("img").attr("src","/online/images/plus_icon.png");
				$(".trigger").parent().css("margin-bottom","0");
			}
			else
			{
				$(".spring").css("display","none");
				$(".trigger").parent().find("img").attr("src","/online/images/plus_icon.png");
				$(".trigger").parent().css("margin-bottom","0");
				$("#" + target).slideDown("slow");
				$(this).parent().css("margin-bottom",".7em");
				$(this).parent().find("img").attr("src","/online/images/minus_icon.png");
			}
		} );



		/*
		==============================================
			Rotating banners
		==============================================
		*/

		if($("#rotating_gallery").length > 0)
		{
			$('#rotating_gallery div:first').css("display","block");
			$('#rotating_gallery').cycle (
				{
					fx: 'fade',
					pause: true,
					pager: '#gallery_pagination', 
					timeout: 2000
				} );
		};



		/*
		==============================================
			T4 workarounds
		==============================================
		*/



		// Colorize even rows in tables 
		if($("#content_main table tr:even").length > 0)
			$("#content_main table tr:even").addClass('SF_even');

		// Add last_item class for the last two elements of the 3rd navigation bar 
		if($("#topic ul li:last-child").length > 0)
			$("#topic ul li:last-child").prev().addClass('last_item');

		// Add a <div class="column_separator"></div> inside every li in the sidebar 
		/*if($("#sidebar ul:not(.simple) li:not(:last-child)").length > 0)
			$("#sidebar ul:not(.simple) li:not(:last-child)").append('<div class="straight_separator"></div>');
		if($(".sidebar ul:not(.simple) li:not(:last-child)").length > 0)
			$(".sidebar ul:not(.simple) li:not(:last-child)").append('<div class="straight_separator"></div>');
		if($("#call_actions ul.listoflinks li:not(:last-child)").length > 0)
			$("#call_actions ul.listoflinks li:not(:last-child)").append('<div class="straight_separator"></div>');*/

		// Removes separator from brochure sidebar 
		/*if($("#sidebar_docs .straight_separator:last-child").length > 0)
			$("#sidebar_docs .straight_separator:last-child").hide();*/

		// Position get a quote three column teaser elements 
		/*if($("#teaser_ins_quote").length > 0)
		{
			var offset = $(".image_banner img:not(#banner_button)").offset();
			$("#teaser_ins_quote #banner_button").css("position","absolute");
			$("#teaser_ins_quote #banner_button").css("top",offset.top + 172);
			$("#teaser_ins_quote #banner_button").css("left",offset.left + 470);
			$("#teaser_ins_quote #banner_button").css("z-index",40);
			$("#teaser_ins_quote #banner_button").width(129);
			$("#teaser_ins_quote #banner_button").height(34);
			$("#teaser_quote_cont").css("position","absolute");
			$("#teaser_quote_cont").css("top",offset.top + 135);
			$("#teaser_quote_cont").css("left",offset.left + 228);
			$("#teaser_quote_cont").css("z-index",40);
		}*/

		// Make doormats clickable
		if($("h1.doormat a").length > 0)
		{
			$("h1.doormat a").each
			(
				function()
				{
					var url = $(this).attr("href");
					$(this).parent().prev().attr("href",url);
				}
			);
		}



		/*
		==============================================
			Accordion fold (Need to be the last one)
		==============================================
		*/



		if($("#accordion").length > 0)
		{
			jQuery('#accordion').Accordion(
			{ 
				event: 'click',
				active: 0, 
				selectedClass: 'active', 
				collapsible: true, 
				autoheight: true
			}).change(function(event, newHeader, oldHeader, newContent, oldContent) 
				{
					var white = $('#accordion_top_white').attr('src');
					var grey = $('#accordion_top_grey').attr('src');
					if($('#accordion .firstDT').hasClass('active'))
					{
						$('.info-section-index .header').css("background","#fff url(" + grey + ") no-repeat left top");
					}
					else
					{
						$('.info-section-index .header').css("background","#fff url(" + white + ") no-repeat left top");
					}
				});
		}



	}
);



function highlight()
{
	$('.highlight').colorBlend();
}

function submitQuote(form)
{
	if (document.getElementById('confirm_assumptions').checked == false) 
	{ 
		alert('Please tick the box to indicate you accept the data protection statement, Terms & Conditions and assumptions we make in providing your quote.'); 
	} 
	else 
	{ 
		document.termsForm.submit();
	}
}
