$(document).ready(function() {
	jsAccordion('#top-about-exp dl', 'click');
	
	navAccordion('#primary-nav', 'click');
	
	imageControl();
	
	imageRollover('img.rollover');
});

var imageCount = 1;

function jsAccordion(e, triggerEvent) {
	$(e).find('dd').hide();
	$(e).find('dt').addClass('dt-toggle');	
	
	$(e).find('dt').bind(triggerEvent, function(){
		if($(e).find('dd').is(':animated') || $(this).next('dd').is(':visible')) {return false;}
			$(e).find('dt.opened').removeClass('opened');
			$(e).find('dd:visible').slideUp(1500);
			$(this).next('dd').slideDown(1500).end().addClass('opened');
	});
	
	$(e).find('.active').trigger(triggerEvent);
};//jsAccordion

function navAccordion(e, triggerEvent) {
	$(e).find('dd').hide();
	$(e).find('dt a.expand').addClass('dt-toggle');
	if ($('a.selected').length > 0) {
		$(e).find('dt.opened').next('dd').show();
		}
	else {
		$(e).find('dt.opened').next('dd').slideDown(1500);
		}
};//navAccordion

function imageControl() {
	var images = $('.image-container').find('img');
	images.each( function(){
		$(this).attr("id","image-"+imageCount);
		imageCount ++;
		}	
	);
	imageCount = 1;
	images.fadeOut(1);
	$("#image-"+imageCount).fadeIn(1);

        var products = $('.copy').find('b');
	
	function selectProduct(productTag) {
		products.each( function(){
			if ($(this).attr("productTag") == productTag) {
				$(this)[0].className = "copy-product-highlight";
			}
			else {
				$(this)[0].className = "";
			}
		});
        }

	selectProduct($(images[imageCount - 1]).attr("productTag"));

	$("#next-image").bind('click', function () {
		$("#image-"+imageCount).fadeOut(1000);
		if (imageCount == images.length) {
			imageCount = 1;
		}
		else {
			imageCount ++;
		}
		$("#image-"+imageCount).fadeIn(1000);
		selectProduct($(images[imageCount - 1]).attr("productTag"));
	});
	
	$("#prev-image").bind('click', function () {
		$("#image-"+imageCount).fadeOut(1000);
		if (imageCount == 1) {
			imageCount = images.length;
		}
		else {
			imageCount --;
		}
		$("#image-"+imageCount).fadeIn(1000);		
		selectProduct($(images[imageCount - 1]).attr("productTag"));
	});	
}

function imageRollover(selector) {
	var images = new Array();
	var on_src = null;
	var i = 0;
	
	$(selector).each( function() {
		on_src = this.src.replace("-off","-on");
		images[i] = $('<img>').attr('src',on_src);
		i++;
		});
	
	
	$(selector).hover(
		 function() {
			this.src = this.src.replace("-off","-on");
		 },
		 function()	 {
			this.src = this.src.replace("-on","-off");
		 }
		);
}
