﻿FloatLayer.prototype.setFloatToTop=setTopFloater;
FloatLayer.prototype.setFloatToBottom=setBottomFloater;
FloatLayer.prototype.initialize=defineFloater;
FloatLayer.prototype.adjust=adjustFloater;
FloatLayer.prototype.align=alignFloater;

var FloatLayers       = new Array();
var FloatLayersByName = new Array();

function addFloatLayer(n,offX,offY,spd){new FloatLayer(n,offX,offY,spd);}
function getFloatLayer(n){return FloatLayersByName[n];}

function alignFloatLayers()
{
    for(var i=0;i<FloatLayers.length;i++)
    {
        FloatLayers[i].align();
    }
}

function getYCoord(el) 
{
    // Padding is the total padding around the floater and the divs in px.
    padding = 25;
	y=0;
	while(el)
	{
		y+=el.offsetTop;
		el=el.offsetParent;
	}
	return y + padding;
}

function FloatLayer(n, dock, spd) 
{
	this.index = FloatLayers.length;
	FloatLayers.push(this);
	FloatLayersByName[n] = this;
    offY = getYCoord(document.getElementById(dock));
	this.name    = n;
	this.tm      = null;
	this.steps   = spd;
	this.alignVertical  =(offY>=0) ? topFloater : bottomFloater;
	this.ifloatY = Math.abs(offY);
}

function defineFloater()
{
    this.layer  = document.getElementById(this.name);
	this.width  = this.layer.offsetWidth;
	this.height = this.layer.offsetHeight;
	this.prevY  = this.layer.offsetTop;
	// this.prevY = getYCoord(document.getElementById('mainXslDiv'));
}

function adjustFloater() 
{
	this.tm = null;
	if(this.layer.style.position!='absolute')
	{
	    return;
	}

    topBoundry = getYCoord(document.getElementById('mainXslDiv'));
    bottomBoundry = document.getElementById('mainXslDiv').offsetHeight;
    browserBoundryTop = document.body.scrollTop;
    
    // Is the window really small?
    if (this.floatY + this.height > browserBoundryTop)
    {
        if (browserBoundryTop < topBoundry)
        {
            this.floatY = topBoundry;
        }
        else
        {
            this.floatY = browserBoundryTop;
        }
    }
    else
    {     
        // To far below?
        if (this.floatY + this.height > bottomBoundry)
        {
            this.floatY = bottomBoundry - this.height;
        }
    }
	var dy = Math.abs(this.floatY-this.prevY);
	
	if (dy < this.steps/2)
	{
		cy = (dy>=1) ? 1 : 0;
    }
	else
	{
		cy = Math.round(dy/this.steps);
    }

	if (this.floatY > this.prevY)
	{
   		this.prevY += cy;
   	}
	else
	{
	     if (this.floatY < this.prevY)
	     {
		     this.prevY -= cy;
		 }
    }
        
	this.layer.style.top  = this.prevY;

	if (cy!=0)
	{
		if(this.tm==null)
		{
		    this.tm = setTimeout('FloatLayers['+this.index+'].adjust()',50);
		}
	}
	else
	{
		alignFloatLayers();
	}
}

function setTopFloater()
{
    this.alignVertical=topFloater;
}

function setBottomFloater()
{
    this.alignVertical=bottomFloater;
}

function topFloater()
{
    this.floatY = document.body.scrollTop + this.ifloatY;
}

function bottomFloater()
{
    this.floatY = document.body.scrollTop + document.body.clientHeight - this.ifloatY - this.height;
}

function writeToDtatus(input){
    document.getElementById("dbg").innerHTML = input ;
    return true;
}

function alignFloater()
{
	if(this.layer==null)
	{
	    this.initialize();
	}
	
    var offY = getYCoord(document.getElementById('mainXslDiv'));
	this.ifloatY = Math.abs(offY);
	
	this.alignVertical();
	
	if(this.prevY!=this.floatY)
	{
		if(this.tm==null)
		{
		    this.tm=setTimeout('FloatLayers['+this.index+'].adjust()',50);
		}
	}
}

function detach()
{
    // We should check screen resolution here.  Disable the scrolling on the floater if screen is really small...
    if(screen.height > 600)
    {
        new FloatLayer('floaterDiv','mainXslDiv',1); 
        lay  = document.getElementById('floaterDiv');
        lay.style.position = 'absolute';
        getFloatLayer('floaterDiv').initialize();
        alignFloatLayers();
    }
    else
    {
        //alert('screen size too small');
    }
	UpdateNavigationContainer('signinContent'); 
}

function ShowScreening() 
{
    var jobid = this.jobid.value;
    var screeningURL = '?jobid='+ jobid + '&tabnum=8&screening=true';
    
    if (this.ro)
    {
        var ro = this.ro.value;
        screeningURL = screeningURL + '&RO=' + ro;
    }
  
    var winScreening = window.open(screeningURL, '', 'width=800,height=450,toolbar=0,location=0,scrollbars=1'); 	
	winScreening.focus();
}

function AnchorJump()
{
    selector = document.getElementById('ddlAnchorJump');
    hashmark = selector.value;
    window.location.hash = hashmark;
    // Only set focus if the slider is floating - otherwise this causes an issue...
    if(screen.height > 600)
    {
        setTimeout ("document.all('ddlAnchorJump').focus()", 100);
    }
}

// =====================
// Function to read in the navigation cookie and update the
// 'containerID' with the HTML needed to represent the login state.
// =====================
function UpdateNavigationContainer(containerID) 
{
    var cookieName = 'TemporaryNameNavigation';
    var content = "";
    var theCookie = ""+document.cookie;
    var ind = theCookie.indexOf('TemporaryNameNavigation');
    if (ind != -1 && cookieName!="")
    {
        var ind1 = theCookie.indexOf(';',ind);
        if (ind1==-1)
        {
            ind1=theCookie.length; 
        }
        content = unescape(theCookie.substring(ind+cookieName.length+1,ind1));
    }

    var container = document.getElementById(containerID);
    if (content == "")
    {
        // Cookie could not be found or is mangled.  Set up the non-logged in state here..
        container.innerHTML = '<a href="https://my.usajobs.gov/Login.aspx">SIGN IN</a> <strong>OR</strong> <a href="https://my.usajobs.gov/Account/Account.aspx">CREATE AN ACCOUNT</a>'; 
    }
    else
    {
        // Found a cookie with a good name in it.  Use it.
        container.innerHTML = '<a href="https://my.usajobs.gov/home.aspx">Welcome ' + content + '!</a>&nbsp;|&nbsp;<a href="https://my.usajobs.gov/Logout.aspx">Sign out</a>';
    }
}

// **************** header JAVASCRIPT ****************
var timeout	= 500;
var closetimer	= 0;
var ddmenuitem	= 0;
var ddmenuitemImg = 0;

// open hidden layer
function mopen(id) {	
	// cancel close timer
	mcancelclosetime();

	// close old layer
	if(ddmenuitem){
	    ddmenuitem.style.visibility = 'hidden';
	}
    
	// get new layer and show it
	ddmenuitem = document.getElementById(id);
	ddmenuitem.style.visibility = 'visible';
	
    if (id == "pd_Search") {
        document.getElementById('pd_MyImg').src = "https://securemedia.newjobs.com/opm/www/usajobs/navigation/arrow-off.gif";
        document.getElementById('pd_InfoImg').src = "https://securemedia.newjobs.com/opm/www/usajobs/navigation/arrow-off.gif";
    }
    else if (id == "pd_My") {
        document.getElementById('pd_SearchImg').src = "https://securemedia.newjobs.com/opm/www/usajobs/navigation/arrow-off.gif";
        document.getElementById('pd_InfoImg').src = "https://securemedia.newjobs.com/opm/www/usajobs/navigation/arrow-off.gif";
    }
    else {
        document.getElementById('pd_SearchImg').src = "https://securemedia.newjobs.com/opm/www/usajobs/navigation/arrow-off.gif";
        document.getElementById('pd_MyImg').src = "https://securemedia.newjobs.com/opm/www/usajobs/navigation/arrow-off.gif";
    }
    ddmenuitemImg = document.getElementById(id + "Img");
    ddmenuitemImg.src = "https://securemedia.newjobs.com/opm/www/usajobs/navigation/arrow-over.gif";
}

// close showed layer
function mclose() {
	if(ddmenuitem) {
	    ddmenuitem.style.visibility = 'hidden';
        document.getElementById('pd_SearchImg').src = "https://securemedia.newjobs.com/opm/www/usajobs/navigation/arrow-off.gif";
        document.getElementById('pd_MyImg').src = "https://securemedia.newjobs.com/opm/www/usajobs/navigation/arrow-off.gif";
        document.getElementById('pd_InfoImg').src = "https://securemedia.newjobs.com/opm/www/usajobs/navigation/arrow-off.gif";
    }
}

// go close timer
function mclosetime() {
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime() {
	if(closetimer) 	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

// close layer when click-out
document.onclick = mclose; 

function dropdownmenu(obj) {
    document.getElementById(obj + "Img").src = "https://securemedia.newjobs.com/opm/www/usajobs/navigation/arrow-over.gif";
}

function delayhidemenu(obj) {
    document.getElementById(obj + "Img").src = "https://securemedia.newjobs.com/opm/www/usajobs/navigation/arrow-off.gif";
}