function flashWrite(flashfile,x,y,flashvar) {
	document.write("<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' width='"+ x +"' height='"+ y +"'>");
	document.write("<param name='movie' value='"+ flashfile +"' />");
	document.write("<param name='quality' value='high' />");
	document.write("<param name='flashvars' value='"+ flashvar +"' />");
	document.write("<param name='wmode' value='transparent' />");
	document.write("<embed src='"+ flashfile +"' wmode='transparent' ");
	document.write("flashvars='"+ flashvar +"'  ");
	document.write("quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='"+ x +"' height='"+ y +"'></embed>");
	document.write("</object>");
}

function printYear() {
	var today = new Date();
	document.write("Test");
}

function writeEmail(email) {
	document.write('<a href="mailto:'+email+'">'+email+'</a>');
}

function changeTextSize(textSize) {
	document.body.className = 'site ' + textSize;
	return false;
}

function printPage() {
	window.print();
	return false;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function clearInput(inputID){

    var title = $(inputID).attr("title");
    var value = $(inputID).val();
    if(title==value){
        $(inputID).val ("");  
    }
}

$(document).ready(function() {
    jQuery.easing.def = "easeInOutExpo";
    $("#nav").append("<li class='menu-curve nodrop'></li>");

    $("#top-links ul li a span").wrapInner("<span class=\"btn\"></span>");

    if ($('div.lightbox').length > 0) {
        $('div.lightbox a').lightBox({
            imageLoading: '/cms/outertemplates/images/lightbox/lightbox-ico-loading.gif',
            imageBtnClose: '/cms/outertemplates/images/lightbox/lightbox-btn-close.gif',
            imageBtnPrev: '/cms/outertemplates/images/lightbox/lightbox-btn-prev.gif',
            imageBtnNext: '/cms/outertemplates/images/lightbox/lightbox-btn-next.gif'
        });
    }

    $(':first-child').addClass('first');
    $(':last-child').addClass('last');
    $('a[rel=newWindow]').attr('target', '_blank');

    $('div.productGrid div.productBox:nth-child(3n+1)').css({
        clear: 'left'
    });

    $('div.catListing div.productBox:nth-child(2n+1)').css({
        clear: 'left'
    });

    shortenBreadcrumb({
        maxWidth: 725
    });

    $('#right-col .textBox').focus(function() {
        var t = $(this).attr('title');
        var v = $(this).val();
        if (t == v) {
            $(this).val('');
        }
    }).blur(function() {
        var t = $(this).attr('title');
        var v = $(this).val();
        if (v == '') {
            $(this).val(t);
        }
    });

    $("#top-links ul li a:not(.logout)").each(function() {
        $(this).click(function(event) {
            event.preventDefault();
            var dest = $(this).attr("href");
            if ($(this).hasClass("selected")) {
                $(dest).slideUp("slow").removeClass("active");
                $(this).removeClass("selected");
            } else {

                $(".tm-drop.active").each(function() {
                    $(".tm-drop.active").css("z-index", "5").slideUp("slow").removeClass("active");
                });
                $("#top-links ul li a.selected").each(function() {
                    $("#top-links ul li a.selected").removeClass("selected");
                });

                $(dest).css("z-index", "10").slideDown("slow").addClass("active");
                $(this).addClass("selected");
            }

        })
    });

    $("#btm-foldout .header").toggle(function() {
        $(this).children(".first").css("background-image", "url(/cms/outertemplates/images/btn/expand-1.gif)");
        $(this).children(".last").html("Open").css("background-image", "url(/cms/outertemplates/images/btn/expand-2.gif)");
        $(this).next().slideToggle("slow");
    }, function() {
        $(this).children(".first").css("background-image", "url(/cms/outertemplates/images/btn/close-1.gif)");
        $(this).children(".last").html("Close").css("background-image", "url(/cms/outertemplates/images/btn/close-2.gif)");
        $(this).next().slideToggle("slow");
    }).css('cursor', 'pointer');

    var grey = '#C0C0C0';
    var animSpeed = 600;

    $('div.searchPostCode input').focus(function() {        
        $('div.searchOrganisation div.searchBox, div.searchCounty div.searchBox').animate({ backgroundColor: grey }, animSpeed);
        $('div.searchOrganisation h3, div.searchCounty h3').animate({ color: grey }, animSpeed);
    }).blur(function() {
        $('div.searchOrganisation div.searchBox').animate({ backgroundColor: '#388418' }, animSpeed);
        $('div.searchCounty div.searchBox').animate({ backgroundColor: '#BC0233' }, animSpeed);

        $('div.searchOrganisation h3').animate({ color: '#388418' }, animSpeed);
        $('div.searchCounty h3').animate({ color: '#BC0233' }, animSpeed);
    });
});

function shortenBreadcrumb(options){
    /*  
        Shortens the breadcrumb to a specified width by removing the text from one list item
        at a time and replacing it with "..." - accepts an options object as an optional
        parameter with two options:
            shortenBreadcrumb({
                breadcrumb : $('div#breadcrumb > ul'),
                maxWidth : 725
            });
            
        The first is the selector for the breadcrumb ul, the second is the maximum width you
        want it to be.  If not set, the max width is the width of the container minus the width
        of the paragraph next to it ("You are here:").
            -JM
    */

    var breadcrumb = $('div#breadcrumb > ul');
    var maxWidth = parseInt($('div#breadcrumb').innerWidth()) - parseInt($('div#breadcrumb p').outerWidth(true)) + "px";
    
    if (options != undefined){
        if (options.breadcrumb != null) { breadcrumb = options.breadcrumb; }
        if (options.maxWidth != null) { maxWidth = options.maxWidth; }
    }
    
    var levelCount = breadcrumb.find('li').size();
    var shortEnough = false;
    var totalWidth;
    while (shortEnough == false) {
        totalWidth = 0;
        breadcrumb.children('li').each (function(){
            totalWidth += $(this).outerWidth(true);
        });
        if (totalWidth > maxWidth){
            var li = breadcrumb.children('li').not('.short').eq(1);
            li.addClass('short');
            li.children('a').attr('title', li.children('a').html());            
            li.children('a').html('...');
        }
        else {
            shortEnough = true;
        }
    }
}