window.menuY = 0;				// The last saved Y-position of the Idea Folio menu

function showPanel(targetName, timerLength) {
	if (document.getElementById) {
	
		if (!timerLength) { timerLength = 3000; }
	
		menuEl = document.getElementById('tabcontent');
		if (menuEl) {	
			// If the Idea Folio menu exists, return it to the top of the window
			var newY = getScrollOffset().y;
			menuEl.style.paddingTop = newY + 'px';
		}
		
		addLog('Showing ' + targetName + "\n");
		target = document.getElementById(targetName);
		
		closeAllPanels();
		
		// Make sure the panel is not already extended
		if (target.style.display != 'block') {
			target.style.display = 'block';
			
			// Create a new transition effect
			fullySupported = self.innerHeight || (document.documentElement && document.documentElement.clientHeight);
			if (fullySupported) {
				window.showEffect = new fx.Height(target, {duration: 300});
				window.showEffect.toggle();
			} else {
				target.style.display = 'block';
				target.style.height = 'auto';
			}
		}
		
		clearTimer(targetName);
		startTimer(targetName, timerLength);
		target.onmouseover = function() { clearTimer(targetName); }
		target.onmouseout = function() { startTimer(targetName, timerLength); }
	}
}

function horzSlidePanel(targetName) {
	if (document.getElementById) {
	
		target = document.getElementById(targetName);
		
		// Make sure the panel is not already extended
		if (target.style.display != 'block') {
			// Show the panel and shrink it, just in case
			target.style.display = 'block';
			target.style.width = '0';
			
			// Create a new transition effect
			fullySupported = self.innerWidth || (document.documentElement && document.documentElement.clientWidth);
			//if (fullySupported) {
			if (false) {
				var showEffect = new fx.Width(target, {duration: 300});
				showEffect.toggle();
			} else {
				target.style.display = 'block';
				target.style.width = 'auto';
			}
		}
		
		//startTimer(targetName);
		//target.onmouseover = function() { clearTimer(targetName); }
		//target.onmouseout = function() { startTimer(targetName); }
	}
}

function closeAllPanels() {
	if (document.getElementsByClassName) {
		allPanels = document.getElementsByClassName("foliopanel");
		
		for (var i = 0; i < allPanels.length; i++) {
			closePanel(allPanels[i].id);
		}
	}
}

function closePanel(panelID) {
	if (document.getElementById) {
		var targetPanel = document.getElementById(panelID);
		
		targetPanel.style.display = 'none';
		targetPanel.style.height = '0';
	}
}
	
function startTimer(targetName, timerLength) {
	if (!timerLength) { timerLength = 3000; }
	clearTimer(targetName); // Clear the timer, if it exists
	var regExp    = new RegExp("\\.",  "gi"); 
	newTargetName = targetName.replace(regExp, "_");
	command = "window." + newTargetName + "Timer = setTimeout(\"hidePanel('" + newTargetName + "');\", " + timerLength + ");";
	addLog('Starting ' + targetName + 'Timer' + "\n");
	eval(command);
}

function scrollMenu() {
	var posy = getScrollOffset().y;
	menuEl = document.getElementById('tabcontent');
	
	if (menuEl) {
		menuEl.style.paddingTop = '0px';
		window.menuY = 0;
	}
}

function clearTimer(targetName) {
	var regExp    = new RegExp("\\.",  "gi"); 
	newTargetName = targetName.replace(regExp, "_");
	command = "clearTimeout(window." + newTargetName + "Timer)";
	addLog('Clearing ' + targetName + 'Timer' + "\n");
	eval(command);
}

function hidePanel(targetName) {
	if (document.getElementById) {
	
		target = document.getElementById(targetName);
		
		if (target.style.display && target.style.display == 'block') {
			var posy = getScrollOffset().y;
			if (posy != window.menuY) {
				scrollMenu();
			}
		
			target = document.getElementById(targetName);
			
			tab = document.getElementById('tab-' + targetName);
			
			if (tab) { tab.className = ''; }
			
			fullySupported = self.innerHeight || (document.documentElement && document.documentElement.clientHeight);
			if (fullySupported) {
				var showEffect = new fx.Height(target, {duration: 300, onComplete: function()
				{
					target.style.display = 'none';
				}});
				showEffect.toggle();
			} else {
				target.style.display = 'none';
				target.style.height = 0;
			}
		}
	}
}

function resetItemImageWidth() {
	if (document.getElementById) {
		itemImg = document.getElementById('FolioTab_ItemAddPanel_Image');
		itemImg.width = 1;
		itemImg.height = 1;
		itemImg.style.width = '1px';
		itemImg.style.height = '1px';
		itemImg.style.width = 'auto';
		itemImg.style.height = 'auto';
	}
}

function addLog(text) {
	if (document.getElementById && document.getElementById('log')) {
		logWin = document.getElementById('log');
		logWin.value = logWin.value + "-- " + text;
		logWin.scrollTop = logWin.scrollHeight;
	}
}

function saveToFolio(target, e) {
	// Animate the save icon
	savedAnimation(e); 
	// Give the animation a bit to finish, then expand the panel
	closePanel('FolioTab_ItemAddPanel');
	setTimeout("showItemPanel('" + target.id + "');", 200);
	
	return false;
}

function removeFromFolio(target, e) {
	showPanel('FolioTab_ItemDeletePanel');
	
	return false;
}

function showItemPanel(targetID) {

	if (document.getElementById) {
	
		showPanel('FolioTab_ItemAddPanel');	// Expand the panel

	}
	
	return false;
}

function folioSideBarItemAdd()
{
	// set id tab and new name and submit
	if (document.forms['Folio.ItemAdd'])
	{
		if (document.forms['Folio.ItemAdd'].elements['Folio.ItemAdd.tab_id'])
			document.forms['Folio.ItemAdd'].elements['Folio.ItemAdd.tab_id'].value = document.forms['Folio.ChangeCurrentTab'].elements['Folio.ChangeCurrentTab.tab_id'].value;
		document.forms['Folio.ItemAdd'].submit();
	}
}

function removeChildNodes(node) {
   for (var i = node.childNodes.length - 1; i >= 0; i--) {   
      node.removeChild(node.childNodes[i]);
   }
}

function in_array(haystack, needle) {
	retVal = false;
	
	for (i = 0; i < haystack.length; i++) {
		if (needle == haystack[i]) retVal = true;
	}
	return retVal;
}

/* - - - - COPIED IN FROM PHOTO GALLERY JS - - - - */

var myWindowWidth = getWindowDimensions()['x'];
var finalSavedX = Math.ceil(myWindowWidth / 2) + 150;
var finalSavedY = 0;
var curSavedX = 0;
var curSavedY = 0;
var changeX = 0;
var changeY = 0;
var animationSpeed = 8;
var saveAni;
var mySavedIcon;
	
function savedAnimation(e) {
	mySavedIcon = document.getElementById("savedIcon");
	
	finalSavedY = getScrollOffset().y;
	
	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		offsets = getScrollOffset();
		posx = e.clientX + offsets.x;
		posy = e.clientY + offsets.y;
	}
	
	curSavedX = (posx - 6);
	curSavedY = (posy - 6);
	mySavedIcon.style.left = curSavedX + "px";
	mySavedIcon.style.top = curSavedY + "px";
	mySavedIcon.style.display = "block";
	
	changeX = Math.ceil((finalSavedX - curSavedX) / animationSpeed);
	changeY = Math.ceil((curSavedY - finalSavedY) / animationSpeed);
	
	saveAni = window.setTimeout("updateSavedIcon()", 50);
}

function updateSavedIcon() {
	if ((curSavedX < finalSavedX) || (curSavedY > finalSavedY)) {
	
		curSavedX = curSavedX + changeX;
		curSavedY = curSavedY - changeY;
		
		//alert(curSavedX + ", " + curSavedY)
		mySavedIcon.style.left = curSavedX + "px";
		mySavedIcon.style.top = curSavedY + "px";
		setTimeout("updateSavedIcon()", 50);
	} else {
		mySavedIcon.style.display = "none";
		window.clearTimeout(saveAni);
	}
}

/* - - - - UTILITY POSITION-FINDING FUNCTIONS FROM: quirksmode.org - - - - */

function getWindowDimensions() {
	var x,y;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.documentElement)
		// Explorer 5 ? mode
	{
		x = document.documentElement.offsetWidth;
		y = document.documentElement.offsetHeight;
		//alert(x + ', ' + y);
	}
	else if (document.body) // other Explorers
	{
		alert('document.body detected');
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	return { x: x, y: y };
}

function getScrollOffset() {
	var x,y;
	if (self.pageYOffset) // all except Explorer
	{
		x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	
	return { x: x, y: y };
}
