window.onerror=null;
var currentPopup=null;
var currentPopupOpt=null;

if(ie()){
	window.document.onmousedown=newsLinkOnMouseDown;
} else {
	window.document.onmousedown=notIeOnMouseDown;
}
function clickedOnPopup(evt){
	if(currentPopup!=null){
		if(ie()){
			var xPos=event.clientX+document.body.scrollLeft;
			var yPos=event.clientY+document.body.scrollTop;
		} else {
			var xPos=evt.clientX+document.body.scrollLeft;
			var yPos=evt.clientY+document.body.scrollTop;
		}
		var rect=new MyRectangle(currentPopup);
		if((rect.left<=xPos)&&(rect.right>=xPos)&&(rect.top<=yPos)&&(rect.bottom>=yPos)){
			return true;
		}
	}
	return false;
}
function hidePopup(popup){
	if(popup==null){
		popup=currentPopup;
	}
	if(popup!=null){
		popup.className="hidden";
		currentPopup=null;
		currentPopupOpt=null;
	}
}
function notIeOnMouseDown(event){}
function newsLinkOnMouseDown(event){
	if(ie()){
		if(!clickedOnPopup(event)){
			hidePopup();
		}
	} else {
		if(clickedOnPopup(event)){
			hidePopup();
		}
	}
}
function ie(){
	return (navigator.userAgent.toLowerCase().indexOf("msie")>=0);
}
function onPopupTagMouseOver(opt, evt){
	if(!ie()){
		var srcElement=evt.target;
		if(evt.target.className!="ig"){
			srcElement=srcElement.parentNode;
		}
		if(srcElement!=currentPopup){
			if(currentPopup!=null){
				hidePopup(currentPopup);
			}
			var popup=selectPopup(srcElement,opt);
			displayPopup(srcElement,popup,evt);
		}
	} else {
		var srcElement=event.srcElement;
		if (event.srcElement.className!="ig"){
			srcElement=srcElement.parentNode;
		}
		if (srcElement!=currentPopup){
			if(currentPopup!=null){
				hidePopup(currentPopup);
			}
			var popup=selectPopup(srcElement,opt);
			displayPopup(srcElement,popup);
		}
	}
}

function onPopupMouseOut(evt){
	if(currentPopup!=null){
		var rect=new MyRectangle(currentPopup);
		if(ie()){
			var xPos=event.clientX+document.body.scrollLeft;
			var yPos=event.clientY+document.body.scrollTop;
			if ((rect.left>=(xPos-5))||(rect.right<=xPos)||(rect.top>=yPos)||(rect.bottom<=yPos)){
				hidePopup(currentPopup);
			}
		} else {
			var xPos=evt.clientX+document.body.scrollLeft;
			var yPos=evt.clientY+document.body.scrollTop;
			var yTop=rect.top-document.body.scrollTop;
			var yBottom=rect.bottom-document.body.scrollTop;
			if ((yTop > yPos) || (yBottom < yPos)||(rect.left > xPos) ||(rect.right < xPos)){
				hidePopup(currentPopup);
			}
		}
	}
}
function selectPopup(srcElement,opt){		
	var integrationIndex=srcElement.getAttribute("ii");
	var popup=document.all("ip"+integrationIndex);
	var as=getLikeChildren(popup,'a','aref');
	for(var i=0;i<as.length;i++)
	{
		var _aref = as[i].getAttribute('aref');
		var idx = _aref.indexOf('videopopup');
		if(idx != -1) {
			var url = _aref.substring(0, idx) + opt;
			//var loc = window.location;
		//	as[i].href = "javascript:window.open('" + url + "', 'videochat','width=750,height=670'); window.location='" + loc + "'";
			as[i].href = "javascript:OpenVideochatWindow('" + url + "', 775, 715); window.location=window.location;";
		}
		else
			as[i].href=as[i].getAttribute('aref')+opt; 
	}
	currentPopupOpt=opt;
	return popup;
}
function displayPopup(nearElement,popup,evt){
	if(ie()){
		var num=popup.num;
		var ord=popup.ord;
		var clientX=event.clientX+document.body.scrollLeft;
		var clientY=event.clientY+document.body.scrollTop;
		var vpp=RicoUtil.toViewportPosition(nearElement);
		popup.className="visible";
		popup.style.top=vpp.y-3;
		popup.style.left=vpp.x+15;
		currentPopup=popup;
	} else {
		var num=popup.num;
		var ord=popup.ord;
		var vpp=RicoUtil.toViewportPosition(nearElement);
		var y=vpp.y-document.body.scrollTop;
		var x=vpp.x-document.body.scrollLeft;
		popup.className="visible";
		popup.style.top=y-15;
		popup.style.left=x+15;//vpp.x+15;
		currentPopup=popup;
	}
}
function MyRectangle(el){
	var vpp=RicoUtil.toViewportPosition(el);
	this.left=vpp.x;
	this.top=vpp.y;
	this.right=this.left+el.clientWidth;
	this.bottom=this.top+el.clientHeight;
}

function OpenVideochatWindow( sUri, iWidth, iHeight )
{
        var sWindowName = 'win' + Math.floor( Math.random()*1000 );
        var iRealWidth = iWidth ? iWidth : 600;
        var iRealHeight = iHeight ? iHeight : screen.height - 300;

        var iLeft = Math.round( (screen.width-iRealWidth)/2 );
        var iTop =  Math.round( (screen.height-iRealHeight)/2 ) - 135;

        var sWindowOptions = 'status=no,menubar=no,toolbar=no';
        sWindowOptions += ',resizable=yes,scrollbars=no,location=no';
        sWindowOptions += ',width=' + iRealWidth;
        sWindowOptions += ',height=' + iRealHeight;
        sWindowOptions += ',left=' + iLeft;
        sWindowOptions += ',top=' + iTop;

        var oWindow = window.open( sUri, /*sWindowName*/ '_blank', sWindowOptions );
        //window.title = 'Видеочат';
        oWindow.focus();
        return oWindow;
}
