jQuery(function() {

    $('html').addClass('js-active');

    // Start Visualiser 
    // Set variables and inject launcher button
    if ($('body').hasClass('windows-pvcucasement-overview') || $('body').hasClass('windows-pvcucasement-designs') || $('div').hasClass('windows-pvcucasement-designs')) {
        AHI.visualiser.type = 'pvcu';
        AHI.visualiser.src = 'pvcu-casement';
        AHI.visualiser.style = 'UPVC Casement';
    }

    if ($('body').hasClass('windows-pvcusash-overview') || $('body').hasClass('windows-pvcusash-designs') || $('div').hasClass('windows-pvcusash-designs')) {
        AHI.visualiser.type = 'pvcu';
        AHI.visualiser.src = 'pvcu-sash';
        AHI.visualiser.style = 'UPVC Sash';
    }

    if ($('body').hasClass('windows-pvcutiltturn-overview') || $('body').hasClass('windows-pvcutiltturn-designs') || $('div').hasClass('windows-pvcutiltturn-designs')) {
        AHI.visualiser.type = 'pvcu';
        AHI.visualiser.src = 'pvcu-tiltturn';
        AHI.visualiser.style = 'UPVC Tilt &amp; Turn';
    }

    if ($('body').hasClass('windows-timbercasement-overview')) {
        AHI.visualiser.type = 'timber';
        AHI.visualiser.src = 'timber-casement';
        AHI.visualiser.style = 'Wooden Casement';
    }

    if ($('body').hasClass('windows-timbersash-overview')) {
        AHI.visualiser.type = 'timber';
        AHI.visualiser.src = 'timber-sash';
        AHI.visualiser.style = 'Wooden Sash';
    }

    if ($('body').hasClass('windows-pvcucasement-overview') || $('body').hasClass('windows-pvcutiltturn-overview') || $('body').hasClass('windows-timbercasement-overview') || $('body').hasClass('windows-timbersash-overview')) {
        $('body #prod-left .lead-text').after('<p><a href="/inc/' + AHI.visualiser.type + '-visualiser.html?width=550" class="ahi-overlay" title="Customise Your ' + AHI.visualiser.style + ' Window"><img src="' + AHI.imagesSrc + 'buttons/design-your-window.gif" alt="design your window" width="254" height="41" /></a></p>');
    }

     /*UPVC Sash removed from main list*/
    if ($('body').hasClass('windows-pvcusash-overview')) {
        $('body #prod-left .lead-text').after('<p><a href="/inc/' + AHI.visualiser.type + '-sash-visualiser.html?width=550" class="ahi-overlay" title="Customise Your ' + AHI.visualiser.style + ' Window"><img src="' + AHI.imagesSrc + 'buttons/design-your-window.gif" alt="design your window" width="254" height="41" /></a></p>');
    }


    if ($('body').hasClass('windows-pvcucasement-designs') || $('body').hasClass('windows-pvcutiltturn-designs')) {
        $('body #prod-left p.preButton').after('<p style="margin: 12px 0 12px 0"><a href="/inc/' + AHI.visualiser.type + '-visualiser.html?width=550" class="ahi-overlay" title="Customise Your ' + AHI.visualiser.style + ' Window"><img src="' + AHI.imagesSrc + 'buttons/design-your-window.gif" alt="design your window" width="254" height="41" /></a></p>');
    }
    
    /*UPVC Sash removed from main list*/
    if ($('body').hasClass('windows-pvcusash-designs')) {
        $('body #prod-left p.preButton').after('<p style="margin: 12px 0 12px 0"><a href="/inc/' + AHI.visualiser.type + '-sash-visualiser.html?width=550" class="ahi-overlay" title="Customise Your ' + AHI.visualiser.style + ' Window"><img src="' + AHI.imagesSrc + 'buttons/design-your-window.gif" alt="design your window" width="254" height="41" /></a></p>');
    }

    if ($('body').hasClass('windows-timbercasement-designs') || $('body').hasClass('windows-timbersash-designs')) {
        $('body #prod-left ul').after('<p style="margin-left: 16px"><a href="/inc/' + AHI.visualiser.type + '-visualiser.html?width=550" class="ahi-overlay" title="Customise Your ' + AHI.visualiser.style + ' Window"><img src="' + AHI.imagesSrc + 'buttons/design-your-window.gif" alt="design your window" width="254" height="41" /></a></p>');
    }

    /*UPVC Sash removed from main list*/
    if ($('body').hasClass('windows-pvcucasement-designs') || $('body').hasClass('windows-pvcutiltturn-designs')) {
        $('body #prod-left ').after('<p><a href="/inc/' + AHI.visualiser.type + '-visualiser.html?width=550" class="ahi-overlay" title="Customise Your ' + AHI.visualiser.style + ' Window"><img src="' + AHI.imagesSrc + 'products/windows/product_visualiser_' + AHI.visualiser.src + '.gif" alt="Create your own ' + AHI.visualiser.type + ' window" width="264" height="264" /></a></p>');
    }
    /*UPVC Sash */
    if ($('body').hasClass('windows-pvcusash-designs') ) {
        $('body #prod-left ').after('<p><a href="/inc/' + AHI.visualiser.type + '-sash-visualiser.html?width=550" class="ahi-overlay" title="Customise Your ' + AHI.visualiser.style + ' Window"><img src="' + AHI.imagesSrc + 'products/windows/product_visualiser_' + AHI.visualiser.src + '.gif" alt="Create your own ' + AHI.visualiser.type + ' window" width="264" height="264" /></a></p>');
    }

   
    if ($('div').hasClass('windows-pvcucasement-designs')) {
        $('div.windows-pvcucasement-designs').after('<p>' + AHI.visualiser.src + '<a href="/inc/' + AHI.visualiser.type + '-visualiser.html?width=550" class="ahi-overlay" title="Customise Your ' + AHI.visualiser.style + ' Window"><img src="' + AHI.imagesSrc + 'buttons/design-your-window.gif" alt="design your window" width="254" height="41" /></a></p>');
    }
    if ($('div').hasClass('windows-pvcusash-designs')) {
        $('div.windows-pvcusash-designs').after('<p>' + AHI.visualiser.src + '<a href="/inc/pvcu-sash-visualiser.html?width=550" class="ahi-overlay" title="Customise Your ' + AHI.visualiser.style + ' Window"><img src="' + AHI.imagesSrc + 'buttons/design-your-window.gif" alt="design your window" width="254" height="41" /></a></p>');
    }
   
    if ($('div').hasClass('windows-pvcutiltturn-designs')) {
        $('div.windows-pvcutiltturn-designs').after('<p>' + AHI.visualiser.src + '<a href="/inc/' + AHI.visualiser.type + '-visualiser.html?width=550" class="ahi-overlay" title="Customise Your ' + AHI.visualiser.style + ' Window">Tilt &amp; Turn UPVC Visualiser</a></p>');
        
        if ($('div.windows-pvcutiltturn-designs').click()) {
        AHI.visualiser.type = 'pvcu';
        AHI.visualiser.src = 'pvcu-tiltturn';
        AHI.visualiser.style = 'UPVC Tilt &amp; Turn';
        }
		
		
    }


    /*phone no code*/
    //$('body #phone-number').append('<script type="text/javascript">//<![CDATA[ SetUtn(); //]]></script>').alert('cdata');


    // End Visualiser


    // Home

    /*$("#block-windows").click(function() {

     $(this).addClass("homepage-block-link-active");
    }
    );*/


    $("#block-windows, #block-doors, #block-conservatories").hover(function() {
        $(this).css({ cursor: "pointer" }).find('p').css({ backgroundColor: "#F3F3F3" });
    }, function() {
        $(this).css({ cursor: "pointer" }).find('p').css({ backgroundColor: "#FFFFFF" });
    });


    //calls to action bottom-right homepage


    $("div.quote-content-link").click(function() {
        window.location = $(this).find("a").attr("href"); return false;
    });

    $("div.quote-content-link").hover(function() {
        $(this).find('p.header a').css({ textDecoration: "underline" });
    }, function() {
        $(this).find('p.header a').css({ textDecoration: "none" });
    });

    // End Home




    if ($("#crumb-trail li:contains('Get a Quote')").find("a").length > 0) $("#prim-nav li.prim-quote").find("a").addClass("inSection");
    if ($("#crumb-trail li:contains('Finance')").find("a").length > 0) $("#prim-nav li.prim-finance").find("a").addClass("inSection");
    if ($("#crumb-trail li:contains('Customer Care')").find("a").length > 0) $("#prim-nav li.prim-customer-care").find("a").addClass("inSection");
    if ($("#crumb-trail li:contains('About Anglian')").find("a").length > 0) $("#prim-nav li.prim-about").find("a").addClass("inSection");

    if ($("#crumb-trail li:contains('Get a Quote')").find("a").length > 0) $("#prim-nav li.prim-quote-16").find("a").addClass("inSection");
    if ($("#crumb-trail li:contains('Finance')").find("a").length > 0) $("#prim-nav li.prim-finance-16").find("a").addClass("inSection");
    if ($("#crumb-trail li:contains('Customer Care')").find("a").length > 0) $("#prim-nav li.prim-customer-care-16").find("a").addClass("inSection");
    if ($("#crumb-trail li:contains('About Anglian')").find("a").length > 0) $("#prim-nav li.prim-about-us-16").find("a").addClass("inSection");

    /*if ($("#crumb-trail li:contains('Offers')").find("a").length > 0) $("#prim-nav li.prim-offers").find("a").addClass("inSection");*/
//Load visual



    // Nav Blocks

    $(window).load(function() {
        $('#nav-stand-link').load("/add-nav-content/nav-stand-link.htm");
    });

    $(window).load(function() {
        $('#nav-stand-nolink').load("/add-nav-content/nav-stand-nolink.htm");
    });



    $(window).load(function() {
        $('#nav-win-upvc-case-link').load("/add-nav-content/nav-prod-link.htm");
    });
    $(window).load(function() {
        $('#nav-win-upvc-tilt-link').load("/add-nav-content/nav-prod-link.htm");
    });
    $(window).load(function() {
        $('#nav-win-upvc-sash-link').load("/add-nav-content/nav-prod-link.htm");
    });


    $(window).load(function() {
        $('#nav-dr-upvc-link').load("/add-nav-content/nav-prod-link.htm");
    });
    $(window).load(function() {
        $('#nav-dr-clsc-link').load("/add-nav-content/nav-prod-link.htm");
    });

    $(window).load(function() {
        $('#nav-dr-french-link').load("/add-nav-content/nav-prod-link.htm");
    });
    $(window).load(function() {
        $('#nav-dr-patio-link').load("/add-nav-content/nav-prod-link.htm");
    });

    $(window).load(function() {
        $('#nav-cn-vic-link').load("/add-nav-content/nav-prod-link.htm");
    });
    $(window).load(function() {
        $('#nav-cn-reg-link').load("/add-nav-content/nav-prod-link.htm");
    });
    $(window).load(function() {
        $('#nav-cn-edw-link').load("/add-nav-content/nav-prod-link.htm");
    });
    $(window).load(function() {
        $('#nav-cn-elz-link').load("/add-nav-content/nav-prod-link.htm");
    });
    $(window).load(function() {
        $('#nav-cn-grm-link').load("/add-nav-content/nav-prod-link.htm");
    });
    $(window).load(function() {
        $('#nav-cn-com-link').load("/add-nav-content/nav-prod-link.htm");
    });
    $(window).load(function() {
        $('#nav-cn-reg-link').load("/add-nav-content/nav-prod-link.htm");
    });


    $(window).load(function() {
        $('#nav-rf-fas-link').load("/add-nav-content/nav-prod-link.htm");
    });
    $(window).load(function() {
        $('#nav-rf-brg-link').load("/add-nav-content/nav-prod-link.htm");
    });
    $(window).load(function() {
        $('#nav-rf-gtr-link').load("/add-nav-content/nav-prod-link.htm");
    });
    $(window).load(function() {
        $('#nav-rf-cld-link').load("/add-nav-content/nav-prod-link.htm");
    });


    $(window).load(function() {
        $('#nav-drv-mul-link').load("/add-nav-content/nav-prod-link.htm");
    });
    $(window).load(function() {
        $('#nav-drv-wea-link').load("/add-nav-content/nav-prod-link.htm");
    });
    $(window).load(function() {
        $('#nav-drv-rol-link').load("/add-nav-content/nav-prod-link.htm");
    });
    $(window).load(function() {
        $('#nav-drv-riv-link').load("/add-nav-content/nav-prod-link.htm");
    });
    $(window).load(function() {
        $('#nav-drv-stn-link').load("/add-nav-content/nav-prod-link.htm");
    });


    $(window).load(function() {
        $('#nav-gdr-grp-link').load("/add-nav-content/nav-prod-link.htm");
    });
    $(window).load(function() {
        $('#nav-gdr-grp-link').load("/add-nav-content/nav-prod-link.htm");
    });
    $(window).load(function() {
        $('#nav-gdr-grp-link').load("/add-nav-content/nav-prod-link.htm");
    });

    $(window).load(function() {
        $('#nav-gcn-link').load("/add-nav-content/nav-prod-link.htm");
    });

    // Product Footers


    $(window).load(function() {
        $('#footer-windows-bespoke-sub').load("/add-footer-content/windows-col2-bespoke-sub.htm");
    });
    $(window).load(function() {
        $('#footer-windows-bespoke').load("/add-footer-content/windows-col2-bespoke.htm");
    });

    $(window).load(function() {
        $('#footer-windows-pvcu').load("/add-footer-content/windows-pvcu.htm");
    });
    $(window).load(function() {
        $('#footer-windows-pvcu-casement').load("/add-footer-content/windows-pvcu-casement.htm");
    });
    $(window).load(function() {
        $('#footer-windows-pvcu-tilt').load("/add-footer-content/windows-pvcu-tilt.htm");
    });
    $(window).load(function() {
        $('#footer-windows-pvcu-sash').load("/add-footer-content/windows-pvcu-sash.htm");
    });

    $(window).load(function() {
        $('#footer-windows-timber').load("/add-footer-content/windows-timber.htm");
    });
    $(window).load(function() {
        $('#footer-windows-timber-casement').load("/add-footer-content/windows-timber-casement.htm");
    });
    $(window).load(function() {
        $('#footer-windows-timber-sash').load("/add-footer-content/windows-timber-sash.htm");
    });
    $(window).load(function() {
        $('#footer-windows-wooden').load("/add-footer-content/windows-wooden.htm");
    });
    $(window).load(function() {
        $('#footer-windows-wooden-casement').load("/add-footer-content/windows-wooden-casement.htm");
    });
    $(window).load(function() {
        $('#footer-windows-wooden-sash').load("/add-footer-content/windows-wooden-sash.htm");
    });
    // doors

    $(window).load(function() {
        $('#footer-doors-bespoke-sub').load("/add-footer-content/doors-col2-bespoke-sub.htm");
    });
    $(window).load(function() {
        $('#footer-doors-bespoke').load("/add-footer-content/doors-col2-bespoke.htm");
    });

    $(window).load(function() {
        $('#footer-doors-entrance').load("/add-footer-content/doors-entrance.htm");
    });
    $(window).load(function() {
        $('#footer-doors-entrance-pvcu').load("/add-footer-content/doors-entrance-pvcu.htm");
    });
    $(window).load(function() {
        $('#footer-doors-entrance-classic').load("/add-footer-content/doors-entrance-classic.htm");
    });
    $(window).load(function() {
        $('#footer-doors-entrance-timber').load("/add-footer-content/doors-entrance-timber.htm");
    });
    $(window).load(function() {
        $('#footer-doors-entrance-wooden').load("/add-footer-content/doors-entrance-wooden.htm");
    });

    $(window).load(function() {
        $('#footer-doors-frenchpatio').load("/add-footer-content/doors-frenchpatio.htm");
    });
    $(window).load(function() {
        $('#footer-doors-frenchpatio-french').load("/add-footer-content/doors-frenchpatio-french.htm");
    });
    $(window).load(function() {
        $('#footer-doors-frenchpatio-patio').load("/add-footer-content/doors-frenchpatio-patio.htm");
    });

    // conservatories

    $(window).load(function() {
        $('#footer-conservatories-bespoke').load("/add-footer-content/conservatories-col2.htm");
    });
    $(window).load(function() {
        $('#footer-conservatories').load("/add-footer-content/conservatories-col1.htm");
    });

    // rooftrim

    $(window).load(function() {
        $('#footer-rooftrim-bespoke').load("/add-footer-content/rooftrim-col2.htm");
    });
    $(window).load(function() {
        $('#footer-rooftrim').load("/add-footer-content/rooftrim-col1.htm");
    });

    // rooftrim - Cladding

    $(window).load(function() {
        $('#footer-rooftrim-cladding-cost').load("/add-footer-content/rooftrim-cladding-cost.htm");
    });
    $(window).load(function() {
        $('#footer-rooftrim-cladding-our').load("/add-footer-content/rooftrim-cladding-our.htm");
    });

    // rooftrim - Guttering

    $(window).load(function() {
        $('#footer-rooftrim-guttering-bespoke').load("/add-footer-content/rooftrim-guttering-bespoke.htm");
    });
    $(window).load(function() {
        $('#footer-rooftrim-guttering-our').load("/add-footer-content/rooftrim-guttering-our.htm");
    });

    // driveways

    $(window).load(function() {
        $('#footer-driveways-bespoke').load("/add-footer-content/driveways-col2.htm");
    });
    $(window).load(function() {
        $('#footer-driveways').load("/add-footer-content/driveways-col1.htm");
    });

    // garage doors

    $(window).load(function() {
        $('#footer-garagedoors-bespoke').load("/add-footer-content/garagedoors-col2.htm");
    });
    $(window).load(function() {
        $('#footer-garagedoors').load("/add-footer-content/garagedoors-col1.htm");
    });


    // How we work

    $(window).load(function() {
        $('#footer-how-we-work').load("/add-footer-content/how-we-work.htm");
    });

    // End Product Footers	


    // Start News - bookmark info

    /*div.bookmark-what*/
    /*p.bookmark-description*/

    $('div.bookmark-what').click(function(event) {
        $("div.bookmark-description").css({ display: "block" });
    });

    $('span.bookmark-description-hide').click(function(event) {
        $("div.bookmark-description").css({ display: "none" });
    });


    // End News - bookmark info



    // Start Testimonials
    if ($('ul#customer-testimonials').length > 0) {
        AHI.testimonials.init();
    }
    // End Testimonials

    // Start FAQs
    if ($('dl.ahi-faqs').length > 0) {
        AHI.faqs.init();
    }
    // End FAQs

    // Start News
    if ($('ul.ahi-news-nav').length > 0) {
        AHI.news.init();
    }
    // End News

    // Other intialisers
    AHI.overlay.init();
    AHI.gallery.init();
    AHI.scroller.init();
    AHI.pngFix();
});

AHI = {
	imagesSrc: '/img/',
	
	overlay: {	
		defaultAjaxContentWidth: 600,
		
		galleries: new Array(),
		
		init: function(){
			
			$('a.ahi-overlay').each(function(){
										 
				// Image link
				if($(this).attr('href').search(/.gif|.jpeg|.jpg|.bmp|.png/i) != -1){
					
					// Check if it's in a group
					if($(this).attr('rel')){
						var galleryGroup = $(this).attr('rel');
						var itemIdentifier = $(this).attr('href');
						
						// If this group doesn't exist we create it here
						if(!AHI.overlay.galleries[galleryGroup]){
							AHI.overlay.galleries[galleryGroup] = new Array();
						}
						
						// Check to see if this element has already been added to the group array
						var inArray = false;
						
						for(var i=0; i < AHI.overlay.galleries[galleryGroup].length; i++){
							if(AHI.overlay.galleries[galleryGroup][i].attr('href') == $(this).attr('href')) inArray = true; 	
						}
						
						// If it hasn't we add it
						if(inArray == false){
							AHI.overlay.galleries[galleryGroup].push($(this));
						}
					}
					
					$(this).click(function(){
						AHI.overlay.showOverlay('image', $(this));
						return false; 
					});
					
				// Ajax link
				}else{
					$(this).click(function(){
						AHI.overlay.showOverlay('ajax', $(this));
						return false;
					});

				}
			});
		},
		
		
		showOverlay: function(method, obj){		
			// append the overlay to the end of the document and fade it in
			
			$('body').append('<div id="ahi-overlay"></div>').find('#ahi-overlay').css('opacity', '0').fadeTo('slow', 0.8, function(){
				
				switch (method){
					case 'ajax':
						AHI.overlay.ajaxRequest(obj);
					break;
					
					case 'image':
						AHI.overlay.imageRequest(obj);
					break;

					case 'gallery':
						AHI.overlay.galleryRequest(obj);
					break;

				}

			});
		},
		
		hideOverlay: function(){
			AHI.overlay.hideLoading();			
			AHI.overlay.hideWindow();
			
			$('#ahi-overlay').fadeOut('slow', function(){
				$(this).remove();
			});
		},
		
		showLoading: function(){
			$('body').append('<div id="ahi-overlay-loading"><img src="'+AHI.imagesSrc+'overlay/loadingbar.gif" alt="Loading..." /></div>');
			$('#ahi-overlay-loading').css({'left': '50%', 'top': '50%', 'margin-top': function(){ return '-'+$(this).height()+'px' }, 'margin-left': function(){ return '-'+$(this).width()/2+'px' }});
		},
		
		hideLoading: function(){
			$('#ahi-overlay-loading').remove();
		},
		
		ajaxRequest: function(obj){
			jQuery.ajax({
				type: 'GET',
				url: obj.attr('href'),
				beforeSend: function(){
					// show loading bar
					AHI.overlay.showLoading();
				},
				success: function(responseText){
					// remove loading bar
					AHI.overlay.hideLoading();
					
					// once ajax call has finished, create the structure we need for the overlay window and position it centrally in relation to the body width
					AHI.overlay.showWindow('ajax', {content: responseText, width: AHI.overlay.getContentWidth(obj.attr('href')),  title: obj.attr('title')});
				},
				error: function(){
					// remove loading bar and close overlay
					AHI.overlay.hideOverlay();
				}
			});
		},
		
		imageRequest: function(obj){
			AHI.overlay.showLoading();
			
			var img = new Image();
			img.onload = function(){
				var group = (obj.attr('rel') == 'undefined' | obj.attr('rel') == null) ? false : obj.attr('rel');
				AHI.overlay.hideLoading();
				AHI.overlay.showWindow('image', {image: img, width: img.width, href: obj.attr('href'), title: obj.attr('title'), group: group});
			};
			
			img.src = obj.attr('href');
		},
		
		galleryRequest: function(href){
			AHI.overlay.showLoading();
			
			var hrefRef = href;
			
			if($.browser.msie) {
				hrefRef = href.split('/');
				hrefRef = hrefRef[hrefRef.length-1];

			}
			
			var parentGallery = $('a[href*="'+hrefRef+'"]').parent('li').parent('ul.ahi-gallery');
			var galleryDesc = $('a[href*="'+hrefRef+'"]').parent('li').find('span.ahi-gallery-desc').html();
			var gallerySubtitle = $('a[href*="'+hrefRef+'"]').parent('li').find('span.ahi-gallery-subtitle').html();
			var group = parentGallery.attr('id');
			
			var img = new Image();
			img.onload = function(){
				AHI.overlay.hideLoading();
				AHI.overlay.showWindow('gallery', {image: img, width: img.width, href: hrefRef, desc: galleryDesc, subtitle: gallerySubtitle, group: group});
			};
			
			img.src = href;
		},
				
		showWindow: function(method, obj){
			switch(method){
				case 'ajax':
					$('body').append('<div id="ahi-overlay-window" class="ahi-overlay-ajax"><div id="ahi-overlay-window-header"><a href="#" class="ahi-overlay-close-btn">Close</a></div><div id="ahi-overlay-window-content">'+obj.content+'</div><div id="ahi-overlay-window-footer"><a href="#" class="ahi-overlay-close-btn">Close</a></div></div>');
					
					if(obj.title != 'undefined' && obj.title != null){
						$('#ahi-overlay-window-header').append('<h2>'+obj.title+'</h2>');
					}
				break;
				
				case 'image':				
					$('body').append('<div id="ahi-overlay-window" class="ahi-overlay-img"><div id="ahi-overlay-window-content"></div><div id="ahi-overlay-window-footer"><a href="#" class="ahi-overlay-close-btn">Close</a></div></div>');
					
					$('#ahi-overlay-window-content').append(obj.image);
					
					if(obj.title != 'undefined' && obj.title != null){
						$('#ahi-overlay-window-footer').prepend('<h2>'+obj.title+'</h2>');
					}
					
					// Handle gallery image navigation
					if(obj.group !== false){
						var group = obj.group;
						
						for(var i=0; i<AHI.overlay.galleries[group].length; i++){
							if(obj.href.search(AHI.overlay.galleries[group][i].attr('href')) > -1) imgPos = i;
						}
						
						var galleryNav = '<div id="ahi-overlay-gallery-nav">';
						
						if(imgPos > 0){
							var title = (AHI.overlay.galleries[group][imgPos-1].attr('title') == 'undefined' || AHI.overlay.galleries[group][imgPos-1].attr('title') == null) ? '' : AHI.overlay.galleries[group][imgPos-1].attr('title');
							// make prev button
							galleryNav += '<div id="ahi-overlay-gallery-nav-previous"><a href="'+AHI.overlay.galleries[group][imgPos-1].attr('href')+'" title="'+title+'" rel="'+AHI.overlay.galleries[group][imgPos-1].attr('rel')+'">Previous</a></div>';
						}
						
						if(imgPos < (AHI.overlay.galleries[group].length-1)){
							var title = (AHI.overlay.galleries[group][imgPos+1].attr('title') == 'undefined' || AHI.overlay.galleries[group][imgPos+1].attr('title') == null) ? '' : AHI.overlay.galleries[group][imgPos+1].attr('title');
							// make next button
							galleryNav += '<div id="ahi-overlay-gallery-nav-next"><a href="'+AHI.overlay.galleries[group][imgPos+1].attr('href')+'" title="'+title+'" rel="'+AHI.overlay.galleries[group][imgPos+1].attr('rel')+'">Next</a></div>';
						}
						
						galleryNav += '</div>';
						
						$('#ahi-overlay-window-content').append(galleryNav);
						
						$('#ahi-overlay-gallery-nav-previous a, #ahi-overlay-gallery-nav-next a').click(function(){
							AHI.overlay.hideWindow();
							AHI.overlay.imageRequest($(this));
							return false;
						});
					}
				break;
					
				case 'gallery':
					$('body').append('<div id="ahi-overlay-window" class="ahi-overlay-img"><div id="ahi-overlay-window-header"><a href="#" class="ahi-overlay-close-btn">Close</a></div><div id="ahi-overlay-window-content"></div><div id="ahi-overlay-window-footer"><a href="#" class="ahi-overlay-close-btn">Close</a></div></div>');
					
					$('#ahi-overlay-window-content').append(obj.image);
					
					if(obj.desc != 'undefined' && obj.desc != null){
						$('#ahi-overlay-window-footer').prepend('<div class="ahi-overlay-gallery-desc">'+obj.desc+'</div>');
					}

					if(obj.subtitle != 'undefined' && obj.subtitle != null){
						$('#ahi-overlay-window-footer').prepend('<div class="ahi-overlay-gallery-subtitle">'+obj.subtitle+'</div>');
					}

					
					// Handle gallery navigation
					var galleryItems = $('#'+obj.group+' li');
					
					galleryItems.each(function(i){
						if($(this).find('.ahi-gallery-link').attr('href').indexOf(obj.href) > -1 ) galleryPosition = i;
					});
					
					var galleryNav = '<div id="ahi-overlay-gallery-nav">';
						
					if(galleryPosition > 0){
						// make prev button
						galleryNav += '<div id="ahi-overlay-gallery-nav-previous"><a href="'+$('#'+obj.group+' li:eq('+(galleryPosition-1)+') a.ahi-gallery-link').attr('href')+'">Previous</a></div>';
					}
					
					if(galleryPosition < (galleryItems.length-1)){
						// make next button
						galleryNav += '<div id="ahi-overlay-gallery-nav-next"><a href="'+$('#'+obj.group+' li:eq('+(galleryPosition+1)+') a.ahi-gallery-link').attr('href')+'">Next</a></div>';
					}
					
					galleryNav += '</div>';
					
					$('#ahi-overlay-window-content').append(galleryNav);
					
					$('#ahi-overlay-gallery-nav-previous a, #ahi-overlay-gallery-nav-next a').click(function(){
						AHI.overlay.hideWindow();
						AHI.overlay.galleryRequest($(this).attr('href'));
						return false;
					});
					
				break;
			}
			
			$('#ahi-overlay-window').css({'width': obj.width+'px', 'left': '50%', 'margin-left': function(){ return '-'+$(this).width()/2+'px' }});
			
			// Check to see if image and description is taller than the window height, if it is then we decare a height to the description
			var viewportHeight = $(window).height() - 100;
			var contentHeight = $('#ahi-overlay-window').height() + parseInt($('#ahi-overlay-window').css('top'));

			// Add a vertical scroll bar to content which is taller than the users window
			if(contentHeight > viewportHeight){
				// For gallery items with a description
				$('.ahi-overlay-gallery-desc').css({ 'height': viewportHeight-360, 'overflow': 'auto' });
				$('.ahi-overlay-ajax #ahi-overlay-window-content').css({ 'height': viewportHeight-70, 'overflow': 'auto' });
			}
			$('#flashcontent').css({'display': 'none' });  //Added to stop Flash appearing over layer 
			
			AHI.overlay.addCloseButtonHandlers();
		},
		
		hideWindow: function(){
            $('#flashcontent').css({'display': 'block' });  //Added to stop Flash appearing over layer
			
			/*// fade added to overaly window removal 21/11/2008
			$('#ahi-overlay-window').fadeOut('slow', function(){
				$(this).remove();
			});*/
			$('#ahi-overlay-window').remove();
			
			},
		
		addCloseButtonHandlers: function(){
			$('.ahi-overlay-close-btn').click(function(){
				AHI.overlay.hideOverlay();
				return false;
			});
		},
				
		getContentWidth: function(url){
			var width = AHI.overlay.defaultAjaxContentWidth;
			
			if(url.search(/\?/) != -1){
				var urlArgStr = url.split('?')[1];
				
				if(urlArgStr.indexOf('width') != -1){
					var urlArgs = urlArgStr.split('&');
					for(var i=0; i < urlArgs.length; i++){
						if(urlArgs[i].search('width') != -1) var width = urlArgs[i].split('=')[1];
					}
				}
			}
			return width;
		}
		
	},
	
	gallery: {		
		init: function(){		
			$('ul.ahi-gallery a.ahi-gallery-link').click(function(){
				AHI.overlay.showOverlay('gallery', $(this).attr('href'));
				return false;
			});
		}
	},
	
scroller: {
		itemsVisible: 4,
		allowedNoOfScrolls: 0,
		scrollCount: 0,
		animating: false,
		animationHandler: null,
		


		
		init: function(){
			$('ul.ahi-gallery').wrap('<div class="ahi-gallery-wrapper"></div>');
			
			$('ul.ahi-scroller').wrap('<div class="ahi-scroller-wrapper"></div>');
			
			$('ul.ahi-gallery, ul.ahi-scroller').each(function(){
				AHI.scroller.render($(this));

			
			});
		},
		
		render: function(obj)
		{
			
		
			
			var wrapperWidth = obj.parent().width();
			var viewportWidth = wrapperWidth - 82; // 82 pixels is for the 2 nav elements at 42px wide each
			var itemsWidth = wrapperWidth / AHI.scroller.itemsVisible;
			var listItems = obj.find('li');			
			
			AHI.scroller.allowedNoOfScrolls = listItems.length - AHI.scroller.itemsVisible;
			
			listItems.css({ 'width': itemsWidth-14, 'padding-left': '7px', 'padding-right': '7px'  }); // 14 pixels for the 7px padding either side
			
			var itemWidth = listItems.eq(0).width() + parseInt(listItems.eq(0).css('padding-left')) + parseInt(listItems.eq(0).css('padding-right'));
			
			var scrollerWidth = itemWidth * listItems.length ;
						
			obj.css({ 'width': scrollerWidth });
						
			if(listItems.length > AHI.scroller.itemsVisible) {
			
				obj.parent().css('height', obj.height()).append('<div class="ahi-scroller-nav-prev"><a href="#">Prev</a></div><div class="ahi-scroller-nav-next"><a href="#">Next</a></div>');
				
				$('.ahi-scroller-nav-prev, .ahi-scroller-nav-next').css({ 'height': obj.height() });
				
				// Remove the left arrow on inital display
				$('.ahi-scroller-nav-prev a').css({ 'visibility': 'hidden'});
				
				$('.ahi-scroller-nav-prev a, .ahi-scroller-nav-next a').each(function()
				{
					var height = $(this).height();
					var parentHeight = $(this).parent().height();
					height = (parentHeight / 2) - (height / 2);
					$(this).css({ 'top': height });
					
				});
				
				$('.ahi-scroller-nav-prev a').click(function()
				{
					if(AHI.scroller.animating === false)
					{
						AHI.scroller.scrollRight(obj);
					}
					return false;
				});
				
				$('.ahi-scroller-nav-next a').click(function()
				{
					if(AHI.scroller.animating === false)
					{
						AHI.scroller.scrollLeft(obj);
					}
					return false;
				});
				
			}
			
			
		},
		
		scrollLeft: function(obj)
		{
		
			AHI.scroller.animating = true;
			
			var listItems = obj.find('li');
			var itemWidth = listItems.eq(0).width() + parseInt(listItems.eq(0).css('padding-left')) + parseInt(listItems.eq(0).css('padding-right'));
			var firstItem = listItems.eq(0);
			var overflow = (listItems.length - AHI.scroller.itemsVisible) * itemWidth;
			
			AHI.scroller.scrollCount++;
			
//			if(parseInt(obj.css('left')) == (overflow * -1))
//			{
//				//firstItem.clone().appendTo(obj);
//				firstItem.clone(true).appendTo(obj);
//				var left = parseInt(obj.css('left')) + itemWidth;
//				obj.css({ 'left': left });
//				firstItem.remove();	
//			}
			
			AHI.scroller.scrollTo(obj, parseInt(obj.css('left')) - itemWidth);
			
		},
		
		scrollRight: function(obj)
		{
			AHI.scroller.animating = true;
			
			var listItems = obj.find('li');
			var lastItem = listItems.eq((listItems.length - 1));
			var firstItem = listItems.eq(0);
			var itemWidth = listItems.eq(0).width() + parseInt(listItems.eq(0).css('padding-left')) + parseInt(listItems.eq(0).css('padding-right'));
			
			AHI.scroller.scrollCount--;
//			
//			if(parseInt(obj.css('left')) == 0)
//			{
//			    
//				//lastItem.clone().prependTo(obj);
//				lastItem.clone(true).prependTo(obj);
//				var left = itemWidth * -1;
//				obj.css({ 'left': left });
//				lastItem.remove();	
//			}
			
			AHI.scroller.scrollTo(obj, parseInt(obj.css('left')) + itemWidth);
		},
		
		scrollTo: function(obj, pos)
		{
			var listItems = obj.find('li');
			var targetPos = parseInt(pos);
			var currentPos = parseInt(obj.css('left'));
			var itemWidth = listItems.eq(0).width() + parseInt(listItems.eq(0).css('padding-left')) + parseInt(listItems.eq(0).css('padding-right'));
			var step = Math.round(itemWidth / 10);
			
			
			if(currentPos != targetPos) 
			{
				$('#animator').text('Current Position: '+currentPos+', Target Position: '+targetPos);
				if(currentPos < targetPos)
				{
					if((targetPos - currentPos) < step)
					{
						obj.css({ 'left': targetPos });	
					}
					else
					{
						obj.css({ 'left': currentPos + step });
					}
					if(AHI.scroller.animationHandler == null)
					{
						AHI.scroller.animationHandler = setInterval(function(){ AHI.scroller.scrollTo(obj, pos) }, 50);
					}
				}
				else
				{
					if(((targetPos - currentPos) * -1) < step)
					{
						obj.css({ 'left': targetPos });	
					}else
					{
						obj.css({ 'left': currentPos - step });
					}
					if(AHI.scroller.animationHandler == null)
					{
						AHI.scroller.animationHandler = setInterval(function(){ AHI.scroller.scrollTo(obj, pos) }, 50);
					}
				}
			}
			else
			{
			    if(AHI.scroller.scrollCount>0 && AHI.scroller.scrollCount>=AHI.scroller.allowedNoOfScrolls)
			        $('.ahi-scroller-nav-next a').css({'visibility':'hidden'});
			    else
			        $('.ahi-scroller-nav-next a').css({'visibility':'visible'});
			        
			    //if(AHI.scroller.scrollCount<0 && (AHI.scroller.scrollCount*-1)>=AHI.scroller.allowedNoOfScrolls)
			    if(AHI.scroller.scrollCount<=0)
			        $('.ahi-scroller-nav-prev a').css({'visibility':'hidden'});
			    else
			        $('.ahi-scroller-nav-prev a').css({'visibility':'visible'});
			        
				$('#animator').text('Done');
				clearInterval(AHI.scroller.animationHandler);
				AHI.scroller.animationHandler = null;
				AHI.scroller.animating = false;
			}
		}
	},
	
	visualiser: {
		type: null,
		src: null,
		style: null,
		
		init: function(){
			// Hide glazing options
			/*if (AHI.visualiser.src==null){				
				if (window.location.href.indexOf('pvcu')!=-1){
					AHI.visualiser.src = 'pvcu-casement';
					AHI.visualiser.type = 'pvcu';					
					AHI.visualiser.style = 'UPVC Casement';
				}else if (window.location.href.indexOf('pvcs')!=-1){
					AHI.visualiser.type = 'pvcu';
       				AHI.visualiser.src = 'pvcu-sash';
        			AHI.visualiser.style = 'UPVC Sash';
				}else if (window.location.href.indexOf('pvct')!=-1){
					 AHI.visualiser.type = 'pvcu';
						AHI.visualiser.src = 'pvcu-tiltturn';
						AHI.visualiser.style = 'UPVC Tilt &amp; Turn';
				}else if (window.location.href.indexOf('wc-')!=-1){
					AHI.visualiser.type = 'timber';
        			AHI.visualiser.src = 'timber-casement';
        			AHI.visualiser.style = 'Wooden Casement';
    
       
				}else if (window.location.href.indexOf('ws-')!=-1){
					AHI.visualiser.type = 'timber';
        			AHI.visualiser.src = 'timber-sash';
        			AHI.visualiser.style = 'Wooden Sash';
				}
				
			}*/
			//alert( AHI.imagesSrc  + "--" +AHI.visualiser.src )
			
			$('#product-visualiser-glazing-all').hide();

			$('#product-visualiser-controls').before('<img alt="" src="' + AHI.imagesSrc + 'visualiser/windows/' + AHI.visualiser.src + '/white_standard_exterior.jpg" width="285" height="285" id="product-visualiser-preview" />');
						
			AHI.visualiser.checkVisualiserHeight($('#product-visualiser'));
			
			$('#product-visualiser #product-visualiser-colleft ul li').each(function(){
				AHI.visualiser.enableOption($(this));
			});
			
			// Show glazing
			$('#product-visualiser .view-glazing').click(function(){
				$('#product-visualiser').hide();
				$('#product-visualiser-glazing-all').show();
				AHI.visualiser.checkVisualiserHeight();
				return false;
			});

//MVT switch
			// Show glazing
//			$('#product-visualiser .view-other').click(function() {
//			    $('#product-visualiser').hide();
//			    $('#product-visualiser').show();
//			    AHI.visualiser.checkVisualiserHeight();
//			    return false;
//			});
//End MVT switch	

			// Show visualiser
			$('#product-visualiser-glazing-all .view-visualiser').click(function(){
				$('#product-visualiser-glazing-all').hide();
				$('#product-visualiser').show();
				AHI.visualiser.checkVisualiserHeight();
				return false;
			});			
			
			// Add view interior / exterior handlers
			$('#product-visualiser-outside-control, #product-visualiser-inside-control').click(function(){
				AHI.visualiser.updatePreview(AHI.visualiser.type, AHI.visualiser.src);
			});
			
			//if(AHI.visualiser.type == 'pvcu'){
				$('#product-visualiser-outside li a').click(function(){
					$('#product-visualiser ul li a img[name="sameasoutside"]').attr('src', $('#product-visualiser-outside li.selected img').attr('src'));
					
					if($('#product-visualiser-inside li img[name="white"]').parent('a').parent('li').hasClass('disabled')){
						var target = $('#product-visualiser-inside li img[name="white"]').parent('a').parent('li');
						AHI.visualiser.enableOption(target);
					}
				});
				
				$('#product-visualiser ul li a img[name="sameasoutside"]').attr('src', $('#product-visualiser-outside li.selected img').attr('src'));
				
				$('#product-visualiser-outside li img[name="whitewoodgrain"]').parent('a').click(function(){
					$('#product-visualiser-inside li').removeClass('selected');
					 $('#product-visualiser-inside li img[name="sameasoutside"]').parent('a').parent('li').addClass('selected');
																										  
					var target = $('#product-visualiser-inside li img[name="white"]').parent('a').parent('li');
					AHI.visualiser.disableOption(target);
				});
			//}
		},
		
		setHandler: function(){
			$(this).parent('li').siblings('li').removeClass('selected');
			$(this).parent('li').addClass('selected');
			AHI.visualiser.updatePreview();
			return false;
		},
				
		disableOption: function(obj){
			obj.addClass('disabled');
			obj.find('a').unbind('click', AHI.visualiser.setHandler);
		},
		
		enableOption: function(obj){
			obj.removeClass('disabled');
			obj.find('a').bind('click', AHI.visualiser.setHandler);
		},
		
		checkVisualiserHeight: function(){
			$('#ahi-overlay-window-content').css({ 'height': 'auto' });
			var viewportHeight = $(window).height() - 100;
			var contentHeight = $('#ahi-overlay-window').height() + parseInt($('#ahi-overlay-window').css('top'));
			//alert('Viewport: '+viewportHeight+', Content: '+contentHeight);
			if(contentHeight > viewportHeight){
				$('#ahi-overlay-window-content').css({ 'height': viewportHeight-70, 'overflow': 'auto', 'padding-right': 0 });
			}else{
				$('#ahi-overlay-window-content').css({ 'height': 'auto', 'padding-right': '10px' });
			}
		},
		
		updatePreview: function(){
			/*if (window.location.href.indexOf('pvcu')!=-1){
					AHI.visualiser.src = 'pvcu-casement';
					AHI.visualiser.type = 'pvcu';					
					AHI.visualiser.style = 'UPVC Casement';
				}else if (window.location.href.indexOf('pvcs')!=-1){
					AHI.visualiser.type = 'pvcu';
       				AHI.visualiser.src = 'pvcu-sash';
        			AHI.visualiser.style = 'UPVC Sash';
				}else if (window.location.href.indexOf('pvct')!=-1){
					 AHI.visualiser.type = 'pvcu';
						AHI.visualiser.src = 'pvcu-tiltturn';
						AHI.visualiser.style = 'UPVC Tilt &amp; Turn';
				}else if (window.location.href.indexOf('wc-')!=-1){
					AHI.visualiser.type = 'timber';
        			AHI.visualiser.src = 'timber-casement';
        			AHI.visualiser.style = 'Wooden Casement';
    
       
				}else if (window.location.href.indexOf('ws-')!=-1){
					AHI.visualiser.type = 'timber';
        			AHI.visualiser.src = 'timber-sash';
        			AHI.visualiser.style = 'Wooden Sash';
				}*/
				
			var view = $('input[name="controls"]:checked').val();
			var exteriorSelected = $('#product-visualiser-outside li.selected a img').attr('name');
			var interiorSelected = ($('#product-visualiser-inside li.selected a img').attr('name') != 'sameasoutside' ) ? $('#product-visualiser-inside li.selected a img').attr('name') : exteriorSelected;
			var glazingSelected = $('#product-visualiser-glazing li.selected a img').attr('name');
			//var strurl= window.location.href.substring(window.location.href.indexOf('?'));
			//alert(window.location.href,strurl)
			switch(AHI.visualiser.type){
				case 'pvcu':					
					
					
					switch(view){
						case 'interior':
							var src = AHI.imagesSrc + 'visualiser/windows/' + AHI.visualiser.src + '/' + interiorSelected+'_'+glazingSelected+'_'+view+'.jpg';
						break;
						case 'exterior':
							var src = AHI.imagesSrc + 'visualiser/windows/' + AHI.visualiser.src + '/' + exteriorSelected+'_'+glazingSelected+'_'+view+'.jpg';
						break;
					}
				break;
				case 'timber':
					/*var view = 'exterior';
					var exteriorSelected = $('#product-visualiser-outside li.selected a img').attr('name');
					var glazingSelected = $('#product-visualiser-glazing li.selected a img').attr('name');
					var src = AHI.imagesSrc + 'visualiser/windows/' + AHI.visualiser.src + '/' + exteriorSelected+'_'+glazingSelected+'_'+view+'.jpg';*/
					//var view = $('input[name="controls"]:checked').val();
					//var exteriorSelected = $('#product-visualiser-outside li.selected a img').attr('name');
					interiorSelected = exteriorSelected;
					//var glazingSelected = $('#product-visualiser-glazing li.selected a img').attr('name');
					switch(view){
						case 'interior':
							var src = AHI.imagesSrc + 'visualiser/windows/' + AHI.visualiser.src + '/' + interiorSelected+'_'+glazingSelected+'_'+view+'.jpg';
						break;
						case 'exterior':
							var src = AHI.imagesSrc + 'visualiser/windows/' + AHI.visualiser.src + '/' + exteriorSelected+'_'+glazingSelected+'_'+view+'.jpg';
						break;
					}
				break;
			}
			$('#product-visualiser img#product-visualiser-preview').attr('src', src);
		}
	},
	
	testimonials: {
		init: function(){
			$('#customer-testimonials-image').after('<div id="customer-testimonials-overlay"><div></div></div>');
			$('#customer-testimonials-overlay').hide();
			
			$('#customer-testimonials li').each(function(i){
				$(this).attr('id', 'customer-testimonial-'+(i+1));
			});
			$('#customer-testimonials-map area').mouseover(function(){
				var id = $(this).attr('href');
				id = id.replace('#', '');
				$('#customer-testimonials-overlay').show().attr('class', id).find('div').html($($(this).attr('href')).html());
			});
			$('#customer-testimonials-map area').mouseout(function(){
				$('#customer-testimonials-overlay').hide();
			});
			
			$('#customer-testimonials-overlay').mouseover(function(){
				$(this).show();
			});
			
			$('#customer-testimonials-overlay').mouseout(function(){
				$(this).hide();
			});
			
			$('#customer-testimonials-map area').click(function(){
				window.open('testimonials/','_parent')
				/*return false;*/
			});			
		}
	},
	
	faqs: {
		init: function(){
			$('dl.ahi-faqs').each(function(){
				// Assign this to self to handle loss of scope
				var self = $(this);
				// Hide all dd's
				AHI.faqs.hideAll($(this));
				//self.find('dd').hide();
				
				$(this).find('dt').click(function(){
					AHI.faqs.hideAll(self);
					AHI.faqs.showFAQ($(this));
					return false;
				});
			});
		},
		hideAll: function(target){
			target.find('dt').removeClass('selected');
			target.find('dd').hide();
		},
		showFAQ: function(target){
			target.addClass('selected');
			target.next().show();
		}
	},
	
	news: {
		init: function(){
			// Hide all article bodys, and hide all sections
			$('.ahi-news-article-body, .ahi-news-section').hide();
			
			// Find out which section is set to 'selected' by default and show it
			var target = $('ul.ahi-news-nav li[class*="selected"] a').attr('href');
			$(target).show();
			
			// Add handlers to hide and show article bodies
			$('.ahi-news-article-title').click(function(){
				AHI.news.hideArticles();
				AHI.news.showArticle($(this));
			});
			
			// Add handlers to hide and show sections
			$('ul.ahi-news-nav li a').click(function(){
				AHI.news.hideSections();
				AHI.news.showSection($(this));
				return false;
			});
		},
		hideArticles: function(){
			$('.ahi-news-article-title[class*="selected"]').removeClass('selected');
			$('.ahi-news-article-body:visible').hide();
		},
		showArticle: function(target){
			target.addClass('selected');
			target.next().show();
		},
		hideSections: function(){
			$('.ahi-news-nav li.selected').removeClass('selected');
			// Hide all articles so they don't open later with selected states etc
			AHI.news.hideArticles()
			// Hide all visible sections
			$('.ahi-news-section:visible').hide();
		},
		showSection: function(target){
			target.parent('li').addClass('selected');
			$(target.attr('href')).show();	
		}
	},
	
	pngFix: function(settings) {
		
		var blankGifSrc = AHI.imagesSrc+'blank.gif';
			
		var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
		var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
		
		if (jQuery.browser.msie && (ie55 || ie6)) {
		
			//fix images with png-source

			$("img[@src$=.png]").each(function() {	   
				$(this).attr('width', $(this).width());
				$(this).attr('height', $(this).height());
				$(this).css({ filter:'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+$(this).attr('src')+'", sizingMethod="crop")' });
				$(this).attr('src', blankGifSrc);
			});
		}
	}
} 









/**************
Thickbox content
***************/


/*
 * Thickbox 3.1 - One Box To Rule Them All.
 * By Cody Lindley (http://www.codylindley.com)
 * Copyright (c) 2007 cody lindley
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
*/
		  
var tb_pathToImage = "../img/loadingAnimation.gif";

/*!!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!!*/

//on page load call tb_init
$(document).ready(function(){   
	tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;
});

//add thickbox to href & area elements that have a class of .thickbox
function tb_init(domChunk){
	$(domChunk).click(function(){
	var t = this.title || this.name || null;
	var a = this.href || this.alt;
	var g = this.rel || false;
	tb_show(t,a,g);
	this.blur();
	return false;
	});
}

function tb_show(caption, url, imageGroup) {//function called when the user clicks on a thickbox link

	try {
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(tb_remove);
			}
		}else{//all others
			if(document.getElementById("TB_overlay") === null){
				$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(tb_remove);
			}
		}
		
		if(tb_detectMacXFF()){
			$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
		}else{
			$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
		}
		
		if(caption===null){caption="";}
		$("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
		$('#TB_load').show();//show loader
		
		var baseURL;
	   if(url.indexOf("?")!==-1){ //ff there is a query string involved
			baseURL = url.substr(0, url.indexOf("?"));
	   }else{ 
	   		baseURL = url;
	   }
	   
	   var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
	   var urlType = baseURL.toLowerCase().match(urlString);

		if(urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp'){//code to show images
				
			TB_PrevCaption = "";
			TB_PrevURL = "";
			TB_PrevHTML = "";
			TB_NextCaption = "";
			TB_NextURL = "";
			TB_NextHTML = "";
			TB_imageCount = "";
			TB_FoundURL = false;
			if(imageGroup){
				TB_TempArray = $("a[@rel="+imageGroup+"]").get();
				for (TB_Counter = 0; ((TB_Counter < TB_TempArray.length) && (TB_NextHTML === "")); TB_Counter++) {
					var urlTypeTemp = TB_TempArray[TB_Counter].href.toLowerCase().match(urlString);
						if (!(TB_TempArray[TB_Counter].href == url)) {						
							if (TB_FoundURL) {
								TB_NextCaption = TB_TempArray[TB_Counter].title;
								TB_NextURL = TB_TempArray[TB_Counter].href;
								TB_NextHTML = "<span id='TB_next'>&nbsp;&nbsp;<a href='#'>Next &gt;</a></span>";
							} else {
								TB_PrevCaption = TB_TempArray[TB_Counter].title;
								TB_PrevURL = TB_TempArray[TB_Counter].href;
								TB_PrevHTML = "<span id='TB_prev'>&nbsp;&nbsp;<a href='#'>&lt; Prev</a></span>";
							}
						} else {
							TB_FoundURL = true;
							TB_imageCount = "Image " + (TB_Counter + 1) +" of "+ (TB_TempArray.length);											
						}
				}
			}

			imgPreloader = new Image();
			imgPreloader.onload = function(){		
			imgPreloader.onload = null;
				
			// Resizing large images - orginal by Christian Montoya edited by me.
			var pagesize = tb_getPageSize();
			var x = pagesize[0] - 150;
			var y = pagesize[1] - 150;
			var imageWidth = imgPreloader.width;
			var imageHeight = imgPreloader.height;
			if (imageWidth > x) {
				imageHeight = imageHeight * (x / imageWidth); 
				imageWidth = x; 
				if (imageHeight > y) { 
					imageWidth = imageWidth * (y / imageHeight); 
					imageHeight = y; 
				}
			} else if (imageHeight > y) { 
				imageWidth = imageWidth * (y / imageHeight); 
				imageHeight = y; 
				if (imageWidth > x) { 
					imageHeight = imageHeight * (x / imageWidth); 
					imageWidth = x;
				}
			}
			// End Resizing
			
			TB_WIDTH = imageWidth + 30;
			TB_HEIGHT = imageHeight + 60;
			$("#TB_window").append("<div id='TB_closeWindowTop'><a href='#' id='TB_closeWindowButtonTop' title='Close'>close</a> or Esc Key</div><a href='' id='TB_ImageOff' title='Close'><img id='TB_Image' src='"+url+"' width='"+imageWidth+"' height='"+imageHeight+"' alt='"+caption+"'/></a>" + "<div id='TB_caption'>"+caption+"<div id='TB_secondLine'>" + TB_imageCount + TB_PrevHTML + TB_NextHTML + "</div></div><div id='TB_closeWindow'><a href='#' id='TB_closeWindowButton' title='Close'>close</a> or Esc Key</div>"); 		
			
			$("#TB_closeWindowButton, #TB_closeWindowButtonTop").click(tb_remove);
			
			if (!(TB_PrevHTML === "")) {
				function goPrev(){
					if($(document).unbind("click",goPrev)){$(document).unbind("click",goPrev);}
					$("#TB_window").remove();
					$("body").append("<div id='TB_window'></div>");
					tb_show(TB_PrevCaption, TB_PrevURL, imageGroup);
					return false;	
				}
				$("#TB_prev").click(goPrev);
			}
			
			if (!(TB_NextHTML === "")) {		
				function goNext(){
					$("#TB_window").remove();
					$("body").append("<div id='TB_window'></div>");
					tb_show(TB_NextCaption, TB_NextURL, imageGroup);				
					return false;	
				}
				$("#TB_next").click(goNext);
				
			}

			document.onkeydown = function(e){ 	
				if (e == null) { // ie
					keycode = event.keyCode;
				} else { // mozilla
					keycode = e.which;
				}
				if(keycode == 27){ // close
					tb_remove();
				} else if(keycode == 190){ // display previous image
					if(!(TB_NextHTML == "")){
						document.onkeydown = "";
						goNext();
					}
				} else if(keycode == 188){ // display next image
					if(!(TB_PrevHTML == "")){
						document.onkeydown = "";
						goPrev();
					}
				}	
			};
			
			tb_position();
			$("#TB_load").remove();
			$("#TB_ImageOff").click(tb_remove);
			$("#TB_window").css({display:"block"}); //for safari using css instead of show
			};
			
			imgPreloader.src = url;
		}else{//code to show html
			
			var queryString = url.replace(/^[^\?]+\??/,'');
			var params = tb_parseQuery( queryString );

			TB_WIDTH = (params['width']*1) + 30 || 630; //defaults to 630 if no paramaters were added to URL
			TB_HEIGHT = (params['height']*1) + 40 || 440; //defaults to 440 if no paramaters were added to URL
			ajaxContentW = TB_WIDTH - 30;
			ajaxContentH = TB_HEIGHT - 45;
			
			if(url.indexOf('TB_iframe') != -1){// either iframe or ajax window		
					urlNoQuery = url.split('TB_');
					$("#TB_iframeContent").remove();
					if(params['modal'] != "true"){//iframe no modal
						$("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton' title='Close'>close</a> or Esc Key</div></div><iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TB_iframeContent' name='TB_iframeContent"+Math.round(Math.random()*1000)+"' onload='tb_showIframe()' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;' > </iframe>");
					}else{//iframe modal
					$("#TB_overlay").unbind();
						$("#TB_window").append("<iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TB_iframeContent' name='TB_iframeContent"+Math.round(Math.random()*1000)+"' onload='tb_showIframe()' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;'> </iframe>");
					}
			}else{// not an iframe, ajax
					if($("#TB_window").css("display") != "block"){
						if(params['modal'] != "true"){//ajax no modal
						$("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton'>close</a> or Esc Key</div></div><div id='TB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px'></div>");
						}else{//ajax modal
						$("#TB_overlay").unbind();
						$("#TB_window").append("<div id='TB_ajaxContent' class='TB_modal' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");	
						}
					}else{//this means the window is already up, we are just loading new content via ajax
						$("#TB_ajaxContent")[0].style.width = ajaxContentW +"px";
						$("#TB_ajaxContent")[0].style.height = ajaxContentH +"px";
						$("#TB_ajaxContent")[0].scrollTop = 0;
						$("#TB_ajaxWindowTitle").html(caption);
					}
			}
					
			$("#TB_closeWindowButton").click(tb_remove);
			
				if(url.indexOf('TB_inline') != -1){	
					$("#TB_ajaxContent").append($('#' + params['inlineId']).children());
					$("#TB_window").unload(function () {
						$('#' + params['inlineId']).append( $("#TB_ajaxContent").children() ); // move elements back when you're finished
					});
					tb_position();
					$("#TB_load").remove();
					$("#TB_window").css({display:"block"}); 
				}else if(url.indexOf('TB_iframe') != -1){
					tb_position();
					if($.browser.safari){//safari needs help because it will not fire iframe onload
						$("#TB_load").remove();
						$("#TB_window").css({display:"block"});
					}
				}else{
					$("#TB_ajaxContent").load(url += "&random=" + (new Date().getTime()),function(){//to do a post change this load method
						tb_position();
						$("#TB_load").remove();
						tb_init("#TB_ajaxContent a.thickbox");
						$("#TB_window").css({display:"block"});
					});
				}
			
		}

		if(!params['modal']){
			document.onkeyup = function(e){ 	
				if (e == null) { // ie
					keycode = event.keyCode;
				} else { // mozilla
					keycode = e.which;
				}
				if(keycode == 27){ // close
					tb_remove();
				}	
			};
		}
		
	} catch(e) {
		//nothing here
	}
}

//helper functions below
function tb_showIframe(){
	$("#TB_load").remove();
	$("#TB_window").css({display:"block"});
}

function tb_remove() {
 	$("#TB_imageOff").unbind("click");
	$("#TB_closeWindowButton").unbind("click");
	$("#TB_window").fadeOut("slow",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
	$("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}

function tb_position() {
$("#TB_window").css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: TB_WIDTH + 'px'});
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
		$("#TB_window").css({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px'});
	}
}

function tb_parseQuery ( query ) {
   var Params = {};
   if ( ! query ) {return Params;}// return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}

function tb_getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
}

function tb_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}



Stamp = new Date();

function timeStamp() {
document.write(new Date);
}



function openWindow(theURL, winName, features) {
    window.open(theURL, winName, 'height=500,scrollbars=yes,width=530,status=yes,toolbar=no,menubar=no,location=no');
}
