
var objGrid = null;
var currentSearchTab = null;

function goHome()
{
	document.location.href = "app";
}

function doConvert(iCol, strNum)
{
    return strNum.replace(/<[^>]*>/g, "");;
}

function searchGrid(grid, form)
{
	if (!grid) 
	{
		return;
	}
	grid.setFilter({text: form.value});
}

function clearSearchGrid(grid, formName)
{
	if (!grid) 
	{
		return;
	}
	document.getElementsByName(formName)[0].value = '';
	grid.setFilter({text: ""});
}


// FUNCTIONS FOR AJAX SEARCH

function showAjaxIndicator()
{
	document.getElementById('ajaxIndicator').style.display="block";
}

function hideAjaxIndicator()
{
	document.getElementById('ajaxIndicator').style.display="none";
}


function searchTabClicked(tab, mode)
{
	currentSearchTab.className = 'searchEntry';
	tab.className = 'searchEntrySelected';
	
	document.getElementById('searchForm').m.value = mode;
	currentSearchTab = tab;
}


function updateSearchFieldFromAutoComplete(a, b) 
{ 
	showAjaxIndicator();
	var link = b.getElementsByTagName("a");
	if (link.length == 1)
	{
		document.location.href = link[0].href;
		return false;
	}
	if (document.getElementById('searchField0').value == "")
	{
		return false;
	}
	document.getElementById('searchForm').h.value = document.getElementById('c').checked;
	document.getElementById('searchForm').submit(); 
}


function checkEmpty(form)
{
	form.h.value = document.getElementById('c').checked;
	return form.s.value == "";
}


function showRevisions(wikiName, masterId)
{
	var pageUrl = "/page/WikiRevisions?" + "n=" + escape(wikiName) + "&m=" + masterId; 
	win = new Window('revisionWindow', {url: pageUrl, className: "alphacube", title: "Wiki Revisions", 
		resizable: false, minimizable: false, maximizable: false, showEffectOptions: {duration: 0.1}, 
		hideEffectOptions: {duration: 0.1}, width:600, height: 400, showEffect: Element.show});

	//win.setAjaxContent(url, {asynchronous: true, onComplete: hideAjaxIndicator}, true, true);
	win.setDestroyOnClose();
	win.showCenter(true);
}

function showLibModify()
{
	showAjaxIndicator();
	win = new Window('libModifyWindow', {className: "alphacube", title: "Modify Favorites", 
		resizable: false, minimizable: false, maximizable: false, showEffectOptions: {duration: 0.1}, 
		hideEffectOptions: {duration: 0.1}, width:800, height: 260, showEffect: Element.show});
	win.setAjaxContent("/page/AjaxSearchMod", {asynchronous: true, onComplete: hideAjaxIndicator}, true, true); 
	win.setDestroyOnClose();
}

function showRecentClasses()
{
	showAjaxIndicator();
	win = new Window('recentClassesWindow', {className: "alphacube", title: "Recent Classes Viewed", 
		resizable: false, minimizable: false, maximizable: false, showEffectOptions: {duration: 0.1}, 
		hideEffectOptions: {duration: 0.1}, width:400, height: 350, showEffect: Element.show}); 
	win.setAjaxContent("/page/AjaxRecentClasses", {asynchronous: true, onComplete: hideAjaxIndicator}, true, true); 
	win.setDestroyOnClose();
}

function showBookRecommendations(url)
{
	showAjaxIndicator();
	win = new Window('bookRecommendations', {className: "alphacube", title: "Book Recommendations", 
		resizable: false, minimizable: false, maximizable: false, showEffectOptions: {duration: 0.1}, 
		hideEffectOptions: {duration: 0.1}, width:800, height: 400, showEffect: Element.show}); 
	win.setAjaxContent(url, {asynchronous: true, onComplete: hideAjaxIndicator}, true, true); 
	win.setDestroyOnClose();
}

function showTextileHelp()
{
	showAjaxIndicator();
	win = new Window('recentClassesWindow', {className: "alphacube", title: "Textile Format Help", 
		resizable: false, minimizable: false, maximizable: false, showEffectOptions: {duration: 0.1}, 
		hideEffectOptions: {duration: 0.1}, width:600, height: 400, showEffect: Element.show}); 
	win.setAjaxContent("/page/TextileHelp", {asynchronous: true, onComplete: hideAjaxIndicator}, true, true); 
	win.setDestroyOnClose();
}

function showLibraryPreview(url)
{
	showAjaxIndicator();
	win = new Window('libraryPreview', {className: "alphacube", title: "Packages in Library", 
		resizable: false, minimizable: false, maximizable: false, showEffectOptions: {duration: 0.1}, 
		hideEffectOptions: {duration: 0.1}, width:800, height: 450, showEffect: Element.show});
	win.setAjaxContent(url, {asynchronous: true, evalScripts:true, onComplete: libraryPreviewLoaded}, true, true); 
	win.setDestroyOnClose();	
}

function libraryPreviewLoaded()
{
	hideAjaxIndicator();
	// Initialize grid
	  objLibraryPreviewGrid = new Zapatec.Grid({
	    convert: doConvert,
	    show_asis: true,
	    source: 'libraryPreviewSource',
	    sourceType: 'html',
	    theme: '/media/style/grid/winxp.css',
	    container: 'libraryPreviewGridContainer',
	    rowsPerPage: 20,
	    sortColumn: 0,
	    paginationContainer: 'libraryPreviewGridPager',
	    paginationContainer2: 'libraryPreviewGridPager2',
	    selectRows: false,
	    selectCells: false
	  });
}

function showPackagePreview(url)
{
	showAjaxIndicator();
	win = new Window('libraryPreview', {className: "alphacube", title: "Members in Package", 
		resizable: false, minimizable: false, maximizable: false, showEffectOptions: {duration: 0.1}, 
		hideEffectOptions: {duration: 0.1}, width:800, height: 450, showEffect: Element.show});
	win.setAjaxContent(url, {asynchronous: true, evalScripts:true, onComplete: packagePreviewLoaded}, true, true); 
	win.setDestroyOnClose();	
}

function packagePreviewLoaded()
{
	hideAjaxIndicator();
	  // Initialize grid
	  objPackagePreviewGrid = new Zapatec.Grid({
		convert: doConvert,
		show_asis: true,
		source: 'packagePreviewSource',
		sourceType: 'html',
		theme: '/media/style/grid/winxp.css',
		container: 'packagePreviewGridContainer',
		rowsPerPage: 20,
		sortColumn: 0,
		paginationContainer: 'packagePreviewGridPager',
		paginationContainer2: 'packagePreviewGridPager2',
		selectRows: false,
		selectCells: false
  });
}

// ********** PALLETTE JS *******************

function PaletteMoveOption(objSourceElement, objTargetElement, moveType)
{
	showAjaxIndicator();
	var aryTempSourceOptions = new Array();
	var aryTempTargetOptions = new Array();
	var x = 0;

	// Leave at least 1 library in search profile.
	if (moveType == false && objSourceElement.length == 1)
	{
		hideAjaxIndicator();
		return;
	}
	
	var elemSelected = 0;
	var selectCSVList = "";
	//looping through source element to find selected options
	for (var i = 0; i < objSourceElement.length; i++)
	{
		if (objSourceElement.options[i].selected)
		{
			//need to move this option to target element
			var intTargetLen = objTargetElement.length++;
			objTargetElement.options[intTargetLen].text = objSourceElement.options[i].text;
			objTargetElement.options[intTargetLen].value = objSourceElement.options[i].value;
			selectCSVList = selectCSVList + objTargetElement.options[intTargetLen].value + ",";
			
			elemSelected++;
		}
		else
		{
			//storing options that stay to recreate select element
			var objTempValues = new Object();
			objTempValues.text = objSourceElement.options[i].text;
			objTempValues.value = objSourceElement.options[i].value;
			aryTempSourceOptions[x] = objTempValues;
			x++;
		}
	}
	
	if (elemSelected == 0 || elemSelected == objSourceElement.length)
	{
		hideAjaxIndicator();
		return;
	}		

	// Handle ajax call.
	var opt = 
	{
		method: 'post',
		postBody: 'lib=' + selectCSVList + '&op=' + moveType,
		onSuccess: hideAjaxIndicator,
		on404: hideAjaxIndicator,
		onFailure: hideAjaxIndicator
	}
			
	new Ajax.Request('/page/AjaxProfileMod', opt);
	
	//sorting and refilling target list
	for (var i = 0; i < objTargetElement.length; i++)
	{
		var objTempValues = new Object();
		objTempValues.text = objTargetElement.options[i].text;
		objTempValues.value = objTargetElement.options[i].value;
		aryTempTargetOptions[i] = objTempValues;
	}

	aryTempTargetOptions.sort(sortPaletteByText);

	for (var i = 0; i < objTargetElement.length; i++)
	{
		objTargetElement.options[i].text = aryTempTargetOptions[i].text;
		objTargetElement.options[i].value = aryTempTargetOptions[i].value;
		objTargetElement.options[i].selected = false;
	}

	//resetting length of source
	objSourceElement.length = aryTempSourceOptions.length;
	//looping through temp array to recreate source select element
	for (var i = 0; i < aryTempSourceOptions.length; i++)
	{
		objSourceElement.options[i].text = aryTempSourceOptions[i].text;
		objSourceElement.options[i].value = aryTempSourceOptions[i].value;
		objSourceElement.options[i].selected = false;
	}
}

function sortPaletteByText(a, b)
{
	if (a.text.toLowerCase() < b.text.toLowerCase()) {return -1}
	if (a.text.toLowerCase() > b.text.toLowerCase()) {return 1}
	return 0;
}

// ***************** Wiki Javadoc Scripts *****************

function convertToEntitiesWiki(str)
{
	return str;
}

function wikiCallback(form, value)
{
	// Get id of version input element using form's id
	var formid = form.id;
	var versionid = formid.substring(0, formid.indexOf('-')) + "_version";
	var version = document.getElementById(versionid).value;
	return "velemid=" + versionid + "&v=" + version + "&value=" + encodeURIComponent(value);
}


function reportWikiError(transport)
{
 	if (transport.status == 444)
 	{
 		alert("The wikidoc was modified by someone else. Your input will be displayed after you " +
 		    "acknowledge this alert. Copy your changes to the clipboard and click on Edit to load " + 
 		    "the latest wiki. You can then reapply your changes.");
 	}
 	var elementid = transport.getResponseHeader('velemid');
 	document.getElementById(elementid).value = transport.getResponseHeader('versionId');
}

function getNewWikiVersion(transport, element)
{
    document.getElementById(element.id + "_version").value = transport.getResponseHeader('versionId');
}

