
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// image viewer functions
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////


function printImage(psImageReportUrl)
{
	var oImageWindow;

	oImageWindow = window.open(psImageReportUrl, "popPDFWin", "resizable=1," + createWinPos(440,400));
}

// image sizes
var iImageWinWidth = screen.availWidth - 10;
var iImageWinHeight = screen.availHeight - 28;

var iImageWidth = iImageWinWidth - 30;
var iImageHeight = iImageWinHeight - 46;

function enlargeFloorPopup(psImageViewerPath, psEncryptedPropertyID, psImageID, psDesc, psTitle) {

	// enlarge building photo
    enlargeImagePopup(psImageViewerPath, "", psImageID, "", "", "false", psTitle, false, psDesc, "false", psEncryptedPropertyID, ""
	    , "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined");
}

function enlargeFloorPopupCDN(psImageViewerPath, psEncryptedPropertyID, psImageID, psDesc, psTitle, entityType) {

    // enlarge building photo
    enlargeImagePopup(psImageViewerPath, "", psImageID, "", "", "false", psTitle, false, psDesc, "false", psEncryptedPropertyID, ""
	    , "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", entityType);
}

// coply clipboard button click handler
function clipboardAlert()
{
	var sAlertText = "Instructions on copying an image to the clipboard:\n\n" +
		" - Right-click on image; a pop-up dialog will appear.\n" +
		" - Select \"Copy\".\n" +
		" - Use Edit/\"Paste Special\" (selecting device independent bitmap)\n" +
		"   to paste the image into the desired application.\n";
	alert(sAlertText);
}

function enlargeLogoPopup(psImageViewerPath, psEncryptedPropertyID, psParam3, psParam4)
{
   //if there's 3 params, param3 is the Title
   //if there's 4 params, param3 is the attachmentid and param4 is the Title
	// enlarge building photo
   if (psParam4==null)
	   enlargeImagePopup(psImageViewerPath, "", "", "", "", "false", unescape(psParam3), false, "", "false", psEncryptedPropertyID, "");
	else 
	   enlargeImagePopup(psImageViewerPath, "", psParam3, "", "", "false", unescape(psParam4), false, "", "false", psEncryptedPropertyID, "");  
}

function enlargePropertyGroupLogoPopup(psImageViewerPath, psEncryptedPropertyID, psEncryptedPropertyGroupID, psParam4, psParam5, psParam6)
{
   //if there are 4 params, param4 is the Title
   //if there are 5 params, param4 is the attachmentid and param5 is the Title
   //if there are 6 params, param4 is the attachmentid, param5 is the Title, param6 is the ImagePath
	// enlarge building photo
   if (psParam5==null)
	   enlargeImagePopup(psImageViewerPath, "", "", "", "", "false", unescape(psParam4), false, "", "false", psEncryptedPropertyID , ""
	    , "undefined", "undefined", "undefined", "undefined", "undefined", "undefined","undefined", "undefined", psEncryptedPropertyGroupID);
	else if (psParam6==null)
	   enlargeImagePopup(psImageViewerPath, "", psParam4, "", "", "false", unescape(psParam5), false, "", "false", psEncryptedPropertyID, ""
	    , "undefined", "undefined", "undefined", "undefined", "undefined", "undefined","undefined", "undefined", psEncryptedPropertyGroupID);
	else 
	   enlargeImagePopup(psImageViewerPath, "", psParam4, "", "", "false", unescape(psParam5), false, "", "false", psEncryptedPropertyID, ""
	    , "undefined", "undefined", "undefined", "undefined", "undefined", "undefined","undefined", psParam6, psEncryptedPropertyGroupID);	      
}

// Creates pop-up window showing an image and it's description (psTitle)
function enlargeImagePopup(
	psImageViewerPath,
	psPropertyID,
	psAttachmentID,
	psFileExt,
	psUNC,
	psIsMapOrAerial,
	psTitle,
	pbNav,
	psDesc,
	psIsPlatMap,
	psEncryptedPropertyID,
	psImageTypeID,
	psIsMasterImage,
	psIsLowResolution,
	psCurrentImage,
	psTotalImages,
	psPropertyAddress,
	psImageItems,
	psComments,
	psImagePath,
	psEncryptedPropertyGroupID,
	entityType
	)
{

	var undefined;
	var sScrolling = ",scrollbars=1";

	// have to set this default large size otherwise need to change back to this after ipx size
	iImageWidth = iImageWinWidth - 30;
	iImageHeight = iImageWinHeight - 66;

	var sImageURL;
	

	if (psPropertyID == "")
	{
		psPropertyID = psEncryptedPropertyID;
	}
	
	if (psFileExt == "ipx" || psFileExt == "mov")
	{
		if (psFileExt == "ipx" && iImageWidth > 640)
		{
			iImageWidth = "640";
			iImageHeight = "480";
		}

		sImageURL = "Get360.ashx?id=" + psPropertyID + "&aid=" + psAttachmentID + "&unc=" + psUNC + "&fileext=" + psFileExt + "&filename=building.ipx&width=" + iImageWidth.toString() + "&height=" + iImageHeight.toString();
	}
	else
		sImageURL = "GetImage.ashx?id=" + psPropertyID + "&aid=" + psAttachmentID + "&fileext=" + psFileExt + "&unc=" + psUNC + "&width=" + iImageWidth.toString() + "&height=" + iImageHeight.toString() + "&IsMapOrAerial=" + psIsMapOrAerial;

    if (psEncryptedPropertyGroupID != "undefined" && psEncryptedPropertyGroupID != undefined && psEncryptedPropertyGroupID != "")
        sImageURL += "&pgid=" + psEncryptedPropertyGroupID
    
	var sEnlargeImageURL = psImageViewerPath + "/EnlargeImage.aspx?psImgURL=" + escape(sImageURL);

	if(psImageTypeID != "undefined" && psImageTypeID != undefined)
		sEnlargeImageURL += "&imagetypeid=" + psImageTypeID

	if (psTitle != "undefined" && psTitle != undefined)
		sEnlargeImageURL += "&psTitle=" + escape(psTitle.replace("/", "FORWARD_SLASH"));

	if (psDesc != "undefined" && psDesc != undefined)
		sEnlargeImageURL += "&psDesc=" + (psDesc);

	if (pbNav != "undefined" && pbNav == true)
		sEnlargeImageURL += "&psNav=1";
	
	if (psIsPlatMap != "undefined" && psIsPlatMap == "true")
		sEnlargeImageURL += "&psPlatMap=1";

	if (psIsLowResolution != null && psIsLowResolution != "undefined" && psIsLowResolution == true)
		sEnlargeImageURL += "&psIsLowResolution=1";
		
	if (psEncryptedPropertyID != "undefined" && psEncryptedPropertyID != undefined)
		sEnlargeImageURL += "&psPID=" + psEncryptedPropertyID;
	if (psIsMasterImage != "undefined" && psIsMasterImage)
	{
		sEnlargeImageURL += "&m=1";
	}
	
	if(psCurrentImage != "undefined" && psCurrentImage != undefined)
		sEnlargeImageURL += "&psCurrentImage=" + psCurrentImage;
		
	if(psTotalImages != "undefined" && psTotalImages != undefined)
		sEnlargeImageURL += "&psTotalImages=" + psTotalImages;
		
	if(psPropertyAddress != "undefined" && psPropertyAddress != undefined)	
		sEnlargeImageURL += "&psPropertyAddress=" + psPropertyAddress;

    if (entityType != "undefined" && entityType != undefined)
        sEnlargeImageURL += "&entityType=" + entityType;

    if (psImagePath == "undefined" || psImagePath == undefined)
	{		

		var oImageWindow = new Object()		
		oImageWindow.win = window.open(sEnlargeImageURL, "imgWin", "top=0,left=0,width=" + iImageWinWidth.toString() + ",height=" + iImageWinHeight.toString() + sScrolling);

		psImagePath = psImageViewerPath;
		oImageWindow.win.document.write("");
		oImageWindow.win.document.close();

		oImageWindow.win.document.write("<html>");
		oImageWindow.win.document.write("<body>");
		oImageWindow.win.document.writeln("<form name=\"frmWin\" id=\"frmWin\" method=\"post\" action=\"" + sEnlargeImageURL + "\" >");
		oImageWindow.win.document.writeln("<input name=\"ImageItems\"  type=\"hidden\" value=\"" + psImageItems + "\">");
		oImageWindow.win.document.writeln("<input name=\"Comments\"  type=\"hidden\" value=\"" + psComments + "\">");
		oImageWindow.win.document.writeln("<input name=\"ImageViewerPath\"  type=\"hidden\" value=\"" + psImagePath + "\">");
		oImageWindow.win.document.write("</form>");
		oImageWindow.win.document.writeln("</body>");
		oImageWindow.win.document.write("<script language=\"javascript\">document.all.frmWin.submit();<\/script>");
		oImageWindow.win.document.writeln("</html>");
		oImageWindow.win.document.close();
	}
	else
	{	
		document.forms[0].action = sEnlargeImageURL;
		document.forms[0].submit();
	}

}

function OpenEnlargedCDNImageWindow(imageViewerPath, id, index, aid, title, uname) {
	var qs = "?";
	if (id != null) {
		qs = qs + "id=" + id.toString() + "&";
	}
	if (index != null) {
		qs = qs + "index=" + index.toString() + "&";
	}
	if (aid != null) {
		qs = qs + "aid=" + aid.toString() + "&";
	}
	if (title != null) {
		qs = qs + "title=" + title.toString() + "&";
	}
	if (uname != null) {
		qs = qs + "uname=" + uname + "&";
	}
	
	qs = qs.substr(0, qs.length-1); //remove last &
	if (/Chrome[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
		var newWindow = window.open(imageViewerPath + qs, "winEnlargeImage", "resizable=1,width=" + (iImageWinWidth - 10).toString() + "px,height=" + (iImageWinHeight - 15).toString() + "px");
		newWindow.moveTo(0, 0);
		newWindow.resizeTo(iImageWinWidth - 10, iImageWinHeight - 15);
	}
	else {
		window.open(imageViewerPath + qs, "winEnlargeImage", "resizable=1,top=0,left=0,width=" + (iImageWinWidth - 10).toString() + "px,height=" + (iImageWinHeight - 15).toString() + "px");
	}
}

function ShowImage(pbIsHiResImageWin, pbEnlargeWinSelfReload, piIndex, psTitle, piTotalImages)
{

	var undefined;

	var sImagePath = document.all.ImageViewerPath.value;
	var sImageURL = "";
	var sCurrentImage = "";
	var iNewIndex = piIndex + 1;

	sCurrentImage = document.all.ImageItems.value.split("|")[piIndex];	
	sComment = document.all.Comments.value.split("|")[piIndex];

	var currentImageItems = sCurrentImage.split("^");

	if (pbEnlargeWinSelfReload != null && pbEnlargeWinSelfReload != undefined && pbEnlargeWinSelfReload == true)
	{
		//sImagePath = sImagePath.substring(6, sImagePath.length);
		sImagePath = sImagePath.replace("../../../", "../");
		sImagePath = sImagePath.replace("../../", "../");
	}
	
	// since SubmarketAttachment only populates AttachmentPathMaster not AttachmentPathHigh
	// see DBAttachmentUtils GetPropertyAttachmentList method
	if (pbIsHiResImageWin || currentImageItems[6] == "true" || currentImageItems[2].toLowerCase() == "mov" || currentImageItems[2].toLowerCase() == "ipx")
	{
		sImageURL = currentImageItems[5];	// Master
	}
	else
	{
		sImageURL = currentImageItems[4];	// High
	}

	enlargeImagePopup(sImagePath, currentImageItems[0], currentImageItems[1], currentImageItems[2], sImageURL, currentImageItems[6], sComment, !pbIsHiResImageWin, "undefined", "undefined", "undefined", "undefined", pbIsHiResImageWin, "undefined", piIndex + 1, piTotalImages, psTitle, document.all.ImageItems.value, document.all.Comments.value, document.all.ImageViewerPath.value, currentImageItems[7]);
}


