
function displayTab(panel, objects) 
{ 
   var panels = objects["panels"];
   var tabs = objects["tabs"];
   var current = 0;
   var state = "";
   var savedPanel = 0;
   // Restore state
   var toolbarCookie = getCookie("Speerio_FileManager_Toolbar_" + objects["controlId"]);
   if (toolbarCookie)
   {
  	   savedState = toolbarCookie.split(";");
  	   savedPanel = parseInt(savedState[0]);
	   if (savedState.length > 1)
	       state = savedState[1];
   }

   if (panel == -1) 
   {
       // First time page is loaded, don't
       // toggle tab shade
       panel = savedPanel;
       savedPanel = -1;
   }
      
   for (var i = 0; i < panels.length; i++) 
   {
       var currPanel = document.getElementById(panels[i]);
       if (currPanel)
       {
  			currPanel.style.display = "none";
   			if (i != panel)
				document.getElementById(tabs[i]).parentNode.className = "";
			else
			{
				if (i == savedPanel)
					state = (state == "block" ? "none" : "block");
		   
				currPanel.style.display = state;
				document.getElementById(tabs[i]).parentNode.className = "selected";
				current = i;
			}
		}
   }

   memorizeState(current+";"+state,"Speerio_FileManager_Toolbar_" + objects["controlId"]);
   eval(objects["controlId"] + "_setScrollerHeight()"); 
}

function toggleFolderExplorer(containerId,clientId,init)
{
	var explorerCell = document.getElementById(containerId);

	if (explorerCell)
		{
			var cookieName = "Speerio_FileManager_Explorer_" + clientId;
			var state = getCookie(cookieName) ;
			if ((state == "block") || (state == "none"))
			{
				if (init)
					explorerCell.style.display = state;
				else
					explorerCell.style.display = (state == "none" ? "block" : "none");
			}	
			else
				explorerCell.style.display = "block";
			memorizeState(explorerCell.style.display,cookieName);
		}
}

function memorizeState(obj,cookieName)
{
	today =	new	Date();
	setCookie(cookieName,obj,new Date(today.getFullYear(),	today.getMonth() + 1, today.getDate()));				
}

function localizeText(label,argList)
{
    var argLength = argList.length;
    for(var a=0;a<argLength;a++)
    {
        var re = new RegExp("\\{" + a + "\\}","gi");
        label = label.replace(re, argList[a]);
    }     
    
    return(label);
}

function checkEnter(event, clickHandlerId)
{ 	
    var NS4 = (document.layers) ? true : false;
	var code = 0;	
	if (NS4)
		code = event.which;
	else
		code = event.keyCode;
	if (code==13) 
	{
		var clickHandler = document.getElementById(clickHandlerId);
		clickHandler.click();
		return(false);
	}
}

function setResizeFields(optionId, widthId, heightId, objects)
{
   objOptionList = document.getElementById(optionId);
   objWidth = document.getElementById(widthId);
   objHeight = document.getElementById(heightId);
   var selection = objOptionList.options[objOptionList.selectedIndex].value;
   objHeight.value = "";
   objWidth.value = "";
   switch(selection)
   {
		case "WidthAutoHeight":
			objHeight.value = objects["autoStaticText"];
			objHeight.disabled = true; 
			objWidth.disabled = false;
			break;
		case "HeightAutoWidth":
			objWidth.value = objects["autoStaticText"];
			objWidth.disabled = true;
			objHeight.disabled = false;
			break;
		case "WidthHeightExact": 
			objWidth.disabled = false;
			objHeight.disabled = false;
			break;
		case "CropAnchorTop":
		case "CropAnchorCenter":
		case "CropAnchorBottom":
		case "CropAnchorLeft":
		case "CropAnchorRight":
			objHeight.disabled = false;
			objWidth.disabled = false;
			break;
   }
}

// Begin: http://www.webreference.com/js/column8/functions.html
/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}


/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


/*
   name - name of the cookie
   [path] - path of the cookie (must be same as path used to create cookie)
   [domain] - domain of the cookie (must be same as domain used to
     create cookie)
   path and domain default if assigned null or omitted if no explicit
     argument proceeds
*/

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// End: http://www.webreference.com/js/column8/functions.html

function launchMetaDataEditor(objects)
{
	showDialog(objects["dataUrlReadWrite"] + "&dialog=meta", 800, 575, true);
}

function launchPhotoViewer(objects, photoCount)
{
	showDialog(objects["dataUrlReadOnly"] + "&dialog=photo", 850, 475);
}

function launchAudioPlayer(objects,audioCount)
{
	showDialog(objects["dataUrlReadOnly"] + "&dialog=audio", 500, 330);
}

function launchTextEditor(objects,fileName)
{
	showDialog(objects["dataUrlReadWrite"] + "&dialog=editor&fileName=" + fileName, 540, 540);
}

function showDialog(url, w, h, resize)
{
	var	dialogWidth =	w;
	var	dialogHeight =	h;
	var	dialogLeft =	(screen.width -	dialogWidth) / 2;
	var	dialogTop =	(screen.height - dialogHeight)	/ 2;

	// In IE, with XP SP2, this does not work unless there is a Postback.
	var	uploadWin =	window.open(url,"_blank",(resize ? "resizable=yes,scrollbars=yes," : "") + "height="+dialogHeight+",width="+dialogWidth+",top="+dialogTop+",left="+dialogLeft);
	return(false);
}

// Returns the HTML for a checkbox that will toggle all the checkboxes
// in the same column
function formatGridHeaderCheckbox(gridId, columnNumber)
{
   var checkboxControllerId = getCheckboxControllerId(gridId);
   var select = getCookie(checkboxControllerId);
   var checkboxHtml = "<input type=\"checkbox\" id=\"" + checkboxControllerId + "\" " + (select == "checked" ? "checked " : "") + "onClick=\"gridCheckboxController_Click(this, " + gridId + "," + columnNumber + ")\">";
   return(checkboxHtml);
}

function formatGridColumn(columnName, dataItem)
{
	if (!dataItem) return;
	if (!dataItem.Table) return;
	var formatString = "";
	var dataValue = dataItem.GetMember(columnName).Value;
	var gridId = dataItem.Table.Grid.Id;
	switch(columnName)
	{
		case "GridIcon":
			 if (dataValue == "")
				 formatString = eval(gridId + "_folderIconPath");
			 else
				 formatString = eval(gridId + "_fileIconPath") + dataValue;
			 formatString = "<img align=\"absmiddle\" src=\"" + formatString + "\">";
			 break;
		case "FormattedFilename":
			 formatString = dataValue;
			 break;
		case "Modified":
			 formatString = dataValue;
			 break;
		case "FileSize":
			 formatString = dataValue;
			 break;
		case "ItemName":
			 formatString = dataValue;
			 break;
		case "Clicks":
			formatString = (dataValue == 0 ? "" : dataValue);
			break;
		case "Size":
			 formatString = dataValue;
			 break;
	}
	return(formatString);
}

function formatSliderPopup(dataItem)
{
	var slider = "<div class=\"Speerio-FileManager-GridSliderPopup\">";
	slider += "<div class=\"Speerio-FileManager-GridSliderPopupContents Normal\">";
	slider += formatGridColumn("GridIcon", dataItem) + " " + formatGridColumn("ItemName", dataItem) ;
	slider += "<p>" + formatGridColumn("Modified", dataItem) + "<br>" + formatGridColumn("Size", dataItem) + "&nbsp;<br><br>";
	slider += "<b>Page " + (dataItem.PageIndex + 1) + (dataItem.Table.Grid.PageCount ? " of " + dataItem.Table.Grid.PageCount : "") + "</b></p>";
	slider += "</div></div>";											
	return(slider);
}

function formatSliderPopupBasic(dataItem)
{
	var slider = "<div class=\"Speerio-FileManager-GridSliderPopupBasic\">";
	slider += "<div class=\"Speerio-FileManager-GridSliderPopupContents Normal\">";
	slider += "<b>Page " + (dataItem.PageIndex + 1) + (dataItem.Table.Grid.PageCount ? " of " + dataItem.Table.Grid.PageCount : "") + "</b></p>";
	slider += "</div></div>";											
	return(slider);
}

// Sets the checkbox values of a column based on the state of the
// checkbox controller for that column.
function gridCheckboxController_Click(grid, select)
{
      var itemIndex = 0;
      var gridItem;
      while(gridItem = grid.Table.GetRow(itemIndex))
      {
		 var checkbox = document.getElementById("checkbox_" + grid.Id + "__" + itemIndex);
		 if (checkbox)
		 {
			if (checkbox.checked != select)
				checkbox.click();
		 }
 	     itemIndex++;
      }
}


function renderMiniToolbar(grid)
{
	return(eval(grid.Id + "_toolbar()"));
}

function renderPagerInfo(grid)
{
	return(eval(grid.Id + "_pagerInfo(grid)"));
}

function renderPageSizeSelector(grid)
{
	return(eval(grid.Id + "_pageSizeSelector()"));
}

function miniToolbar_click(toolbarButtonId, toolbarFieldId, button, message, targetItem)
{
	if (button == "ButtonDelete")
	{
		if (!confirm(message))
			return(false);
	}

	var toolbarButton = document.getElementById(toolbarButtonId);
	var toolbarField = document.getElementById(toolbarFieldId);
	if ((toolbarButton) && (toolbarField))
	{
		toolbarField.value = button + (targetItem ? "|" + targetItem : "");
		toolbarButton.click();		
	}
	return(false);
}

function changeFolder(folder, objects)
{
	var navField = document.getElementById(objects["navigateDirNameId"]);
	var navButton = document.getElementById(objects["buttonNavigateId"]);
	if (navField && navButton)
	{
		navField.value = objects["currentPath"];
		if (navField.value.substring(navField.value.length-1) != "/")
			navField.value += "/";
		navField.value += folder;
		navButton.click();
	}
}


function dumpProps(obj, parent) {
   // Go through all the properties of the passed-in object 
   for (var i in obj) {
      // if a parent (2nd parameter) was passed in, then use that to 
      // build the message. Message includes i (the object's property name) 
      // then the object's property value on a new line 
      if (parent) { var msg = parent + "." + i + "\n" + obj[i]; } else { var msg = i + "\n" + obj[i]; }
      // Display the message. If the user clicks "OK", then continue. If they 
      // click "CANCEL" then quit this level of recursion 
      if (!confirm(msg)) { return; }
      // If this property (i) is an object, then recursively process the object 
      if (typeof obj[i] == "object") { 
         if (parent) { dumpProps(obj[i], parent + "." + i); } else { dumpProps(obj[i], i); }
      }
   }
}

