var winList=new Array();
var winNames=new Array();
var winLastIndex=-1;

function jwin_getIndexWinByName(winNameP){
  var ret=-1;
  for(var i=0;i<winNames.length;i++)
    if (winNames[i]==winNameP) return i;
  return ret;
}
function jwin_open(linkP,nameP,widthP,heightP,propertiesP){
  var mscroll, mtop, mleft, winObj;
  var retValue=false;
  
  var winObjInd=jwin_getIndexWinByName(nameP);
  var winObj=null;
  if (winObjInd>-1)
    winObj=winList[winObjInd];

  if (winObj!=null && !winObj.closed){
    winObj.focus();
    if (winObj.location.href!=linkP.href){
      winObj.location.href=linkP.href;
    }
    retValue=true;
  }else{
    //ak nie je null a som tu tam bolo zatvorene ale v list a index zostal
    var winIndex=(winObj==null) ? winLastIndex+1 : winObjInd;
    if (screen){
      scrScroll = (screen.width<700) ? "yes," : "no";
      scrLeft = (screen.width<700) ? 0 : (screen.width-widthP)/2;
      scrTop = (screen.height<600) ? 0 : (screen.height-heightP)/2;
    }else{
    	scrTop = 100;
    	scrLeft = 100;
    	scrScroll = "yes";
    }
    winList[winIndex]=window.open(linkP.href,nameP,"width="+widthP+",height="+heightP+",top="+scrTop+",left="+scrLeft+",directories=no,menubar=no,toolbar=no,location=nos,status=no,resizable=yes,copyhistory=no,scrollbars="+scrScroll+propertiesP);
    
    if (winList[winIndex]==null || typeof(winList[winIndex])=="undefined")
      retValue=false;
    else{
      retValue=true;
      winNames[winIndex]=nameP;
      winLastIndex=winIndex;
      linkP.target=nameP;
      winList[winIndex].focus();
    }
  }  
  
  return retValue;  
}
function jwin_resizeByName(nameP,widthP,heightP,moveToXP,moveToYP,winFocusP){
  var winObjInd=jwin_getIndexWinByName(nameP);
  if (winObjInd>-1)
    return jwin_resize(winList[winObjInd],widthP,heightP,moveToXP,moveToYP,winFocusP);
  return false;
}
function jwin_resize(winP,widthP,heightP,moveToXP,moveToYP,winFocusP)
{
  var mtoX, mtoY;
  
  if (screen){
  	if (moveToXP==0)
    	mtoX=xMouse;
		else if (moveToXP==-1) //centruj horizontal
			 mtoX = (screen.width<700) ? 10 : (screen.width-widthP)/2;
		else
			 mtoX = moveToXP;
  	if (moveToYP==0)
    	mtoY=yMouse;
		else if (moveToYP==-1) //centruj vertikal
		   mtoY = (screen.height<600) ? 10 : (screen.height-heightP)/2;
		else
			mtoY = moveToYP;
  }
  else{
	 	mtoY = 10;
		mtoX = 10;
  }
  if (widthP>0 && heightP>0)
    winP.resizeTo(widthP,heightP);
	winP.moveTo(mtoX,mtoY);
	if (winFocusP)
    winP.focus();
	return true;
}
function jwin_open_u(url,nameW,widthW,heightW,properties,winfocus){
  var bScreen = is.ie || is.ns;
  var mscroll, mtop, mleft, winObj;
  if (bScreen){
    mscroll = (screen.width<700) ? "yes" : "no";
    mleft = (screen.width<700) ? 0 : (screen.width-widthW)/2;
    mtop = (screen.height<600) ? 0 : (screen.height-heightW)/2;
  }
  else{
  	mtop = 0;
  	mleft = 0;
  	mscroll = "yes";
  }
  
	winObj = window.open(url, nameW,"width="+widthW+",height="+heightW+",top="+mtop+",left="+mleft+",directories=no,menubar=no,toolbar=no,location=no,status=no,resizable=no,copyhistory=no,scrollbars="+mscroll+properties);
  if (winfocus){ winObj.focus(); }  
  return winObj;
}

