var accessArray = new Array();

function setupCreateEditLightbox(isNew, lightboxId, nameOld, clientOld)
{
	divHide('popLayer',true);
	
	if(readCookie("TrunkAccount") == "0")
	{
		location.href = serverUrl + "LightBoxes.aspx";
		return;
	}
	
	var popObject = new Object();
	
	accessArray = new Array();
	
	popObject.closeLowerAccess = true;
	popObject.darkOutBackground = true;
	
	var name = "";
	var client = "";
	
	if(isNew)
	{
		document.getElementById("popLayerHeadline").innerHTML = "<b>Create new lightbox</b>";
	}
	else
	{
		document.getElementById("popLayerHeadline").innerHTML = "<b>Edit lightbox</b>";
		
		name = nameOld;
		client = clientOld; 
	}

	popObject.content = '';
	popObject.content +=  '<table width="416" border="0" cellspacing="0" cellpadding="10">';
	popObject.content += '	<tr>';
	popObject.content += '		<td>';
	popObject.content += '		<br />';
	popObject.content += '		<table border="0" cellspacing="0" cellpadding="0">';
	popObject.content += '			<tr>';
	popObject.content += '				<td>Lightbox name</td>';
	popObject.content += '				<td><img src="gfx/_blank.gif" alt="" width="10" height="1" border="0"/></td>';
	popObject.content += '				<td colspan="3"><input type="text" id="newLightboxName" value="' + name + '" style="width: 260;"></td>';
	popObject.content += '			</tr>';
	popObject.content += '			<tr>';
	popObject.content += '				<td>Client</td>';
	popObject.content += '				<td><img src="gfx/_blank.gif" alt="" width="10" height="1" border="0"/></td>';
	popObject.content += '				<td colspan="3"><input type="text" id="newClient" value="' + client + '" style="width: 260;"></td>';
	popObject.content += '			</tr>';
		
	/*
	popObject.content += '			<tr>';
	popObject.content += '				<td colspan="5"><br>';
	popObject.content += '				<b>Access:</b>';
	popObject.content += '				<div id="newAccess">None</div>';
	popObject.content += '				<br>';
	popObject.content += '				<b>Add access:</b></td>';
	popObject.content += '			</tr>';
	popObject.content += '			<tr>';
	popObject.content += '				<td>Email</td>';
	popObject.content += '				<td><img src="gfx/_blank.gif" alt="" width="10" height="1" border="0"/></td>';
	popObject.content += '				<td><input type="text" id="newLightboxAccess" style="width: 260;"></td>';
	popObject.content += '				<td><img src="gfx/_blank.gif" alt="" width="10" height="1" border="0"/></td>';
	popObject.content += '				<td><a href="JavaScript:addAccessToLightBox(\'new\')" onfocus="blur()">Add</a></td>';
	popObject.content += '			</tr>';
	*/
	
	popObject.content += '		</table>';
	popObject.content += '		<br>';
	popObject.content += '		</td>';
	popObject.content += '	</tr>';
	popObject.content += '</table>';
	popObject.content += '<table width="416" border="0" cellspacing="0" cellpadding="10">';
	popObject.content += '	<tr>';
	popObject.content += '		<td align="right">';
	popObject.content += '		<table border="0" cellspacing="0" cellpadding="0" align="right">';
	popObject.content += '			<tr>';
	popObject.content += '				<td><a href="JavaScript:divHide(\'popLayer\',' + popObject.closeLowerAccess + ');" onfocus="blur()"><img src="gfx/button_cancel.gif" alt="" width="52" height="20" border="0"/></a>';
	popObject.content += '				<td><img src="gfx/_blank.gif" alt="" width="10" height="1" border="0"/></td>';
	
	if(isNew)
		popObject.content += '				<td><a href="JavaScript:createLightbox()" onfocus="blur()"><img src="gfx/button_create.gif" alt="" width="52" height="20" border="0"/></a>';
	else
		popObject.content += '				<td><a href="JavaScript:updateLightboxDetails(' + lightboxId + ')" onfocus="blur()"><img src="gfx/button_update.gif" alt="" width="52" height="20" border="0"/></a>';
	
	popObject.content += '			</tr>';
	popObject.content += '		</table>';
	popObject.content += '		</td>';
	popObject.content += '	</tr>';
	popObject.content += '</table>';
	
	document.getElementById("popLayerContent").innerHTML = popObject.content;
	
	ArtistWebEffects.set_opacity(0,"popLayer");				
	divShow('popLayer', 1, 1, popObject.closeLowerAccess, popObject.darkOutBackground);
	
	document.getElementById("newLightboxName").focus(); 
	
	popLayerReposition();
	ArtistWebEffects.set_opacity(100,"popLayer");				
}

function addAccessToLightBox(itemArrayIndex)
{
	if(itemArrayIndex == "new")
	{
		var email = document.getElementById('newLightboxAccess').value;
	
		if(isValidEmail(email))
		{
			accessArray.push((email + ",2"));
			document.getElementById('newLightboxAccess').value = "";
			displayAccess();
		}
		else
		{
			displayAccess();
			document.getElementById("newAccess").innerHTML += "<b class='red'>Email is not valid</b><br>";
		}
	}
	else
	{
		accessArray.splice(itemArrayIndex,1);
		displayAccess();
	}
	
	popLayerReposition();
}

function updateOtherUserAccessToLightBox(itemArrayIndex, newAccess)
{
	var itemArray = accessArray[itemArrayIndex].split(",");
	
	accessArray[itemArrayIndex] = (itemArray[0] + "," + newAccess);
}

function displayAccess()
{
	var itemArray;
	
	var content = '<table border="0" cellpadding="0" cellspacing="0">';
	
	for(var i=0; i<accessArray.length; i++)
	{
		itemArray = accessArray[i].split(",");
		
		content += '	<tr>';
		content += '		<td>' + itemArray[0] + '</td>';
		content += '		<td><img src="gfx/_blank.gif" alt="" width="10" height="1" border="0"/></td>';
		
		if(itemArray[1] == 1)
			content += '		<td><input type="radio" name="newAccess' + i + '" value="full" onfocus="blur()" checked></td>';
		else
			content += '		<td><input type="radio" name="newAccess' + i + '" value="full" onfocus="blur()" onclick="updateOtherUserAccessToLightBox(' + i + ',1);"></td>';
			
		content += '		<td>Edit</td>'
		content += '		<td><img src="gfx/_blank.gif" alt="" width="10" height="1" border="0"/></td>';
		
		if(itemArray[1] == 2)
			content += '		<td><input type="radio" name="newAccess' + i + '" value="read" onfocus="blur()" checked></td>';
		else
			content += '		<td><input type="radio" name="newAccess' + i + '" value="read" onfocus="blur()" onclick="updateOtherUserAccessToLightBox(' + i + ',2);"></td>';
		
		content += '		<td>Comment</td>'
		content += '		<td><img src="gfx/_blank.gif" alt="" width="10" height="1" border="0"/></td>';
		content += '		<td><a href="JavaScript:addAccessToLightBox(' + i + ')" onfocus="blur()">Delete access</a></td>';
		content += '	</tr>';
	}
	
	content += '</table>'
	
	document.getElementById("newAccess").innerHTML = content;
}

function createLightbox()
{
	dName = document.getElementById('newLightboxName');

	if(dName.value == "" || dName.value == "Please add name!")
		dName.value = "Please add name!";
	else
		dataRequest(serverUrl + "ajax/Lightbox.aspx?action=create&lightboxId=0&content=null&name=" + escape(document.getElementById('newLightboxName').value) + "&clientName=" + escape(document.getElementById('newClient').value), lightboxSeverCreate);
}

function updateLightboxDetails(lightboxId)
{
	dataRequest(serverUrl + "ajax/Lightbox.aspx?action=update&lightboxId=" + lightboxId + "&content=null&name=" + escape(document.getElementById('newLightboxName').value) + "&clientName=" + escape(document.getElementById('newClient').value), lightboxSeverUpdate);
}

function lightboxSeverCreate(xmlFeed, status, url)
{
	if(status != 0)
	{
		if(confirm("Lightbox failed to create.\r\n\r\nDo you want to try again?"))
		{
			dataRequest(url, lightboxSeverCreate);
		}
	}
	else
	{
		if(document.getElementById("lightboxDiv"))
		{
			var xmlDoc = xmlFeed.responseXML;
			var node = xmlDoc.getElementsByTagName('images').item(0);
			var d = document.getElementById("lightboxSelectorDdl");
			addOption = new Option(node.getAttributeNode("lightboxname").value + " (0)", node.getAttributeNode("lightboxid").value, false, true);
			d.options[d.options.length] = addOption;
			try{
	            comboUpdateField(d);
	        }
	        catch(e) 
	        {
	        }
			changeLightbox();
			divHide("popLayer",true);
		}
		else
			location.href = serverUrl + "Lightbox.aspx";
	}
}

function lightboxSeverUpdate(xmlFeed, status, url)
{
	if(status != 0)
	{
		if(confirm("Lightbox failed to update.\r\n\r\nDo you want to try again?"))
		{
			dataRequest(url, lightboxSeverUpdate);
		}
		else
		{
			divHide("popLayer",true);
		}
	}
	else
	{
		location.href = getUrl(true, true, true);
	}
}
