$(document).ready(function(){
	$('div#collaborators').cycle({
		fit : 1,
		width : 280,
		timeout : 5000
	});	
	//activateExpandBehaviors();
	activateShiftBehaviors();
	activateNewsItemBehaviors();
	
	$(".slidePanel").click(function(){
		var id = "#" + $(this).attr("id");
		
		$(".slidePanel").not(id).each(function(){
			var sibling_id = "#" + $(this).attr("id"); 
			$(sibling_id + " .panelContent .default").fadeTo(500, .01);
			$(sibling_id + " .panelContent .expanded").fadeTo(500, .01);
			$(sibling_id).animate({width: 50}, 700).css("overflow", "visible").addClass("collapsed").removeClass("expanded");
			$(sibling_id + " h2 span strong").hide();
			$(sibling_id + " h3").fadeTo(700, 1);
		});
		
		$(id).animate({width: 770}, 700).css("overflow", "visible").addClass("expanded").removeClass("collapsed");;
		$(id + " .panelContent .default").fadeTo(500, .01);
		$(id + " h3").fadeTo(500, .01);
		$(id + " h2 span strong").fadeTo(700, 1);
		$(id + " .panelContent .expanded").fadeTo(500, 1);
		
		$("div.adhoc").remove();

	});
	
	$("#body").mouseleave(resetSlidePanelChildOpacity);
	$("#news").mouseenter(resetSlidePanelChildOpacity);
	
	$(".slidePanel .default").hover(
		function()
		{
			//BRING THE OPACITY OF HOVERED PANEL UP TO 1
			$(this).children().fadeTo(300, 1);
			//FADE ITEMS ON THE OTHER PANELS
			$(".slidePanel .default").not(this).each(function(){
				$(this).children().not(".color").fadeTo(300, 0.5);
				$(this).children(".color").fadeTo(300, 0);
			});
		},
		function()
		{}
	);
	$("#logo").click(resetSlidePanels);

});

	function resetSlidePanelChildOpacity(){
		$(".slidePanel .panelContent .default").children(":visible").fadeTo(300, 1).removeClass("active");
	}
	
function resetSlidePanels(){
	$(".slidePanel").each(function(){
		var id = "#" + $(this).attr("id");
		$(id).animate({ width: 192 }, 700).css("overflow", "visible");
		$(id + " h3").fadeTo(400, .01);
		$(id + " h2 span strong").fadeTo(800, 1);
		$(id + " .panelContent .expanded").fadeTo(500, 0);
		$(id + " .panelContent .default").fadeTo(1000, 1);
		$(id).removeClass("expanded collapsed");
		$("div.adhoc").remove();
	});
}
	
function activateExpandBehaviors() {
	activateExpandStudioBehavior();
	activateExpandStudioOneBehavior();
	activateExpandStudioTwoBehavior();
	activateExpandPlanningBehavior();
	activateExpandPlanningOneBehavior();
	activateExpandPlanningTwoBehavior();
	activateExpandPlanningThreeBehavior();
}

function deactivateBehaviors() {
	$('div#body h2.heading, div#body div').unbind('click');
}

function hideNewsItems() {
	$('div.fullNewsItem').hide();
}

/* Principals */

function activateExpandStudioBehavior () {
	$('div#studio h2.heading').unbind('click');
	$('div#studio h2.heading').click(function(){expandStudio()});
}

	function expandStudio (){
		hideNewsItems();
		$('div#planning div.activity').animate({
			width: 60
		}, 400);
		$('div#planning').animate({
			width: 180
		}, 500);
		$('div#studio').animate({
			width: 780
		}, 600);
		$('div#studio div.principal').animate({
			width: 390
		}, 700);
		
		$('div#studio div.columnOneFull').show();
		
		setTimeout(function(){
			activateExpandBehaviors();
			activateContractStudioBehavior();
		}, 700);
	}

function activateContractStudioBehavior () {
	$('div#studio h2.heading').unbind('click');
	$('div#studio h2.heading').click(function(){contractPrincipals()});
}

	function contractStudio(){
		hideNewsItems();
		$('div#planning div.activity').animate({
			width: 192
		}, 700);
		$('div#planning').animate({
			width: 576
		}, 600);
		$('div#studio').animate({
			width: 384
		}, 500);
		$('div#studio div.principal').animate({
			width: 192
		}, 400);
		
		$('div#studio div.columnOneFull').hide();
		
		$('div#planning div.columnOneFull').hide();
		$('div#planning div.columnOneLimited').show();
		
		setTimeout(function(){
			activateExpandBehaviors();
		}, 700);
	}
	
	/* Principal One */

	function activateExpandStudioOneBehavior () {
		$('div#studio div#principalOne').unbind('click');
		$('div#studio div#principalOne').click(function(){expandStudioOne()});
	}
	
		function expandStudioOne (){
			hideNewsItems();
			$('div#planning div.activity').animate({
				width: 60
			}, 400);
			$('div#planning').animate({
				width: 180
			}, 500);
			$('div#studio').animate({
				width: 780
			}, 600);
			$('div#studio div#principalTwo').animate({
				width: 60
			}, 700);
			$('div#studio div#principalOne').animate({
				width: 720
			}, 800);
			
			$('div#studio div#principalOne div.columnOneFull').show();
			$('div#studio div#principalOne div.columnTwo').show();
				
			setTimeout(function(){
				activateExpandBehaviors();
				activateContractPrincipalOneBehavior();
			}, 800);
		}
	
	function activateContractStudioOneBehavior () {
		$('div#studio div#principalOne').unbind('click');
		$('div#studio div#principalOne').click(function(){contractStudioOne()});
	}
	
		function contractStudioOne (){
			hideNewsItems();
			$('div#planning div.activity').animate({
				width: 60
			}, 400);
			$('div#planning').animate({
				width: 180
			}, 500);
			$('div#studio').animate({
				width: 780
			}, 600);
			$('div#studio div.principal').animate({
				width: 390
			}, 700);
			
			$('div#studio div.columnOneFull').show();
			
			setTimeout(function(){
				activateExpandBehaviors();
				activateContractStudioBehavior();
			}, 700);
		}
		
	/* Principal Two */
	
	function activateExpandStudioTwoBehavior () {
		$('div#studio div#principalTwo').unbind('click');
		$('div#studio div#principalTwo').click(function(){expandStudioTwo()});
	}
	
		function expandStudioTwo (){
			hideNewsItems();
			$('div#planning div.activity').animate({
				width: 60
			}, 400);
			$('div#planning').animate({
				width: 180
			}, 500);
			$('div#studio').animate({
				width: 780
			}, 600);
			$('div#studio div#principalOne').animate({
				width: 60
			}, 700);
			$('div#studio div#principalTwo').animate({
				width: 720
			}, 800);
			
			$('div#studio div#principalTwo div.columnOneFull').show();
			$('div#studio div#principalTwo div.columnTwo').show();
			
			setTimeout(function(){
				activateExpandBehaviors();
				activateContractStudioTwoBehavior();
			}, 800);
		}
	
	function activateContractStudioTwoBehavior () {
		$('div#studio div#principalTwo').unbind('click');
		$('div#studio div#principalTwo').click(function(){contractStudioTwo()});
	}
	
		function contractStudioTwo (){
			hideNewsItems();
			$('div#planning div.activity').animate({
				width: 60
			}, 400);
			$('div#planning').animate({
				width: 180
			}, 500);
			$('div#studio').animate({
				width: 780
			}, 600);
			$('div#studio div.principal').animate({
				width: 390
			}, 700);
			
			$('div#studio div#principalOne div.columnOneFull').show();
			
			setTimeout(function(){
				activateExpandBehaviors();
				activateContractStudioBehavior();
			}, 700);
		}

/* Activities */

function activateExpandPlanningBehavior () {
	$('div#planning h2.heading').unbind('click');
	$('div#planning h2.heading').click(function(){expandPlanning()});
}

	function expandPlanning (){
		hideNewsItems();
		$('div#studio div.principal').animate({
			width: 60
		}, 400);
		$('div#studio').animate({
			width: 120
		}, 500);
		$('div#planning').animate({
			width: 840
		}, 600);
		$('div#planning div.activity').animate({
			width: 280
		}, 700);
		
		$('div#planning div.columnOneLimited').hide();
		$('div#planning div.columnOneFull').show();
		
		setTimeout(function(){
			activateExpandBehaviors();
			activateContractPlanningBehavior();
		}, 700);
	}

function activateContractPlanningBehavior () {
	$('div#planning h2.heading').unbind('click');
	$('div#planning h2.heading').click(function(){contractPlanning()});
}

	function contractPlanning (){
		hideNewsItems();
		$('div#studio div.principal').animate({
			width: 192
		}, 700);
		$('div#studio').animate({
			width: 384
		}, 600);
		$('div#planning').animate({
			width: 576
		}, 500);
		$('div#planning div.activity').animate({
			width: 192
		}, 400);
		
		$('div#studio div.columnOneFull').hide();
		
		$('div#planning div.columnOneFull').hide();
		$('div#planning div.columnOneLimited').show();
		
		setTimeout(function(){
			activateExpandBehaviors();
		}, 700);
	}
	
	/* Activity One */
	
	function activateExpandPlanningOneBehavior () {
		$('div#planning div#activityOne').unbind('click');
		$('div#planning div#activityOne').click(function(){expandActivityOne()});
	}
	
		function expandActivityOne (){
			hideNewsItems();
			$('div#studio div.principal').animate({
				width: 60
			}, 400);
			$('div#studio').animate({
				width: 120
			}, 500);
			$('div#planning').animate({
				width: 840
			}, 600);
			$('div#planning div#activityTwo, div#planning div#activityThree').animate({
				width: 60
			}, 700);
			$('div#planning div#activityOne').animate({
				width: 720
			}, 800);
			
			$('div#planning div#activityOne div.columnOneLimited').hide();
			$('div#planning div#activityOne div.columnOneFull').show();
			$('div#planning div#activityOne div.columnTwo').show();
			
			setTimeout(function(){
				activateExpandBehaviors();
				activateContractPlanningOneBehavior();
			}, 800);
		}
	
	function activateContractPlanningOneBehavior () {
		$('div#planning div#activityOne').unbind('click');
		$('div#planning div#activityOne').click(function(){contractActivityOne()});
	}
	
		function contractActivityOne (){
			hideNewsItems();
			$('div#studio div.principal').animate({
				width: 60
			}, 400);
			$('div#studio').animate({
				width: 120
			}, 500);
			$('div#planning').animate({
				width: 840
			}, 600);
			$('div#planning div.activity').animate({
				width: 280
			}, 700);
			
			$('div#planning div.columnOneLimited').hide();
			$('div#planning div.columnOneFull').show();
			
			setTimeout(function(){
				activateExpandBehaviors();
				activateContractPlanningBehavior();
			}, 700);
		}
	
	/* Activity Two */
	
	function activateExpandPlanningTwoBehavior () {
		$('div#planning div#activityTwo').unbind('click');
		$('div#planning div#activityTwo').click(function(){expandActivityTwo()});
	}
	
		function expandActivityTwo () {
			hideNewsItems();
			$('div#studio div.principal').animate({
				width: 60
			}, 400);
			$('div#studio').animate({
				width: 120
			}, 500);
			$('div#planning').animate({
				width: 840
			}, 600);
			$('div#planning div#activityOne, div#planning div#activityThree').animate({
				width: 60
			}, 700);
			$('div#planning div#activityTwo').animate({
				width: 720
			}, 800);
			
			$('div#planning div#activityTwo div.columnOneLimited').hide();
			$('div#planning div#activityTwo div.columnOneFull').show();
			$('div#planning div#activityTwo div.columnTwo').show();
			
			setTimeout(function(){
				activateExpandBehaviors();
				activateContractPlanningTwoBehavior();
			}, 800);
		}
	
	function activateContractPlanningTwoBehavior () {
		$('div#planning div#activityTwo').unbind('click');
		$('div#planning div#activityTwo').click(function(){contractActivityTwo()});
	}
	
		function contractActivityTwo (){
			hideNewsItems();
			$('div#studio div.principal').animate({
				width: 60
			}, 400);
			$('div#studio').animate({
				width: 120
			}, 500);
			$('div#planning').animate({
				width: 840
			}, 600);
			$('div#planning div.activity').animate({
				width: 280
			}, 700);
			
			$('div#planning div.columnOneLimited').hide();
			$('div#planning div.columnOneFull').show();
			
			setTimeout(function(){
				activateExpandBehaviors();
				activateContractPlanningBehavior();
			}, 700);
		}
	
	/* Activity Three */
	
	function activateExpandPlanningThreeBehavior () {
		$('div#planning div#activityThree').unbind('click');
		$('div#planning div#activityThree').click(function(){expandActivityThree()});
	}
	
		function expandActivityThree (){
			hideNewsItems();
			$('div#studio div.principal').animate({
				width: 60
			}, 400);
			$('div#studio').animate({
				width: 120
			}, 500);
			$('div#planning').animate({
				width: 840
			}, 600);
			$('div#planning div#activityTwo, div#planning div#activityOne').animate({
				width: 60
			}, 700);
			$('div#planning div#activityThree').animate({
				width: 720
			}, 800);
			
			$('div#planning div#activityThree div.columnOneLimited').hide();
			$('div#planning div#activityThree div.columnOneFull').show();
			$('div#planning div#activityThree div.columnTwo').show();
			
			setTimeout(function(){
				activateExpandBehaviors();
				activateContractPlanningThreeBehavior();
			}, 800);
		}
	
	function activateContractPlanningThreeBehavior () {
		$('div#planning div#activityThree').unbind('click');
		$('div#planning div#activityThree').click(function(){contractActivityThree()});
	}
	
		function contractActivityThree (){
			hideNewsItems();
			$('div#studio div.principal').animate({
				width: 60
			}, 400);
			$('div#studio').animate({
				width: 120
			}, 500);
			$('div#planning').animate({
				width: 840
			}, 600);
			$('div#planning div.activity').animate({
				width: 280
			}, 700);
			
			$('div#planning div.columnOneLimited').hide();
			$('div#planning div.columnOneFull').show();
			
			setTimeout(function(){
				activateExpandBehaviors();
				activateContractPlanningBehavior();
			}, 700);
		}

/* Shifters */

var availableNewsPositions = 5;
var newsItemCount = 0;
var shiftPosition = 0;
var maxShiftPosition = 0;
var adHocOffset = 80;
var shiftAmount = 167;
var newsReturnTimer = false;
var newsReturnTimerInterval = 30000;

function activateShiftBehaviors() {
	newsItemCount = $('div#news div.newsItem').length;
	$('div#news div#newsItems').width(newsItemCount * shiftAmount + 120);
	maxShiftPosition = 0;
	minShiftPosition = (availableNewsPositions - newsItemCount) * shiftAmount;
	-minShiftPosition;
	activateLeftShiftBehavior();
	activateRightShiftBehavior();
}

function activateLeftShiftBehavior(){
	if (shiftPosition >= maxShiftPosition) {
		$('div#news div#shifter_left img').hide();
		$('div#news div#shifter_left').css('cursor', 'auto');
		$('div#news div#shifter_left').unbind('click');
	} else {
		$('div#news div#shifter_left img').show();
		$('div#news div#shifter_left').css('cursor', 'pointer');
		$('div#news div#shifter_left').unbind('click');
		$('div#news div#shifter_left').click(function(){
			resetNewsReturnTimer();
			$('div.adhoc').remove();
			if (shiftPosition < maxShiftPosition) {
				var shiftBy = shiftPosition + shiftAmount;
				$('div#news div#newsItems').animate({"left" : shiftBy}, function(){			
					shiftPosition = parseInt($('div#news div#newsItems').css('left'));
					activateRightShiftBehavior();
				});
			}
			if (shiftPosition + shiftAmount >= maxShiftPosition) {
				$('div#news div#shifter_left img').hide();
				$('div#news div#shifter_left').css('cursor', 'auto');
				$('div#news div#shifter_left').unbind('click');
			}
		});
	}
}

function activateRightShiftBehavior(){
	if (shiftPosition <= minShiftPosition) {
		$('div#news div#shifter_right img').hide();
		$('div#news div#shifter_right').css('cursor', 'auto');
		$('div#news div#shifter_right').unbind('click');
	} else {
		$('div#news div#shifter_right img').show();
		$('div#news div#shifter_right').css('cursor', 'pointer');
		$('div#news div#shifter_right').unbind('click');
		$('div#news div#shifter_right').click(function(){
			resetNewsReturnTimer();
			$('div.adhoc').remove();
			if (shiftPosition > minShiftPosition) {
				var shiftBy = shiftPosition - shiftAmount;
				$('div#news div#newsItems').animate({"left" : shiftBy}, function(){			
					shiftPosition = parseInt($('div#news div#newsItems').css('left'));
					activateLeftShiftBehavior();
				});
			}
			if (shiftPosition - shiftAmount <= minShiftPosition) {
				$('div#news div#shifter_right img').hide();
				$('div#news div#shifter_right').css('cursor', 'auto');
				$('div#news div#shifter_right').unbind('click');
			}
		});
	}
}

function activateNewsItemBehaviors () {
	$('div.newsItem').click(function(){
		resetNewsReturnTimer();
		$('div.adhoc').remove();
		var position = $(this).position();
		$(this).find('div.fullNewsItem').each(function(){
			var thisClone = $(this).clone().addClass('adhoc').appendTo($('div#body')).css({'left': position.left + shiftPosition + adHocOffset, 'bottom': 0}).slideDown(100);
			$(thisClone).click(function(){
				$(this).hide();
			});
		});
	});
}

function resetNewsReturnTimer () {
	clearTimeout(newsReturnTimer);
	newsReturnTimer = setTimeout(function(){
		returnNewsToBeginning();
	}, newsReturnTimerInterval);
}

function returnNewsToBeginning () {
	hideNewsItems();
	$('div#news div#newsItems').animate({"left" : 0}, function(){
		shiftPosition = 0;
		activateRightShiftBehavior();
		// Deactivate the left button.
		$('div#news div#shifter_left img').hide();
		$('div#news div#shifter_left').css('cursor', 'auto');
		$('div#news div#shifter_left').unbind('click');
	});
}
