$(document).ready(	function(){
	featuresInit();
	givingInit();
});

function featuresInit() {
	seenIn = $('#asSeenIn');
	if(seenIn.length == 0) return;
	
	setTimeout('seenIn.fadeOut("slow")', 7000);
	
	featureImgs = $('#features img');
	featureImgs.hide();
	featureImgs.eq(0).show();

	var carouselDiv = $('#carousel');

	carouselDiv.wrap('<div id="carouselWrap"></div>');

	var carouselWrapDiv = $('#carouselWrap');
	carouselWrapDiv.css({ 
		overflow:"hidden",
		//margin: "-88px 0 0 333px",
		// no thought to expansion forces editing style below
		margin: "-111px 0 0 333px",
		width: "448px",
		height: "180px",
		position: "relative"
	});
	covers =  $('.cover');	
	coverWidth = covers.eq(0).width();
	carouselWidth = covers.size()*coverWidth;
	
	carouselDiv.css({ 
		width: carouselWidth+"px",
		position: "absolute",
		margin: "0"
	});
	
	carouselWrapDiv.after("<span id='carouselBack'></span><span id='carouselForward'></span>");
	var carouselBack = $('#carouselBack');
	var carouselForward = $('#carouselForward');
	carouselBack.css({ 
		display: "block",
		width: "15px",
		height: "16px",
		background: "url(../images/portfolio/slideshow-btn.gif) 0 0 no-repeat",
		position: "absolute",
		margin: "-130px 0 0 308px",
		cursor: "pointer"
	});
	carouselBack.hover(function() {
		$(this).css("backgroundPosition","0 -50px");
	}, function() {
		$(this).css("backgroundPosition","0 0");
	});
	
	carouselForward.css({ 
		display: "block",
		width: "15px",
		height: "16px",
		background: "url(../images/portfolio/slideshow-btn.gif) -21px 0 no-repeat",
		position: "absolute",
		margin: "-130px 0 0 777px",
		cursor: "pointer"
	});
	carouselForward.hover(function() {
		$(this).css("backgroundPosition","-21px -50px");
	}, function() {
		$(this).css("backgroundPosition","-21px 0");
	});
	carouselBack.hide();
	carouselBack.click(function () { 
		carouselPos = parseInt(carouselDiv.css("margin-left"));		
		carouselDiv.css("margin-left", carouselPos + coverWidth);
		carouselBack.show();
		carouselForward.show();
		if(carouselPos + coverWidth==0) carouselBack.hide();
	});
	carouselForward.click(function () { 
		carouselPos = parseInt(carouselDiv.css("margin-left"));		
		carouselDiv.css("margin-left", carouselPos - coverWidth);
		carouselBack.show();
		carouselForward.show();
		if(carouselPos - coverWidth==-carouselWidth+coverWidth) carouselForward.hide();
	});
	
	covers.eq(0).children('p').show();
	currentFeat = 0;
	covers.each(function(i){		
  		covers.eq(i).click(function(event){
			
			featureImgs.eq(currentFeat).fadeOut("slow");
			featureImgs.eq(i).fadeIn("slow");
			
			covers.each(function(j){
				covers.eq(j).children('p').hide();
			});
			covers.eq(i).children('p').show();
			currentFeat = i;
		});
	});
	
}

function givingInit() {
	var givingWrapper = $('#givingWrapper');
	if(givingWrapper.length == 0) return;

	givingItems = givingWrapper.children('.givingItem');
	
	givingTitles = givingItems.children('h3');
	
	// clone h3s, wrap them in li tags and place them all inside of a ul
	
	var titleWrapper = document.createElement('ul');
	givingWrapper.append(titleWrapper);
	titleWrapper.id = "titleWrapper";
	newTitles = givingTitles.clone().appendTo(titleWrapper);

	newTitles.eq(0).addClass('on');
	givingItems.hide();
	givingItems.eq(0).show();
	newTitles.each(function(i){
		newTitles.eq(i).wrap('<li></li>');
		newTitles.eq(i).click(function(event){
			givingItems.hide();
			givingItems.eq(i).show();
			newTitles.removeClass('on');
			newTitles.eq(i).addClass('on');
		});
	});
	
	if(givingItems.length > 1){
		lastParas = givingItems.children('p:last-child');
		lastParas.append(" <a class='next'></a>");
	
		lastLinks = lastParas.children('a.next');

		lastLinks.each(function(i){
	  		lastLinks.eq(i).click(function(event){
				j = i+1;
				if (j==lastLinks.length) j = 0;
			  	givingItems.eq(i).hide();
				givingItems.eq(j).show();
			  	newTitles.eq(i).removeClass('on');
				newTitles.eq(j).addClass('on');
				event.preventDefault();
			});
		});
	}
}
