
// set wX or hY 0 if you want that dimension maximized
// i.e <a href="javascript:PhotoGallery('myDir','100percent.htm','0','0')">?</a>

// function PhotoGallery(Directory,NameOfPicOrPage,wX,hY) {
// var OnceOpened	= 0;
function PhotoGallery(mylink,wX,hY) {
	var refocus = true; // vőiks olla valitav
	var mywin, href;
	if (typeof(mylink) == 'string'){
		href=mylink;
	} else { 
		href=mylink.href;
	} 

//var dir			= Directory;
var	windowName	= "PhotoGallery";
var	w			= 0;
var	h			= 0;

// If you know the size of pic and if in HTML there is some extra
// space, (lets say for comments), then this is the space comment is using.
// addSome variables shoud be actually as "input" -> ( ... wX,wY,addSomeW, ... )
var	addSomeW	= 30,
	addSomeH	= 30,
	popW		= wX + addSomeW,
	popH		= hY + addSomeH,
	scrllBar		= "yes",
	size 		= "small",
	leftPos		= 0,
	topPos		= 0;

// slash testing - add slash if not there
// 					remove slash if dir lenght is 1 and is slash
	//if ((dir.length >= 2) && (dir.substring(dir.length-1,dir.length) != "/" )) { dir = dir + "/" }
	//if ((dir.length = 1) && (dir == "/")) { dir = "" }

// make an number popup window, which h & w divides with 2
// and remove "addSome" anomalia because it is
// used later for testing if pop == addSome
	
	if ((popW % 2) != 0){ popW = popW + 1; addSomeW	= addSomeW + 1; }
	if ((popH % 2) != 0){ popH = popH + 1; addSomeH	= addSomeH + 1; }

// which is better??
//	w = screen.availWidth - 10;
//	h = screen.availHeight - 30;
	w = screen.width - 20;
	h = screen.height - 30;

// if undef 
// or if there is some anomalia (is it possible??)
// or if you need much smaller popups than window
// i.e if(screen.width <= 640) { w = 450; }
// 	if(screen.width <= 800) { w = 600; }
//	if(screen.width <= 1024) { w = Math.round(screen.width * 0.9); } 
// Cacscading or Non Cascading, by % ... Javascript is pure fun :=)


	if ((w <= 0) || (screen.width < 640)) { w = 450; }
	if ((h <= 0) || (screen.height < 450)) { h = 450; }

// if too damn big popup (may NOT be removed)
	if ((popW > w) || ((addSomeW != 0)&&(popW == addSomeW)&&(w > 0)))
		{ leftPos = "ziro"; popW = w; scrllBar = "yes"; size = "big"; }
	if ((popH > h) || ((addSomeH != 0)&&(popH == addSomeH)&&(h > 0)))
		{ topPos = "ziro"; popH = h; scrllBar = "yes"; size = "big"; }

// if it wasn't the too damn big popup
	if (size == "small"){
		scrllBar = "no"; 
		popW = popW - 10;
		popH = popH - 10;}
//TEMPORARY TEST
		popW = popW + 10;
		popH = popH + 10;
//

// some positioning for a) to damn big and undef popups b) normal popups
	if (leftPos == "ziro"){ leftPos = 0 } else { leftPos = (w - popW)/2 }
	if (topPos == "ziro"){ topPos	= 0	} else { topPos	 = (h - popH)/2 }

mywin  = window.open(
	"",
	windowName,
	"width="		+ popW		+
	",height="		+ popH		+
	",scrollbars="	+ scrllBar	+
	",left="		+ leftPos	+
	",top="			+ topPos	+ 
	",toolbar=no"	+
	",status=no"	+
	",resize=yes"	+
	",menubars=no"	+
	",directories=no"
	);

mywin.resizeBy(popW-mywin.width,popH-mywin.height);
mywin.moveTo(leftPos, topPos);
//	var str = "<html><head><titile><\/titile><\/head><body style=\"margin:0px;padding:0px;\"><img src=\""+ href +"\" border=\"0\"><\/body><\/html>";
var popuphtml = '<html><head><title><\/title><\/head><body onBlur=\"self.close()\" style=\"margin:0px;padding:0px;\">' + 
	'<img src=\"'+href+'\" boder=\"0\" name=\"imagemax\"'
	+ ' onLoad=\"window.resizeTo(' + popW + ', ' + popH + ')\">'
	+'</body></html>';
	//+ ' onLoad=\"window.resizeTo(' + popW + ', ' + popH + ')\">'
	//+ ' onLoad=\"window.resizeBy(' + (popW-mywin.width) + ', ' + (popH-mywin.height) + ')\">'
	//+ ' onLoad="window.resizeTo(document.imagemax.width+10, document.imagemax.height+30)">'


	// if we just opened the window
	if (mywin.closed || (! mywin.document.URL) || (mywin.document.URL.indexOf("about") == 0)) {
		//mywin.location=href;
		//mywin.document.clear();
		//mywin.document.write(str);
		mywin.document.open();
		mywin.document.write(popuphtml);
		mywin.document.close();
	} else {
		//mywin.location=href;//..............
		//mywin.document.clear();
		//mywin.document.write(str);
		mywin.document.open();
		mywin.document.write(popuphtml);
		mywin.document.close();
		if(refocus) {
   			mywin.focus();
		} else {
			mywin.focus(); //.........
		}

	}
	return false;
// Win_02 = window.open('', 'win02');
// Win_02.document.write(somevar);
// close the current window
//window.close( );
// close a child window
//windowname.close( );
// close the parent window
//window.opener.close( );

// test to see if window exists
//if (typeof(windowName) != "undefined") { }
// test to see if a window is still open
//if (!(windowName.closed)) { }
// resize the current window to 200 pixels wide by 100 high
//window.resizeTo(200, 100);

// add 100 pixels to the width of the window
//window.resizeBy(100,0);

// move the window to a position
// - 100 pixels over from the left hand side of the screen
// - 50 pixels down from the top of the screen
//window.moveTo(100, 50):

// move the window back up 25 pixels
//window.moveBy(0, -25);
/*
if(OnceOpened != 0) {
if(window.pop.closed) {
pop = window.open(
	RelpathToNameOfPicOrPage,
	windowName,
	"width="		+ popW		+
	",height="		+ popH		+
	",scrollbars="	+ scrllBar	+
	",left="		+ leftPos	+
	",top="			+ topPos	+ 
	",toolbar=no"	+
	",status=no"	+
	",resize=yes"	+
	",menubars=no"	+
	",directories=no"
	);
}
else { pop.location.href = RelpathToNameOfPicOrPage }
}
else {
pop = window.open(
	RelpathToNameOfPicOrPage,
	windowName,
	"width="		+ popW		+
	",height="		+ popH		+
	",scrollbars="	+ scrllBar	+
	",screenX="		+ leftPos	+
	",screenY="		+ topPos	+
	",left="		+ leftPos	+
	",top="			+ topPos	+ 
	",toolbar=no"	+
	",status=no"	+
	",resize=no"	+
	",menubars=no"	+
	",directories=no"
	);
	OnceOpened = 1;
}


// If this script is used many times row then previous window must be closed
// ie. <body onBlur="self.close"> ... Other wise it do not resize window.
// Sux if previous window not closed :(
 pop.focus();
*/
}

// function reallyClosed() {
// 	if(OnceOpened != 0) {
// 		if(!window.pop.closed) {
// 			pop.opener='';
// 			pop.close();
// 			OnceOpened = 0;
// 		}
//	}
// }

