window.onload = initFunctions;

function initFunctions(){
	initRollovers();
}

if (window.attachEvent) window.attachEvent("onload", sfHover);
// http://www.htmldog.com/articles/suckerfish/dropdowns/
function sfHover() {
	var sfEls = document.getElementById("listActionsDropDown").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}

function toggleSideInfo(el){
	var block = document.getElementById(el); 
	var image = document.getElementById(el+"Button"); 
	
	if(block.style.display == "none"){
		block.style.display = "block";
		image.src = "/images/button-close.gif";
	}else{
		block.style.display = "none";
		image.src = "/images/button-open.gif";
	}	
};

//Tim written functions
//TODO: delete items as a javascript function doesn't check to see if permissions exists until deleteItem.php -- need to coordinate access check
function deleteItem(itemID, listID){
	if (confirm("Item deletion is permanent and cannot be undone. Are you sure you want to delete this item?")) {
		document.location.href = "deleteItem.php?itemID=" + itemID + "&listID=" + listID;
	}
}

function checkboxClick(listItemID, listID){
	document.location.href = "clickListItem.php?listItemID=" + listItemID + "&listID=" + listID;
}

function deleteList(listID){
 	if (confirm("List deletion is permanent and cannot be undone. Are you sure you want to delete this list?")) {
		document.location.href = "deleteList.php?listID=" + listID;
	}
}

function popupEmailList(listID){
 	reply = prompt("Please enter email recipents separated by a space and/or comma.");
	document.location.href = "emailList.php?listID=" + listID + "&emails=" + reply;
}

function ajaxNotice(content){

	if(content){
		var json_content = JSON.decode(content);

		if(!$('ajaxNotice')){
			new Element('div', {'id': 'ajaxNotice'}).inject($$('body')[0]);
		}
		
		$('ajaxNotice').set("text",json_content.msg);
		$('ajaxNotice').setStyle('background-color', "#fcff00");
		$('ajaxNotice').set('tween', {duration: 'long'});
		$('ajaxNotice').tween('background-color', '#fff');
		$('ajaxNotice').setStyle("opacity",1);
		
		var myFunction = function(){ $('ajaxNotice').fade("out");};
		myFunction.delay(2000);
	}
}

function escapeStringForURL(s){
	s = s.replace(/\\/g, "\\\\"); //replace backslash with escaped backslash
	s = s.replace(/\"/g, "\\\""); //replace backslash with escaped backslash
	return encodeURIComponent(s);
}