var twittLinkHandler = function(){
    $$('div.twitt_box').each(function(e){ 
        e.select('div.pager a').each(function(a){ a.onclick = function(){ 
                var href = this.href.indexOf( '#') == -1 ? this.href : this.href.substring( 0, this.href.indexOf( '#') );
                new Ajax.Updater( e.id, ( href.indexOf( '?') == -1 ? href + "?plain=1" : href + "&plain=1" ) + "&element=twitt&rnd=" + Math.round( Math.random() * 100000 ), {
                    method: 'get',
                    onComplete: twittLinkHandler
                }); 
                document.location.href = "#" + e.select( "ul" )[0].id; 
                var p = href.match(new RegExp( /p=(\d+)/g));
                if ( p && p.length ) { document.location.href += "/" + p[0]; }
                return false; 
            }
    
        }); 
    });
}
var twittLinkHandlerInit = function()
{
    var p = document.location.hash.match(new RegExp( /([a-zA-Z0-9_]+)\/p=(\d+)/));
    if ( p && p.length == 3 ) { 
        document.location.href = "#" + p[0]; 
        var href = $( p[1] ).parentNode.select( 'div.pager a')[0].href;
        href = href.indexOf( '?') == -1 ? href : href.substring( 0, href.indexOf( '?') );
        new Ajax.Updater( $( p[1] ).parentNode, href + "?p=" + p[2] + "&plain=1&element=twitt&rnd=" + Math.round( Math.random() * 100000 ), {
            method: 'get',
            onComplete: twittLinkHandler
        }); 
    }
    else {
        twittLinkHandler.call();
    }

    window.setTimeout( twittReload, 60000 );
}

function twittReload( container )
{
    if ( (document.location.hash == '' && document.location.href.indexOf( 'p=' ) == -1 ) || document.location.href.indexOf( 'p=0') != -1  ) { 
        var href = document.location.pathname + "?plain=1&element=twitt" + "&rnd=" + Math.round( Math.random() * 100000 );
        new Ajax.Updater( $( "twitter_container" ), href, {
            method: 'get',
            onComplete: twittLinkHandler
        }); 
    }

    window.setTimeout( twittReload, 60000 );
}
Event.observe(window,'load',twittLinkHandlerInit);

