var PlayHomeSlideshow;

function slideSwitch()
{
	var $active = $('#slideshowHome img.activeHomeSlide');

	if ( $active.length == 0 ) $active = $('#slideshowHome img:last');

	var $next =  $active.next().length ? $active.next()
		: $('#slideshowHome img:first');

	$active.addClass('last-activeHomeSlide');

	$next.css({opacity: 0.0})
		.addClass('activeHomeSlide')
		.animate({opacity: 1.0}, 1000, function() {
			$active.removeClass('activeHomeSlide last-activeHomeSlide');
		});
}

jQuery.preloadImages = function()
{
	for(var i = 0; i<arguments.length; i++)
	{
		jQuery("<img/>").attr("src", arguments[i]);
	}
}

function loadSlides() {
	$('#slideshowHome .inactiveSlide').each(function() {
		$(this).attr('src', 'images/HomeSlideshow/' + $(this).attr('id'));
	});
	PlayHomeSlideshow = setInterval( 'slideSwitch()', 5000 );
}

$(function() {
	$('#slideshowHome .inactiveSlide').removeAttr('src');

	var panelWidth = 860;
	if($.browser.msie && $.browser.version=="6.0")
	{
		panelWidth = 857;
	}
	else if($.browser.msie && $.browser.version=="7.0")
	{
		$("#accordion .panel, #accordion .panel2").css({'width':860});
	}

	//$('.text_box').remove();

	//var PlayHomeSlideshow = setInterval( 'slideSwitch()', 5000 );

	$('a.external').click(function(){
		window.open(this.href);
		return false;
	});

	if(!($.browser.msie && $.browser.version=="6.0"))
	{
		$.preloadImages('images/NavBar1_OVER.png', 'images/NavBar2_OVER.png', 'images/NavBar3_OVER.png', 'images/NavBar4_OVER.png', 'images/NavBar1_ON.png', 'images/NavBar2_ON.png', 'images/NavBar3_ON.png', 'images/NavBar4_ON.png');
	}
	$('#clickHere').click(function(){
		//$(this).fadeOut('1000');
		if($('#staff_80s').is(':visible'))
		{
			$('#staff_80s').fadeOut('1000');
		}
		else
		{
			$('#staff_80s').fadeIn('1000');
		}

	});
	
	$('#accordion .panel:not(.active)').css({'width' : 0, 'display' : 'none' });

	
	$('.trigger').click(function()
	{
		var rel = $(this).attr('rel');
		var panel = $('div[rel="' + rel + '"]');
		if(panel.hasClass('active'))
		{
//			console.log('panel ' + rel + ' already active, ignoring.');
			return;
		}

		var $active_panel = $('#accordion .panel.active');

		if(rel == 'panel1')
		{
			PlayHomeSlideshow =  setInterval( 'slideSwitch()', 5000 );
		}
		else
		{
			clearInterval(PlayHomeSlideshow);
		}

		if(!($.browser.msie && $.browser.version=="6.0"))
		{
			if ($active_panel.attr('rel') != 'panel1')
			{
				var lastTriggerImg = $('.trigger[rel=' + $active_panel.attr('rel') +']').children('img:first');
				var lastTriggerImgSrc = lastTriggerImg.attr('src').split('_');

				//alert(childImg.attr('src'));

				lastTriggerImg.attr('src', lastTriggerImgSrc[0] + '_OFF.png');
				//alert(childImg.attr('src'));
			}
		}

		/*
		if ($(this).hasClass('vert'))
		{
			var childImg = $(this).children('img:first');
			var target = childImg[0].attr('src').split('_');
			childImg[0].attr('src', target[0] + '_OFF.png');
		}
		*/

		//$('.panel.active').animate( { width:"0"}, { queue:false, duration:752, easing:'linear' },
		$('#accordion .panel.active').animate( { 'width':0}, 752, 'linear',
		function() {
			$(this).css({'display':'none'});
			//alert('open!');
			}
		);
		//$('.panel.active').hide(500);

		$active_panel.removeClass('active');
		$('#accordion .panel[rel=' + rel +']').addClass('active');
		$('#accordion .panel.active').css({'display':'block'});
		//alert('closed!');
		$('#accordion .panel.active').animate( { 'width':panelWidth }, { queue:false, duration:750, easing:'linear' });

		//alert($(this).attr('rel'));

		if ($(this).attr('rel') != 'panel1')
		{

			var childImg = $(this).children('img:first');

			//alert(childImg.attr('src'));
			if(!($.browser.msie && $.browser.version=="6.0"))
			{
				var target = childImg.attr('src').split('_');
				childImg.attr('src', target[0] + '_ON.png');
				//alert(childImg.attr('src'));
			}
		}

	});
	

	/*disable to remove pause on hover*/
//	$('#slideshowHome').hover(function() {
//	    clearInterval(PlayHomeSlideshow);
//	},
//	function() {
//		PlayHomeSlideshow =  setInterval( 'slideSwitch()', 5000 );
//	});

	$('#Craftslightbox a').lightBox({containerResizeSpeed: 350});

	if(!($.browser.msie && $.browser.version=="6.0"))
	{
		$('#accordion div.trigger.vert img').hover(function() {
			var trigSource = $(this).attr('src').split('_');
			if(trigSource[1] != 'ON.png')
			{
				$(this).attr('src', trigSource[0] + '_OVER.png');
			}
		},
		function () {
			var trigSource = $(this).attr('src').split('_');
			//alert(trigSource[1]);
			if(trigSource[1] != 'ON.png')
			{
				$(this).attr('src', trigSource[0] + '_OFF.png');
			}
		});
	}
	//$('#container').pngFix();

	$('#photos_longevity .bubble_box').hide();

	$.each($('#faces area'), function() {
		//var rel = $(this).attr('rel');
		$('#' + $(this).attr('rel') + ' div.bubble-mid').append($(this).attr('alt').replace(" - ", '<br />'));
		
		$(this).removeAttr('title').removeAttr('alt');

	});
	
	$('#staff_photo').removeAttr('title').removeAttr('alt');
	
	$('#faces area').click(function(e) {
		e.preventDefault();
	});
	
	$('#faces area').hover(function() {
		var rel = $(this).attr('rel');
		$('.bubble_box[id=' + rel + ']').show(200);
		//$('.bubble_box[id=' + rel + ']').animate({opacity: 1}, 200);
	},
	function() {
		var rel = $(this).attr('rel');
		$('#photos_longevity .bubble_box[id=' + rel + ']').hide(400);
		//$('.bubble_box[id=' + rel + ']').animate({opacity: 0}, 400);
		
	});

	$('*[alt], *[title]').removeAttr('alt').removeAttr('title');

	$('#sustain_read_more').click(function() {
		$('#sustain_box p').fadeIn(1000);
		$(this).hide(500);
		$('.sustainability_box').animate({'height' : 500}, 1000);
	});
});