var deleteListeners = new Array();
function addDeleteListener(listener) {
	deleteListeners.push(listener);
}

function addToCBListRight(cb, type, txt, index) {
	var owner = txt;
	var box = _div("blockItem");	
	var thumbFrameDiv = _div(null,"rtThumbDiv");
	var thumbTitleDiv = _div(null, "rtThumbTitleDiv");
	var url = "watch.htm?id="+cb.id;
	
	box.appendChild( thumbTitleDiv );
	
	var cbTitle = smileyEscape(cb.caption);	
	cbTitle = capString(cbTitle, 25);
	thumbTitleDiv.appendChild(_a(url, cbTitle));	
	
	var i = getSearchThumbNail(cb);
	i.setAttribute("id", "blockImg");
	
	createStatusHover(i, cb.email, cb.status);
	

	var a = _a(url);
	a.appendChild(i);
	//td.appendChild(a);
	box.appendChild(thumbFrameDiv);
	thumbFrameDiv.appendChild(a);
	
	var txt = _div("blockText");		
	if( cb.screen_name )
  {
  	var nameOutput = capString(cb.screen_name, 12);
    var a = _a("userHome.htm?id=" + cb.screen_name, nameOutput);
	  txt.appendChild(a);
	  txt.appendChild(_br());
	}
	if( cb.modified )
  {
  	txt.appendChild(textNode(cb.modified));
		txt.appendChild(_br());
  }
  txt.appendChild(textNode("Frames: " + cb.count));
	
	if (cb.mine) {
		createBlockActions(txt, cb.id, type, owner, index, cb.caption);
	}
	box.appendChild(txt);
	return box;
}

function createBlockActions(div, id, type, owner, index, title){
	div.appendChild(_br());

	div.appendChild(_a("monitor.htm?id="+id, "Manage"));
	
	div.appendChild(_br());

	div.appendChild(_a("upload.htm?id="+id, "Upload"));

	div.appendChild(_br());

	div.appendChild(_a("settings.htm?id="+id, "Settings"));
}

function createNext(div, currentPage, total, pageCount, length, callback){
	if (currentPage != (pageCount - 1) && total > 0) {
		var a = _a(null, ">");
		a.setAttribute("id", "pageNumber");
		callback(a, (currentPage + 1) * length);
		div.appendChild(a);
	}
}
function createPageNumbers(div, currentPage, pageCount, length, callback){
	if (pageCount > 1) {
		newPageNumber(div, 0, currentPage, length, callback);
		
		// show start elipse?
		if(currentPage > 3 && pageCount > 7){
			newPageElipse(div);	
		}
		
		var delta = 1;
		var count = pageCount - 2;
		if (pageCount > 6) {
			count = 5;
			if (currentPage >= 3) {
				delta = currentPage - 2;
			}
			delta = Math.min(delta, pageCount - count);
			if( pageCount - currentPage <= 3){
				--delta;
			}
			
		}
		for (var i = 0; i < count; ++i) {
			var ii = i + delta;
			newPageNumber(div, ii, currentPage, length, callback);
		}
		
		// show end elipse?
		if(pageCount - currentPage > 4 && pageCount > 7){
			newPageElipse(div);	
		}
		
		newPageNumber(div, pageCount - 1, currentPage, length, callback);
	}	
}
function createPrevious(div, currentPage, total, length, callback){
	if (currentPage != 0 && total > 0) {
		var a = _a(null, "<");
		a.setAttribute("id", "pageNumber");
		callback(a, (currentPage - 1) * length);
		div.appendChild(a);
	}
}
function createStatusHover(div, email, status){
	var hover = "";
	if(email != null){
		hover += "<strong>" + email + "</strong><br />";
	}
	if(status == "C"){
		hover += "Not Accepting Pix";
	}else if(status == "P"){
		hover += "Pix Pending Approval";	
	}
	if(hover != ""){
		div.onmouseover = function(){overlib(hover);};
		div.onmouseout = function(){nd();};
	}	
}
function doUserClick(userId) {
	window.location = "userHome.htm?id=" + userId;
}
function fireDeleteEvent(id) {
	for (var i = 0; i < deleteListeners.length; ++i) {
		var deleteCallback = deleteListeners[i];
		deleteCallback(id);
	}
}


function populateSearchResultsRight(results, isOwner) {
  isOwner = isOwner || false;
	var data = results.DATA;	
	var cbList = getElement('right');
	killChildren(cbList);
	var callback = function(anchor, index) {
		anchor.href = "#";
		anchor.onclick = function () {
			rightSearch(results.S_TYPE, results.S_TXT, index);
		}
	}
	var tpaging = createPagingDiv("pagingDivTop", results.START, results.PERPAGE, results.TOTAL, callback);
	cbList.appendChild(tpaging);
	
	var blocksBox = _div("blocksBox");
	cbList.appendChild(blocksBox);
	if(isOwner && data.length == 0){
		var startBox = _div("startBox");
		startBox.innerHTML = "<a href='start.htm' id='saveItButton'>Make a cellblock</a> After you make some cellblocks, they'll show up here when you log in.";
		blocksBox.appendChild(startBox);
	}
	
	for (var i = 0; i < data.length; ++i) {
		var cb = data[i];
		var cBox = addToCBListRight( cb, results.S_TYPE, results.S_TXT, results.START);
		blocksBox.appendChild(cBox);
	}
	var bpaging = createPagingDiv("pagingDivBottom", results.START, results.PERPAGE, results.TOTAL, callback);
	cbList.appendChild(bpaging);
}

function searchLink(id, type, txt, index) {
	var link = "watch.htm?id=" + id; 
	return link;
}
function newPageElipse(div){
	var styleSpan = document.createElement("span");
	styleSpan.setAttribute("id", "pageElipse");
	styleSpan.appendChild(textNode("..."));
	div.appendChild(styleSpan);
}
function newPageNumber(div, pageNumber, currentPage, length, callback){
	if (pageNumber == currentPage) {
		var styleSpan = document.createElement("span");
		styleSpan.setAttribute("id", "pageCurrent");
		styleSpan.appendChild(textNode( (pageNumber + 1)));
		div.appendChild(styleSpan);
	} else {
		var a = _a(null, (pageNumber + 1));
		a.setAttribute("id", "pageNumber");
		callback(a, pageNumber * length);
		div.appendChild(a);
	}
}
function createPagingDiv(idz, start, length, total, callback) {
	var div = _div(idz, idz);
	var pageCount = parseInt(total / length);
	if (pageCount * length != total) {
		++pageCount;
	}
	var currentPage = parseInt(start / length);
	createPrevious(div, currentPage, total, length, callback);
	
	createPageNumbers(div, currentPage, pageCount, length, callback);
	createNext(div, currentPage, total, pageCount, length, callback);
	
	return div;
}
function getSearchThumbNail(cb) {
	var i;
	if(cb.locked) {
		i = image("lockBlock.jpg");
	} else if(cb.count <= 0) {
		i = image("voidBlock.jpg");
	} else {
		i = cbThumbNail(cb);
	}
	return i;
}
function delCB(id, title){
	var con1 = confirm("Delete this cellblock?  This cannot be undone.");
	if (con1){
		var capTitle = capString(title, 10);
		var con2 = confirm("WARNING: You are about to delete cellblock \""+capTitle+"\" and all of its contents.  Are you sure?");
		if(con2){
			cellBlockController.deleteCellblock(id, function (data){
				//fireDeleteEvent(id);
				window.location = _AX + "/userHome.htm";
			});
		}
	}
}
function cbSearch(params) {
	var txt = getElement("searchText").value;
	var type = "all";//getElement("searchType").value;
	window.location=searchUrl(type, txt, 0);
}
function searchUrl(type, txt, index) {
	var url = searchFwdUrl(type, txt, index);
	return _AX + '/searchResults.htm?' + url;
}
function searchFwdUrl(type, txt, index) {
	var url =  "type="+type+"&txt="+txt+"&index="+index;
	return url;
}
function submitRequest(update) {
	var error = 0;
	var propsMap = {};
	
	var errElsToClear = new Array();
	errElsToClear.splice(errElsToClear.length,0,"cbTitleError");
	errElsToClear.splice(errElsToClear.length,0,"cbPassError");
	clearElements(errElsToClear);
	
	if(update == null){
		error += takenCBEmail();
	}
	error += validCBTitle();
	
	if(error == 0){
		// email and categories only specified on start
		var email = null;
		
		if(update == null){
			var emailEntry = getElement("cbEmail");
			email = emailEntry.value;
			if(takenCBEmail() == 1){
				return;	
			}
		}
		// if password protected, get it
		var password = null;
		var passErr = getElement("cbPassError");
		var protectAccess = getElement("protectAccess");
		if (protectAccess.checked) {
			password = strTrim(getElement("passProtect").value);
			if(isErrorPassword(password)){
	   			passErr.appendChild(textNode("*"));
				alert("Please enter a password for your cellblock");
				return;
			} else {
				password = escape(password);
				//alert("Escaped password = " + password);
			}
		} 
		
		// these will be specified for start or update
		propsMap.preview = getPreview();
		propsMap.title = getElement("cbTitle").value;
		propsMap.msg = getElement("cbOwnerMsg").value;
		propsMap.tags = getElement("cbTags").value;
		propsMap.upload = getUpload();
		propsMap.contrib = getContribute();
		propsMap.playMode = getPlayMode();
		propsMap.open = getElement("open").checked;
		propsMap.password = password;
		
		// notify props
		if(getElement("ncn_enabled") != null) {
			propsMap.ncn_count = getElement("ncn_count").value;
			propsMap.ncn_email = getElement("ncn_email").value;
			propsMap.ncn_enabled = getElement("ncn_enabled").checked;
		}
		// acknowledgment props
		// commented out sender acknowledgment stuff MKB 3/16/07
		if(getElement("sae_enabled") != null) {
			propsMap.sae_subject = getElement("sae_subject").value;
			propsMap.sae_body = getElement("sae_msg_body").value;
			propsMap.sae_enabled = getElement("sae_enabled").checked;
		}
		
		// whitelist props
		if(getElement("cbw_enabled") != null) {
			propsMap.cbw_emails = getElement("cbw_emails").value;
			propsMap.cbw_enabled = getElement("cbw_enabled").checked;
		}
		if(update == null){
			overlib("Creating your cellblock ... please wait.");
			cb = { callback:propsCreated, errorHandler:propsError};
			propsMap.email = email;
			cellBlockController.create(propsMap, cb);
		}
		else {
			//
			overlib("Updating your cellblock ... please wait.");
			cb = { callback:propsUpdated, errorHandler:propsError};
			propsMap.aka = update;
			cellBlockController.update(propsMap, cb);
		} 
	}
}
function getContribute(){
	var gens = document.getElementsByName("cbContribute");
	for (var i=0; i < gens.length; i++){
		if (gens[i].checked) {
     		return(gens[i].value);
   		}
  	}
	return "";
}
function getUpload(){
	var gens = document.getElementsByName("cbUpload");
	for (var i=0; i < gens.length; i++){
		if (gens[i].checked) {
     		return(gens[i].value);
   		}
  	}
	return "";
}
function getPlayMode(){
	var gens = document.getElementsByName("playMode");
	for (var i=0; i < gens.length; i++){
		if (gens[i].checked) {
     		return(gens[i].value);
   		}
  	}
	return "";
}
function getPreview(){
	var gens = document.getElementsByName("cbPreview");
	for (var i=0; i < gens.length; i++){
		if (gens[i].checked) {
     		return(gens[i].value);
   		}
  	}
	return "";
}
function propsCreated(data){
	nd();
	if(data != null){
		window.location = _AX + "/watch.htm?id="+data;
	}
	else{
		propsError();	
	}
}
function propsUpdated(data){
	nd();
	if(data != null){
		window.location = _AX + "/watch.htm?id="+data;
	}
	else{
		propsError();
}
}
function propsError(data){
	if (data == "MAX_TOTAL") {
		nd();
		alert("You have reached the limit of 25 total cell blocks.");	
	} else {
		overlib("There was an error creating your cellblock.  Please try again.", TIMEOUT, 2000);
	}
}
function isErrorPassword(password){
	// alpha numeric w/ underscore, dash and period allowed
//	var regExp = /^[a-zA-Z0-9\_\-\.]+$/
//	if(password.search(regExp) == -1 ){
//		return 1;	
//	}	
	if(password == "") {
		return 1;
	}
	return 0;
}
function starItem(name){
	var div = getElement(name);
	killChildren(div);
	div.appendChild(textNode("*"));
}
function clearItem(name){
	var div = getElement(name);
	killChildren(div);
}
function clearElements(elArray) {
	for (var i = 0; i < elArray.length; i++) {
		var thisEl = getElement(elArray[i]);
		killChildren(thisEl);
	}
}
