var secs;
var timerID = null;
var timerRunning = false;
var delay = 500;

function getX(event){
    var myWidth = 0;
    var myMouseX = 0;
    myMouseX = event.clientX // mouse position
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
    } else if( document.documentElement && ( document.documentElement.clientWidth ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
    }
    if((myMouseX + 390) > myWidth) {
        return myMouseX - 390;
    } else {
        return myMouseX - 10;
    }
}

function getY(event){
    var myHeight = 0;
    var myMouseY = 0;
    if (typeof(window.innerHeight) == 'number') {
        //Non-IE
        myMouseY = event.pageY // mouse position on the page
        myHeight = window.innerHeight - event.clientY; // room under mouse position on the window
    } else if (document.documentElement && document.documentElement.clientHeight) {
        //IE 6+ in 'standards compliant mode'
        myMouseY = document.documentElement.scrollTop + event.clientY // mouse position on the page
        myHeight = document.documentElement.clientHeight - event.clientY; // room under mouse position on the window
    }
    if (myHeight < 214) {
        return myMouseY - 204;
    } else {
        return myMouseY - 10 ;
    }
}

function onMouseOverDekking(dekkingId, column, kostensoortId, actionUrl, event) {
    var popupElement = $('popup');
    popupElement.setStyle({
		left: (getX(event)) + 'px',
        top: (getY(event)) + 'px'
    });
	var url = $$('base')[0].readAttribute('href') + '/dekking/beschrijving.do';
    new Ajax.Request(url, {
        method: 'get',
        parameters: {
            dekkingId: dekkingId
        },
        onException: ajaxErrorHandler,
        onFailure: ajaxErrorHandler,
        onSuccess: function(response) {
			if(response.responseText != "empty") {
				$('popup_content').replace(response.responseText);
			}
        },
        onComplete: function(response) {
            // add handlers for video link
			if(response.responseText != "empty") {
				$('popup').show();
				document.getElementById("popup_content").scrollTop = -99999999; //setting scrollTop property to very high value
			}
            if (pageTracker) pageTracker._trackPageview(url);
        }
    });
}

function onMouseOverFootnote(me, dekkingId, event) {
    var popupElement = $('popup');
    popupElement.setStyle({
        left: (getX(event)) + 'px',
        top: (getY(event)) + 'px'
    });
    var url = $$('base')[0].readAttribute('href') + '/dekking/footnote.do';
    new Ajax.Request(url, {
        method: 'get',
        parameters: {
            dekking: dekkingId
        },
        onException: ajaxErrorHandler,
        onFailure: ajaxErrorHandler,
        onSuccess: function(response, json){
            $('popup_content').replace(response.responseText);
        },
        onComplete: function() {
            $('popup').show();
			document.getElementById("popup_content").scrollTop = -99999999; //setting scrollTop property to very high value
            if (pageTracker) pageTracker._trackPageview(url);
        }
    });
}

function onMouseOutDekking() {
    StartTheTimer();
}

function onMouseInDekking(){
    StopTheTimer();
}
function onMouseOutFootnote() {
    StartTheTimer();
}

function onMouseInFootnote(){
    StopTheTimer();
}


function InitializeTimer()
{
    StopTheTimer();
}

function StopTheTimer()
{
    if(timerRunning){
        clearTimeout(timerID);
        
    }
    timerRunning = false;
    secs = 1;
}

function StartTheTimer()
{
    if (secs==0)
    {
        $('popup').hide();
        StopTheTimer();
    }
    else
    {
        self.status = secs;
        secs = secs - 1;
        timerRunning = true;
        timerID = self.setTimeout("StartTheTimer()", delay)
    }
}
