﻿
/*  Print this page */

var focusIn = function() {
    $("img." + dynamicImageClass).remove();
    $(document).unbind('focusin', focusIn);
    $("head link[href$='print.css']").remove();
}

var dynamicImageClass = "dynamicImageClass";

function addImagesAsBackgrounds() {
    $(document.body).find("div").each(function(i) {
        if (this.style.backgroundImage != "") {
            var imageSrc = this.style.backgroundImage;
            imageSrc = imageSrc.replace('url(', '').replace(')', '');
            var imageAlt = this.title;
            //$(this).css('background', '');
            $(this).append('<img class="' + dynamicImageClass + '" alt="' + imageAlt + '" src="' + imageSrc + '" />');
        }
    });
}

function applyPrinterFriendlyStyles() {

//  insert custom styles here
    $("head").append('<link media="print" href="/Style%20Library/Woodland%20Trust/print.css" type="text/css" rel="Stylesheet" />');

//    if ($('div.pageContentAreaRight').length > 0)
//        $('div.pageContentAreaRight').css('float', 'left');

}

function printThisPage(defaultMode) {

    addImagesAsBackgrounds();

    if (defaultMode) {
        window.print();
    }
    else {
        applyPrinterFriendlyStyles();
        window.print();
    }

    $(document).bind('focusin', focusIn);
}

//  Tab indexing fix (invisible menu item on the top banner)
$(document).ready(function() {
    $('ul.logo').filter('ul.logo-Vertical').find('a.menuLink').attr('tabindex', '3');
});

$('input.autoselect[type=text]').live('focus', function() {
	if ($(this).attr('value') == $(this).attr('title')) {
		$(this).attr('value', '');
	}
});
$('input.autoselect[type=text]').live('focusout', function() {
	if ($.trim($(this).attr('value')) == '') {
		$(this).attr('value', $(this).attr('title'));
	}
});
$('div.hover, div.hoveron, input.hover[type=button], input.hoveron[type=button]').live('mouseenter mouseleave', function() {
	$(this).toggleClass('hover hoveron');
})

$('a.hover img, img.hover, input.hover[type=image]').live('mouseenter', function() {
	_switchHoverImg(this, true);});
$('a.hover img, img.hover, input.hover[type=image]').live('mouseleave', function() {
	_switchHoverImg(this, false);
});

function _switchHoverImg(src, addHover) {
	var fullName = $(src).attr('src');
	var extentionInd = fullName.lastIndexOf(".");
	var fileName = fullName.substring(0, extentionInd);
	if(addHover) {
		fileName = fileName + '-hover';
	}
	else {
		var hoverInd = fileName.lastIndexOf('-hover');
		if (hoverInd > -1) {
			fileName = fileName.substring(0, hoverInd);
		}
	}
	fileName = fileName + fullName.substring(extentionInd);
	$(src).attr('src', fileName)
}
