
/* main.js */

(function($) {
/**
* Adds odd, even, first and last classes to appropriate table and list descendants.
*/
$.fn.decorate = function(options) {
var options = $.extend(true, {}, $.fn.decorate.defaults, options);


return this.each(function() {
// Perhaps tr's should get first and last class too? td's should get even and odd?
if(options.even) $('tbody > tr:odd, li:odd', this).addClass(options.evenClass); // zero based, so odd and even are switched
if(options.odd) $('tbody > tr:even, li:even', this).addClass(options.oddClass);
if(options.first) $('tr:first-child, td:first-child, th:first-child, li:first-child', this).addClass(options.firstClass);
if(options.last) $('tr:last-child, td:last-child, th:last-child, li:last-child', this).addClass(options.lastClass);
return $(this);
});
}

$.fn.decorate.defaults = {
even : true, odd : true, first : true, last : true,
evenClass : 'even', oddClass : 'odd', firstClass : 'first', lastClass : 'last'
}
})(jQuery);



function SetPageHeight () {
	this.content = $(".size3of5:first");
	this.subcontent = $(".right1of5:first");
	this.subnav = $(".left1of5:first");
	this.init();
}

SetPageHeight.prototype.init = function () {
	if(this.subnav){
		if($(this.content).height() < $(this.subnav).height()){
			this.setHeight();
		}
	}
}

SetPageHeight.prototype.setHeight = function () {
	var newHeight = ($(this.subnav).height() + 20) + 'px';
	$(this.content).css('height', newHeight);
}

function OpenCountrySelector () {
	this.trigger = $("#open-lang");
	this.target = $("#lang-dropdown");
	this.init();
}

OpenCountrySelector.prototype.init = function () {

	/* get the link that triggers the drop down and prevent the default action on click */
	$($(".lang-selector")[0]).click(function(event){event.preventDefault()});
	
	this.trigger.mouseenter(jQuery.proxy(this.showLang, this));
	this.trigger.mouseleave(jQuery.proxy(this.hideLang, this));
	this.toHeight = this.target.innerHeight();
	this.target.height(0)
}

OpenCountrySelector.prototype.showLang = function () {
	$($(".lang-selector")[0]).css('background-color','#f7f6f6');
	this.target.show();
	this.target.animate( {
		height : this.toHeight
	},200);
}

OpenCountrySelector.prototype.hideLang = function () {
	$($(".lang-selector")[0]).css('background-color','#ffffff');
	this.target.animate( {
		height : 0
	},200, jQuery.proxy(this.closeLang, this));
}

OpenCountrySelector.prototype.closeLang = function () {
	this.target.hide();
	this.target.clearQueue();
}

/*** Vertical scrolling ticker-tape
/ Structure: <div>(=container)<ul><li></li>etc.</ul></div>
/ containerId = string containing the ID of the container around the ticker-list 
/ animTime = integer containing time to animate the scrolling in milliseconds
/ pauseTime = integer containing time to pause between scroll animations in milliseconds
***/
/** code is commented as the ticker functionality is no more in use **/
/*
function Ticker (animTime, pauseTime, containerId) {
	this.animTime = animTime;
	this.pauseTime = pauseTime;
	this.containerId = containerId;
	this.tickerContainer = $('#' + this.containerId + ' > ul');
	var base = this;
	
	$('#' + this.containerId + ' > ul').mouseover(function () {
		base.tickerContainer.stop();
		$($('#' + base.containerId + ' > ul > li')[0]).stop();
	});
	$('#' + this.containerId + ' > ul').mouseleave(function () {clearTimeout(base.timeOutId); base.timeOutId = setTimeout(function () {base.nextTick()}, base.animTime);});
	
	this.timeOutId = setTimeout(function(){base.nextTick()}, this.pauseTime);
}
Ticker.prototype.nextTick = function () {
	var base = this;
	var topTick = $($('#' + this.containerId + ' > ul > li')[1]).position().top;
	clearTimeout(this.timeOutId);
	this.tickerContainer.animate( {
			'margin-top': '-' + topTick + 'px'
		}, base.animTime, 'linear', function () {
			$($('#' + base.containerId + ' > ul > li')[0]).css('opacity', '');
			$('#' + base.containerId + ' > ul').append($('#' + base.containerId + ' > ul > li')[0]);
			$('#' + base.containerId + ' > ul').css('margin', '0');
			base.timeOutId = setTimeout(function (){base.nextTick()}, base.pauseTime);
		}
	);
	$($('#' + base.containerId + ' > ul > li')[0]).animate( {
			opacity:0
		}, Math.floor(base.animTime /1.2), 'linear', function () {}
	);
}
*/
/* Code comment ends here */

/*** Wapper 
/ Structure: <div class='wapper'>(=container)<ul><li></li>etc.</ul><div class="wappercontentbox">content</div>etc.</div>
/ list-items <li> correspond one on one with wappercontentboxes <div class='wappercontentbox'>
***/
function Wapper (id, index) {
	var ID = id;
	this.myElements = $('#' + ID + ' > ul > li');
	this.myContent = $('#' + ID + ' > .wappercontentbox');
	
	var i = this.myElements.length;
	
	this.currentActive = $(this.myElements[index]);
	this.currentActiveContent = $(this.myContent[index]);
	
	$(this.currentActive).css('background-color', '#F0F0F0');
	$(this.currentActiveContent).fadeIn(100);
	
	while(i--) {
		$(this.myElements[i]).bind('mouseenter', [this, i], this.activate);
		$(this.myElements[i]).bind('mouseleave', [this, i], this.deActivate);
	}
	
	$('.wapper > ul').mouseleave(jQuery.proxy(this.stopAll, this));
}

Wapper.prototype.activate = function (e) {
	var base = e.data[0];
	var index = e.data[1];
	
	$(base.currentActive).stop().animate( { backgroundColor : '#FFFFFF' }, 80);
	$(base.currentActiveContent).fadeOut(100);
	
	$(base.myElements[index]).stop().css( { backgroundColor : '#F0F0F0' });
	$(base.myContent[index]).fadeIn(200);

	base.currentActive = $(base.myElements[index]);
	base.currentActiveContent = $(base.myContent[index]);
}

Wapper.prototype.deActivate = function (e) {
	$(e.currentTarget).stop().animate( { backgroundColor : '#FFFFFF' }, 80);
	$(base.currentActiveContent).fadeOut(100);
}

Wapper.prototype.stopAll = function () {
	this.currentActive.stop().animate( { backgroundColor : '#F0F0F0' }, 10);
}

/*** scrollHashLinks
/ Scroll the page to the location of a hashlink in stead of directly jumping to it (normal browser behaviour)
***/
function scrollHashLinks () {
	var scrollLinks = $("a[href^='#']");
	var base = this;
	for (var i=0; i < scrollLinks.length; i++) {
		if ($(scrollLinks[i]).attr('href').length > 1) {
			var idLink = $(scrollLinks[i]).attr('href').substr(1);
			if($('#' + idLink).length > 0) {
				var targetOffset = $('#' + idLink).offset().top;
				scrollLinks[i].myOffset = targetOffset;
				$(scrollLinks[i]).attr('href', 'javascript:;');
				$(scrollLinks[i]).click(function(){base.scrollPage(this.myOffset)});
			}
		}
	}
}

scrollHashLinks.prototype.scrollPage = function (offset) {
	$('html,body').animate({scrollTop: offset}, 1000);
}		

$(document).ready(function() {
    handleDisclaimer();//Check whether disclaimer is added on the page or not, if present then show disclaimer
	var openSelector = new OpenCountrySelector();
	var pageHeight = new SetPageHeight();
$('table').decorate(); // Add first, last, even and odd classes
	var animatedHashLinks = new scrollHashLinks();
	//if($('#ABN-ticker').length) var tick = new Ticker(1000, 3000, 'ABN-ticker'); //commented for removing animation functionality used on latest news section
	if($('#wapper01').length) var w = new Wapper('wapper01', 2);
   	if($('#banner_0').length)
		getTimeZone();
	else
		$('.box4of5 .box-inner').css('display','block');
});

function searchForm()
{
document.searchform.submit();
}
function changeURL(obj)
	{
		if(obj && obj.parentNode)
		{
			if(obj.parentNode.getElementsByTagName('select')[0] && obj.parentNode.getElementsByTagName('div')[0])
			{
				var selectElem = obj.parentNode.getElementsByTagName('select')[0].selectedIndex;
				var divElem = obj.parentNode.getElementsByTagName('div')[0];
				if(divElem.getElementsByTagName("a").length > selectElem){
					var nodeLink = divElem.getElementsByTagName("a")[selectElem];	
					obj.href = nodeLink.href;	
				}
			}
		}
	}
	
	

/*** 
/ Check  Disclaimer on the page and if present then check the cookie of the browser
***/
function handleDisclaimer()
{	
	if($('#disclaimer').length > 0){
		hideUnhideDisclaimer('inline');
		$("#disclaimerDisagree").attr("href", window.location.href);
		$('#disclaimerAgree').click(function() { setDisclaimerType(); });
		$("#disclaimerAgree").attr("href", window.location.href);
		checkCookieValue();	
	}
	else{
		$('#main-content').css('display','inline');
		$('.content .size1of5').css('display','inline');
		$('.content .left1of5').css('display','inline');
	}
}

/*** 
/ check the value of the cookie, if present hide disclaimer and show the actual content
***/
function checkCookieValue()
	{	
		var disclaimerId = $.trim($('#disclaimer h1.hideDisclaimerType').text());
		if(disclaimerId){
			var Disclaimer = 'Disclaimer_' + disclaimerId;
			var cookieValue = getCookie($.trim(Disclaimer));
		}
		if(cookieValue){
			if (cookieValue == disclaimerId ){
				hideUnhideDisclaimer('none');
			}
		}
	}

/*** 
/ check the displayStyle 
/ displayStyle = string containing the style of the disclaimer
***/
function hideUnhideDisclaimer(displayStyle)
	{	
		if(displayStyle == 'inline'){
			$('#main-content').css('display','none');
			$('#disclaimer').css('display',displayStyle);
			$('.content .size1of5').css('display','none');
			$('.content .left1of5').css('display','none');
		}
		else{
			$('#main-content').css('display','inline');
			$('#disclaimer').css('display',displayStyle);
			$('.content .size1of5').css('display','inline');
			$('.content .left1of5').css('display','inline');
		}
	}	
/*** 
/ hide the disclaimer and set the cookie for that perticular disclaimer
***/
function setDisclaimerType()
	{
		var disclaimerId = $.trim($('#disclaimer h1.hideDisclaimerType').text());
		//hideUnhideDisclaimer('none');
		var Disclaimer = 'Disclaimer_'+ disclaimerId
		setCookie(Disclaimer,disclaimerId);
	}
	
/*** 
/ set the value of the cookie
/cookieName = string containing name of the cookie to be stored
/value = string containing value of the cookie to be stored
***/	
function setCookie(cookieName,value)
	{
		document.cookie=cookieName+ "="+escape(value) + "; path=/";
	}

/*** 
/ get the value of the cookie
/cookieName = string containing name of the cookie to be fetched
/return = string containing value of the cookie or ""
***/	
function getCookie(cookieName)
{
if (document.cookie.length>0)
  {
  cStart=document.cookie.indexOf(cookieName + "=");
  if (cStart!=-1)
    {
    cStart=cStart + cookieName.length+1;
    cEnd=document.cookie.indexOf(";",cStart);
    if (cEnd==-1) cEnd=document.cookie.length;
    return unescape(document.cookie.substring(cStart,cEnd));
    }
  }
return "";
}
//To Show local time on home page banner
var objTimeZone;
var currentMinutes;
var currentHours;
var strLocation;
function getTimeZone(){
	if(typeof strTimeZones != 'undefined'){
		var strSplitTimezone = strTimeZones.split(":");
		var displayBannerIndex = strSplitTimezone[0];
		if(strSplitTimezone.length == 3 )
		{
			currentHours = strSplitTimezone[1];
			currentMinutes = strSplitTimezone[2];
			objTimeZone = "#banner_"+displayBannerIndex+ " "+"p";
			strLocation = $(objTimeZone).html();
			FormatZoneTime();
			setInterval(incrementClock,60000);
		}
		$(".box4of5 .box-inner").css("display","none");
		$('#banner_'+displayBannerIndex).css("display","block");
	}
}
//function to Format and display the time on AM/PM basis
function FormatZoneTime(){
	currentHours = (currentHours < 10 ? "0" : "") + parseFloat(currentHours);
	currentMinutes = (currentMinutes < 10 ? "0" : "") + parseFloat(currentMinutes);	

	//Choose either "AM" or "PM" as appropriate
	var timeOfDay = (currentHours < 12) ? "AM" : "PM";
	var hourOfDay = (currentHours > 12) ? currentHours - 12 : currentHours;

	//Compose the string for display
	var currentTimeString = strLocation + "-" + hourOfDay + ":" + currentMinutes + " " + timeOfDay;
	//Update the time display
	$(objTimeZone).html(currentTimeString);
}

//function to increase the time on the displayed banner
function incrementClock(){
	currentMinutes++;
	if(currentMinutes >= 60){
		currentMinutes = 0;
		currentHours++;
	}
	if(currentHours > 23){
		currentHours = currentHours - 24;
	}
	FormatZoneTime();
}
//Code added for Feedback Form
var submitLink = $('<a></a>');
$('.nextButton').after(submitLink.append($('<span></span>').html($('.nextButton').attr('value'))).attr({'class':'nextButton', 'href' : 'javascript:document.FeedbackForm.submit();'})).hide();

