$(document).ready(function () {



    // Scroll on down, this .js has controls for:
    //	  Flyouts
    //	  Jargon Buster Tooltips
    //    Toggle show/hide content on

    //locate print button
    $('a.print').click(printPage);

    // Build flyouts
    $.fn.flyouts = function (options) {

        options = $.extend({
            effect: 'show', // show|slide|fade
            effectDuration: 200,
            hideDelay: 5,
            markFirstLast: false
        }, options || {});

        this.each(function () {

            var zIndex = 1000;

            function getSubnav(ele) {
                if (ele.nodeName.toLowerCase() == 'li') {
                    var subnav = $('> ul', ele);
                    return subnav.length ? subnav[0] : null;
                } else {
                    return ele;
                }
            }

            function hide() {
                var subnav = getSubnav(this);
                if (!subnav) { return; }

                $(subnav).data('cancelHide', false);
                setTimeout(function () {
                    if ($.data(subnav, 'cancelHide')) { return; }
                    $(subnav).parents('#login').removeClass('open');
                    switch (options.effect) {
                        case 'slide':
                            $(subnav).slideUp(options.effectDuration);
                            break;
                        case 'fade':
                            $(subnav).fadeOut(options.effectDuration);
                            break;
                        default:
                            $(subnav).hide();
                    }
                }, options.hideDelay);
            }

            function show() {
                var subnav = getSubnav(this);
                if (!subnav) { return; }

                $(subnav).parents('#login').addClass('open');

                var px = $(subnav).parent().position().left;
                var py = $(subnav).parent().position().top;
                var ox = $(subnav).attr('foposition').match(/-{0,1}[0-9]{1,}\,-{0,1}[0-9]{1,}/) ? Math.round($(subnav).attr('foposition').split(',')[0]) : 0;
                var oy = $(subnav).attr('foposition').match(/-{0,1}[0-9]{1,}\,-{0,1}[0-9]{1,}/) ? Math.round($(subnav).attr('foposition').split(',')[1]) : 0;

                //$.data(subnav, );

                $(subnav).data('cancelHide', true).css({ zIndex: zIndex++, left: (px + ox) + 'px', top: (py + oy) + 'px' });

                switch (options.effect) {
                    case 'slide':
                        $(subnav).stop(true, true).slideDown(options.effectDuration);
                        break;
                    case 'fade':
                        $(subnav).stop(true, true).fadeIn(options.effectDuration);
                        break;
                    default:
                        $(subnav).show();
                }

            }

            if (options.markFirstLast) {
                $('ul li:first-child', this).addClass('first');
                $('ul li:last-child', this).addClass('last');
            }

            $('ul', this).css({ 'position': 'absolute' });
            $('ul, li', this).hover(show, hide);

        });

    };





    // Initialise flyouts (target them here)
    $('ul#login').flyouts({ effect: 'slide', markFirstLast: true });





    // Jargon Buster Tooltips 
    $('a.jargon').tooltip({
        track: true,
        delay: 0,
        showURL: false,
        showBody: " - ",
        fade: 250
    });
    $("#block").click($.tooltip.block);


    // Toggle show/hide content on Project Pages
    $(".expandable").hide();

    $('a.ContentTrigger').click(function () {
        var title = $(this).attr("title");
        $('.expandable').each(function () { if ($(this).attr("title") == title) { if ($(this).css('display') == 'none') { $(this).css('display', 'inline'); } else { $(this).css('display', 'none'); } } });
        var image = $(this).children("img:first").attr('src');
        if (image.indexOf('more.png') != -1) {
            image = image.replace('more', 'less');
        } else {
            image = image.replace('less', 'more');
        }
        $(this).children("img:first").attr('src', image);
        return false;
    });

});

    function printPage() {
	var a, content = '', base;
	base = $('.base').html();
	$('#content').each( function(i){ content += $(this).html() + '\r\n'; });
	content.replace(/(secure|members)\/_uploads/g, '_uploads');

	if (content == '') {
	    //contentWide
	    $('#contentWide').each(function (i) { content += $(this).html() + '\r\n'; });
	    content.replace(/(secure|members)\/_uploads/g, '_uploads');
	}

    if (content == '') {
	    //contentWide
        $('#contentFullWidth').each(function (i) { content += $(this).html() + '\r\n'; });
	    content.replace(/(secure|members)\/_uploads/g, '_uploads');
	}

	if (content != '') {
		a = window.open('','','width=800,height=700,scrollbars=1');
		a.document.open("text/html");
		a.document.write('<html><head><title>Powerlink</title><link rel="stylesheet" type="text/css" href="' + base + 'media/css/powerlink.css"><link rel="stylesheet" type="text/css" href="' + base + 'media/css/print.css"><!--[if lte IE 9]><link rel="stylesheet" href="' + base + 'media/css/powerlink-ie.css"><![endif]--><!--[if lte IE 8]><link rel="stylesheet" href="' + base + 'media/css/powerlink-ie.css"><![endif]--><!--[if lte IE 7]><link rel="stylesheet" href="' + base + 'media/css/powerlink-ie7.css"><![endif]--><base href="' + base + '" /></head><body style="padding:10px;"><img src="' + base + 'media/images/powerlink_logo.png"><br /><br />')
		a.document.write(content);
		a.document.write('</body></html>');
		a.document.close();
		a.print(); 
	}
	return false;
};
