
var sliderCount = 0;
var currentSlide;
var clickedSlide;
var expandedWidth;
var slideSpeed = 500;
var galleryImage;
var dropDownOn = false;
var shareTimer = 0;

$(document).ready(function()
{

	//$(document).pngFix(); 

	$('.banner-tab').click(function()
	{
		var l = $(this).find('a').attr('href');
		location = l;
	});
	
	$('.slider-text').click(function()
	{
		var l = $(this).find('.view a').attr('href');
		document.location = l;
	});
	
	$('.slider-option').hover(function()
	{
		$(this).css('cursor','pointer');
	},function()
	{
		
	});
	
	$('.service-float').each(function()
	{
		$(this).hover(function()
		{
			$(this).find('.service-text').stop(true,true).slideDown();
		},function()
		{
			$(this).find('.service-text').stop(true,true).slideUp();
		});
	});

	if($('#contact-form').length)
	{
		var o = $('#selected-title').offset();
		//$('#contact-form ul').css({left:o.left,top:o.top});
		$('#selected-title a').click(function()
		{
			$('#contact-form ul').slideToggle('fast',function()
			{
				if($(this).is(':visible')) dropDownOn = true;
				else dropDownOn = false;
			});
			
		});
		$('#contact-form ul a').click(function()
		{
			var val = $(this).html();
			$('#selected-title a').html(val);
			$('#contact_title').attr('value',val);
			$('#contact-form ul').slideUp('fast');
		});	
		
		$(document).click(function()
		{
			if(dropDownOn) $('#contact-form ul').slideUp('fast',function()
			{
				dropDownOn = false;
			});
		});
		
	}
	
	$('#navigation ul li a span').each(function()
	{
		var w = $(this).parent().width();
		$(this).css('width',w + 16);
	});
	
	$('#navigation ul li a').hover(function()
	{
		$(this).parent().addClass('over');
	},	function()
		{
			$(this).parent().removeClass('over');
	});
	
	$('#navigation ul li ul').hover(function()
	{
		$(this).parent().addClass('over');
	},	function()
		{
			$(this).parent().removeClass('over');
	});
	
	$('.banner-image').mouseenter(function()
	{
		$('#image-roll').stop(true,true).slideDown(100);
	}).mouseleave(function()
	{
		$('#image-roll').stop(true,true).slideUp(100);
	});
	
	
	
	// set up rollovers for news banner at bottom
	$('#news-ticker').mouseover(function()
	{
		$(this).css('background-image','url(/images/news-panel-on.png)');
	}).mouseout(function()
	{
		$(this).css('background-image','url(/images/news-panel-off.png)');
	});
	$('#share a').mouseover(function()
	{
		clearTimeout(shareTimer);
		$(this).children('img').attr('src','/images/share-panel-on.png');
		$('#share-options').fadeIn();
	}).mouseout(function()
	{
		$(this).children('img').attr('src','/images/share-panel-off.png');
		shareTimer = setTimeout(function() { $('#share-options').fadeOut(); },1000);
	});
	$('#twitter a').mouseover(function()
	{
		$(this).children('img').attr('src','/images/twitter-panel-on.png');
	}).mouseout(function()
	{
		$(this).children('img').attr('src','/images/twitter-panel-off.png');
	});
	
	$('#share-options').mouseenter(function()
	{
		clearTimeout(shareTimer);
		$('#share p a img').attr('src','/images/share-panel-on.png');
	}).mouseleave(function()
	{
		$('#share p a img').attr('src','/images/share-panel-off.png');
	});
	
	if($('.press-group').length)
	{
		currentGroup = $('.press-group').first();
		$('.press-group').each(function()
		{
			if($(this).is(':hidden')) {
				$(this).css('left',875);
				$(this).css('display','block');
			}
		});
		
		$('.prev').hide();
		if($('.press-group').length < 2) $('.next').hide();
		
		$('.next').click(function()
		{
			$('.prev').fadeIn();
			var n = currentGroup.next();
			if(n.length)
			{
				currentGroup.animate({left:-875},500,'easeOutCirc');
				n.animate({left:0},500,'easeOutCirc');
				if(!n.next().length) $('.next').fadeOut();
				currentGroup = n;
			}
		});
		$('.prev').click(function()
		{
			$('.next').fadeIn();
			var n = currentGroup.prev();
			if(n.length)
			{
				currentGroup.animate({left:875},500,'easeOutCirc');
				n.animate({left:0},500,'easeOutCirc');
				if(!n.prev().length) $('.prev').fadeOut();
				currentGroup = n;
			}
		});
	}
	
	// check for slideshow
	
	if($('#slider').length) initSlider();
	
	$('.banner-tab').mouseenter(function()
	{
		$(this).css('cursor','pointer');
		$(this).css('background-image','url(/images/banner-panel-bg-over.png)');	
		$(this).find('.more').attr('src','/images/pulse.gif');
		$(this).children('.rollover-text').stop(true,true).slideDown(500);
	}).mouseleave(function()
	{
		
		//$(this).children('.rollover-text').stop(true,true);
		//$(this).css('background-image','url(images/banner-panel-bg.png)');	
		$(this).children('.rollover-text').stop(true,true).slideUp(500,function()
		{
			$(this).parent().css('background-image','url(/images/banner-panel-bg.png)');	
		});	
		$(this).find('.more').attr('src','/images/panel-arrow.png');
	});
	
	$('#banner').each(function()
		{
			$(this).data('currentImage',$(this).children('.banner-image:first-child'));
		});

		setTimeout('nextBannerImage()',5000);
		
	// image gallery browser
	
	// set first image
	if($('.gallery-image').length)
	{
		galleryImage = $('.gallery-image').first();
		$('#gallery-nav a').each(function()
		{
			$(this).data('currentImage',false);
			$(this).hover(function()
			{
				if(!$(this).data('currentImage')) $(this).children('img').attr('src',($(this).children('img').attr('src').replace('off','on')));
			},function()
			{
				if(!$(this).data('currentImage')) $(this).children('img').attr('src',($(this).children('img').attr('src').replace('on','off')));
			});
			$(this).click(function()
			{
				clearTimeout(gInterval);
				var t = $(this).attr('title');
				if(t == 'Next')
				{
					var g = galleryImage.next('.gallery-image');
					if(!g.length) g = $('.gallery-image').first();
					galleryImage.fadeOut();
					g.fadeIn();
					galleryImage = g;
				} else if(t == 'Prev')
				{
					var g = galleryImage.prev('.gallery-image');
					if(!g.length) g = $('.gallery-image').last();
					galleryImage.fadeOut();
					g.fadeIn();
					galleryImage = g;
				} else
				{
					var imageNum = parseInt(t);
					var g = $('#gallery-'+imageNum);
					if(g.length && g.attr('id') != galleryImage.attr('id'))
					{
						//alert('loading image ' + imageNum);
						galleryImage.fadeOut();
						g.fadeIn();
						galleryImage = g;
					}
				}
				setCurrentImage();
				autoGallery();	
			});
		});	
		
		setCurrentImage();
		autoGallery();				
		hideGalleryBrowser();
		
		$('#banner').mouseenter(function()
		{
			showGalleryBrowser();
		}).mouseleave(function()
		{
			hideGalleryBrowser();
		});
	}
	
	
	
});

var submit_contact_form = function()
{
	var s = true;
	if($('#contact_title').attr('value') == '') { alert('Please choose your title'); s = false; }
	else if($('#cfirst').attr('value') == '') { alert('Please enter your first name'); s = false; }
	else if($('#clast').attr('value') == '') { alert('Please enter your last name'); s = false; }
	else if($('#cemail').attr('value') == '') { alert('Please enter your email address'); s = false; }
	else if($('#ctel').attr('value') == '') { alert('Please enter your telephone number'); s = false; }
	
	if(s) {
		var p = $('#contacter').serialize();
		$.post('/ajax/enquire',p,function(d)
		{
			if(d == 'invalid') alert('Please enter a valid email');
			else {
				$('#instructions').hide();
				$('#contact-form').html(d);
			}
		});
	}
}

var mc_update = function()
{
	$('#contacter').submit();
}

var autoGallery = function()
{
	gInterval = setInterval(function()
		{
			$('#gallery-next-prev a').last().click();
		},5000);
}

var hideGalleryBrowser = function()
{
	galleryNavTimer = setTimeout(function()
	{
		$('#gallery-nav').hide('slide',{direction:'down'},300);
	},2000);
}

var showGalleryBrowser = function()
{
	clearTimeout(galleryNavTimer);
	if($('#gallery-nav').is(':hidden')) $('#gallery-nav').show('slide',{direction:'down'},300);
}

var load_press = function(doc,media,link)
{
	$('.press-category').removeClass('selected');
	if(link == 0) $('.press-category').first().addClass('selected');
	else $(link).parent().addClass('selected');
	$('#press-articles').fadeOut('slow',function()
	{
		$(this).load('/ajax/press/'+doc+'/'+media,function()
		{
			$(this).fadeIn();
		});
	})
}

var setCurrentImage = function()
{
	// set link up as selected
	
	var n = galleryImage.attr('id').replace('gallery-','');
	$('#gallery-nav a').each(function()
	{
		if(n != $(this).attr('title')) {
			if($(this).data('currentImage')) $(this).children('img').attr('src',($(this).children('img').attr('src').replace('on','off')));
			$(this).data('currentImage',false);
		}
		else {
			$(this).data('currentImage',true);
			$(this).children('img').attr('src',($(this).children('img').attr('src').replace('off','on')));
		}
	});
	//alert(n);
}


nextBannerImage = function()
{
	//alert('showing nxt image');
	if(!$('#banner .banner-image').length) return false;
	$('#banner').each(function()
	{
		var c = $(this).data('currentImage');
		var n = c.next('.banner-image');
		if(!n.length) n = $(this).children('.banner-image').first();
		//if(!c.next('.drop-image').length) n = $(this).children('.drop-image:first-child');
		//else n = c.next('.drop-image');
		//console.log(n.attr('class'));
		c.fadeOut(700);
		n.fadeIn(700);
		$(this).data('currentImage',n);
		var num = parseInt(n.attr('id').replace('bi-',''));
		$('#banner .more').attr('src','/images/panel-arrow.png');
		if(num == 1 || num == 4)
		{
			// pulse first tab
			$('#tab-1 .more').attr('src','/images/pulse.gif');
		}
		if(num == 2 || num == 5)
		{
			// pulse second tab
			$('#tab-2 .more').attr('src','/images/pulse.gif');
		}
		if(num == 3 || num == 6)
		{
			// pulse second tab
			$('#tab-3 .more').attr('src','/images/pulse.gif');
		}
	});
	setTimeout('nextBannerImage()',5000)
}

var initSlider = function()
{
	var numSlides = $('.slider-option').length;
	var sliderWidth = $('#slider').width();
	var w =  sliderWidth / numSlides;
	expandedWidth = sliderWidth - ((numSlides-1) * 35);
	//console.log(w);
	
	$('.slider-option').each(function()
	{
		$(this).css('width',w);
		$(this).css('left',(w * sliderCount));
		$(this).data('openLeft',(35 * sliderCount));
		sliderCount++;
		$(this).data('closedLeft',(sliderWidth - (35 * sliderCount)));
		
		
		$(this).children('.slider-button').children('a').click(function()
		{
			selectSlide($(this).parent().parent());
		});
		
		if(sliderCount == numSlides) setTimeout('startSlides()',2000);
	});
	
}

var startSlides = function()
{
	selectSlide($('.slider-option').first());
}

var selectSlide = function(slide)
{
	//console.log('starting slider');
	clickedSlide = slide;
	var leftPos = 0;
	//if(currentSlide) currentSlide.animate({width:34,left:$(this).data('closedLeft')},500);
	//else {
		$('.slider-option').each(function()
		{
			if($(this).attr('id') != clickedSlide.attr('id')) {
				//console.log('closing');//$(this).animate({width:34,left:$(this).data('closedLeft')},500);
				$(this).addClass('closed');
				$(this).children('.slider-text').fadeOut();
				$(this).animate({width:34,left:leftPos},slideSpeed,'easeOutCirc');
				leftPos += 35;
			}
			else  {
				//console.log('opening');
				$(this).removeClass('closed');
				$(this).animate({width:expandedWidth,left:leftPos},slideSpeed,'easeOutCirc',function()
				{
					$(this).children('.slider-text').fadeIn();
				});
				leftPos += expandedWidth;
			}
		});
	//}
	//slide.animate({width:expandedWidth,left:$(this).data('openLeft')},500);
	currentSlide = slide;
}

var tld = 'blueforest';

nospam = function(n)
{
	location = 'mailto:' + n + '@' + tld + '.com';
}



