
// 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 () {

    //$$('a[href$=.pdf]').invoke('addClassName', 'pdf');

    $$('#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);
    }

    try {

        var location_sub = /([^\/]+?)\/([^\/]+?)\.asp/;
        var folder_name, file_name, window_location;
        window.location.href.gsub(location_sub, function (match) {
            window_location = match[0];
            folder_name = match[1];
            file_name = match[2];
        });


        $$('.menu a').each(function (element) {
            $(element).readAttribute('href').gsub(location_sub, function (match) {
                if (match[0] === window_location) {
                    $(element).up('li').addClassName('selected');
                    $(element).up('li').up('li').addClassName('selected');
                }

                if (match[1] === 'main-navigation' && match[2] === file_name) {
                    $(element).up('li').addClassName('selected');
                    $(element).up('li').up('li').addClassName('selected');
                }

                if (match[2] === folder_name) {
                    $(element).up('li').addClassName('selected');
                    $(element).up('li').up('li').addClassName('selected');
                }

                /*
                if (match[2] === file_name) {
                $(element).up('li').addClassName('selected');
                $(element).up('li').up('li').addClassName('selected');
                }
                */
            });
        });
    } catch (err) { }


    // Set focus for the tiny mce editor
    try {
        var ed = tinyMCE.activeEditor;
        ed.focus();
    }
    catch (e) { }

});

function emailink(){
window.location = "mailto:"+"?subject=Finalta - Financial Service Benchmarking" + "&body=This web page might interest you: " + window.location.href;
}
