//
//	Copyright © 2003-2005 Juha Leinonen. All Rights Reserved.
//

function ShowPicture(theHref,optTitle,optBase) {
//
//	Launches Yet Another Popup Viewer :-)
//  This is a stand-alone viewer function that replaces 
//  functions ShowPic, fnSave, fnLoad & Resize, and the Viewer.htm.
//
	try {
		var oImg = new Image();
		oImg.src=theHref;
		properties='height=1,width=1,status=no,location=no,scrollbars=yes,resizable=yes';
		var imgWindow = window.open('','viewer2',properties,'true');
		if (imgWindow.loaded == false) { //jl/041122
			imgWindow.document.open("text/html","replace");
			alert(imgWindow.document.documentElement.innerHTML);
			imgWindow.document.close;
		}

		if (optBase == null) {
			// Set default base addr
			optBase = 'http://www.coolcats.se/';
		}
		if (optTitle == null) {
			// Set default title
			optTitle = 'Viewer [click image to close window | klicka bilden för att stänga fönstret]</title>';
		}
		html = '<html>';
		html += '<head>';
		html += '<title>'+optTitle+'</title>';
		html += '<meta http-equiv="imagetoolbar" content="no">';
		html += '<link href="' + optBase + 'styles.css" type="text/css" rel="stylesheet">';
// Make resize logic inline in order to prevent a timing error
//		html += '<script type="text/javascript" src="' + optBase + 'funcresizer.js"></script>';
		html += '<script type="text/javascript">' +
'	var loaded = false;' +
'	function ResizeForShowPicture() {' +
'		var scrollbarWidth = 40;' +
'		var iLeftMargin = 20;' +
'		var iRightMargin = 20;' +
'		var iTopMargin = 20;' +
'		var iBottomMargin = 20;' +
'		var iTaskbarHeight = 33;' +
'		try {' +
'			var X = 0;' +
'			if (screen.width < iLeftMargin+theImage.width+scrollbarWidth+iRightMargin) {' +
'				X = iLeftMargin;' +
'			} else {' +
'				if (screen.width < iLeftMargin+theImage.width+scrollbarWidth+iRightMargin) {' +
'					X = screen.width  - iLeftMargin - theImage.width - scrollbarWidth - iRightMargin;' +
'				} else {' +
'					X = screen.width - theImage.width - scrollbarWidth - iRightMargin;' +
'				}' +
' ' +
'			}' +
'			window.moveTo(X,iTopMargin);' +
' ' +
'		} catch(e) {}' +
'		try {' +
'			var iWidth = theImage.width+scrollbarWidth;' +
'			var iHeight = theImage.height+50;' +
'			if (screen.width < iLeftMargin+theImage.width+scrollbarWidth+iRightMargin) {' +
'				iWidth = screen.width - iRightMargin - iLeftMargin;' +
'			}' +
'			if (screen.height < iBottomMargin+theImage.height+scrollbarWidth+iBottomMargin) {' +
'				iHeight = screen.height - iBottomMargin - iBottomMargin - iTaskbarHeight;' +
'			}' +
'			window.resizeTo(iWidth, iHeight);' +
'			if(parseInt(navigator.appVersion)>3) {window.focus();}' +
'		} catch(e) {};' +
'};';
		html += '</script>';
		html += '</head>';
		html += '<body style="margin:0;padding:0" onload="loaded = true;">';
		html += '<a href="javascript:window.close();">';
		html += '<img alt="[click image to close window | klicka bilden för att stänga fönstret]" id="theImage" src="'+oImg.src+'" onload="javascript:ResizeForShowPicture();" border="0">';
  		html += '</a>';
		html += '</body>';
		html += '</html>';
		imgWindow.document.open("text/html","replace");
		imgWindow.document.write(html);
		imgWindow.document.close;
		if(parseInt(navigator.appVersion)>3) {imgWindow.focus();}
	} catch(e) {
		alert("Failed to show the picture");
	}

}

function ShowPic(oPersistObj,theHref,optViewer) {
//
//	Launches the picture page, pic.htm, while also storing the specified image href.
//	The picture page will then read the stored href and build the necessary IMG tag.
// 
	try {
		fnSave(oPersistObj,theHref);
		if (optViewer == null) {
			optViewer = "viewer.htm";
		}
		var iNewWindow = window.open(optViewer,"cat","status=no,location=no,scrollbars=yes,resizable=yes");
		if(parseInt(navigator.appVersion)>3) {iNewWindow.focus();}
	} catch(e) {
//		alert("Failed to show the picture");
	}
}

function fnSave(oPersistObj,aValue){
//	Persistently stores the specified value (to a hardcoded attribute)

	// An attribute is written to the persistent object.
    oPersistObj.setAttribute("sPersistValue",aValue);
	// The object is explicitly saved into an XML Store.
    oPersistObj.save("oXMLStore");
}

function fnLoad(oPersistObj){
//	Returns the value that is persistently stored (in a hardcoded attribute)
	// The XML Store is loaded.
    oPersistObj.load("oXMLStore");
	// The information is retrieved from the persistent object.
 	return(oPersistObj.getAttribute("sPersistValue"))
}

function SyncPic(oPersistObj,oTarget) {
// Loads the inner HTML of specified target with the stored href
	var strInner = "";
	var theHref = "";
	try {
		theHref = fnLoad(oPersistObj);
		strInner += '<a href="javascript:window.close();">';
		strInner += '<img alt="Click to close" id="theImage" src="' + theHref + '" border=0>';
  		strInner += '</a>';
		oTarget.innerHTML = strInner;
		// Resize window to size of image
		//window.resizeTo(theImage.width,theImage.height)
	} catch(e) {
		alert("Failed to sync the picture");
	}
}
function Resize(oPersistObj,oTarget) {
// Resize window to size of image
	var scrollbarWidth = 40;	// Approx width of a scrollbar.
	var iLeftMargin = 20;		// Just a margin
	var iRightMargin = 20;		// A safety margin, in case scrollbar is wider 
	var iTopMargin = 20;		// Just a margin
	var iBottomMargin = 20;		// A safety margin, in case scrollbar is higher 
	var iTaskbarHeight = 33;

	// Align window to top/right corner
	try {
		var X = 0;
		if (screen.width < iLeftMargin+theImage.width+scrollbarWidth+iRightMargin) {
			X = iLeftMargin;	//window is larger than screen
		} else {
			if (screen.width < iLeftMargin+theImage.width+scrollbarWidth+iRightMargin) {
				// Image width larger than screen width
				X = screen.width  - iLeftMargin - theImage.width - scrollbarWidth - iRightMargin;
			} else {
				X = screen.width - theImage.width - scrollbarWidth - iRightMargin;
			}

		}
		window.moveTo(X,iTopMargin);

	} catch(e) {}
	
	// Resize window to image or screen size
	try {
		var iWidth = theImage.width+scrollbarWidth;
		var iHeight = theImage.height+50;
		if (screen.width < iLeftMargin+theImage.width+scrollbarWidth+iRightMargin) {
			// Image width larger than screen width
			iWidth = screen.width - iRightMargin - iLeftMargin;
		}
		if (screen.height < iBottomMargin+theImage.height+scrollbarWidth+iBottomMargin) {
			// Image height larger than screen height
			iHeight = screen.height - iBottomMargin - iBottomMargin - iTaskbarHeight;
		}
		window.resizeTo(iWidth, iHeight);

	} catch(e) {}
}

function CenterScrollbars(imgObj) {
	try {
		var X = imgObj.width - document.body.offsetWidth;
		var Y = imgObj.height - document.body.offsetHeight;
  		window.scrollTo(X/2,Y/2);
	} catch(e) {
//		alert("Failed to CenterScrollbars");
	}
}
