
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }

function changeLeftColumn(catLinkId, accordianId) {
	var thisAccordian = document.getElementById(accordianId);
	var thisLink = document.getElementById(catLinkId);
	if (thisAccordian!=null && accordianId!="") {
		//if (thisAccordian.className == "accordianDiv closed")
			switchAccordian(accordianId);
		if (catLinkId!="") {
			var allCatLinks = dojo.html.getElementsByClassName("accordianLink", thisAccordian);
			for (x in allCatLinks) {
				allCatLinks[x].style.fontWeight="";
			}
			if (thisLink != null)
				thisLink.style.fontWeight = "bold";
		}	
	}
}

function editDeleteLinks(itemID, itemValue, itemType) {
	var editStr = "<a href=\"#\" onclick=\"editItemSetup('" + itemID + "', unescape('" + escape(itemValue) + "')); return false;\" >Edit</a><br/>";
	var deleteStr = "<a href=\"#\" onclick=\"deleteItem(" + itemID + ", '" + itemType + "'); return false;\" >Delete</a>"; 
	return editStr + deleteStr;
}

function editItemSetup(itemID, itemName) {
	var nameInput = document.getElementById("newItemName");
	var idInput = document.getElementById("itemID");
	if (nameInput!=null && idInput!=null) {
		nameInput.value = itemName;
		idInput.value = itemID;
		try{nameInput.focus();}catch(e){/*squelch for IE*/}
		document.getElementById("formTitle").innerHTML = (itemID=='') ? "New " + document.getElementById("formType").value : "Update " + document.getElementById("formType").value;
		setDisplay("submit_New", itemID=="");
		setDisplay("submit_Update", itemID!="");
	}
	setDisplay("formBox", true);
}

function fnSortItems(first, second)
{
	var str1 = first.getAttribute("date");
	var str2 = second.getAttribute("date");

	if (str1 == str2)
		return 0;
	if (str1 < str2)
		return -1;
	if (str1 > str2)
		return 1;
}

function fnSortCats(first, second)
{
	var str1 = first.getElementsByTagName("a")[0].innerHTML.trim().toLowerCase();
	var str2 = second.getElementsByTagName("a")[0].innerHTML.trim().toLowerCase();

	if (str1 == str2)
		return 0;
	if (str1 < str2)
		return -1;
	if (str1 > str2)
		return 1;
}

function loadTinyMCEContent(objId, content) {
	document.getElementById(objId).contentWindow.init(content, "saveFunction");
}

//For reordering images
function saveImageOrder(id, galleryID) {
	var orderString = junkdrawer.serializeList(document.getElementById(id));
	dojo.io.bind({
		url: "prebuilt/saveImageOrder.asp",
		method: "post",
		content: {
			orderString:  orderString, 
			galleryID:  galleryID
		},
		load: function(load, data, e) {
		//    alert(data);
			alert("Your ordering has been saved.");
			var url = window.location.href.replace("&amp;mode=reorder", "");
			window.location.href = url;
		}
	});
}

function setContentPaneURL(widgetId, url) {
	var widget = dojo.widget.byId(widgetId);
	if (widget!=null) 
		widget.setUrl(url);
}

function setDisplay(id, boolToDisplay) {
	var elt = document.getElementById(id);
	if (elt!=null) {
		if (boolToDisplay)
			elt.style.display = "";
		else
			elt.style.display = "none";
	}
}

function showHideList(list) {
    for (x in list) {
        var el = document.getElementById(list[x]);
        if (el!=null) {
            if ( el.style.display != 'none' ) 
				el.style.display = 'none';
			else 
				el.style.display = '';
		}
    }
}

function switchAccordian(id){
	var elt = document.getElementById(id);
	if (elt) {
		/*var accordianDivs = dojo.html.getElementsByClassName("item", elt, "div");
		for (x in accordianDivs) {
			if (accordianDivs[x].className.indexOf("open")!=-1) 
				accordianDivs[x].className = "accordianDiv closed";
			else if (accordianDivs[x].id == id) {
				accordianDivs[x].className = "accordianDiv open";								
			}
		}
		*/
		var itemDivs = dojo.html.getElementsByClassName("item", elt.parentNode, "div");
		for (x in itemDivs) {
			if (itemDivs[x].className.indexOf("open")!=-1) 
				itemDivs[x].className = "item";
			else if (itemDivs[x].parentNode.id == id) {
				itemDivs[x].className = "item open";
			}
		}
		
		var itemDivs = dojo.html.getElementsByClassName("content", elt.parentNode, "div");
		for (x in itemDivs) {
			if (itemDivs[x].parentNode.id == id && itemDivs[x].className.indexOf("Bottom")!= -1) {
			    itemDivs[x].className = "content list border borderBottom";
			}
			else if (itemDivs[x].parentNode.id == id) {
				itemDivs[x].className = "content list border";
			}
			else if (itemDivs[x].className.indexOf("Bottom")!= -1)
			    itemDivs[x].className = "content list border closed borderBottom";
			else
			    itemDivs[x].className = "content list border closed";
		}
	}
}

function updateTitleCount(id, diff) {
	elt = document.getElementById(id);
	if (elt!=null) {
		elt.innerHTML = parseInt(elt.innerHTML) + diff;
		return elt.innerHTML;
	}
}

//class names don't always work so change style
function toggleItem(elementId, show) {
	var item = document.getElementById(elementId);
	if(item) {
		if(show)
			item.style.display = "block";
		else
			item.style.display = "none";
	}
}

//generic TinyMCE creation function
function addEditor(elementId) {
	var editor = new tinymce.Editor(elementId, window.tinymceConfig);
	editor.render();
}

// Function: displayXHTMLContentBlock
// Params:  editorId - string - id of div/textarea for editor
//			width - number - width of WYSWIYG editor (optional)
// Description: Displays an XHTML editor inplace of a content block. Note that 
// the edit, cancel, submit buttons must be named with editorIdValue + Submit
function displayXHTMLContentBlock(editorId, width) {	
	
	addEditor(editorId);	
	
	//TODO: refactor this into CSS
	document.getElementById(editorId).parentNode.style.padding = '0 0 0 0';  //FF2/3 and IE styling
	document.getElementById(editorId + '_tbl').style.width= (width ? width + "px" : "506px"); //IE6 and Safari
	document.getElementById(editorId + '_tbl').style.cssFloat='left'; //FF2
	document.getElementById(editorId + '_tbl').style.marginBottom='20px'; //FF2	
	
	toggleItem(editorId + "Submit", true);  
	toggleItem(editorId + "Cancel", true); 
	toggleItem(editorId + "Edit", false);
	return false;//cancel event;
}

//cleans up an XHTML Editor and returns content block to its original settings
function removeXHTMLContentBlock(editorId) {
	toggleItem(editorId + "Submit", false);  
	toggleItem(editorId + "Cancel", false); 
	toggleItem(editorId + "Edit", true);
	
	tinyMCE.activeEditor.remove();//TODO: look at Tiny API and check on getting a specific editor
	
	//TODO: refactor this into CSS
	document.getElementById(editorId).parentNode.style.padding = '12px 12px 0';//FF2/3 and IE stylign
	return false;//cancel event
}