(function() {

    var TEXT_BLOCK_OPACITY = 0.8;   // 0-1
    var BANNER_TIMEOUT = 5500;      //ms
    var BANNER_IMAGES = {
        home: ['banners/nsMagazine.jpg','banners/faithinne.jpg', 'banners/riskwatch.jpg', 'banners/3737.jpg', 'banners/3784.jpg', 'banners/3788.jpg'],
        how: ['banners/faithinne.jpg', 'banners/3861.jpg', 'banners/realise.jpg', 'banners/loanfund.jpg'],
        who: ['banners/faithinne.jpg', 'banners/3737.jpg', 'banners/3788.jpg', 'banners/C&CF.jpg'],
        what: ['banners/3788.jpg', 'banners/faithinne.jpg', 'banners/ns.jpg','banners/loanfund.jpg'],
        contact: ['banners/C&CF.jpg', 'banners/3737.jpg', 'banners/realise.jpg', 'banners/3788.jpg']
    };
    var BANNER_INTERVAL = null;
    var BANNER_PRELOADER = [];
    var SWITCH_LINKS = {
        who_ns: [
            { caption: 'New Sector magazine and website: editorial and design six times a year', img: 'banners/nsMagazine.jpg' },
			{ caption: 'New Sector magazine and website: editorial and design six times a year', img: 'banners/nsWebsite.jpg' }
        ],
		 who_sewm: [
            { caption: 'Social Enterprise West Midlands: marketing workshops, case studies, press releases, magazine features etc', img: 'banners/sewmProfiles.jpg' },
			{ caption: 'Social Enterprise West Midlands: marketing workshops, case studies, press releases, magazine features etc', img: 'banners/sewmPressRel.jpg' },
			{ caption: 'Social Enterprise West Midlands: marketing workshops, case studies, press releases, magazine features etc', img: 'banners/sewmworkshops.jpg' }
        ],
		  who_coopsuk: [
            { caption: 'Co-operatives UK: reports, brochures, leaflets, display graphics etc', img: 'banners/3710.jpg' },
			{ caption: 'Co-operatives UK: reports, brochures, leaflets, display graphics etc', img: 'banners/coops_ethnic.jpg' },
			{ caption: 'Co-operatives UK: reports, brochures, leaflets, display graphics etc', img: 'banners/CommunityInvestmentCoopsUK.jpg' },
			{ caption: 'Co-operatives UK: reports, brochures, leaflets, display graphics etc', img: 'banners/coopsLegalLeaflet.jpg' }
        ],
		   who_ccf: [
            { caption: 'Co-operative & Community Finance: newsletters, press releases, website updates, display graphics and other marketing materials', img: 'banners/C&CF.jpg' },
			{ caption: 'Co-operative & Community Finance: newsletters, press releases, website updates, display graphics and other marketing materials', img: 'banners/ICOF_banner.jpg' },
			{ caption: 'Co-operative & Community Finance: newsletters, press releases, website updates, display graphics and other marketing materials', img: 'banners/ICOF_prospect.jpg' }
        ],
		    who_sunder: [
            { caption: 'Sunderland City Council: Riskwatch magazine, editorial and design three times a year', img: 'banners/riskwatch.jpg' }
        ],
			 who_clf: [
            { caption: 'Co-operative Loan Fund: newsletters, press releases, website updates, display graphics and other marketing materials', img: 'banners/loanfund.jpg' },
			{ caption: 'Co-operative Loan Fund: newsletters, press releases, website updates, display graphics and other marketing materials', img: 'banners/CLFWeb.jpg' },
			{ caption: 'Co-operative Loan Fund: newsletters, press releases, website updates, display graphics and other marketing materials', img: 'banners/CLFLeaflet.jpg' },
			{ caption: 'Co-operative Loan Fund: newsletters, press releases, website updates, display graphics and other marketing materials', img: 'banners/CLFBanner.jpg' }
        ],
			  who_nhs: [
            { caption: 'We produce a range of prescription pads and documentation on behalf of NHS Tees Esk & Wear Valley', img: 'banners/NHS_chart1.jpg' },
			{ caption: 'We produce a range of prescription pads and documentation on behalf of NHS Tees Esk & Wear Valley', img: 'banners/NHS_chart2.jpg' }
        ], 
			  who_coopfuture: [
            { caption: 'Co-operative Futures: website, training and leaflets', img: 'banners/3737.jpg' },
			{ caption: 'Co-operative Futures: website, training and leaflets', img: 'banners/Co-opFutureWeb.jpg' },
			{ caption: 'Co-operative Futures: website, training and leaflets', img: 'banners/3717.jpg' }
        ],
			   who_bssec: [
            { caption: 'Birmingham & Solihull Social Economy Consortium: website, multimedia case studies, resource pack and other marketing materials', img: 'banners/bssec.jpg' },
			{ caption: 'Birmingham & Solihull Social Economy Consortium: website, multimedia case studies, resource pack and other marketing materials', img: 'banners/bssecMulti.jpg' },
			{ caption: 'Birmingham & Solihull Social Economy Consortium: website, multimedia case studies, resource pack and other marketing materials', img: 'banners/BSSEC_web.jpg' }
        ],
			    who_age: [
            { caption: 'Age Concern Durham County: leaflets and display graphics', img: 'banners/ageconcern.jpg' },
			  { caption: 'Age Concern Durham County: leaflets and display graphics', img: 'banners/3890.jpg' },                { caption: 'Age Concern Durham County: leaflets and display graphics', img: 'banners/3678.jpg' }
        ],
				 who_letter: [
            { caption: 'Letterbox Library: regular children\s books catalogue', img: 'banners/3861.jpg' }
        ],
				  who_si: [
            { caption: 'Shared Interest: magazine, reports, brochures, leaflets, display graphics etc', img: 'banners/qr.jpg' },
			{ caption: 'Shared Interest: magazine, reports, brochures, leaflets, display graphics etc', img: 'banners/SI_Social_Accounts.jpg' },
			{ caption: 'Shared Interest: magazine, reports, brochures, leaflets, display graphics etc', img: 'banners/sharedInterestBanners.jpg' }
        ],
				   who_cdlink: [
            { caption: 'We have produced display graphics on behalf of County Durham Link, and have recently produced its 2009 communications strategy.', img: 'banners/ns.jpg' }
        ],
				    who_tself: [
            { caption: 'The Social Enterprise Loan Fund: website, folder and display graphics', img: 'banners/tself.jpg' },
			{ caption: 'The Social Enterprise Loan Fund: website, folder and display graphics', img: 'banners/3890tself.jpg' },
			{ caption: 'The Social Enterprise Loan Fund: website, folder and display graphics', img: 'banners/tselfBanner.jpg' }
        ],
					who_mblink: [
			{ caption: 'Middlesbrough LINk: folder, leaflets, stationery etc', img: 'banners/MbroLinkLeaflet1.jpg' },
			{ caption: 'Middlesbrough LINk: folder, leaflets, stationery etc', img: 'banners/MbroLink2.jpg' }
        ],					 
         // <-- separate each switch link with a comma; all the caption/image pairs must be inside a [ ] array
        
        what_logos: [	// <-- you can put switch links on any page; they must be <a> tags with class="switch_link" applied; each one must have a unique fragment in the HREF -- the js will do the rest
            { caption: 'Logo and business cards created for Ethos Public Relations', img: 'banners/3875.jpg' }, 
			{ caption: 'Logo and brand guidelines for Eva Training & Consultancy', img: 'banners/3788.jpg' }, 
			{ caption: 'Logo and stationery for Sustainability North East', img: 'banners/3654.jpg' }, 
			{ caption: 'Branding and marketing materials for Durham County Cricket Club', img: 'banners/3662.jpg' }
        ],
		 what_brochures: [	// <-- you can put switch links on any page; they must be <a> tags with class="switch_link" applied; each one must have a unique fragment in the HREF -- the js will do the rest
            { caption: 'Children\'s books catalogue for Letterbox Library', img: 'banners/3861.jpg' },
			 { caption: 'Leaflet jointly promoting social enterprises in Swindon', img: 'banners/3737.jpg' },
			  { caption: 'Co-operative consortia brochure for Co-operatives UK', img: 'banners/coopsuk.jpg' },
			   { caption: 'Customer folder and inserts for Shared Interest', img: 'banners/3649.jpg' },
			     { caption: 'Farmers\' Market leaflet for Headingley Development Trust', img: 'banners/farmrsMkt.jpg' },
				   { caption: 'Brochure for Churches Regional Commission', img: 'banners/faithinne.jpg' }
        ],
		   what_newsletters: [	// <-- you can put switch links on any page; they must be <a> tags with class="switch_link" applied; each one must have a unique fragment in the HREF -- the js will do the rest
                { caption: 'New Sector magazine, editorial and design six times a year', img: 'banners/ns.jpg' },
			 { caption: 'Shared Interest\' members magazine, editorial and design four times a year', img: 'banners/qr.jpg' },
			  { caption: 'Co-operative & Community Finance newsletter, editorial and design four times a year', img: 'banners/C&CF.jpg' },
			   { caption: 'Co-operative Loan Fund newsletter, editorial and design four times a year', img: 'banners/loanfund.jpg' },
			   { caption: 'Riskwatch magazine for Sunderland City Council, editorial and design three times a year', img: 'banners/riskwatch.jpg' }
        ],
		    what_reports: [	// <-- you can put switch links on any page; they must be <a> tags with class="switch_link" applied; each one must have a unique fragment in the HREF -- the js will do the rest
                { caption: 'Diversity report for Co-operatives UK, design and print', img: 'banners/3867.jpg' },
			 { caption: 'Case studies for Birmingham & Solihull Social Economy Consortium, research, editorial and design', img: 'banners/bssec.jpg' },
			 { caption: 'Social accounts for Shared Interest, editorial and design', img: 'banners/SI_Social_Accounts.jpg' },
			 { caption: 'Community Investment book for Co-operatives UK, design and print', img: 'banners/CommunityInvestmentCoopsUK.jpg' },
			 { caption: 'Publication series for Northern Centre for Mental Health, design and print', img: 'banners/nCMH.jpg' }

        ],
			 what_websites: [	// <-- you can put switch links on any page; they must be <a> tags with class="switch_link" applied; each one must have a unique fragment in the HREF -- the js will do the rest
                { caption: 'Website for The Social Enterprise Loan Fund', img: 'banners/tself.jpg' },
				{ caption: 'Website for Co-operative Futures', img: 'banners/futuresWeb.jpg' },
			    { caption: 'Multimedia case studies for Birmingham & Solihull Social Economy Consortium', img: 'banners/bssecMulti.jpg' },
				{ caption: 'Website for 19 Upper Lake Bed & Breakfast', img: 'banners/upperlakeWeb.jpg' }
			
        ],
			  what_advertising: [	// <-- you can put switch links on any page; they must be <a> tags with class="switch_link" applied; each one must have a unique fragment in the HREF -- the js will do the rest
                { caption: 'Point-of-sale poster for Tropical Forest', img: 'banners/3224.jpg' },
				 { caption: 'Bus shelter advertisements for Newcastle Fairtrade Partnership', img: 'banners/FTPosterBUS.jpg' },
				 { caption: 'Web advert for Durham Musical Theatre Company', img: 'banners/3862.jpg' },
				  { caption: 'Packaging design for fair trade soap', img: 'banners/fairTradeSoap.jpg' }
        ],
			   what_exhibition: [	// <-- you can put switch links on any page; they must be <a> tags with class="switch_link" applied; each one must have a unique fragment in the HREF -- the js will do the rest
			  { caption: 'Banners for The Workers Educational Association', img: 'banners/wea_banners.jpg' },				
              { caption: 'Banners for Newcastle Fairtrade Partnership', img: 'banners/3784.jpg' },
			  { caption: 'Exhibition graphics for Age Concern', img: 'banners/3890.jpg' },
			  { caption: 'Banners for Shared Interest', img: 'banners/sharedInterestBanners.jpg' },
			  { caption: 'Banners for Co-operative & Community Finance', img: 'banners/ICOF_banner.jpg' }
        ],
			    what_workshops: [	// <-- you can put switch links on any page; they must be <a> tags with class="switch_link" applied; each one must have a unique fragment in the HREF -- the js will do the rest
               { caption: 'Marketing workshop being delivered to social enterprises on behalf of Social Enterprise West Midlands', img: 'banners/sewm.jpg' }
        ]
    }



    $(document).ready(function() {
    
        // use js/jquery to make the background translucent
        $('div#text_back').fadeTo('fast', TEXT_BLOCK_OPACITY);
    
        // animate the home page if js is enabled
        $('body#home div#page').css('display', 'none');
        $('body#home div#banner').css('display', 'none');
        $('body#home div#page').show('fadeIn', function() {
            $('body#home div#banner').show('slideLeft');
        });
        
        
        // disable menu link for current page
        var pageName = window.location.href.split('/').pop();
        $('ul#menu li a').each(function() { 
            var linkPage = $(this).attr('href').split('/').pop();
            if (linkPage == pageName) $(this).attr('href', '#').addClass('current_menu');
        });
        
        
        // enable click functionality on switch links       
        $('a.switch_link').click(function(event) {
            event.preventDefault();
            var sl = $(this).attr('href').split('#')[1];
            if (window.switchLink && sl && (sl === window.switchLink)) return; // prevent double click
            window.switchLink = sl;
            
            if (BANNER_INTERVAL) window.clearInterval(BANNER_INTERVAL); // stop current cycle
            
            if (window.switchLink && window.switchLink in SWITCH_LINKS)
            {                
                $('div#banner').slideToggle('fast', function() { 
                    $('div#caption, div#caption_back').hide();
                
                    // preload banner images
                    for (var i = 0; i < SWITCH_LINKS[window.switchLink].length; i++)
                    {
                        var img = new Image();
                        img.src = SWITCH_LINKS[window.switchLink][i]['img'];
                        BANNER_PRELOADER.push(img);
                    }
                
                    // load first image
                    var sw = SWITCH_LINKS[window.switchLink].shift();
                    $('div#banner').css('background-image', 'url(' + sw['img'] + ')');
                    $('div#caption, div#caption_back').text(sw['caption']);
                    SWITCH_LINKS[window.switchLink].push(sw);
                
                    // cycle images
                    BANNER_INTERVAL = window.setInterval(function() {
                        var sw = SWITCH_LINKS[window.switchLink].shift();
                        $('div#banner').css('background-image', 'url(' + sw['img'] + ')');
                        $('div#caption, div#caption_back').html(sw['caption']);
                        SWITCH_LINKS[window.switchLink].push(sw);
                    }, BANNER_TIMEOUT);
                    
                    $('div#banner').slideToggle('fast', function() {
                        // show caption
                        $('div#caption_back').fadeIn('slow', function() { 
                            $(this).fadeTo('fast', TEXT_BLOCK_OPACITY, function() {
                                $('div#caption').fadeIn('slow');
                            }) 
                        } );                    
                    });
                });
            }
        });
        
        
        // init and run the banner image cycler
        window.bodyId = $('body').attr('id');
        
        if (BANNER_IMAGES && BANNER_IMAGES[window.bodyId] && BANNER_IMAGES[window.bodyId].length > 0)
        {
            for (var i = 0; i < BANNER_IMAGES[window.bodyId].length; i++)
            {
                var img = new Image();
                img.src = BANNER_IMAGES[window.bodyId][i];
                BANNER_PRELOADER.push(img);
            }

            BANNER_INTERVAL = window.setInterval(function() {
                var img = BANNER_IMAGES[window.bodyId].shift();
                $('div#banner').css('background-image', 'url(' + img + ')');
                BANNER_IMAGES[window.bodyId].push(img);
            }, BANNER_TIMEOUT);
        }
    });
})();