
/* 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
***/
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 () {}
	);
}

/*** 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' }, 200);
	$(base.currentActiveContent).fadeOut(100);
	
	$(base.myElements[index]).stop().animate( { backgroundColor : '#F0F0F0' }, 200);
	$(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' }, 200);
	$(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() {
	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');
	if($('#wapper01').length) var w = new Wapper('wapper01', 2);
});

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;	
				}
			}
		}
	}
