var basehref; // set with jQuery $('base').attr('href');

// init & start
$(document).ready(function(){
//$(function() {
		
	// set base url var
	basehref = $('base').attr('href');
	
	// dropdown menu
	initmenu();
	
	// hide language selector? 
	// (geen cookie = geen session = niet-werkende languageswitch)
	if( $.cookie(acceptcookie) == null ){
		$('#header ul').hide();
	}
	
	// rssfeedscroll
	scrollChildrenInit('#newsitems','swing',30,6,2500);
	
	// png fix IE 6
	jQuery.each(jQuery.browser, function(i, val) {
		if(i=="msie" && jQuery.browser.version < 7){
			//$('img[src$="png"]').addClass('png');
			$(document).pngFix();
		}
	});
	
	// clickable td's en blocks
	$('#investorservices td,.subitemblock')
	.css('cursor','pointer')
	.click(function(){
		
		var lnk = $('a:first',$(this));
		var url = $(lnk).attr('href');
				
		window.location = url; 
		return false;

	});
	
});


// dropdown menu
function initmenu()
{
	var width = 0;
	
	$('ul:first',$("#page #navigation ul li"))
	.each(function(){
		
		var ul = $(this);
		width = 0;
				
		$('li',$(ul).show()).each(function(){
			var liwidth = $('a',$(this)).text().length;
			//alert( liwidth );
			width = (liwidth * 9) > width ? (liwidth * 9) : width;
		});
		
		$(ul).width(width).css('display','none');
		
		$('li',$(ul)).width(width);
		
		var apadL = $('li:first a:first',$(ul)).css('paddingLeft').replace('px','');
		var apadR = $('li:first a:first',$(ul)).css('paddingRight').replace('px','');
		
		$('li a',$(ul)).css('width', width - ((apadL/1) + (apadR/1)) + 'px');
	});
	
	
	//if (document.all) { // IE... ?
		$("#page #navigation ul li")
		.hover(
			function(){ 
				$('ul:first',$(this).addClass("sfHover")).show();
			}, function() { 
				$('ul:first',$(this).removeClass("sfHover")).hide();
			} 
		);
	//}
	
}

// scroll banners / imgs etc
function scrollChildrenInit(elem,type,speed,margin,wait){
	
	var top = 0;
	
	if( $(elem).length ){
		
		// html rommel
		$(elem)
		.html(
			$(elem)
			.html()
			.replace(/>\s*</g,'><')
		);
		
		var i = $(elem).children().css({'marginBottom':margin + 'px'}).length;	
		
		if(i > 1){
		
			var outer = $(':first',$(elem)).outerHeight(margin);
			var height = "-" + outer + "px"; 
			
			var thisspeed = Math.round( outer * speed);
						
			// animate
			var firstelem = $(':first',
				
				$(elem)
				.hover(
					function(){
						
						$(':first',$(this))
						.stop(true,false);
					},
					function(){
						
						top = $(':first',$(elem)).css('margin-top').substr(1).replace(/px/,'');
						
						top = top ? top : 0;
												
						var ffspeed = Math.round( ((outer /1) - (top /1)) * speed);
						
						//left = null;
						
						$(':first',$(this))
						.stop(true,false)
						.animate({
							"marginTop": height
						}, ffspeed ,type,function(){
							
							$(this)
							.appendTo(elem)
							.css({
								'margin-top':'0px'
							});
							
							scrollChildren(elem,type,thisspeed,wait,margin);
							
						});
					}
				)
			);
			
			scrollChildren(elem,type,thisspeed,wait,margin);
		}
	}
	
}

// scroll banners / imgs etc
function scrollChildren(elem,type,thisspeed,wait,margin){
	
	var outer = null
	var height = null
	
	outer = $(':first',$(elem)).outerHeight(margin);
	height = "-" + outer + "px"; 
		
	$(':first',$(elem))
	.wait(wait)
	.animate({
		"marginTop": height
	}, thisspeed ,type,function(){
						
		$(this)
		.appendTo(elem)
		.css({
			'margin-top':'0px'
		});
		
		scrollChildren(elem,type,thisspeed,wait,margin);
		
	});
}


$.fn.wait = function(time, type) {
	
	var timeout = null;
	
	time = time || 0;
	type = type || "fx";
		
	if(time > 0){
			
		return $(this).queue(type, function() {
			var self = $(this);
			timeout = setTimeout(function() {
				$(self).dequeue();
			}, time);
		});
		
	} else {
		return $(this);
	}
	
};