    /**
     *  This function will update an elements style display attribute toggling
     *  it between hidden and (by default) block, although this can be overwritten 
     *  by passing inline or another string as the showState variable
     *
     *  @author Dave Folan, Michael Fox
     *  @param (String) OR (Object) targetId    Id or HtmlElement that needs toggling.
     *  @param (String) showState               The showState to use (defaults to block) 
     **/
    function toggleHidden(target, showState) {            
        
        if ( typeof( target ) != "object" )  {        
            var targetElement = $('#'+target);
        } else {
            var targetElement = $(target);
        }
        
        if ( typeof( showState ) == "undefined" ) { 
           targetElement.toggle();        
        } else {            
           targetElement.show();
        }        
    }

    /**
     *  This function toggles an elements className between open and closed 
     *
     *  @author Dave Folan, Michael Fox
     *  @param (String) targetId Id of the element to toggle.
     **/     
    function toggleOpenClose(targetId) { 
          
        var targetElement = $('#'+targetId);
        
        if ( targetElement.hasClass("open") ) {
        
            targetElement.removeClass("open");
            targetElement.addClass("closed");
            
        } else {
        
            targetElement.removeClass("closed");
            targetElement.addClass("open");      
        }
    }
    
    /**
     *  Function to open and close facets. When facets are in this stucture:
     *   <headerElement id="ID" onclick="toggleFacetOpenClose(ID)">Header</headerElement>
     *   <listContainer id="childof-ID">
     *       <listItem>1</listItem>
     *       <listItem>2</listItem>
     *       <listItem>3</listItem>
     *   </listContainer>
     *
     *  @TODO: create two functions from this: toggleOpenClose and toggleHidden(targetId, showState = 'block')
     *  @author Dave Folan
     *  @param (String) headerId Id of the facet to close.
     **/
    function toggleFacet( facetId ) {
        toggleOpenClose( facetId );
        toggleHidden( 'childof-' + facetId );
    }
    
    /**
     *  This function is to override the function above when trying to click the link of the parent element,
     *  as clicking on the link will trigger the li's event.
     *  So by using this the toggleOpenClose function wont work!
     *
     *  @author Dave Folan, Michael Fox
     *  @param (String) headerId Id of the element to override onclick functionality of.
     **/
    function overrideToggleOpenClose( headerId ) {
        var target = $('#' + headerId);
        target.removeAttr( "id" );
        var listElement  = $( '#childof-' + headerId );
        listElement.attr( "id", "childof-empty" );
    }
        
    /**
     *  Function to hide the click element and display the hidden element (i.e. Clicking "Search again button"
     *  will set the button to hidden and will show the hidden search form.
     *   <click_element id="click_element-ID" onclick="displayHidden(ID)">Click text</click_element>
     *   <hidden_element id="hidden_element-ID">
     *      Element Content
     *   </listContainer>
     *
     *  @TODO: change to use toggleHidden fucntion
     *  @author Dave Folan, Michael Fox
     *  @param (String) clickId Id of the element to hide.
     *  @param (String) showId Id of the element to display.
     **/
    function clickToDisplay(clickId, displayId) {       
        $('#' + clickId).hide();        
        $('#' + displayId).show();
    }
    
    /**
     *  This is the function to show and hide the extra facets using the more/less links.
     *  
     *  this will get the click Object and change it from more to less and vice versa
     *  it will then change the displayId's hidden elements with class togglable to blank/hidden
     *
     *  @author Dave Folan, Michael Fox
     *  @param (String) clickId Id of the more/less link.
     *  @param (String) displayId Id of the element containing the hidden facets.
     **/     
     function toggleShowMore(clickId, displayId) {
     
        var clickElement = $('#' + clickId );
        
        if ( clickElement.children(".show-more" ).size() > 0 ) {
        
            var innerHTML = clickElement.html();
            innerHTML = innerHTML.replace(/more/g, "less");     
            clickElement.html(innerHTML);
            
        } else if ( clickElement.children(".show-less" ).size() > 0 ) {
            var innerHTML = clickElement.html();
            innerHTML = innerHTML.replace(/less/g, "more");
            clickElement.html(innerHTML);  
        }     
        
        clickElement.siblings(".togglable").each( function() { toggleHidden( $(this) ); } );
     }

    /**
     *  When called this will set a cookie using the data given 
     *  Leaving days empty will mean the cookie expires when browser is closed.
     *  Setting days to -1 will erase cookie @see eraseCookie
     *
     *  @see http://www.quirksmode.org/js/cookies.html
     *  @author Dave Folan (editted from quirksmode.org)
     *  @param (String) name name of the cookie
     *  @param (String) value value to give the cookie
     *  @param (Int)    days The number of days before the cookie expires
     **/
    function createCookie(name,value,days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
    }
    
    /**
     *  Will return the value of the cookie or null
     *
     *  @see http://www.quirksmode.org/js/cookies.html
     *  @author Dave Folan (editted from quirksmode.org)
     *  @param (String) name name of the cookie to look up
     **/
    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
    }
    
    /**
     *  Will delete a cookie (by setting it to a previous date)
     *
     *  @see http://www.quirksmode.org/js/cookies.html
     *  @author Dave Folan (editted from quirksmode.org)
     *  @param (String) name name of the cookie to delete
     **/
    function eraseCookie(name) {
        createCookie(name,"",-1);
    }
