﻿// Flag as to whether the control was initialized
var _advSpIsInitalized = false;

// The number of steps to take to render the expansion/contraction
var _advSpNumberOfSteps = 10;

// How many milliseconds each frame takes to render
var _advSpStepIncrement = 30;

// How high the expanded control is (set on init)
var _advSpExpandedHeight = 0;

// The client ID of the DOM control
var _advSpPanelClientId = GetAdvancedSearchPanelClientId();

// The current status of the panel
var _advSpIsVisible = false;

// True when moving, False when not
var _advSpIsMoving = false;


/**
 * Initalizes the control vars
 */
function _InitalizeAdvancedSearchPanel()
{
	// If the panel was not initialized yet, initalize it
	if( !_advSpIsInitalized )
	{
		// Set the expanded height to the height of the control
		_advSpExpandedHeight = parseInt(document.getElementById(_advSpPanelClientId).style.height.replace("px",""));
		
		// The control is now initalized
		_advSpIsInitalized = true;
	}
}


/**
 * Toggles the visibility of the advanced search panel
 * param visible	True or False :: True = show, False = hide
 */
function ToggleAdvancedSearchPanel(visible)
{
	// If the control was not previously initalized
	_InitalizeAdvancedSearchPanel();
	
	// If not already moving
	if( ! _advSpIsMoving )
	{
		// If expanding, set the height to 0 and start increasing
		if( visible )
		{
			document.getElementById(_advSpPanelClientId).style.height = "0px";
			document.getElementById(_advSpPanelClientId).style.display = "";
			_advSpIsMoving = true;
			_ToggleAdvancedSearchPanel_Inc( visible, 0 );
			_advSpIsVisible = true;
		}
		else // If contracting, set the height to max and start decrementing to 0
		{
			document.getElementById(_advSpPanelClientId).style.height = _advSpExpandedHeight + "px"
			_advSpIsMoving = true;
			_ToggleAdvancedSearchPanel_Inc( visible, _advSpNumberOfSteps );
			_advSpIsVisible = false;
		}
	}
}


/**
 * Toggles the visibility of the advanced search panel
 * param visible	True or False :: True = show, False = hide
 * param step		What frame to place the panel at
 */
function _ToggleAdvancedSearchPanel_Inc(visible, step)
{
	// Set the increment height = (height / total_steps) * current_step
	document.getElementById(_advSpPanelClientId).style.height = ((_advSpExpandedHeight / _advSpNumberOfSteps) * step) + "px";

    // Trigger custom event actions
	_TriggerEventHandler(step, (visible ? true : false));
	
	// Call again if not done
	if( (step < _advSpNumberOfSteps && visible) || (step > 0 && !visible) )
		setTimeout("_ToggleAdvancedSearchPanel_Inc(" + visible + "," + (visible ? (step + 1) : (step - 1)) + ")", _advSpStepIncrement);
	else
	{
		_advSpIsMoving = false;
		
		// Hide it if it's done and hidden
		if( !visible )
			document.getElementById(_advSpPanelClientId).style.display = "none";
	}
}

/**
 * Function to trigger custom events (even though I wrote all of the above) to tie in specific actions at specific frames.
 * This function makes it easier to hook in event code without ruining the base logic.
 * param frameNumber    The frame number occuring
 * param isOpeneing     True when the panel is opening, False when it is retracting
 */
function _TriggerEventHandler(frameNumber, isOpening)
{
    switch( frameNumber )
    {
        // Minor hack to trigger at frame 8 FOR INTERNET EXPLODER 6 to hide the drop down that shows though the advanced panel
        case 2:
            // Home Page
            try { document.getElementById("ctl00_cphMain_ucBrowseCategoryList_eddlLanguage").style.display = (isOpening ? "none" : ""); } catch (e) { }
            break;
        case 4:
            // Search Page
            try { document.getElementById("ctl00_cphMain_ucCompanyList_ddlPageSize").style.display = (isOpening ? "none" : ""); } catch (e) { }
            break;
    }
}