function centerQuickViewImage(quickViewTableHeight) {
    // Apparently safari makes equal space of the three rows, but not firefox
    if (safariVersion == -1) {
        // heighten tr's above and below the quickViewImage, so they can be used as drag areas
        var quickViewImageHeight = document.getElementById("quickViewImage").height;
        document.getElementById("quickViewDragTop").style.height = Math.abs((quickViewTableHeight - quickViewImageHeight) / 2) + "px";
        document.getElementById("quickViewDragBottom").style.height = Math.abs((quickViewTableHeight - quickViewImageHeight) / 2) + "px";
        //console.info("quickViewTableHeight: " + quickViewTableHeight + ", quickViewImageHeight: " + quickViewImageHeight + ", top: " + document.getElementById("quickViewDragTop").style.height + ", Bottom: " + document.getElementById("quickViewDragBottom").style.height);
    }
}


function updateCurrentQuickView(qwImgUrl) {

    var qwi = document.getElementById("quickViewImage");
    if (qwi) {        
        qwi.src = qwImgUrl;
        var t = setTimeout("updateCurrentQuickView(currentQuickViewImg)", 200);
    }
}


function setupQuickViewAjax(imageId)
{
    if (imageArray[imageId] == null)
        dataRequest(serverUrl + "ajax/Lightbox.aspx?action=getImages&lightboxId=0&content=" + imageId, quickViewImageLoaded);
    else
        setupQuickView(imageId, false)
}

function quickViewImageLoaded(xmlFeed, status, url)
{
	if(status != 0)
	{
		if(confirm("Update failed.\r\n\r\nDo you want to try again?"))
		{
			dataRequest(url, insureSeverUpdate);
		}
	}
	else
	{
	    var xmlDoc = xmlFeed.responseXML;
		
	    if(xmlDoc.documentElement.nodeName == "trunk")
        {
            if(xmlDoc.documentElement.attributes[0].name == "relogon")
            {
                if(xmlDoc.documentElement.attributes[0].value == "true")
                    location.href = "LogOn.aspx";
            }
        }
	

		var node = xmlDoc.getElementsByTagName('images').item(0);
		var imageIds = node.getAttributeNode("imagestring").value;					
		var nodeChild = node.getElementsByTagName("image").item(0);
		
		var image = new Object();
		image.id = nodeChild.getElementsByTagName("id")[0].firstChild.nodeValue;
		image.no = nodeChild.getElementsByTagName("no")[0].firstChild.nodeValue;
		image.videoTypeId = nodeChild.getElementsByTagName("videoTypeId")[0].firstChild.nodeValue;
		image.nextVideoImageId = nodeChild.getElementsByTagName("nextVideoImageId")[0].firstChild.nodeValue;
		image.artist = nodeChild.getElementsByTagName("artist")[0].firstChild.nodeValue;
		image.artistId = nodeChild.getElementsByTagName("artistid")[0].firstChild.nodeValue;
		image.alert = nodeChild.getElementsByTagName("alert")[0].firstChild.nodeValue;
			
	    setupQuickView(image.id, false, null, null, image);
	}
}

var currentQuickViewImg = "";
var qwload = null;
function setupQuickView(imageId, isFromLightbox, top, left, imageObject)
{
   
	if(readCookie("TrunkAccount") == "0")
	{
		location.href = serverUrl + "/image.aspx?image=" + imageId;
		return true;
	}
	
	var plLeft = 1;
	var plTop = 1;			
	var popLayer = document.getElementById("popLayer");		
	if (popLayer.style.display != "none") {	
		
		plLeft = popLayer.style.left;
		plTop = popLayer.style.top;
	}
	
	divHide('popLayer',true);
	
	var framecontentBottom = document.getElementById("framecontentBottom");
    var height = framecontentBottom.offsetTop + framecontentBottom.clientHeight;
	var imageSize = returnImageSizeBasedOnPageHeight(height);
		
	var myImage = new Object();
	var popObject = new Object();
	
	if (imageObject != null)
	    myImage = imageObject;
	else if(isFromLightbox)
		myImage = lightboxImageArray[imageId];
	else 
		myImage = imageArray[imageId];
		
	var quickViewTableHeight = (imageSize + 16);
	
	popObject.closeLowerAccess = true;
	popObject.darkOutBackground = true;
	
	currentQuickViewImg = serverUrl + imageDir + imageSize + "s/" + myImage.no + '.jpg';
	qwloadUrl = serverUrl + "/qwload.aspx?i=" + escape(currentQuickViewImg);
	popObject.content = '';

    /* safari history back hack*/
	if (isHistoryPage()) {
	
	    qwload = openMiniHelper(qwloadUrl);
        self.focus();
        if (!qwload.opener) qwload.opener = self;
        
	}
	
	var showHideCloseHtml = 'onmouseover="showQuickViewClose();" onmouseout="hideQuickViewClose();"';
	if (myImage.videoTypeId == "IsVideo") showHideCloseHtml = "";
	
	popObject.content += '<table id="quickViewDrag" ' + showHideCloseHtml + ' width="' + (imageSize + 16) + 'px" height="' + quickViewTableHeight + 'px" border="0" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF">';
	popObject.content += '	<tr id="quickViewDragTop" ><td colspan="3"></td></tr>';
	popObject.content += '	<tr>';
	popObject.content += '	    <td id="quickViewDragLeftSide"  width="8px">&nbsp;</td>';
	popObject.content += '		<td width="1%" align="left" valign="center">';
	
	//image here
	popObject.content += '<a href="JavaScript:divHide(\'popLayer\',' + popObject.closeLowerAccess + ');" onfocus="blur()">';
	//console.info(myImage);
	if (myImage.videoTypeId != "IsVideo") {		
		popObject.content += '<img id="quickViewImage" style="" src="' + currentQuickViewImg + '" alt="" border="0" onload="centerQuickViewImage(' + quickViewTableHeight + ')"/>';
	} else {		
		popObject.content += '<div id="quickViewImage" style="width: 500px; height: 500px;">' + flashEmbedStr(serverUrl + "/swf/player.swf?mov=" + escape(serverUrl) + "video/500/" + myImage.no + ".flv", "flv_player", 500, 500, '#FFFFFF','opaque') + '</div>';
	}
	
	popObject.content += '</a></td>';
	
	var artistName = myImage.artist;
	artistName = artistName.replace(/\s/g, "+").toUpperCase();

	
	popObject.content += '	    <td id="quickViewDragRightSide" >&nbsp;</td>';
	popObject.content += '	</tr>';
	popObject.content += '	<tr id="quickViewDragBottom" ><td colspan="3"></td></tr>';
	popObject.content += '</table>';
	popObject.content += '<table width="' + (imageSize + 16) + '" border="0" cellspacing="0" cellpadding="10">';
	popObject.content += '	<tr>';
	popObject.content += '		<td width="70%" align="left" valign="bottom" nowrap><div class="quickViewInfo"><a style="font-family: Times New Roman; font-size: 12px; font-weight: bold;" href=\"' + getUrlV2("root", "none") + artistName + '/images\"  onfocus=\"blur()\">' + myImage.artist.toUpperCase() + '</a><br>';
	        
	popObject.content += '		IMAGE: ' + myImage.no + ',  ';	
	if (myImage.storyCollectionId && myImage.storyCollectionId != "") {
	    popObject.content += '		<a href="' + getUrlV2("root", "none") + artistName + '/images/story%23%20' + myImage.storyCollectionId + '" onfocus="blur()">View story</a><br>';
	    //popObject.content += '		<a href="Search.aspx?searchString=story%23+' + myImage.storyCollectionId + '" onfocus="blur()">view story</a> |';    
	}
	popObject.content += ' <a href="RequestPrice.aspx?imageIds=' + myImage.id + '" onfocus="blur()">request</a> | <a onclick="gotoImageDetails(' + myImage.id + ');" href="' + getUrlV2("root", "query") + '#bm' + myImage.id + '" onfocus="blur()">details</a> | <a href="ImagePopup.aspx?imageId=' + myImage.id + '" target="_blank" onfocus="blur()">print</a><br>';
	
	popObject.content += '<div id="quickViewDragLowerInfoText"><a onfocus="blur()" href="JavaScript:addRemoveImage(' + myImage.id + ', false, false)">'
    popObject.content += '<img id="popLayer' + myImage.id + 'IsInLightbox" width="8" height="8" style="border-width: 0px;" alt="" src="gfx/image_icon_lightbox_off.gif"/>'
    popObject.content += '</a>&nbsp;'
    popObject.content += '<a onfocus="blur()" href="JavaScript:addRemoveImage(' + myImage.id + ', false, false)">LIGHTBOX</a><br><br>'
	
	popObject.content += '		<a href="JavaScript:displayPrevNextImage(' + myImage.id + ',false,' + isFromLightbox + ', qwGetTop(), qwGetLeft());" onfocus="blur()"><img style="border-width: 0px;" src="gfx/button_prev.gif" /></a> <a href="JavaScript:displayPrevNextImage(' + myImage.id + ',true,' + isFromLightbox + ', qwGetTop(), qwGetLeft());" onfocus="blur()"><img style="border-width: 0px;" src="gfx/button_next.gif" /></a></span></td>';
	popObject.content += '		<td id="quickViewDragLowerInfoTd2" >';
		
    popObject.content += '		</td><td id="quickViewDragLowerInfoTd3"  width="24%" valign="bottom" align="right" nowrap>';
    
	popObject.content += '		<a href="JavaScript:divHide(\'popLayer\',' + popObject.closeLowerAccess + ');" onfocus="blur()">close</a></td>';
	popObject.content += '	</tr>';
	popObject.content += '</table><div id="quickViewClose" style="padding: 8px; background: #ffffff; top: 1px; left: 0px; float: right; position: absolute; z-index: 2000; display: none;" onmouseover="showQuickViewClose();"><img src="gfx/box_but_close.gif" alt="" width="19" height="19" border="0" onmousedown="divHide(\'popLayer\',true);" style="cursor: pointer;" /></div>';
	
	document.getElementById("popLayerContent").innerHTML = popObject.content;
	//document.getElementById("popLayerHeadline").innerHTML = '<table border="0" cellspacing="0" cellpadding="0"><tr><td><b>' + myImage.no + '</b></td><td valign="bottom"><img src="gfx/_blank.gif" alt="" width="5" height="1" border="0"/></td><td><a href="ImagePopup.aspx?imageId=' + myImage.id + '" target="_blank" onfocus="blur()"><img src="gfx/icon_print.gif" alt="" width="13" height="12" border="0"/></a></td></tr></table>';
	document.getElementById('popLayerTopSizer').width = 1;
	document.getElementById("popLayerHeadline").innerHTML = "";
	document.getElementById('popLayerDrag').style.display = "none";	
	document.getElementById('popLine').style.backgroundColor = "#ffffff";
	
	
	
	/*
	if(!document.getElementById('lightboxImages') && lightboxImageArray[imageId] != null)
		document.getElementById("popLayerIsInLightbox").src = "gfx/image_icon_lightbox_on.gif";
	*/							
	
	//ArtistWebEffects.set_opacity(0,"popLayer");				
	divShow('popLayer', plLeft, plTop, popObject.closeLowerAccess, popObject.darkOutBackground);
	
	//var t = setTimeout("updateCurrentQuickView(currentQuickViewImg)", 200);
	
	//document.getElementById('popLayerDrag').onmousedown = function(){popLayerDragStart();};	
	//document.getElementById('quickViewDrag').onmousedown = function(){popLayerDragStart();};
    	
    document.getElementById('quickViewDragTop').onmousedown = function(){popLayerDragStart();};	
    document.getElementById('quickViewDragBottom').onmousedown = function(){popLayerDragStart();};	
    document.getElementById('quickViewDragRightSide').onmousedown = function(){popLayerDragStart();};	
    document.getElementById('quickViewDragLeftSide').onmousedown = function(){popLayerDragStart();};	
    document.getElementById('quickViewDragLowerInfoTd2').onmousedown = function(){popLayerDragStart();};	
    document.getElementById('quickViewDragLowerInfoTd3').onmousedown = function(){popLayerDragStart();};	
    document.getElementById('quickViewDragLowerInfoText').onmousedown = function(){popLayerDragStart();};	

	popLayerReposition(false, top, left);
	    
	addRemoveImageText(myImage.id);
	//ArtistWebEffects.set_opacity(100,"popLayer");					 

	if(ReturnTrueFalseAsBoolean(getUserSettingFromCookie("user","doFirstDisplayQuickviewAlert")))
    {
        updateUserSettings(true, true, "user_doFirstDisplayQuickviewAlert", "false");
        
        try
		{
			//displayAlertLayer("<b>Hint:</b> Move on quicker<br><br>To close enlarged view click anywhere on the image.", 220, 0, "BR", 100, 100, zIndexGlobal);
			//zIndexGlobal ++;
		}
		catch(e)
		{
		}
    }
        
}


function displayPrevNextImage(imageId, getNext, isFromLightboxBar, top, left)
{
	var container, containerNode;

	if(isFromLightboxBar)
		container = document.getElementById('lightboxImagesDiv');
	else
	{
		if(document.getElementById('lightboxImages'))
			container = document.getElementById('lightboxImages');
		
		if(document.getElementById('searchResultImages'))
			container = document.getElementById('searchResultImages');
			
		if(document.getElementById('emailedImages'))
			container = document.getElementById('emailedImages');
			
		if(document.getElementById('lightboxPageImages'))
			container = document.getElementById('lightboxPageImages');
			
	}
		
	var first = 0;
	var prev = 0;
	var current = 0;
	var target = 0;
	//alert("1");
	for(var i=0; i<container.childNodes.length; i++)
	{
		containerNode = container.childNodes[i];
		
		// Make sure it is an element and not whitespace
		if(containerNode.nodeType == 1)
		{
			if(first == 0)
				first = containerNode.id.replace("divLightbox","").replace("lightboxPageDiv","").replace("div","");
			
			prev = current;
			current = containerNode.id.replace("divLightbox","").replace("lightboxPageDiv","").replace("div","");
			
			if(getNext && prev == imageId)
			{
				target = current;
				break;
			}
			
			if(!getNext && current == imageId)
			{
				target = prev;
			}
		}
	}
	
	if(target == 0)
	{
		if(getNext)
			target = first;
		else
			target = current;
	}

	//divHide('popLayer',true);
	
	setupQuickView(target, isFromLightboxBar, top, left);
}

var closeQuickViewFlag = 0;

function showQuickViewClose() {
    closeQuickViewFlag = 1;
    var popLayer = document.getElementById("popLayer");
    var quickViewClose = document.getElementById("quickViewClose");
    quickViewClose.style.left = (popLayer.clientWidth - 36) + "px";
    
    quickViewClose.style.display = "block";
}

function hideQuickViewClose() {
    if (closeQuickViewFlag == 0)
        document.getElementById("quickViewClose").style.display = "none";
    else {
        closeQuickViewFlag = 0;
        setTimeout("hideQuickViewClose()",1000);
    }
}

function qwGetLeft() {
    return document.getElementById('popLayer').style.left;
}

function qwGetTop() {
    return document.getElementById('popLayer').style.top;
}
