/// <summary>
/// Check the current request to see if it has the needed requirements to run GallupPaging
/// </summary>
function hasPagingSupport() 
{
	if (typeof (hasPagingSupport.support) != "undefined")
	{
		return hasPagingSupport.support;
    }
	
	var ie55 = /msie 5\.[56789]/i.test( navigator.userAgent );
	
	hasPagingSupport.support = ( typeof (document.implementation) != "undefined" &&
			document.implementation.hasFeature( "html", "1.0" ) && 
			typeof (document.body.innerHTML) == "string" || ie55 )
			
	return hasPagingSupport.support;
	
};

function loadPagingPages ( el ) 
{
	if (( !hasPagingSupport() ) ||
	( el.pages.length != 0 )) return;
	
    var pageHash = document.location.hash;
    if ((pageHash == null) || (pageHash == ''))
        pageHash = "#1";
        
	var _pages = getElementsByClass("page", el.element, "div");
	
	if (_pages.length > 0)
	{
        for (i = 0; i < _pages.length; i++) 
        {
	        _pages[i].className = "pagehide";
    		
            el.pages[i] = _pages[i];
            
            if (pageHash == _pages[i].id.replace('page','#'))
            {
                el.selectedIndex = i;
            }
        }
        
        if (el.selectedIndex == null || el.pages[ el.selectedIndex ] == null )
	    {
           el.selectedIndex = 0;
	    }
	    
	    el.pages[ el.selectedIndex ].className = "pageshow";
	}
};

function setSelectedIndex ( el,  n ) 
{
	if (el.selectedIndex != n) 
	{
		if (el.selectedIndex != null && el.pages[ el.selectedIndex ] != null )
		{
	        el.pages[ el.selectedIndex ].className = "pagehide";
		}
        
	    var _activeLinks = getElementsByClass("pagingnavigationoption" + el.selectedIndex, el.element.parentNode, "a");
	    
	    if (_activeLinks.length > 0)
	    {
            for (i = 0; i < _activeLinks.length; i++) 
            {
		        _activeLinks[i].className = "pagingnavigationoption" + el.selectedIndex;
            }
	    }
		
	    el.pages[ n ].className = "pageshow";
		el.selectedIndex = n;
	    
	    if(_ff)		
	        document.getElementById('top').focus();
	    else
	        document.getElementById('pagingnavigationtop').focus();
	    
		
		highlightLinks ( el );
	}	
};

function highlightLinks ( el )
{
	    var _activeLinks = getElementsByClass("pagingnavigationoption" + el.selectedIndex, el.element.parentNode, "a");
	    
	    if (_activeLinks.length > 0)
	    {
            for (i = 0; i < _activeLinks.length; i++) 
            {
		        _activeLinks[i].className += " pagingnavigationoptionon";
            }
	    }
}

function createNavigation ( el ) 
{
    // Add Paging, if there are more then 1 page
    if (el.pages.length > 1)
	{
	    var text1 = document.createTextNode('<span>Page:</span>');
	   
	    var div1 = createNavigationBlock("pagingnavigationtop");
	    var div2 = createNavigationBlock("pagingnavigationbottom");
	
        for (i = 0; i < el.pages.length; i++) 
        {    
            div1.appendChild(createNavigationLink( el , i));
            div2.appendChild(createNavigationLink( el , i));
        }
	
		 var wrapper = document.getElementById( "pagingwrapper" );
		 var clearingDiv = document.createElement( "div" );
		 clearingDiv.className = "clearer";
		 clearingDiv.innerHTML = " ";
		 wrapper.insertBefore( clearingDiv, wrapper.firstChild );
		 wrapper.insertBefore( div1, clearingDiv );
	    el.element.appendChild(div2);
	    
	    highlightLinks (el);
	}
};



function createNavigationBlock(cssClass)
{
	var text = document.createTextNode('Page:');
	
	var span = document.createElement( "SPAN" );
	span.appendChild(text);
	
	var div = document.createElement( "DIV" );
	div.className = "pagingnavigation " + cssClass;
	div.id = cssClass;
	div.appendChild(span);
	
	return div;
};

function createNavigationLink( el , position)
{
    var _num = (position + 1);
            
	var a = document.createElement( "A" );
	a.href = "#" + _num;
	a.className = "pagingnavigationoption" + position;
	a.innerHTML = _num;
	
	a.onclick = function () { 
	
	setSelectedIndex ( el,  position);
	
	if (_ff == true)
	    return false;
	else
	    return true;
	
	};
	
	return a;
}

/// <summary>
/// 
/// </summary>
function startPaging( el ) 
{
    if ( !hasPagingSupport() || el == null ) return;
	
	this.element = el;
	this.selectedIndex = null;
	this.pages = [];
	
	// Load the Pages
    loadPagingPages ( this );
    
	createNavigation( this );
	
};

   
/// <summary>
/// 
/// </summary>
function initializationPaging() 
{ 
    // Grab the main node
    var _pages;
    var _continue = false;
    
    try
    {
        _pages = document.getElementById('pages');
    
        if (_pages.tagName.toLowerCase() == "div") 
        {
            _continue = true;
        }
    }
    catch(e)
    {}
    
    if (_continue == true)
    {
        startPaging(_pages);
    }
        
};

// initialization hook up

// DOM2
if ( typeof window.addEventListener != "undefined" )
	window.addEventListener( "load", initializationPaging, false );

// IE 
else if ( typeof window.attachEvent != "undefined" ) {
	window.attachEvent( "onload", initializationPaging );
	//window.attachEvent( "onunload", disposeAllTabs );
}

else {
	if ( window.onload != null ) {
		var oldOnload = window.onload;
		window.onload = function ( e ) {
			oldOnload( e );
			initializationPaging();
		};
	}
	else 
		window.onload = initializationPaging;
}