var imageIds;
var action;
var lightboxId;
var lightboxids;
var lightboxnames;

function importLightbox(ids, lboxids, lnames)
{
	imageIds = ids;
	lightboxids = lboxids;
	lightboxnames = lnames;

	divHide('popLayer',true);
	
	var popObject = new Object();
	
	popObject.closeLowerAccess = true;
	popObject.darkOutBackground = true;
	
	popObject.content =  '<form name="importLightboxForm" id="importLightboxForm">';
	popObject.content += ' <table width="400" border="0" cellspacing="0" cellpadding="8">';
	popObject.content += '	<tr>';
	popObject.content += '		<td valign="top" align="left">';
	
	popObject.content += '		You had added images to the lightbox before login on.<br>';
	popObject.content += '		<br>';
	popObject.content += '		What would you like to do with these images?<br>';
	popObject.content += '		<br>';
	popObject.content += '		<table border="0" cellspacing="0" cellpadding="2">';
	popObject.content += '			<tr>';
	popObject.content += '				<td><input type="radio" name="importLightboxActionRad" onfocus="blur()" value="1" checked></td>';
	popObject.content += '				<td>Discard images.</td>';
	popObject.content += '			</tr>';
	popObject.content += '			<tr>';
	popObject.content += '				<td><input type="radio" name="importLightboxActionRad" onfocus="blur()" value="2"></td>';
	popObject.content += '				<td>Add to an existing lightbox.</td>';
	popObject.content += '			</tr>';
	popObject.content += '			<tr>';
	popObject.content += '				<td><input type="radio" name="importLightboxActionRad" onfocus="blur()" value="3"></td>';
	popObject.content += '				<td>Create new a lightbox with the images.</td>';
	popObject.content += '			</tr>';
	popObject.content += '		</table>';
	
	popObject.content += '		</td>';
	popObject.content += '	</tr>';
	popObject.content += '</table>';
	popObject.content += '<table width="400" border="0" cellspacing="0" cellpadding="10">';
	popObject.content += '	<tr>';
	popObject.content += '		<td align="right">';
	popObject.content += '		<table border="0" cellspacing="0" cellpadding="0">';
	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></td>';
	popObject.content += '				<td><img src="gfx/_blank.gif" alt="" width="10" height="1" border="0"/></td>';
	popObject.content += '				<td><a href="JavaScript:handleImportLightboxSelection();" onfocus="blur()"><img src="gfx/button_continue.gif" alt="" width="72" height="20" border="0"/></a></td>';
	popObject.content += '			</tr>';
	popObject.content += '		</table>';
	popObject.content += '		</td>';
	popObject.content += '	</tr>';
	popObject.content += '</table>';	
	popObject.content += '</form>';
	
	document.getElementById("popLayerContent").innerHTML = popObject.content;
	document.getElementById("popLayerHeadline").innerHTML = "<b>Import lightbox</b>";
	document.getElementById('popLayerTopSizer').width = 1;

    ArtistWebEffects.set_opacity(0,"popLayer");				
	divShow('popLayer', Math.floor((document.body.clientWidth - 400)/2), 100, popObject.closeLowerAccess, popObject.darkOutBackground);
	
	popLayerReposition();
	ArtistWebEffects.set_opacity(100,"popLayer");				
}

function handleImportLightboxSelection()
{
	var nextObject = new Object();
	
	action = returnSelectedRadioValue('importLightboxActionRad');
		
	divHide('popLayer',true);
	
	var popObject = new Object();
	
	popObject.closeLowerAccess = true;
	popObject.darkOutBackground = true;
	
	if(action == 2)
	{
		popObject.content = '';
		popObject.content +=  '<table width="416" border="0" cellspacing="0" cellpadding="10">';
		popObject.content += '	<tr>';
		popObject.content += '		<td>';
		popObject.content += '		<b>Select lightbox</b><br>';
		popObject.content += '		<select name="lightboxDdl" id="lightboxDdl" style="width: 260;">';
	
		for(var i=0; i<lightboxids.length; i++)
		{
			popObject.content += '		<option value="' + lightboxids[i] + '">' + lightboxnames[i] + '</option>';
		}
		
		popObject.content += '		</select>';
		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>';
		popObject.content += '				<td><a href="JavaScript:importLightboxSubmit()" onfocus="blur()"><img src="gfx/button_continue.gif" alt="" width="72" 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;
		document.getElementById("popLayerHeadline").innerHTML = "<b>Import lightbox</b>";
		document.getElementById('popLayerTopSizer').width = 1;

        ArtistWebEffects.set_opacity(0,"popLayer");				
		divShow('popLayer', Math.floor((document.body.clientWidth - 400)/2), 100, popObject.closeLowerAccess, popObject.darkOutBackground);
		
		popLayerReposition();
		ArtistWebEffects.set_opacity(100,"popLayer");				
	}
		
	if(action == 3)
	{
		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="" 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="" style="width: 260;"></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>';
		popObject.content += '				<td><a href="JavaScript:importLightboxSubmit();" onfocus="blur()"><img src="gfx/button_continue.gif" alt="" width="72" 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;
		document.getElementById("popLayerHeadline").innerHTML = "<b>Import lightbox</b>";
		document.getElementById('popLayerTopSizer').width = 1;

        ArtistWebEffects.set_opacity(0,"popLayer");				
		divShow('popLayer', Math.floor((document.body.clientWidth - 400)/2), 100, popObject.closeLowerAccess, popObject.darkOutBackground);
		
		popLayerReposition();
		ArtistWebEffects.set_opacity(100,"popLayer");				
	}
}

function importLightboxSubmit()
{
	if(action == 2)
	{
		lightboxId = document.getElementById('lightboxDdl').value;
		updateLightbox(true, true);
	}
	
	if(action == 3)
	{
		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 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
	{
		var xmlDoc = xmlFeed.responseXML;
		var node = xmlDoc.getElementsByTagName('images').item(0);
			
		lightboxId = node.getAttributeNode("lightboxid").value;
		
		updateLightbox(true, true);
	}
}

function updateLightbox(server, cookie)
{
	if(server)
		updateLightboxServer(imageIds);
	
	if(cookie)
		updateLightboxCookie(imageIds);
}

function updateLightboxServer(imageIds)
{
	dataRequest(serverUrl + "ajax/Lightbox.aspx?action=addImages&lightboxId=" + lightboxId + "&content=" + imageIds, importLightboxOnServerDone);
}

function updateLightboxCookie(imageIds)
{
	createCookie("TrunkLightbox" + lightboxId, imageIds, 1);
}

function importLightboxOnServerDone(xmlFeed, status, url)
{
	if(status != 0)
	{
		if(confirm("Lightbox failed to update.\r\n\r\nDo you want to try again?"))
		{
			dataRequest(url, lightboxSeverCreate);
		}
	}
	else
	{
		importLightboxDone();
	}
}

function importLightboxDone()
{
	divHide('popLayer',true);
	
	var popObject = new Object();
	
	popObject.closeLowerAccess = true;
	popObject.darkOutBackground = true;
	
	popObject.content =  '<table width="400" border="0" cellspacing="0" cellpadding="8">';
	popObject.content += '	<tr>';
	popObject.content += '		<td valign="top" align="left"><div id="importLightboxDiv">';
	
	popObject.content += '		Images where successfully added.';
	
	popObject.content += '		</td>';
	popObject.content += '	</tr>';
	popObject.content += '</table>';
	popObject.content += '<table width="400" border="0" cellspacing="0" cellpadding="10">';
	popObject.content += '	<tr>';
	popObject.content += '		<td align="right"><a href="JavaScript:divHide(\'popLayer\',' + popObject.closeLowerAccess + ');" onfocus="blur()"><img src="gfx/button_continue.gif" alt="" width="72" height="20" border="0"/></a></td>';
	popObject.content += '	</tr>';
	popObject.content += '</table>';	
	
	document.getElementById("popLayerContent").innerHTML = popObject.content;
	document.getElementById("popLayerHeadline").innerHTML = "<b>Import lightbox</b>";
	document.getElementById('popLayerTopSizer').width = 1;

    ArtistWebEffects.set_opacity(0,"popLayer");				
	divShow('popLayer', Math.floor((document.body.clientWidth - 400)/2), 100, popObject.closeLowerAccess, popObject.darkOutBackground);
	
	popLayerReposition();
	ArtistWebEffects.set_opacity(100,"popLayer");				
}

function returnSelectedRadioValue(name)
{
	var field = eval('document.getElementById("importLightboxForm").' + name);
	for( i = 0; i < field.length; i++ )
	{
		if(field[i].checked)
			return field[i].value;
	}
}