function Flash(obj) { var objNames = 'id,width,height,align,codebase,classid,style'.split(','); var parNames = 'allowScriptAccess,movie,quality,bgcolor,flashvars'.split(','); var embNames = 'width,height,name,type,pluginspage,align,allowScriptAccess,movie,quality,bgcolor,src,flashvars,style'.split(','); var flashvars = []; for (var i in obj.flashvars) flashvars.push(i +'='+ encodeURIComponent(obj.flashvars[i])); obj.flashvars = flashvars.join('&amp;'); var objDef = { classid : 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000', codebase : 'http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + (obj.version || '8,0,0,0') }; var parDef = { allowScriptAccess : 'sameDomain', movie : obj.src }; var embDef = { type : 'application/x-shockwave-flash', pluginspage : 'http://www.macromedia.com/go/getflashplayer', allowScriptAccess : 'sameDomain' }; for (var i=0,a=[],n,v; n = objNames[i]; i++) { v = (obj[n] || objDef[n]); if (v) a.push(n +'="'+ v +'"'); } var out = '\n<object '+ a.join(' ') +'>\n'; for (var i=0,a=[],n,v; n = parNames[i]; i++) { v = (obj[n] || parDef[n]); if (v) a.push(' <param name="'+ n +'" value="'+ v +'" />\n'); } out += a.join(''); for (var i=0,a=[],n,v; n = embNames[i]; i++) { v = (obj[n] || embDef[n]); if (v) a.push(n +'="'+ v +'"'); } out += ' <embed '+ a.join(' ') +' />\n</object>\n'; document.write(out); }

// minHeight plugin
jQuery.getMinNative = function(){
	if (!this.minNative) {
		var $test = jQuery('<div style="position: absolute; top: -10px; width: 1px; min-width: 2px">')
			.appendTo('body');
		this.minNative =
			( $test[0].offsetWidth && $test[0].offsetWidth == 2 )
				? 'minHeight' : 'height';
		$test.remove();
	}
	return this.minNative;
};
jQuery.fn.minHeight = function( h ) {
	return this.each(function(){
		this.style[jQuery.getMinNative()] = h;
	});
};

jQuery.fn.createDropDown = function( map, level, $parent ) {
	var html = '<div class="js-dropdown-holder'+ (level ? ' lvl-'+ level : '') +'" style="display: none;"><div class="js-dropdown"><ul>';
	for (var i=0; i<map.length; i++)
		html += '<li><a href="'+ map[i][1] +'">'+ map[i][0] +'</a></li>';
	html += '</ul></div>';
	var $holder = $(html).appendTo('#wrapper');

	var $nav = $('ul a', $holder[0]);
	for (var i=0; i<$nav.length; i++) {
		var a = Nordea.drop.map[($nav[i] +"").split(Nordea.drop.base)[1]];
		if (a[2])
			$($nav[i].parentNode)
				.addClass('have-subs')
				.createDropDown(a[2], level ? level + 1 : 1, $holder);
	}
	return this;
};

jQuery.fn.showDropDown = function(){
	return this;
};
jQuery.fn.hideDropDown = function(){
	return this;
};


// popup window function
function newWindow( obj, width, height ) {
	window.open(obj.href, "", "width="+ width +",height="+ height +",left="+ (screen.width - width) / 2 +",top="+ (screen.height - height) / 2 +",toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1");
	return false;
}

// pad numbers with zeroes
function padDigits(n, totalDigits) {
	n = n.toString(); 
	var pd = ''; 
	if (totalDigits > n.length) {
		for (i = 0, l = (totalDigits-n.length); i < l; i++) {
			pd += '0';
		}
	}
	return pd + n.toString();
}

$('html').addClass('js');
var Nordea = {
	init: function() {
		// IE background cache fix
		if (!window.opera) try { document.execCommand("BackgroundImageCache", false, true); } catch(e) {};
		this
			.fixInterface()
			.fixAccess()
			.nordeaLinks()
			.siteMapInterface()
			.dropDown()
			.fundDate();
	},
	fixInterface: function() {
		// min height fixes
		var ih = $('#newsBlock').height() - 30;
		$('#investBlock div.deco').minHeight( ih+"px");
		$('#privateClientsBlock').minHeight($('#corporateClientsBlock').height() +"px");
		$('form:not(.discounts) div.field:not(.long):not(.check)').each(function(){
			var lh = $('label', this).height() - 6;
			var $i = $('input, select', this);
			if ((lh -= $i.height()) > 0) {
				$i.css({margin: parseInt(lh / 2)+ "px 0"});
			}
		});
		$('#pageNavigation').each(function(){
			var $r = $('#rightSide');
			$('#content').minHeight(
				($r[0] && $r[0].offsetHeight > this.offsetHeight
					? $r[0].offsetHeight : this.offsetHeight) + 15 + "px"
			);
		});

		// add icones to document links
		var a = ['doc', 'pdf', 'xls', 'ppt'];
		for ( var i=0; i<a.length; i++ ) {
			var $a = $('a[@href$=".'+ a[i] +'"]');
			if ($a.size())
				$a.append('<span class="js-img">&nbsp;<img src="css/images/'+ a[i] +'.gif" /></span>')
					.click(this.openNewWindowHandler)
		}

		var base = null, $b;
		if (($b = $('base')).size())
			base = $b.attr('href');

		$('a[@href*="://"]:not([@href*="'+ (base || 'nordea.lt') +'"])')
			.click(this.openNewWindowHandler);

		// add icones to external links
		if (document.body.id != "index") {
			var $a = $('#content').find('a[@href*="://"]:not([@href*="'+ (base || 'nordea.lt') +'"]):not([img])')
			if ($a.size())
				$a.append('<span class="js-img">&nbsp;<img class="js-img" src="css/images/external.gif" /></span>');
	
			// alternate tables
			var altClass = "alt,odd".split(",").join(", table.");
			$("table."+ altClass)
				.find('tbody')
					.filter(':odd')
						.addClass('odd')
					.end()
					.find('tr:odd')
						.addClass('odd')
					.end()
				.end();
		}

		return this;
	},
	openNewWindowHandler: function() {
		for (var i in this.events.click) {
			if (this.events.click[i] == arguments.callee) {
				window.open(this.href);
				return false;
			}
			break;
		}
	},
	fixAccess: function() {
		if ( $.browser.mozilla ) {
			$().keydown(function(e) {
				var $n = parseInt(String.fromCharCode(e.keyCode));
				if (!isNaN($n)
						&& e.shiftKey && e.altKey
						&& ($n = $('a[@accesskey='+ $n +']')).size()) {
					$n[0].focus();
					document.location.href = $n.href();
					return false;
				}
			});
		}
		return this;
	},
	siteMapInterface: function() {
		var $list = $('dl.sitemap-list');
		if ($list.size()) {
			$list
				.addClass("sitemap-list-js")
				.find('li[li]')
					.prepend('<a href="#"><img src="css/images/expand.gif" /></a>')
					.find('a:first')
					.click(function(){
						var $img = $('img', this);
						if ($img[0].src.indexOf('expand') >= 0) {
							$img
								.attr('src', 'css/images/collapse.gif')
								.parent().parent().addClass('show-sub');
						} else {
							$img
								.attr('src', 'css/images/expand.gif')
								.parent().parent().removeClass();
						}
						return false;
					});
		}
		return this;
	},
	nordeaLinks: function() {
		// Nordea sites block animation
		$('#nordeaLinksBlock', function(){
			var $c = $(this);
			var $ul = $('ul', this).wrap('<div id="linksJSBox"><div></div></div>').parent().parent();
			$ul.css({'display': "none", 'top': $c.height()-$ul.height()-15 + "px"});
			var $h2 = $('h2', this).css({'cursor': "pointer"});

			var timeOut, hover;
			var rollOver = function(){
				clearTimeout(timeOut);
				if (!hover) timeOut = setTimeout(function(){
						$('ul', $ul).css({'visibility': "hidden"});
						$ul.animate({
							opacity: "show", height: "show", width: "show", top: "show", left: "show"
						}, "normal", function(){
							$('ul', $ul).css({'visibility': ""});
							$ul.css('overflow', "visible");
						});

						hover = true;
					}, 100);
			};
			var rollOut = function(){
				clearTimeout(timeOut);
				if (hover) timeOut = setTimeout(function(){
						$('ul', $ul).css({'visibility': "hidden"});
						$ul.animate({
							opacity: "hide", height: "hide", width: "hide", top: "hide", left: "hide"
						}, "normal", function(){
							hover = false;
						});
					}, 300);
			};
			
			$h2.html('<a href="javascript:void(0)">'+ $h2.html() +'</a>');
			var $c = $(this).hover(rollOver, rollOut)
				.find("a").focus(rollOver).blur(rollOut);
		});
		return this;
	},
	dropDown: function() {
		$nav = $('#pageNavigation');
		if ($nav.size() && this.sitemap) {
			this.drop.parse(this.sitemap);
			this.drop.base = (""+ $('#logo a')[0]).replace(/(lt|en)\/?$/, '');
			this.drop.create($nav.find('a:not(.active)'));
		}
		return this;
	},
	drop: {
		map: {},
		parse: function(map) {
			for (var i=0; i<map.length; i++) {
				this.map[map[i][1]] = map[i];
				if (map[i][2])
					this.parse(map[i][2]);
			}
		},
		create: function($nav) {
			for (var i=0; i<$nav.length; i++) {
				var map = this.map[($nav[i] +"").split(this.base)[1]];
				if (map && map[2])
					$($nav[i].parentNode)
						.addClass('have-subs')
						.createDropDown(map[2]);
			}
		}
	},
	fundDate: function() {
		$('#FundDuration').bind('change', function() {
			var $from = $('#FundDateFrom'),
				$to   = $('#FundDateTo'),
				dateFrom = new Date(),
				dateTo   = new Date(),
				y = dateTo.getFullYear(),
				m = dateTo.getMonth();
			switch ($(this).val()) {
				case '1men': dateFrom.setMonth(m-1); break;
				case '3men': dateFrom.setMonth(m-3); break;
				case '6men': dateFrom.setMonth(m-6); break;
				case '1m'  : dateFrom.setYear(y-1);  break;
				case '3m'  : dateFrom.setYear(y-3);  break;
				case '5m'  : dateFrom.setYear(y-5);  break;
				default    : dateFrom.setYear(y-1);
			}
			$from.val(dateFrom.getFullYear() + '-' + padDigits(parseInt((dateFrom.getMonth())+1), 2) + '-' + padDigits(parseInt(dateFrom.getDate()), 2));
			$to.val(dateTo.getFullYear() + '-' + padDigits(parseInt((dateTo.getMonth())+1), 2) + '-' + padDigits(parseInt(dateTo.getDate()), 2));
		});
		$('#FundDateFrom, #FundDateTo').bind('change', function() {
			$('#FundDuration').find('option[@value=other]')[0].selected = true;
		});
		return this;
	}
};

$(function(){
    Nordea.init();
    
    $('#content a.leadPdfDownload').click(function(){
        var organization = 805895;
        var event = 206434;
        var leadNumber = Math.round((new Date().getTime() * Math.random()));
        var uri = "http://tbl.tradedoubler.com/report?organization="+organization+"&event="+event+"&leadNumber="+leadNumber;
        document.getElementById('tb').src = uri;
    });
});
