var dropped=0;
var intID;
var hideinterval = 2500;
var atmenu=false;
var newsInterval=9000;
var dataobj;
var thelength=280;
var scrollspeed=1;
var cache=1;
var cacheTop;
var initWait=5000;
var isOpera = navigator.userAgent.indexOf("Opera")>-1;
var warner = "glossary";
var old_refid;
var dtop = 30;
// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false;


// If NS -- that is, !IE -- then set up for mouse capture
//if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

//--- news scroller - initialization ---
function initializeNews(){
	dataobj=document.getElementById("divnews");
    thelength=dataobj.offsetHeight;

	scrollnews();
}
//--- scroll the news box ---
function scrollnews() {
	dataobj.style.top=dtop+"px";
    dtop-=scrollspeed;
	if (dtop<(thelength-200)*(-1)) {
        initWait=5000;
        dtop = 210;
    }
	setTimeout("scrollnews()",initWait);
	// reset the timeout after the initial interval has elapsed
	initWait=50;
}


function drop(n) {
	var sub = document.getElementById("submenu"+n);
	hide(dropped); //hide the already dropped submenu
	if (sub)
	{
		var menu = document.getElementById("menu"+n);
		var cornerX = document.getElementById("menucontainer").offsetLeft;
		var cornerY = document.getElementById("menucontainer").offsetTop;
		//set submenu position
		sub.style.left = (menu.offsetWidth + cornerX)+"px";
		sub.style.top = (8 + menu.offsetTop+cornerY)+"px";
		
		dropped=n;
		if(!isOpera) {
			var ifr = document.getElementById('ifr');
			// Init Iframe to have the same position and size as the div element and smaller zIndex 
			ifr.style.width = sub.offsetWidth; 
			ifr.style.height = sub.offsetHeight;
			ifr.style.top = sub.style.top;	
			ifr.style.left = sub.style.left; 
			ifr.style.zIndex = 899; 
			//ifr.style.display = "none";
			// make the div and the iframe visible
			ifr.style.display  = "block";
		}
		sub.style.visibility	= "visible";  
		intID = window.setInterval("hidemenu()",hideinterval);
	}
}

function hide(n) {
	document.getElementById("submenu"+n).style.visibility="hidden";
document.getElementById('ifr').style.display="none";
	clearInterval(intID);
}

function hidemenu() {
	if (!atmenu)
	{
		hide(dropped);
	}
}

function on() {
	atmenu=true;
}

function off() {
	atmenu=false;
}


function doSearch() {
	document.getElementById("hiddenfld").value = document.getElementById("searchfld").value + " site:dif.bg";
	//document.getElementById("searchform").submit();
	return (true);
}

function checkSearch() {

}

function switchNews() {
	var container=document.getElementById("newscontainer");
	var contents = document.getElementById("divnews");
	//pause news switching
	window.clearInterval(intervalid);
	//apply transition filter
	if (currentnews==newscnt)
	{
		currentnews=0;
	} else {
		currentnews++;
	}
	s=navigator.userAgent;
	
	if (s.indexOf("MSIE")>-1 && s.indexOf("Opera")<0 && s.indexOf ("Firefox")<0)
	{
		container.filters[0].apply();
		contents.innerHTML = "<p>"+news[currentnews]+"</p>";
		container.filters[0].play();

	} else {
		contents.innerHTML = "<p>"+news[currentnews]+"</p>";
	}

	window.setTimeout("switchNews()",newsInterval);
}

function yourOpinion() {
	var status = document.getElementById("opinion").style.display;
	if(status=="block") {
		status="none";	
	} else {
		status="block";
	}
	document.getElementById("opinion").style.display=status;
}	

//next functions are used to prevent comboboxes from overlapping
function showDiv(menuHandle){
	// Search for the div and the iframe elements
	var dv = document.getElementById(menuHandle);
	var ifr = 	document.getElementById('ifr'); 
	// Init Iframe to have the same position and size as the div element and smaller zIndex 
	ifr.style.width = dv.offsetWidth; 
	ifr.style.height = dv.offsetHeight; 
	ifr.style.top = dv.style.top;	
	ifr.style.left = dv.style.left; 
	ifr.style.zIndex = dv.style.zIndex - 1; 
	ifr.style.display = "none"; 
	// make the div and the iframe visible
	dv.style.visibility	= "visible";  
	ifr.style.display  = "block"; 
 }	
 function  hideDiv(){ 
	// Search for the div and the iframe elements
	var dv = document.getElementById('myDiv');
	var ifr = document.getElementById('myIframe'); 
	// hide the div and the iframe
	dv.style.visibility = "hidden"; 
	ifr.style.display = "none"; 
 }

 
function showFaq() {
	n=document.getElementById("cbFaq").value;
	document.getElementById("faq").innerHTML = faq[n];
}

function requestData(method,url,asyncMode, callback,showmsg) {
    //prepare the message to the user
    if(showmsg) {
        var warningDiv=document.getElementById(warner);
        warningDiv.innerHTML = "<img src='images/loading.gif' alt='Loading...' />";
    }
	if (window.XMLHttpRequest)     // Object of the current windows
	{
			request = new XMLHttpRequest();     // Firefox, Safari, ...
	}
	    else
        	if (window.ActiveXObject)   // ActiveX version
        	{
        		   request = new ActiveXObject("Microsoft.XMLHTTP");    // Internet Explorer
        	}
	request.onreadystatechange = function() { // instructions to process the response };

	    if (request.readyState == 4)
		{
			// received, OK
            //try {
    			eval(callback+" (request.responseText)");
            //}
            //catch(e) {
            //    alert("Exception: "+e);
            //}
		} else {

            //alert("AJAX INIT ERROR");
			}
		}
    if(method=="POST" || method=="post") {
        qry=url.split("?");
        request.open (method,qry[0],asyncMode);
        request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        request.setRequestHeader("Content-Length",qry[1].length+"\"");
        request.send(qry[1]);
    } else {
        request.open (method,url,asyncMode);
	    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        request.send(null);
    }

}

function glossary(id) {
    var newreq="getdefinition.php?id="+id;
    requestData("get",newreq,true,"showDefinition",true);
    reposition();
}

function reposition() {
    var dX, dY;
    var wdiv=document.getElementById(warner);
    var cont=document.getElementById("mainpage");
    var oW=cont.offsetWidth+(window.screen.availWidth-cont.offsetWidth)/2;
    var oH=cont.offsetHeight+190;
    wdiv.style.display="block";
    var W=wdiv.offsetWidth;
    var H=wdiv.offsetHeight;
    if ((W+tempX)>oW) dX=tempX-W; else dX=tempX;
    if ((H+tempY)>oH) dY=tempY-H; else dY=tempY;
    wdiv.style.left=dX+"px";
    wdiv.style.top=dY+"px";

}

function showDefinition(result) {
    if (result) {
        document.getElementById(warner).innerHTML=result;
    } else {
        document.getElementById(warner).innerHTML="<p> Error occurred. We apologize for the inconvenience.</p>";
    }
    reposition();
}

function hideDef() {
    document.getElementById(warner).style.display="none";

}


// Main function to retrieve mouse x-y pos.s
function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.documentElement.scrollLeft;
    tempY = event.clientY + document.documentElement.scrollTop;
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX;
    tempY = e.pageY;
  }
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0};
  if (tempY < 0){tempY = 0};
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  return true;
}


function hiliteOff(odiv){
    odiv.style.backgroundColor="transparent";
    //alert(document.body.scrollTop);

}

function hiliteDef(refid) {
    if(old_refid) {
        document.getElementById("div_"+old_refid).style.backgroundColor="transparent";
    }
    var dhi = document.getElementById("div_"+refid);
    dhi.style.backgroundColor="#f3feb3";
    old_refid=refid;

}
