(function () {
	var resetInput = function () {
		var element = (typeof $(this).hasClassName === 'function') ? $(this) : $(arguments[0]);
		if (!element.hasClassName('prefilled') && element.getValue() === element.defaultValue) {
			element.setValue('');
		}
		return element;
	};
	var clearForms = function () {
		var form_elements = $$('input[type=text]', 'textarea');
		form_elements.invoke('observe', 'focus', resetInput);
		form_elements.invoke('observe', 'blur', function () {
			if ($F(this).blank()) {
				this.setValue(this.defaultValue);
			}
		});
		$$('form').invoke('observe', 'submit', function () {
			this.select('input[type=text]', 'textarea').map(resetInput);
		});
	};

	document.observe('dom:loaded', clearForms);
})();

// requires Prototype.js version 1.6 or greater
Element.addMethods({
	reorder_list: function (element, column_count) {
		// this function fixes the display order of floated lists
		element = $(element);
		element.select('ul').invoke('remove');
		var old_list = $A(element.select('li')),
			new_list = $A(old_list);

		var limit = old_list.size();
		for (var i = 0, j = 0, k = 1; i < limit; i++) {
			if (j >= limit) {
				j = k;
				k++;
			}

			new_list[j] = $(old_list[i]).cloneNode(true);
			j = j + column_count;
		}

		element.update('');

		for (var i = 0; i < limit; i++) {
			element.insert({
				bottom: new_list[i]
			});
		}
		
		return element;
	}
});

document.observe('dom:loaded', function () {

	$$('#tools_content a.bookmark').invoke('observe', 'click', function (event) {
		event.stop();
		Try.these(
			function () { window.external.AddFavorite(window.location.href, document.title); },
			function () { window.sidebar.addPanel(document.title, window.location.href, ''); },
			function () { alert('Please add this page to your favourites.'); }
		);
	});
	
	$$('#tools_content a.print').invoke('observe', 'click', function (event) {
		event.stop();
		window.print();
	});

	if (typeof Vx === 'undefined') {
		var options = {
			duration: 0.6,
			afterFinish: function () {
				animating = false;
			}
		}
		var stretcher = $$('.stretcher'), stretch = $$('.stretch'), animating = false;
		stretch.map(function (el) {
			el.writeAttribute('_height', (el.getHeight()+10) + 'px');
		});
		stretch.invoke('setStyle', { overflow: 'hidden', height: '1px' });
		stretcher.invoke('observe', 'click', function (event) {
			event.stop();
			if (!animating) {
				animating = true;
				var content = this.next('.stretch');
				if (content.getHeight() === 1) {
					content.morph('height:' + content.readAttribute('_height'), options);
				} else {
					content.morph('height:1px', options);
				}
			}
		});
	}
	
	$$('.breadcrumbs').each(function (bc) {
		if (bc.down('ul')) {
			bc.down('ul').insert('<li>' + document.title.split(' | ').slice(1).join(' | ') + '</li>');
		}
	});
	
	if (typeof Vx === 'undefined') {
		$$('.four_column ul').invoke('reorder_list', 4);
	}
	
	
	
											
	$$('.vx_menu a[href]').select(function (a) { return window.location.pathname.startsWith(a.readAttribute('href')); })	
	//select means on click of the link read attrribute of the link.
	
		.invoke('ancestors')
		.invoke('select', function (li) { return li.tagName.toUpperCase() === 'LI' })
		.flatten()
		.invoke('addClassName', 'selected');
		
		
		
		
		
});

function emailink(){
window.location = "mailto:"+"?subject=Finalta - Financial Service Benchmarking" + "&body=This web page might interest you: " + window.location.href;
}
