// Search Field Functions

var HORIZONTAL_MENU_TYPE = "horizontal";
var topmostClass = "AspNet-Menu";
var flyoutWidth = 250;
var scrollBarWidth = 10;
var userAgent = navigator.userAgent;
var versionOffset = userAgent.indexOf("MSIE");
var isIE = (versionOffset >= 0);
var isPreIE7 = false;
var defaultSearchText = "Enter search term...";
if (isIE) {
    fullVersionIE = parseFloat(userAgent.substring(versionOffset+5, userAgent.length));
    majorVersionIE = parseInt('' + fullVersionIE);
    isPreIE7 = majorVersionIE < 7;
}

//doesn't seem to be used
function validateInput() 
{
    if (document.forms[0].q1.value == "") 
    {
	    alert("You must enter a valid search string!");
	    document.forms[0].q1.focus();
	    return false;
    }
    if (document.forms[0].q1.value == defaultSearchText)
    {
	    return false;
    }
    if (!document.forms[0].q1.value == "")
    {
        Q =(((document.forms[0].q1.value.replace(/[%&!?^]/g," ")).replace(/^ +/,"")).replace(/ +$/,"")).replace(/ +/g," "); 
        if (Q == "") 
        {
	        alert("You must enter a valid search string!");
	        return false;
        }
        document.forms[0].q1.value = Q; 
    }
    return true;
}

//intranet search functionality
function detectEnter ()
{
	if (window.event.keyCode == 13)
	{
        processSearch ();
	}
}

function clearField ()
{
    if (document.forms[0].q1.value == defaultSearchText) 
    {
        document.forms[0].q1.value = "";
    }
}

function resetField ()
{
    if (document.forms[0].q1.value== "")
    {
        document.forms[0].q1.value = defaultSearchText;
    }
}

function processSearch ()
{
    if (document.forms[0].onsubmit()) 
    {
        var page = "http://qpssearch.qldpol/results.asp";
		var query = document.forms[0].q1.value;
		var catalog = document.forms[0].ct.value;
		location.href = page + "?q1=" + query + "&ct=" + catalog;
	}
}
//end search functions

//intranet Floating 'Top' function
function setTop ()
{
	yPos = 0;
	topObj = document.all["topAnchor"];
	
	tmpFooterHeight = document.all["footer"].offsetTop;
	tmpBodyHeight = document.body.clientHeight + document.body.scrollTop;
	
	if (tmpFooterHeight < tmpBodyHeight)
	{
		yPos = tmpFooterHeight - 25;
	} 
	else
	{
		yPos = tmpBodyHeight - 25;
	}
	xPos = document.body.clientWidth - 35;
	
	topObj.style.top = yPos;
	topObj.style.left = xPos;
	topObj.style.visibility = 'visible';	
}

//function setIntranetFooter ()
//{
//    // only run setIntranetFooter for intranet sites
//    if (document.getElementById("policeIntranet")) {
//        
//        var windowHeight;
//        
//        // Get the height of the browser window
//        if (typeof(window.innerHeight) == 'number') {
//            windowHeight = window.innerHeight;
//        }
//        else {
//            if (document.documentElement && document.documentElement.clientHeight) {
//	            windowHeight = document.documentElement.clientHeight;
//            }
//            else {
//	            if (document.body && document.body.clientHeight) {
//		            windowHeight = document.body.clientHeight;
//	            }
//            }
//        }

//        // Set the height of the contentContainer 
//        if (windowHeight > 0) {
//            if (document.getElementById("footerWrapper")) {
//                var footerElement = document.getElementById("footerWrapper");
//                var footerHeight = footerElement.offsetHeight;
//                
//        
//                if (document.getElementById("contentContainer")) {
//                    var contentElement = document.getElementById('contentContainer');
//                    var contentHeight = contentElement.offsetHeight;
//                    
//                    alert(windowHeight + ' '+ contentHeight + ' ' +footerHeight);
//                    if ((contentHeight + footerHeight) < windowHeight) {
//                        
//                        contentElement.style.height = (windowHeight - footerHeight);
//                        alert(windowHeight + ' '+ contentHeight + ' ' +footerHeight);
//                    }
//                }
//            }
//        }
//    }
//}

////window.onresize = function() {
//    if (document.getElementById("policeIntranet")) {
//        setIntranetFooter();
//    } else {
//        setFooter();
//    }
//}

// Navigation Functions
function navHover (navElement)
{
    var navElement = document.getElementById(navElement);
    if (navElement)
    {
        var lis = navElement.getElementsByTagName("LI");
        for (var i = 0; i < lis.length; i++)
        {
            lis[i].onmouseover = function()
            {
                var childmenu = this.getElementsByTagName("UL")[0];
                var level = 0;

                if (childmenu != null)
                {
                    childmenu.style.display = "block";
                }
                //determine the type of navigation element hovered over (horizontal or vertical) and its level.
                var firstFlyoutSecondaryNav = this.parentElement.id.indexOf('navmenu-v') > -1;
                var secondFlyoutSecondaryNav = this.parentElement.parentElement.parentElement.id.indexOf('navmenu-v') > -1

                if (firstFlyoutSecondaryNav || secondFlyoutSecondaryNav)
                { //hovering over first level side nav
                    this.className += " iehover";
                    if (childmenu != null)
                    {
                        //change its position if it is too close to the bottom
                        if (tooCloseToBottomPageEdge(this, childmenu))
                        {
                            var contentContainer = document.getElementById("contentContainer");

                            //find the top offset for this menu item
                            var ULTop = getElementTopPosition(this);
                            ULTop += contentContainer.offsetTop

                            //find the top offset for the flyout
                            var childTop = getElementTopPosition(childmenu);
                            childTop += contentContainer.offsetTop;

                            var childHeight = childmenu.clientHeight;
                            if (childHeight == 0)
                            {
                                // let it render relatively
                            }
                            else
                            { // shift it up
                                var childMenuTop = this.clientHeight + (childHeight * -1);

                                if (firstFlyoutSecondaryNav)
                                {
                                    childMenuTop -= 2;
                                }
                                if (Math.abs(childMenuTop) > ULTop)
                                {
                                    childMenuTop = (ULTop - contentContainer.offsetTop) * -1;
                                }
                                childmenu.style.top = childMenuTop;
                            }
                        }
                    }

                }
                else if (this.parentElement.id.indexOf('h-menu') > -1)
                { //hovering over first level LI
                    if (childmenu != null)
                    {
                        if (tooCloseToRightPageEdge(this, childmenu))
                        {
                            childmenu.style.left = (250 - this.clientWidth) * -1;
                        }
                        else
                        {
                            childmenu.style.left = 0
                        }
                        childmenu.style.display = "block";
                    }
                    this.className += " iehover";
                }
                else if (this.parentElement.parentElement.parentElement.id.indexOf('h-menu') > -1)
                { //hovering over second level LI
                    level = 2;
                    if (childmenu != null)
                    {
                        if (tooCloseToRightPageEdge(this, childmenu) && (!tooCloseToLeftPageEdge(this)))
                        {
                            this.className += " iehoverleft";
                        }
                        else
                        {
                            this.className += " iehover";
                        }
                    }
                    else
                    {
                        this.className += " iehover";
                    }
                }
                this.style.zIndex = 5;

                //set up iframe mask to solve hover over select box issues.
                if ((typeof (childmenu) != "undefined") && (childmenu != null))
                {
                    var grandchild = childmenu.firstChild;
                    while (grandchild)
                    {
                        if (grandchild.tagName == "LI")
                        {
                            if ((typeof (grandchild.iFrameFormElementMask) != "undefined") && (grandchild.iFrameFormElementMask != null))
                            {
                                grandchild.iFrameFormElementMask.style.display = "block";

                                //set the grandchild iframemask to the dimensions of its parent element's if the grandchild LI dimensions are 0.
                                var w = grandchild.offsetWidth;
                                if ((grandchild.offsetWidth == 0) && (typeof (this.iFrameFormElementMask) != "undefined") && (this.iFrameFormElementMask != null) && (this.iFrameFormElementMask.style.width.length > 0))
                                {
                                    w = this.iFrameFormElementMask.style.width;
                                }
                                grandchild.iFrameFormElementMask.style.width = w;

                                var h = grandchild.offsetHeight + 5;
                                if ((grandchild.offsetHeight == 0) && (typeof (this.iFrameFormElementMask) != "undefined") && (this.iFrameFormElementMask != null) && (this.iFrameFormElementMask.style.height.length > 0))
                                {
                                    h = this.iFrameFormElementMask.style.height;
                                }
                                grandchild.iFrameFormElementMask.style.height = h; /* fudge to cover margins between menu items */;
                            }
                        }
                        grandchild = grandchild.nextSibling;
                    }
                }
            }

            lis[i].onmouseout = function()
            {
                this.className = this.className.replace(new RegExp(" iehoverleft\\b"), "");
                this.className = this.className.replace(new RegExp(" iehover\\b"), "");

                var childmenu = this.getElementsByTagName("UL")[0];
                if (childmenu != null)
                {
                    childmenu.style.display = "none";
                }
                this.style.zIndex = 0;
            }
            if (isPreIE7 && ((typeof (lis[i].iFrameFormElementMask) == "undefined") || (lis[i].iFrameFormElementMask == null)))
            {
                var iFrameFormElementMask = document.createElement("IFRAME");
                iFrameFormElementMask.scrolling = "no";
//                iFrameFormElementMask.src = "javascript:false;";
                iFrameFormElementMask.frameBorder = 0;
                iFrameFormElementMask.style.display = "none";
                iFrameFormElementMask.style.position = "absolute";
                iFrameFormElementMask.style.left = "0px";
                iFrameFormElementMask.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";

                iFrameFormElementMask.style.zIndex = -1;
                lis[i].insertBefore(iFrameFormElementMask, lis[i].firstChild);
                lis[i].iFrameFormElementMask = iFrameFormElementMask;
            }
        }
    }   
}


//******************
//Determines if the element is too close to the right edge of the window

function tooCloseToRightPageEdge(dropDown, flyout) {

    //Check that the element is too close to 

    if (document.body.clientWidth < getElementLeftPosition(dropDown) + (flyoutWidth * 2) + scrollBarWidth) {

        return true;

    }

    return false;

}

//determines if there is enough room on the left to show a flyout menu

function tooCloseToLeftPageEdge(dropDown) {

    if (getElementLeftPosition(dropDown) < flyoutWidth) {

        return true;

    }

    return false;

}


function getElementLeftPosition(element) {

    var offsetLeft = element.offsetLeft;

    var parent = element.parentElement;

    while (parent && parent.className != topmostClass) {

        offsetLeft += parent.offsetLeft;

        parent = parent.parentElement;

    }

    return offsetLeft;

}

function getElementTopPosition(element) {

    var offsetTop = element.offsetTop;

    var parent = element.parentElement;

    while (parent && parent.id != "navmenu-v") {

        offsetTop += parent.offsetTop;
        //alert(parent.tagName);
        parent = parent.parentElement;

    }
    return offsetTop;

}

function tooCloseToBottomPageEdge(element, childmenu) {
    var windowHeight = document.body.clientHeight;
    var contentContainer = document.getElementById("contentContainer");
    var contentHeight = contentContainer.clientHeight + contentContainer.offsetTop;
    
    var topPosition = getElementTopPosition(element);
    if (windowHeight < contentHeight) {
        contentHeight = windowHeight;
    }

    var childMenuHeight = childmenu.clientHeight;
    
    //make a guess as to how high it is
    if (childMenuHeight == 0) {
        childMenuHeight = element.clientHeight * childmenu.getElementsByTagName("A").length;
    }
    
    if ((topPosition + contentContainer.offsetTop + childMenuHeight) > contentHeight) {
        return true; 
    }
    return false;
}

//}

// Accessibility Functionality
var normalFont = 80;
function setPlusText()
{
    normalFont = normalFont + 10;
    size = normalFont + "%";
    var contentArea = document.getElementById('content');
    var tdCellArray = contentArea.getElementsByTagName('td');
    contentArea.style.fontSize = size;
    for (t = 0 ; t <= tdCellArray.length - 1 ; t++)
    {
        tdCellArray[t].style.fontSize = size;
    }
    return false;
}

function setMinusText()
{
    normalFont = normalFont - 10;
    size = normalFont + "%";
    var contentArea = document.getElementById('content');
    var tdCellArray = contentArea.getElementsByTagName('td');
    contentArea.style.fontSize = size;
    for (t = 0 ; t <= tdCellArray.length -1 ; t++)
    {
        tdCellArray[t].style.fontSize = size;
    }
    return false;
}
