

   var fbwindow;
   function PopupPic(sPicURL,stitle) {
     //alert(sPicURL)
     try{
       if(fbWindow)
         fbWindow.windowfocus();
       alert('ok')
     }catch(e){

     }

     fbwindow = window.open( 
			"templates/default/popupWindow.html?"+sPicURL,
			'newWin',
			"resizable=1,HEIGHT=200,WIDTH=200,location=NO,menubar=NO,status=NO,toolbar=NO"
			);
   }

iZoom = 0;
iZoomState = 0;	// 0 - idle, 1 -zoom in, 2 - zoom out

iFotoPanelOpacity = 0;
var oPanel;
var oLastSourceObjectId = '';
var imgLoading = new Image;
var oMovingObject = null;
imgLoading.src = 'templates/default/img/loading.gif';

var img = new Image;
imgLoaded = false;

var sPost = (navigator.userAgent.indexOf('Firefox') !=-1)?'px':'';

var panel_dim = new Array();
var img_dim = new Array();
var dims = new Array();
dims[0] = panel_dim;
dims[1] = img_dim;

var aThreads = new Array();
var iThreadsCount = 0;
var iThreadsIndex = 0;
var iMaxThreadsCount = 10;
var bThreadsExecuting = false;

function process_threads(){
	bThreadsExecuting = true;
	while ((iThreadsIndex >=0) && (iThreadsIndex < iMaxThreadsCount) && (iThreadsCount > 0)){	
		// execute thread
		sThread = aThreads[iThreadsIndex];
		// exec(sThread)
	
		// goto next	
		iThreadsIndex = ((++iThreadsIndex) < iMaxThreadsCount)?iThreadsIndex:0;
		iThreadsCount--;	
	}
	bThreadsExecuting = false;
}

function append_thread(sThread){
	if (iThreadsCount < iMaxThreadsCount) {
		newIdx = (iThreadsIndex + iThreadsCount ) % iMaxThreadsCount;
		aThreads[newIdx] = sThread;
		iThreadsCount++;
	}
	process_threads();
}

function setOpacity(obj, opacity) {

	opacity = (opacity == 100)?99.999:opacity;
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

function getOpacity(obj, opacity) {

	opacity = (opacity == 100)?99.999:opacity;


  // IE/Win
	if (obj.style.opacity) {
		return obj.style.opacity;
	} else {return -1;};
	
}

function zoomIn(movingObjectId,dimId){	
	if (iZoomState == 2) {
		window.setTimeout("zoomIn('"+movingObjectId+"',"+dimId+")", 250);
		return;
	}
	
	iZoomState = 1;

	var movingObject = gEBI(movingObjectId);
	
	if (movingObject) {		
		
		if (iZoom < 0) {zoom = 0;};
		
		dx = (dims[dimId]['iCenterLeft'] - dims[dimId]['iSourceLeft'])*(iZoom/100);
		dy = (dims[dimId]['iCenterTop'] - dims[dimId]['iSourceTop'])*(iZoom/100);
		dw = (dims[dimId]['iCenterWidth'] - dims[dimId]['iSourceWidth'])*(iZoom/100);
		dh = (dims[dimId]['iCenterHeight'] - dims[dimId]['iSourceHeight'])*(iZoom/100);
		
		movingObject.style.left = (dims[dimId]['iSourceLeft'] + dx)+sPost;
		movingObject.style.top = (dims[dimId]['iSourceTop'] + dy)+sPost;
		movingObject.style.height = (dims[dimId]['iSourceHeight'] + dh)+sPost;
		movingObject.style.width = (dims[dimId]['iSourceWidth'] + dw)+sPost;
		
		bZoom = (iZoom != 100);
		
		iZoom += 7;
		
		if (iZoom > 100) {		
			iZoom = 100;
		}
		
		if ((iZoomState ==1) ){		
			if (bZoom) {
				oMovingObject = movingObject;
				window.setTimeout("zoomIn('"+movingObjectId+"',"+dimId+")", 50);
			}	else {
				iZoomState = 0;
			}
		} else {
			iZoomState = 0;
		}
		
	}
}

function zoomOut(movingObjectId,dimId){	
	if (iZoomState == 1) {
			window.setTimeout("zoomOut('"+movingObjectId+"',"+dimId+")", 250);
			return;
	}
	iZoomState = 2;
	
	movingObject = gEBI(movingObjectId);

	if (movingObject) {						
		
		dx = (dims[dimId]['iCenterLeft'] - dims[dimId]['iSourceLeft'])*(iZoom/100);
		dy = (dims[dimId]['iCenterTop'] - dims[dimId]['iSourceTop'])*(iZoom/100);
		dw = (dims[dimId]['iCenterWidth'] - dims[dimId]['iSourceWidth'])*(iZoom/100);
		dh = (dims[dimId]['iCenterHeight'] - dims[dimId]['iSourceHeight'])*(iZoom/100);
		
		movingObject.style.left = (dims[dimId]['iSourceLeft'] + dx)+sPost;
		movingObject.style.top = (dims[dimId]['iSourceTop'] + dy)+sPost;
		movingObject.style.height = (dims[dimId]['iSourceHeight'] + dh)+sPost;
		movingObject.style.width = (dims[dimId]['iSourceWidth'] + dw)+sPost;
		
		bZoom = (iZoom != 0);
		
		iZoom -= 20;
		
		if (iZoom < 0) {		
			iZoom = 0;
		}
		
		if ((iZoomState ==2) ){
			if (bZoom) {
				oMovingObject = movingObject;
				window.setTimeout("zoomOut('"+movingObjectId+"',"+dimId+")", 50);
			} else {
				iZoomState = 0;
			}
		}  else {
			iZoomState = 0;
		}
		
	}
}

function fadeIn(objId,opacity) {

	if (document.getElementById) {
		obj = document.getElementById(objId);			
	
		if (opacity > -1) {
			iFotoPanelOpacity = opacity;			
		}
		obj.style.visibility = 'visible';
		obj.style.display = 'block';
		
		bFade = (iFotoPanelOpacity != 100);
		iFotoPanelOpacity += 7;			
		if (iFotoPanelOpacity > 100) {
			iFotoPanelOpacity = 100;
		}
		
    if (bFade) {
			setOpacity(obj, iFotoPanelOpacity);			
      window.setTimeout("fadeIn('"+objId+"',"+iFotoPanelOpacity+")", 50);
		} else {
			iFotoPanelOpacity = 100;
			setOpacity(obj,iFotoPanelOpacity);
		}
	}
}

function fadeOut(objId,opacity) {

	if (document.getElementById) {
		obj = document.getElementById(objId);
		if (opacity > -1) {
			iFotoPanelOpacity = opacity;
		}
				
		bFade = (iFotoPanelOpacity != 40);
		iFotoPanelOpacity -= 12;			
		if (iFotoPanelOpacity < 40) {
			iFotoPanelOpacity = 40;
		}
		
    if (bFade) {
			setOpacity(obj, iFotoPanelOpacity);
			
			if (iFotoPanelOpacity <=40) {
				obj.style.display = 'none';
				obj.style.visibility = 'hidden';
			}
			window.setTimeout("fadeOut('"+objId+"',"+iFotoPanelOpacity+")", 50);
		} else {
			iFotoPanelOpacity = 40;
			setOpacity(obj,iFotoPanelOpacity);
		}
	}
}


function onImgLoad(){
	oPanel = gEBI('fotoPanel');
	var oPanelId = 'fotoPanel';
	var oPanelImg = gEBI('fotoPanelImg');
	var oPanelImgLoading = gEBI('fotoPanelImgLoading');
	var oPanelTitle = gEBI('fotoPanelTitle');	
	var oPanelInfo = gEBI('fotoPanelInfo');		var oPanelClose = gEBI('fotoPanelClose');
	var oLimiter = gEBI('panelImgLimiter');
	
		
	
	dims[0]['iCenterLeft'] = (dims[0]['iCenterLeft']-((img.width > 500)?(img.width/3):0));
	dims[0]['iCenterWidth'] = img.width+26;
	dims[0]['iCenterHeight'] = img.height+40;
	
	var yLimit = 0;
	
	if (oLimiter) {
		yLimit = oLimiter.offsetTop;
		var oParent = oLimiter.offsetParent;
		while (oParent) {			
			yLimit += oParent.offsetTop;
			oParent = oParent.offsetParent;
		}
		if ((yLimit > 30) && ((dims[0]['iCenterTop']+dims[0]['iCenterHeight']) > yLimit)) {
			dims[0]['iCenterTop'] = yLimit - dims[0]['iCenterHeight'];
			if (dims[0]['iCenterTop'] < 40) {
				dims[0]['iCenterTop'] = 40;
			};
		}
	}
	
//	oPanel.style.left = (iCenterLeft-((img.width > 500)?(img.width/3):0))+sPost;
//	oPanel.style.top = iCenterTop+sPost;	
	fadeIn('fotoPanel',-1);
		iZoom = 0;
	zoomIn('fotoPanel',0);	
	
	oPanelImg.src = img.src;	  
	//oPanelImg.style.width = img.width+sPost;
	//oPanelImg.style.height = img.height+sPost;
	//oPanel.style.width = (oCenterWidth)+sPost;
	//oPanel.style.height = (oCenterHeight)+sPost;
	oPanelImgLoading.style.visibility = 'hidden';
	oPanelImgLoading.style.display = 'none';
	oPanelImg.style.visibility = 'visible';		
	oPanelImg.style.display='block';
	oPanelInfo.style.display ='block';
	oPanelInfo.style.visibility ='visible';	oPanelClose.style.left = (img.width) + sPost;		oPanelClose.style.top = (5) + sPost;
	
	
	fadeIn('fotoPanelImg',-1);
	
	imgLoaded = true;
	
}


function showPic(sPicURL,stitle,img_w,img_h,oSourceObjectId,oFromObjectId){
	if ((oSourceObjectId == oLastSourceObjectId) ) return;
	
	if (iZoomState!=0) {
		window.setTimeout("showPic('"+sPicURL+"','"+stitle+"',"+img_w+','+img_h+",'"+oSourceObjectId+"','"+oFromObjectId+"')", 100);
		return;
	}
	
	oLastSourceObjectId = oSourceObjectId;

	oPanel = gEBI('fotoPanel');
	var oPanelId = 'fotoPanel';
	var oPanelImg = gEBI('fotoPanelImg');
	var oPanelImgLoading = gEBI('fotoPanelImgLoading');
	var oPanelTitle = gEBI('fotoPanelTitle');
	var oPanelTd = gEBI('fotoPanelTd');	
	var oPanelInfo = gEBI('fotoPanelInfo');
	var oFromObject = gEBI(oFromObjectId);
	var oSourceObject = gEBI(oSourceObjectId);
	var oLimiter = gEBI('panelImgLimiter');
	
	imgLoaded = false;
	
	dims[0]['iCenterLeft'] = oFromObject.offsetLeft;//+oFromObject.style.width/2;
	dims[0]['iCenterTop'] = oFromObject.offsetTop+(oFromObject.offsetHeight)+2;// + oFromObject.style.height;	
	dims[0]['iCenterWidth'] = 240;
	dims[0]['iCenterHeight'] = 240;
	
	var oParent = oFromObject.offsetParent;
	while (oParent) {
		dims[0]['iCenterLeft'] += oParent.offsetLeft;
		dims[0]['iCenterTop'] += oParent.offsetTop;
		oParent = oParent.offsetParent;
	}
	
	var yLimit = 0;
	if (oLimiter) {
		yLimit = oLimiter.offsetTop;
		var oParent = oLimiter.offsetParent;
		while (oParent) {			
			yLimit += oParent.offsetTop;
			oParent = oParent.offsetParent;
		}
		if ((yLimit > 30) && ((dims[0]['iCenterTop']+dims[0]['iCenterHeight']) > yLimit)) {
			dims[0]['iCenterTop'] = yLimit - dims[0]['iCenterHeight'];
			if (dims[0]['iCenterTop'] < 40) {
				dims[0]['iCenterTop'] = 40;
			};
		}
	}
	
	dims[0]['iSourceLeft'] = oSourceObject.offsetLeft;
	dims[0]['iSourceTop'] = oSourceObject.offsetTop;
	dims[0]['iSourceWidth'] = oSourceObject.offsetWidth;
	dims[0]['iSourceHeight'] = oSourceObject.offsetHeight;
	
	var oParent = oSourceObject.offsetParent;
	while (oParent) {
		dims[0]['iSourceLeft'] += oParent.offsetLeft;
		dims[0]['iSourceTop'] += oParent.offsetTop;
		oParent = oParent.offsetParent;
	}
		
	setOpacity(oPanelImg,0);
	fadeIn('fotoPanel',1);
	zoomIn('fotoPanel',0);	
	
	
	oPanel.style.visibility = 'visible';		
	oPanel.style.display='block';
	//oPanel.style.width = 240+sPost;
	//oPanel.style.height = 240+sPost;		
	
	oPanelImg.style.visibility = 'hidden';
	oPanelImg.style.display = 'none';
	oPanelImg.style.width = "100%";//50+sPost;
	oPanelImg.style.height = "100%";//50+sPost;
	
	oPanelImgLoading.style.visibility = 'hidden';		
	oPanelImgLoading.style.display='none';
	oPanelTd.style.width = '100%'; //200+sPost;
	oPanelTd.style.height = '100%'; //200+sPost;
	oPanel.style.backgroundImage = 'url('+imgLoading.src+')';
	oPanel.style.backgroundRepeat = 'no-repeat';
	oPanel.style.backgroundPosition = 'center center';
	oPanelInfo.style.display ='none';
	oPanelInfo.style.visibility ='hidden';
	//oPanelImg.onload = fitImg;		
	
	oPanelTitle.innerHTML = stitle;
	
	
	//oPanel.style.top = iCenterTop+sPost;
	//oPanel.style.left = iCenterLeft+sPost;
	
	img = new Image;
	img.onload = onImgLoad;
	img.src = sPicURL;	
	
	
}



function hidePic(){
	var oPanel = gEBI('fotoPanel');
	if ((oPanel) && (iZoomState==0)){
		//oPanel.style.display='none';		
		//oPanel.style.visibility = 'hidden';
		fadeOut('fotoPanel',-1,oPanel);
		zoomOut('fotoPanel',0);
		oLastSourceObjectId = null;
	}
}
