/*
Concrete V3 Javascript 
Ian McNamara
2009.12.12
*/

/*
Added  initialization to hide the nav color logos


*/


// SETTINGS -> //
// Navigation:
var navWaitForClose = 1;
var navOpenCloseTime = 300;
var navIconFadeTime = 300;
var navMsgFadeInTime = 300;
var navMsgFadeOutTime = 300;
// Reason:
var reasonActivateTime = 1000;
var reasonDeactivateTime = 500;
var reasonActivateHeight = 14.9;	
var reasonColorLogoTime = 500;
var reasonColorLogoEase = "easeOutQuad";
var reasonFloatMin = 3.1;
var reasonFloatMax = 11.9;
var reasonFloatTime = 900;
var reasonFloatStartTime = 800;
var reasonFloatStartEase = "easeInOutQuad";
var reasonFloatEase = "easeInOutQuad";

// <-- SETTINGS //

// VARS -> //
// Navigation:
var navCloseTimer;
var navStatus = "closed";
var navTargetOpacity;
var navIconObject;
// Reasons
var reasonFloatDist = Math.abs(reasonFloatMax - reasonFloatMin);
var reasonStatusArray;	// inactive, active, final
var reasonIconStartPosArray;
var reasonTempString;
var reasonIcon;
var reasonIconStartPos;
var reasonColorLogo;
var reasonNumber;
var targetFloatValue;
// Free:
var thisTime;
// <- VARS //

// FUNCTIONS -> //

function debugAlert(msg) {
	$("#debug_box").html(msg);
}

function getAdjustedTime(currentValue,targetValue,maxValueDX,maxTime) {
	// Catch values with 'px' attached to them:
	if (currentValue.indexOf("px") >= 0) { currentValue = Number(currentValue.substr(0,currentValue.length-2)); }
	// Return 1 or greater.
	return Math.max(1,(maxTime * ((Math.abs(targetValue - currentValue)) / maxValueDX)));
}
function adjustedAnimation(animateObject,param,targetValue,maxChange,maxTime,easeName,callback) {
	var callbackLine;
	if (!easeName) { easeName = "easeOutCubic"; }
	if (!callback) { callbackLine = ""; } else { callbackLine = ", callback"; }
	var adjustedTime = getAdjustedTime(animateObject.css(param),targetValue,maxChange,maxTime);
	var command = " " +
		"	animateObject.animate( " +
			" { " + param + " : '"+targetValue+"' }, " +
			" " + adjustedTime + "," +
			"'"+easeName+"'" + 
			callbackLine +
		" ); ";				
	eval(command);
}
function sayHello() {
	alert("hello");
}
function parrot(cracker) {
	alert("parrot says: "+cracker);
}


// Navigation:
function navClearCloseTimer() {
	clearTimeout(navCloseTimer);
}
function navOpen(animate) {
	// Clear the timer.
	navClearCloseTimer();
	// Only open the nav if it is closed
	if (navStatus == "closed") {
		// Stop any other animation
		$("#nav_msg_bg").stop(true);
		// Determine the time.
		if (animate) { thisTime = navOpenCloseTime; } else { thisTime = 0; }
		// Animate the nav.
		adjustedAnimation($("#nav_msg_bg"),"height",22,22,thisTime);
		// Update the status.
		navStatus = "open";
	}
}
function navClose(animate,useTimer) {
	// Do we need to wait for the timer?
	if (useTimer) {
		// Set the timer and then do the closing.
		navCloseTimer = setTimeout("navClose("+animate+",false);",navWaitForClose);
	} else {
		// But only close if the nav is open:
		if (navStatus == "open") {
			// Stop any other animation & clear the timer
			navClearCloseTimer(); $("#nav_msg_bg").stop(true);
			// Determine the time.
			if (animate) { thisTime = navOpenCloseTime; } else { thisTime = 0; }
			// Animate the nav.
			adjustedAnimation($("#nav_msg_bg"),"height",1,22,thisTime);
			// Update the status.
			navStatus = "closed";
		}
	}
}
function navShowHideMessage(doShow,animate,title) {
	var fadeFunction;
	var fadeTime;
	var codeString;
	var targetObject;
	var targetOpacity;
	
	//if (doShow) { fadeFunction = "fadeIn"; } else { fadeFunction = "fadeOut"; }
	if (doShow) { targetOpacity = 1; } else { targetOpacity = 0; }
	if (animate) { 
		if (doShow) { fadeTime = navMsgFadeInTime; } else { fadeTime = navMsgFadeOutTime; }
	} else {
		fadeTime = 0;
	}
	if (title == "read" || title == "view" || title == "discover" || title == "ask") {
		targetObject = $("#nav_msg_"+title);
	}

	switch(title) {
		case "read": case "view": case "discover": case "ask":
			// Stop any other animations on the targetObject:
			targetObject.stop(true);
			codeString = "adjustedAnimation(targetObject,'opacity',targetOpacity,1,navMsgFadeInTime)";
			eval(codeString);
			break;
		default: 
			// Do it all!
			navShowHideMessage(doShow,animate,"read");
			navShowHideMessage(doShow,animate,"view");
			navShowHideMessage(doShow,animate,"discover");
			navShowHideMessage(doShow,animate,"ask");
			break;
	}
}

function navShowHideColorIcon(showIcon,navLinkObject) {
	// Compute the target opacity for this icon.
	if (showIcon) { navTargetOpacity = 1; } else { navTargetOpacity = 0; }
	// Get the navIconObject
	navIconObject = $(navLinkObject).find(".nav_icon_color");
	// Stop any animations on it.
	navIconObject.stop(true);
	// Run a new animation to show/hide it.
	adjustedAnimation(navIconObject,"opacity",navTargetOpacity,1,navIconFadeTime,"easeOutCubic");
}


// Reasons:

function reasonInitialize() {
	// Get the concrete_reason divs
	var reasonDivArray = $(".concrete_reason");
	// Create the status array, and set each value to "inactive"
	// Also, set the value in each of the divs so we can match them later.
	// Also also, record the starting positions of the icons.
	reasonStatusArray = new Array(reasonDivArray.length);
	reasonIconStartPosArray = new Array(reasonDivArray.length);
	for (var i = 0; i < reasonDivArray.length; i++) {
		reasonStatusArray[i] = "inactive";
		reasonDivArray.eq(i).attr("value",i);
		reasonTempString = $(".concrete_reason:eq("+i+") .con_reason_icon").css("top");
		reasonIconStartPosArray[i] = new Number(reasonTempString.substr(0,reasonTempString.length-2));
	}
}
function reasonHoverOver(reasonDiv) {
	// Is this reason currently inactive?
	if (reasonStatusArray[reasonDiv.attr("value")] == "inactive") {
		// The reason is inactive.
		// Grab the icon
		reasonIcon = $(reasonDiv).find(".con_reason_icon");
		reasonIconStartPos = reasonIconStartPosArray[reasonDiv.attr("value")];
		// Start the animation towards the reasonActivateHeight
		adjustedAnimation(reasonIcon,"top",reasonIconStartPos - reasonActivateHeight,reasonActivateHeight,reasonActivateTime,"easeOutQuad",function(){reasonComplete(reasonDiv);});
		// Update the status:
		reasonStatusArray[reasonDiv.attr("value")] = "active";
	} else {
	
	}
}
function reasonHoverOut(reasonDiv) {
	// Is the reason current active?
	if (reasonStatusArray[reasonDiv.attr("value")] == "active") {
		// The reason is active.
		// Grab the icon
		reasonIcon = $(reasonDiv).find(".con_reason_icon");
		// Stop the current animation.
		reasonIcon.stop(true);
		
		reasonIconStartPos = reasonIconStartPosArray[reasonDiv.attr("value")];
		// Start the animation towards the starting point.
		adjustedAnimation(reasonIcon,"top",reasonIconStartPos,reasonActivateHeight,reasonDeactivateTime,"easeOutBounce");
		// Update the status:
		reasonStatusArray[reasonDiv.attr("value")] = "inactive";
		
	} else {
	
	}
}
function reasonComplete(reasonDiv) {
	//alert("reasonComplete START");
	// Well well well... it finally made it.
	// Set the status so it's left alone from now one.
	reasonStatusArray[reasonDiv.attr("value")] = "final";
	// Bring in the color!
	reasonColorLogo = $(reasonDiv).find(".con_logo_color");
	reasonBWLogo = $(reasonDiv).find(".con_logo_bw");
	// 
	adjustedAnimation(reasonBWLogo,"opacity",0,1,reasonColorLogoTime,reasonColorLogoEase);
	adjustedAnimation(reasonColorLogo,"opacity",0.6,0.6,reasonColorLogoTime,reasonColorLogoEase);
	// Beautiful!  Splendid!
	// Grab the icon and throw it to the over function.
	reasonIcon = $(reasonDiv).find(".con_reason_icon");
	reasonFloatIcon(reasonIcon);

}
function reasonFloatIcon(iconObject) {
	// Drop the icon down to the bottom hover position.
	// The callback function will feed the icon to the helper.
	reasonNumber = $(iconObject).parent().parent().parent().attr("value");
	shadowObject = $(iconObject).parent().find(".con_reason_shadow");

	targetFloatValue = reasonIconStartPosArray[reasonNumber] - reasonFloatMin;

	
	$(iconObject).animate(
		{top:targetFloatValue+"px"},reasonFloatStartTime,reasonFloatStartEase,
		function(){ reasonFloatIconHelper(iconObject,shadowObject,reasonNumber,1);}
	);
	

	$(shadowObject).animate({opacity:1},reasonFloatStartTime,reasonFloatStartEase);

	
}
function reasonFloatIconHelper(iconObject,shadowObject,reasonNumber,floatDir) {

	// Compute the target float value
	if (floatDir > 0) {
		targetFloatValue = reasonIconStartPosArray[reasonNumber] - reasonFloatMax;
		$(shadowObject).animate({opacity:0.4},reasonFloatTime,"easeOutSine");
	} else {
		targetFloatValue = reasonIconStartPosArray[reasonNumber] - reasonFloatMin;
		$(shadowObject).animate({opacity:1.00},reasonFloatTime,"easeInSine");
	}
	
	$(iconObject).animate(
		{top:targetFloatValue+"px"},reasonFloatTime,reasonFloatEase,
		function(){ reasonFloatIconHelper(iconObject,shadowObject,reasonNumber,(floatDir * -1));}
	);
}


// <- FUNCTIONS //



// JQUERY EVENTS (and such) -> //


// Navigation Hovering


$("#nav").hover(
	function(){
		// Open the message background
		navOpen(true);
		// debugAlert("enter");
	},
	function(){
		// Close the background, using a timer.
		navClose(true,true);
		// debugAlert("leave");
	}
);

// Navigation Link Hovering
$(".nav_link").hover(
	function(){
		// Show the message
		navShowHideMessage(true,true,$(this).attr("value"));
		// Change the color of the text
		$(this).children().children("b").css("color","#85d750");
		$(this).children().children("b").css("textDecoration","underline");
		// Show the color icon:
		navShowHideColorIcon(true,$(this));
	},
	function(){
		// Hide the message, using a timer.
		navShowHideMessage(false,true,$(this).attr("value"));
		// Change the text color
		$(this).children().children("b").css("color","#77c148");
		$(this).children().children("b").css("textDecoration","none");
		// Hide the color icon:
		navShowHideColorIcon(false,$(this));
	}
);


// Image Gallery Hover
$("img.gallery").hover(
	function() {
		$(this).css("border-color","#0099cc");
	},
	function() {
		$(this).css("border-color","#dddddd");
	}
);

// Reasons Section
$(".concrete_reason").hover(
	function() {
		reasonHoverOver($(this));
	},
	function() {
		reasonHoverOut($(this));
	}
);

 
// Initialization!
$(document).ready(
	function() { 	
		// Navigation:
		
		// navShowHideMessage(false,false,"all");
		// navClose(false,false);
		
		
		// Reason:
		reasonInitialize();
	}	
);


 
// <-- JQUERY EVENTS (and such) //

