var floatingMenuId = 'floatdiv';
var wndUpdater = null;
var floatingMenu = 
{
	targetX: -280,
	targetY: -370,

    hasInner: typeof(window.innerWidth) == 'number',
    hasElement: typeof(document.documentElement) == 'object'
        && typeof(document.documentElement.clientWidth) == 'number',

    menu:
        document.getElementById
        ? document.getElementById(floatingMenuId)
        : document.all
          ? document.all[floatingMenuId]
          : document.layers[floatingMenuId], 
    updaterInterval: 0               
};

floatingMenu.move = function ()
{
    floatingMenu.menu.style.left = floatingMenu.nextX + 'px';
    floatingMenu.menu.style.top = floatingMenu.nextY + 'px';
}

floatingMenu.computeShifts = function ()
{
    var de = document.documentElement;

    floatingMenu.shiftX =  
        floatingMenu.hasInner  
        ? pageXOffset  
        : floatingMenu.hasElement  
          ? de.scrollLeft  
          : document.body.scrollLeft;  
    if (floatingMenu.targetX < 0)
    {
        floatingMenu.shiftX +=
            floatingMenu.hasElement
            ? de.clientWidth
            : document.body.clientWidth;
    }

    floatingMenu.shiftY = 
        floatingMenu.hasInner
        ? pageYOffset
        : floatingMenu.hasElement
          ? de.scrollTop
          : document.body.scrollTop;
    if (floatingMenu.targetY < 0)
    {
        if (floatingMenu.hasElement && floatingMenu.hasInner)
        {
            // Handle Opera 8 problems
            floatingMenu.shiftY +=
                de.clientHeight > window.innerHeight
                ? window.innerHeight
                : de.clientHeight
        }
        else
        {
            floatingMenu.shiftY +=
                floatingMenu.hasElement
                ? de.clientHeight
                : document.body.clientHeight;
        }
    }
}

floatingMenu.calculateCornerX = function()
{
    if (floatingMenu.targetX != 'center')
        return floatingMenu.shiftX + floatingMenu.targetX;

    var width = parseInt(floatingMenu.menu.offsetWidth);

    var cornerX =
        floatingMenu.hasElement
        ? (floatingMenu.hasInner
           ? pageXOffset
           : document.documentElement.scrollLeft) + 
          (document.documentElement.clientWidth - width)/2
        : document.body.scrollLeft + 
          (document.body.clientWidth - width)/2;
    return cornerX;
};

floatingMenu.calculateCornerY = function()
{
    if (floatingMenu.targetY != 'center')
        return floatingMenu.shiftY + floatingMenu.targetY;

    var height = parseInt(floatingMenu.menu.offsetHeight);

    // Handle Opera 8 problems
    var clientHeight = 
        floatingMenu.hasElement && floatingMenu.hasInner
        && document.documentElement.clientHeight 
            > window.innerHeight
        ? window.innerHeight
        : document.documentElement.clientHeight

    var cornerY =
        floatingMenu.hasElement
        ? (floatingMenu.hasInner  
           ? pageYOffset
           : document.documentElement.scrollTop) + 
          (clientHeight - height)/2
        : document.body.scrollTop + 
          (document.body.clientHeight - height)/2;
    return cornerY;
};

floatingMenu.doFloat = function()
{
    // Check if reference to menu was lost due
    // to ajax manipuations
    if (!floatingMenu.menu)
    {
        menu = document.getElementById
            ? document.getElementById(floatingMenuId)
            : document.all
              ? document.all[floatingMenuId]
              : document.layers[floatingMenuId];

        initSecondary();
    }

    var stepX, stepY;

    floatingMenu.computeShifts();

    var cornerX = floatingMenu.calculateCornerX();

    var stepX = (cornerX - floatingMenu.nextX) * 1; //.07;
    if (Math.abs(stepX) < .5)
    {
        stepX = cornerX - floatingMenu.nextX;
    }

    var cornerY = floatingMenu.calculateCornerY();

    var stepY = (cornerY - floatingMenu.nextY) * 1; //.07;
    if (Math.abs(stepY) < .5)
    {
        stepY = cornerY - floatingMenu.nextY;
    }

    if (Math.abs(stepX) > 0 ||
        Math.abs(stepY) > 0)
    {
        floatingMenu.nextX += stepX;
        floatingMenu.nextY += stepY;
        floatingMenu.move();
    }

    setTimeout('floatingMenu.doFloat()', 20);
};

// addEvent designed by Aaron Moore
floatingMenu.addEvent = function(element, listener, handler)
{
    if(typeof element[listener] != 'function' || 
       typeof element[listener + '_num'] == 'undefined')
    {
        element[listener + '_num'] = 0;
        if (typeof element[listener] == 'function')
        {
            element[listener + 0] = element[listener];
            element[listener + '_num']++;
        }
        element[listener] = function(e)
        {
            var r = true;
            e = (e) ? e : window.event;
            for(var i = element[listener + '_num'] -1; i >= 0; i--)
            {
                if(element[listener + i](e) == false)
                    r = false;
            }
            return r;
        }
    }

    //if handler is not already stored, assign it
    for(var i = 0; i < element[listener + '_num']; i++)
        if(element[listener + i] == handler)
            return;
    element[listener + element[listener + '_num']] = handler;
    element[listener + '_num']++;
};

var textResponse = function() {
	// alert('Response: ' + r);
	var val = document.getElementById('floatcontent');
	if(val) val.innerHTML = wndUpdater.response;
};

function updateSidebar(url) {
	wndUpdater.reset();
	wndUpdater.method = 'GET';
	wndUpdater.onCompletion = textResponse;
	wndUpdater.runAJAX();
}

floatingMenu.hide = function()  
{  
    floatingMenu.menu.style.display='none';  
    document.getElementById('mfloatdiv').style.display='block';
    document.getElementById('mfloatdiv').style.position='fixed';
    document.getElementById('mfloatdiv').style.bottom = -50;
    document.getElementById('mfloatdiv').style.right = -50;
    createCookie('', 'fwnd_show', 'false', 30);
    if(floatingMenu.updaterInterval>0)  {
    	clearInterval(floatingMenu.updaterInterval);
    	floatingMenu.updaterInterval = 0;
    }
    return false;  
}  
   
floatingMenu.show = function() {  
	 floatingMenu.menu.style.display='block';  
     document.getElementById('mfloatdiv').style.display='none';
     createCookie('', 'fwnd_show', 'true', 30);
     if(floatingMenu.updaterInterval==0)  {
   		updateSidebar('/toif.css');
   		floatingMenu.updaterInterval = setInterval('updateSidebar(\'/toif.css\')', 60000 * 1); 
     }
     return false;  
}  

floatingMenu.initFloat = function()
{
	wndUpdater = new sack('/toif.css');
	
	floatingMenu.menu =
        document.getElementById
        ? document.getElementById(floatingMenuId)
        : document.all
          ? document.all[floatingMenuId]
          : document.layers[floatingMenuId];

	var version = 7; // we assume a sane browser
	if(navigator.appName=="Microsoft Internet Explorer") {
		if (navigator.appVersion.indexOf("MSIE") != -1) {
			version = parseFloat(navigator.appVersion.split("MSIE")[1]);
		}
	}
	if(navigator.cookieEnabled && version>6) {
		var v=readCookie('fwnd_show'); 
		if(v==null) v = 'flase';
		
		if(v=='true') {
		    floatingMenu.show();  
		}
		else {
		    floatingMenu.hide();  
		}
		floatingMenu.initSecondary();
	    floatingMenu.doFloat();
		/* if (document.layers) {
			floatingMenu.init();
		}
		else
		{
		    floatingMenu.init();
		    floatingMenu.initSecondary();
		}*/
	}
	else {
	    floatingMenu.menu.style.display='none';  
	    document.getElementById('mfloatdiv').style.display='none';
	}
};

// Some browsers init scrollbars only after
// full document load.
floatingMenu.initSecondary = function()
{
	var v=readCookie('fwnd_x'); 
	if(v) {
		var n = parseInt(v);
		floatingMenu.targetX = (v=='center')?'center':(isNaN(n)?-280:n);
	}
	v=readCookie('fwnd_y'); 
	if(v) {
		var n = parseInt(v);
		floatingMenu.targetY = (v=='center')?'center':(isNaN(n)?-370:n);
	}
	
	floatingMenu.computeShifts();
    floatingMenu.nextX = floatingMenu.calculateCornerX();
    floatingMenu.nextY = floatingMenu.calculateCornerY();
    floatingMenu.move(); 
}

function move_upper_left()
{
floatingMenu.targetX=10;
floatingMenu.targetY=10;
createCookie('','fwnd_x', 10,30);
createCookie('','fwnd_y', 10,30);
}
 
function move_upper_center()
{
floatingMenu.targetX='center';
floatingMenu.targetY=10;
createCookie('','fwnd_x', 'center',30);
createCookie('','fwnd_y', 10,30);
}
 
function move_upper_right()
{
floatingMenu.targetX=-280;
floatingMenu.targetY=10;
createCookie('','fwnd_x', -280,30);
createCookie('','fwnd_y', 10,30);
}
 
function move_center_left()
{
floatingMenu.targetX=10;
floatingMenu.targetY='center';
createCookie('','fwnd_x', 10,30);
createCookie('','fwnd_y', 'center',30);
}
 
function move_center_center()
{
floatingMenu.targetX='center';
floatingMenu.targetY='center';
createCookie('','fwnd_x', 'center',30);
createCookie('','fwnd_y', 'center',30);
}
 
function move_center_right()
{
floatingMenu.targetX=-280;
floatingMenu.targetY='center';
createCookie('','fwnd_x', -280,30);
createCookie('','fwnd_y', 'center',30);
}
 
function move_lower_left()
{
floatingMenu.targetX=10;
floatingMenu.targetY=-370;
createCookie('','fwnd_x', 10,30);
createCookie('','fwnd_y', -370,30);
}
 
function move_lower_center()
{
floatingMenu.targetX='center';
floatingMenu.targetY=-370;
createCookie('','fwnd_x', 'center',30);
createCookie('','fwnd_y', -370,30);
}
 
function move_lower_right()
{
floatingMenu.targetX=-280;
floatingMenu.targetY=-370;
createCookie('','fwnd_x', -280,30);
createCookie('','fwnd_y', -370,30);
}

